在Pandoc lua筛选器中连接字符串片段
我正在尝试创建一个pandoc过滤器,它将帮助我汇总数据。我见过一些创建目录的过滤器,但我想根据在标题中找到的内容组织索引 例如,下面我想提供一个基于标题中标记日期的内容摘要(一些标题不包含日期…) 我首先尝试查看标题的内容。其目的是为不同的日期/时间模式应用一个简单的正则表达式在Pandoc lua筛选器中连接字符串片段,lua,pandoc,Lua,Pandoc,我正在尝试创建一个pandoc过滤器,它将帮助我汇总数据。我见过一些创建目录的过滤器,但我想根据在标题中找到的内容组织索引 例如,下面我想提供一个基于标题中标记日期的内容摘要(一些标题不包含日期…) 我首先尝试查看标题的内容。其目的是为不同的日期/时间模式应用一个简单的正则表达式 [nwatkins@sapporo foo]$ cat test.lua function Header(el) return pandoc.walk_block(el, { Str = function(
[nwatkins@sapporo foo]$ cat test.lua
function Header(el)
return pandoc.walk_block(el, {
Str = function(el)
print(el.text)
end })
end
不幸的是,这似乎为每个空格分隔的字符串应用了打印状态,而不是允许我分析整个标题内容的串联:
[nwatkins@sapporo foo]$ pandoc --lua-filter test.lua test.md
1
May
2018
not
...
在过滤器中有没有一种规范的方法可以做到这一点?我还没有在Lua过滤器文档中看到任何帮助函数。更新:开发版本现在提供了新函数
pandoc.utils.stringify
和pandoc.utils.normalize\u date
。它们将成为下一个pandoc版本(可能是2.0.6)的一部分。通过这些,您可以使用以下代码测试标题是否包含日期:
function Header (el)
content_str = pandoc.utils.stringify(el.content)
if pandoc.utils.normalize_date(content_str) ~= nil then
print 'header contains a date'
else
print 'not a date'
end
end
目前还没有helper函数,但我们计划在不久的将来提供一个
pandoc.utils.tostring
函数
同时,以下代码片段(摘自)应能帮助您获得所需内容:
--- convert a list of Inline elements to a string.
function inlines_tostring (inlines)
local strs = {}
for i = 1, #inlines do
strs[i] = tostring(inlines[i])
end
return table.concat(strs)
end
-- Add a `__tostring` method to all Inline elements. Linebreaks
-- are converted to spaces.
for k, v in pairs(pandoc.Inline.constructor) do
v.__tostring = function (inln)
return ((inln.content and inlines_tostring(inln.content))
or (inln.caption and inlines_tostring(inln.caption))
or (inln.text and inln.text)
or " ")
end
end
function Header (el)
header_text = inlines_tostring(el.content)
end
您需要匹配
标题,而不是Str
。有关更多信息,请参阅。。。
--- convert a list of Inline elements to a string.
function inlines_tostring (inlines)
local strs = {}
for i = 1, #inlines do
strs[i] = tostring(inlines[i])
end
return table.concat(strs)
end
-- Add a `__tostring` method to all Inline elements. Linebreaks
-- are converted to spaces.
for k, v in pairs(pandoc.Inline.constructor) do
v.__tostring = function (inln)
return ((inln.content and inlines_tostring(inln.content))
or (inln.caption and inlines_tostring(inln.caption))
or (inln.text and inln.text)
or " ")
end
end
function Header (el)
header_text = inlines_tostring(el.content)
end