简体   繁体   中英

How to modify an inline element in Pandoc using lua filter?

I am beginner in Pandoc and Lua who is experimenting with converting Word documents to Markdown. I want to convert chapter headings in Word to paragraph of text in Markdown. Furthermore, I want to insert some text before and after the chapter headings.

To achieve that, I used the following lua filter (sample.lua)

function Header(el)
    if el.level == 1 then
       return {"something before (",el.content,") something after"}
    end
end

after which I performed the conversion using

pandoc --lua-filter=sample.lua -s file.docx -t markdown -o file.txt

where file.docx is just minimal example document containing one chapter heading. However, using this filter, I obtained

something before (

Chapter title

) something after

What I want to get, however, is

something before (Chapter title) something after

but since (if I am not mistaken) el.content is inline element, there are linebreaks around it. I tried to solve this problem by using pandoc documentation and various lua functions, but to no avail, which is why I would kindly ask for help.

Try this instead:

function Header(el)
  if el.level == 1 then
    return {{"something before ("} .. el.content .. {") something after"}}
  end
end

The reason is that el.content is a list of inline elements, and it can be be extended by concatenating lists with additional content. The operator .. is the concatenation operator; it works with strings and pandoc lists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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