2017-12-20 2 views
1

데이터를 요약하는 데 도움이 될 팬드 필터를 만들려고합니다. 목차를 만드는 몇 가지 필터를 보았지만 헤더 내에있는 내용을 기반으로 색인을 구성하고 싶습니다. 예를 들어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