2017-12-20 13 views
1

私はデータを要約するのに役立つpandocフィルタを作成しようとしています。目次を作成するフィルタがいくつか見られましたが、ヘッダー内のコンテンツに基づいてインデックスを整理したいと思います。例えばPandocルアフィルタの文字列フラグメントの連結

、私は、ヘッダーでタグ付けされた日付に基づいてコンテンツの要約を提供したいの下に(一部のヘッダが、日付は含まれません...)

[[email protected] foo]$ cat test.md 
# 1 May 2018 
some info 

# not a date 
some data 

# 2 May 2018 
some more info 

私が見しようとすることでスタートを切りましたヘッダーの内容意図は、異なる日付/時刻パターンに対して単純な正規表現を適用することでした。

[[email protected] foo]$ cat test.lua 
function Header(el) 
    return pandoc.walk_block(el, { 
    Str = function(el) 
     print(el.text) 
    end }) 
end 

残念ながら、これはむしろ私は、全体のヘッダーの内容を分析することができ連結より各スペースで区切られた文字列、の印刷状態を適用するようだ:行うための標準的な方法はあり

[[email protected] foo]$ pandoc --lua-filter test.lua test.md 
1 
May 
2018 
not 
... 

これはフィルターですか?私はまだLuaフィルターのドキュメントでヘルパー関数を見ていません。

+0

あなたが代わりに 'Str'の' Header'、で一致する必要があります。詳細はhttps://pandoc.org/lua-filters.htmlを参照してください... – mb21

答えて

1

更新:devバージョンには、新しい機能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機能を提供する計画を持っています。これらを使用すると、ヘッダが次のコードで日付が含まれているかどうかをテストすることができます。一方

、(this discussionから取られた)次のスニペットは、あなたが必要なものを手に入れるお手伝いをする必要があります

--- 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