繁体   English   中英

在Pandoc lua过滤器中串联字符串片段

[英]Concatenating string fragments in Pandoc lua filters

我正在尝试创建一个pandoc过滤器,以帮助我汇总数据。 我已经看到了一些创建目录的过滤器,但是我想根据标题中的内容来组织索引。

例如,下面我想根据标题中的标记日期提供内容摘要(某些标题将不包含日期...)

[nwatkins@sapporo foo]$ cat test.md
# 1 May 2018
some info

# not a date
some data

# 2 May 2018
some more info

我首先尝试查看标题的内容。 目的是仅将简单的正则表达式应用于不同的日期/时间模式。

[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.stringifypandoc.utils.normalize_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

尚无辅助函数,但我们计划在不久的将来提供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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM