我正在使用asciidoctor为我的家乡即将发生的事件生成静态站点,我们为外地客人提供了酒店和名胜古迹的列表。每个位置在地图上都有一个位置,类型(酒店,餐厅等)以及是否是活动的赞助商。
我已经阅读了Block Processors上的文档,这似乎是最好的方法,但是我不知道如何用可以以这种方式处理的元数据标记标题。
当前,我们对文档中的每个位置都有这样的描述
== Locations
[[Fancy_Hotel]]
=== Fancy Hotel
[location, 55, 73, hotel, sponsor]
A nice hotel in the middle of town
[[Chain_Restaurant]]
=== Chain Breakfast Restaurant
[location, 98, 16, restaurant]
A good place for food
,我在构建页面时会读取元数据,然后使用可单击的链接填充地图,这些链接可导航到相应的条目。以下代码可以工作,但是如果可能的话,我想将元数据移到标题上方,因此我不会过分地依赖父级。
require 'asciidoctor'
require 'asciidoctor/extensions'
$Locations = Array.new
def make_map_svg locs
# Do some magic here
locs.each {|a| puts a}
end
class LocationBlock < Asciidoctor::Extensions::BlockProcessor
use_dsl
named :location
on_context :paragraph
name_positional_attributes ['x', 'y', 'type', 'sponsor']
def process parent, reader, attrs
x = ((attrs.delete 'x') || 0).to_i
y = ((attrs.delete 'y') || 0).to_i
type = attrs.delete 'type'
sponsor = (attrs.delete 'sponsor') == 'sponsor'
$Locations.push({
x:x,
y:y,
type:type,
sponsor:sponsor,
title:parent.title,
parent.id
})
create_paragraph parent, reader.lines, attrs, {}
end
end
Asciidoctor::Extensions.register do
block LocationBlock
end
Asciidoctor.convert_file "./Locations.adoc"
make_map_svg $Locations
make_map_svg函数可以正常工作,问题在于弄清楚如何使元数据条目更不易破解。
答案 0 :(得分:0)
您可能会认为位置数据是元数据,但是使用块处理器对其进行处理意味着它不是元数据。块占据了Asciidoctor的AST中的一个位置,并且预计将占据输出中的一个位置;在该位置应该有渲染的东西。
标题的特殊之处在于,它们不仅定义了节/文档标题的文本,而且还定义了标题和节的子元素的容器。如果使用块处理器,则它必须紧随标题之后,否则不与标题和其他子元素一起包含在容器中。将元素结构从您的[location ...]
块导航到有标题的部分将比仅使用parent
更具挑战性。
如果位置数据是真正的元数据,因为您不希望在[location ...]
存在的地方呈现任何内容,则可以考虑将逻辑实现为预处理器,该预处理器可以从行中的注释中读取位置信息您的来源。
使用预处理器方法解析注释的一个优势是,无论您是否已安装/激活扩展程序,使用您的Asciidoctor源的任何人都可以看到相同的呈现(除了CSS)。