【发布时间】:2020-11-14 08:50:15
【问题描述】:
我想编写一个 lua 过滤器,在从 markdown 转换为 PDF 时强制 Pandoc 使用紧凑列表。我注意到带有嵌套表/文本/div 的列表不会使用 \tightlist 选项,因为 Pandoc AST 对每个列表项使用 Para 而不是 Plain。我尝试修改示例here 以强制所有BulletList 和OrderedList 项目为Plain,但当项目包含嵌套内容时我无法使其工作。 pandoc mwe.txt -f markdown -t native --lua-filter the-filter.lua 为第一个列表项返回 Para:
[BulletList
[[Plain [Str "list",Space,Str "1"]]
,[Plain [Str "list",Space,Str "1"]]
,[Plain [Str "list",Space,Str "1"]]]
,Para [Str "Some",Space,Str "paragraph"]
,BulletList
[[Para [Str "list",Space,Str "2"]
,Div ("",["class"],[])
[Para [Str "Nested",Space,Str "div"]]]
,[Plain [Str "list",Space,Str "2"]]
,[Plain [Str "list",Space,Str "2"]]]]
我不知道如何处理这个问题:
- 我是否应该使用
walk_block并将每个列表项更改为Plain? - 如何处理
#blocks> 1 的情况?如何将Para更改为Plain,然后包含任何嵌套内容(比如我有两个嵌套的 div)?
mwe.txt
- list 1
- list 1
- list 1
Some paragraph
- list 2
::: {.class}
Nested div
:::
- list 2
- list 2
the-filter.lua
local List = require 'pandoc.List'
function compactifyItem2 (blocks)
if (#blocks == 1) then
if (blocks[1].t == 'Para') then
return {pandoc.Plain(blocks[1].content)}
else
return blocks
end
elseif (#blocks == 2) then -- I assume I have to change the Para and nest the child content
if (blocks[1].t == 'Para') then
blocks.content = pandoc.Plain(blocks[1].content) .. blocks[2].content
return {blocks.content}
end
else
return blocks
end
end
function compactifyList (l)
l.content = List.map(l.content, compactifyItem2)
return l
end
return {{
BulletList = compactifyList,
OrderedList = compactifyList
}}
【问题讨论】: