Pandoc的具有编号的示例列表不能与自定义编写器一起使用。

Pandoc有一个名为example_lists的惊人扩展,可在整个文档中连续编号。 我们试图使用自定义编写器来生成html,但是结果html中的编号被破坏了。 考虑以下MD代码:

(@) item 1
(@) item 2

## header ##

(@) item 3

Pandoc默认情况下生成以下html页面:

1. item 1
2. item 2
  header
3. item 3

但是使用自定义编写器(我们使用了带有pandoc --print-default-data-file sample.lua的示例)时,它会生成以下内容:

1. item 1
2. item 2
  header
1. item 3

示例lua编写器包含以下用于有序列表处理的代码:

function OrderedList(items)
  local buffer = {}
  for _, item in pairs(items) do
    table.insert(buffer, "<li>" .. item .. "</li>")
  end
  return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
end

如果为items表中的对的第一个元素添加print

function OrderedList(items)
  local buffer = {}
  for elem, item in pairs(items) do
    print(elem)
    table.insert(buffer, "<li>" .. item .. "</li>")
  end
  return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
end

我们将只看到最终清单项目的数字:

1
2
1

因此,我认为问题不在于编写器本身。您有任何想法如何解决这个问题吗?

点赞
用户465100
用户465100

浏览pandoc源代码寻找自定义写入器(src/Text/Pandoc/Writers/Custom.hs),你可能会发现OrderedList函数实际上有四个参数,其中第三个是列表样式。你应该对Example列表样式感兴趣。因此,你可以相应地更新OrderedList实现:引入全局变量以计算Example列表中的总项目数,根据列表样式修改函数代码(为Example列表中的ol标签添加开始属性)。

-- 用于计数实例(@)
local ExampleIdx = 1

function OrderedList(items, num, sty, delim)
  local buffer = {}
  for _, item in pairs(items) do
    table.insert(buffer, "<li>" .. item .. "</li>")
  end
  local start = ""
  if sty == "Example" then
    if ExampleIdx > 1 then
      start = ' start="' .. ExampleIdx .. '" '
    end
    ExampleIdx = ExampleIdx + table.getn(items)
  end
  return '<ol' .. start .. '>\n' .. table.concat(buffer, "\n") .. "\n</ol>"
end
2014-08-28 17:23:22
用户1261777
用户1261777

实际上,你不需要像 Artem Pelenitsyn 的回答中那样保留一个 ExampleIdx 全局变量。你所要做的就是让你的有序列表项编写器对第二个参数(起始数字:num 在 Pelenitsyn 的代码中)敏感。请注意,可以使用 pandoc -t native 来检查传递给编写器的 AST;你会看到起始数字已由阅读器适当设置。

function OrderedList(items, num)
  local buffer = {}
  for _, item in pairs(items) do
     table.insert(buffer, "<li>" .. item .. "</li>")
  end
  local start = ""
  if num > 1 then
    start = ' start="' .. num .. '" '
  end
  return '<ol' .. start .. '>\n' .. table.concat(buffer, "\n") .. "\n</ol>"
end
2014-08-29 15:35:58