简体   繁体   English

如何使用 lua 过滤器修改 Pandoc 中的内联元素?

[英]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.我是 Pandoc 和 Lua 的初学者,正在尝试将 Word 文档转换为 Markdown。我想将 Word 中的章节标题转换为 Markdown 中的文本段落。此外,我想在章节标题前后插入一些文本。

To achieve that, I used the following lua filter (sample.lua)为此,我使用了以下 lua 过滤器 (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 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.其中 file.docx 只是包含一个章节标题的最小示例文档。 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.但是因为(如果我没记错的话)el.content 是内联元素,所以它周围有换行符。 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.我试图通过使用 pandoc 文档和各种 lua 函数来解决这个问题,但无济于事,这就是为什么我会请求帮助。

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.原因是el.content是一个内联元素列表,可以通过将列表与附加内容连接起来进行扩展。 The operator .. is the concatenation operator;运算符..是串联运算符; it works with strings and pandoc lists.它适用于字符串和 pandoc 列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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