潘多克·卢阿(Pandoc Lua):如何走进Div?
我需要两个公告列表以不同的对待。
当我简单地将元素返回普通的弹头列表中时,将正确解析DIV内部的一个。
但是,当我更改普通的公告列表并返回Rawinline时,如何使Div内部的行走工作?
# normal chapter
* a normal list
* with items
* or more
* to finish
::: Special
# special chapter
* at the start
* something else
* the end
:::
预期的结果就是这样。每个列表的项目都对处理方式有所不同。
<h1 class="normalHeader" id="normal-chapter">normal chapter</h1>
<ul>
<li class="normal-item">a normal list</li>
<li class="normal-item">with items</li>
<li class="normal-item">or more</li>
<li class="normal-item">to finish</li>
</ul>
<section id="special-chapter" class="Special" class="specialHeader">
<h1 class="specialHeader">special chapter</h1>
<ul >
<li class="special-item">at the start</li>
<li class="special-item">something else</li>
<li class="special-item">the end</li>
</ul>
</section>
LUA过滤器:
Div = function (el)
if el.classes[1] == 'Special' then
return pandoc.walk_block(el, {
Header = function(el)
el.attributes['class']='specialHeader'
return el
end,
BulletList = function(el)
print("specialltest")
local mylist ='<ul >\n'
for i, item in ipairs(el.content) do
local first = item[1]
if first then
mylist = mylist .. '<li class="special-item">' .. pandoc.utils.stringify(first) .. '</li>\n'
end
end
mylist = mylist .. '</ul>\n'
return pandoc.RawInline('html', mylist)
end,
})
end
return el
end
Header = function (el)
el.attributes['class']='normalHeader'
return el
end
BulletList = function (el)
print("normalltest")
local mylist ='<ul >\n'
for i, item in ipairs(el.content) do
local first = item[1]
if first then
mylist = mylist .. '<li class="normal-item">' .. pandoc.utils.stringify(first) .. '</li>\n'
end
end
mylist = mylist .. '</ul>\n'
return pandoc.RawInline('html', mylist)
-- works if I return el without treatment
end
I need the two BulletList to be treated differently.
The one inside the Div is correctly parsed when I simply return the element in the normal BulletList.
But how to make the walk inside the div work when I change the normal Bulletlist and return RawInline?
# normal chapter
* a normal list
* with items
* or more
* to finish
::: Special
# special chapter
* at the start
* something else
* the end
:::
The expected result would be like this. The items of each list are differently treated.
<h1 class="normalHeader" id="normal-chapter">normal chapter</h1>
<ul>
<li class="normal-item">a normal list</li>
<li class="normal-item">with items</li>
<li class="normal-item">or more</li>
<li class="normal-item">to finish</li>
</ul>
<section id="special-chapter" class="Special" class="specialHeader">
<h1 class="specialHeader">special chapter</h1>
<ul >
<li class="special-item">at the start</li>
<li class="special-item">something else</li>
<li class="special-item">the end</li>
</ul>
</section>
Lua filter:
Div = function (el)
if el.classes[1] == 'Special' then
return pandoc.walk_block(el, {
Header = function(el)
el.attributes['class']='specialHeader'
return el
end,
BulletList = function(el)
print("specialltest")
local mylist ='<ul >\n'
for i, item in ipairs(el.content) do
local first = item[1]
if first then
mylist = mylist .. '<li class="special-item">' .. pandoc.utils.stringify(first) .. '</li>\n'
end
end
mylist = mylist .. '</ul>\n'
return pandoc.RawInline('html', mylist)
end,
})
end
return el
end
Header = function (el)
el.attributes['class']='normalHeader'
return el
end
BulletList = function (el)
print("normalltest")
local mylist ='<ul >\n'
for i, item in ipairs(el.content) do
local first = item[1]
if first then
mylist = mylist .. '<li class="normal-item">' .. pandoc.utils.stringify(first) .. '</li>\n'
end
end
mylist = mylist .. '</ul>\n'
return pandoc.RawInline('html', mylist)
-- works if I return el without treatment
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是解决此问题的可能方法。它定义了两个过滤器,一个用于普通环境,一个用于某些Divs中的特殊环境。要使用的过滤器在DIV内选择。过滤器使用非标准的遍历顺序(
traverse ='topdown'
)。我们将普通过滤器应用于整个文档,以确保将其应用于未包裹在DIV中的元素。请注意,某些过滤器功能现在返回
false
作为第二个值:向Pandoc发出信号,表明该元素的子树不应进一步过滤。上述过滤器不起作用的原因是,潘多克(Pandoc)穿越文档树自下而上,即,它首先处理深嵌套的元素。因此,它在处理div之前处理嵌套的子弹列表。之后,DIV不再包含子弹列表,而是原始内容。过滤器未处理后者。
这也是为什么代码为标题工作的原因:
标题
被修改而不是更换,因此DIV过滤器可以进一步修改它。Below is a possible way to solve this. It defines two filters, one for the normal environments, one for the special environments inside some divs. The kind of filter to be used is selected inside the div. The filters use a non-standard traversal order (
traverse = 'topdown'
). We apply the normal filter to the whole document to make sure that it will be applied to elements not wrapped in a div.Note that some filter functions now return
false
as a second value: this signals to pandoc that the element's subtree should not be filtered any further.The reason for the above filter not working is that pandoc traverses the document tree bottom-up, i.e., it processes the deeply nested elements first. So it processes the nested bullet list before it processes the div. After that, the div no longer contains a bullet list but raw content; the latter is not processed by the filter.
That's also why the code did work for the heading: the
Header
is modified instead of replaced, so the Div filter can modify it further.