Search code examples
filterluapandoc

Return the text string of el.content in pandoc filter


When using pandoc filter, I just want to get the text of el.content, but it return a Table!

The .md as follows(just for debug):

[It's so easy!]{color="red"}. Today is Monday.

I want to get the string It's so easy! be printed. So, I write the code:

function Span(el)
  color  = el.attributes['color']
  strTxt = el.content
  print(strTxt)
end

but it's not true! By using el.text also the same!


Solution

  • The module pandoc.utils contains a function stringify which will convert an element into a list:

    function Span(el)
      -- Print just the text in the span; removes all markup.
      print(pandoc.utils.stringify(el))
    end
    

    This will print It’s so easy! (note the effect of pandoc's smart handling of apostrophes: a closing curly quote has replaced the straight apostrophe ').

    It's also possible to print the output in specific markup language (requires pandoc 2.17 or later):

    function Span(el)
      -- Prints the span's contents as Markdown
      print(pandoc.write(pandoc.Pandoc{pandoc.Plain(el)}, 'markdown'))
    end
    

    Consult the Lua filters docs for more info on how to use them.