[英]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.stringify
和pandoc.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.