Pandoc:Lua过滤器,将{{helloworld}}替换为 abc

问题描述 投票:0回答:1

manual中,我找到了一个pandoc lua过滤器的示例:

return {
  {
    Str = function (elem)
      if elem.text == "{{helloworld}}" then
        return pandoc.Emph {pandoc.Str "Hello, World"}
      else
        return elem
      end
    end,
  }
}

我想将{{helloworld}}替换为<div>abc</div>。我的尝试:

return {
  {
    Str = function (elem)
      if elem.text == "{{helloworld}}" then
        return pandoc.RawInline('html','<div>abc</div>')
      else
        return elem
      end
    end,
  }
}

...但这给了我以下输出:

<p></p>
<div>abc</div>
<p></p>

如何摆脱空白的p标签?

其他信息

我从markdown转换为html,并且markdown文件如下所示:

enter image description here

编辑1

基于@ mb21的想法,我将Str更改为Para,将RawInline更改为RawBlock,将elem.text更改为elem.content,但这不起作用:

return {
  {
    Para = function (elem)
      if elem.content == "{{helloworld}}" then
        return pandoc.RawBlock('html','<div>abc</div>')
      else
        return elem
      end
    end,
  }
}

编辑2

此方法...

return {
  {
    Para = function (elem)
      if elem.content[1].text == "{{helloworld}}" then
        return pandoc.RawBlock('html','<div>abc</div>')
      else
        return elem
      end
    end,
  }
}
lua pandoc
1个回答
0
投票

Pandoc在段落中包装内联元素...,因此您需要将RawInline替换为RawBlock,并在Paragraph而不是Str上进行匹配:

return {
  {
    Para = function (elem)
      if elem.content[1].text == "{{helloworld}}" then
        return pandoc.RawBlock('html','<div>abc</div>')
      else
        return elem
      end
    end,
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.