繁体   English   中英

Pandoc lua过滤器:如何保留脚注的格式?

[英]Pandoc lua filter: How to preserve footnotes' formatting?

我尝试实现分页媒体模块的CSS生成内容中定义的脚注。
根据这个定义,脚注必须是内联span 我写了一个pandoc lua过滤器的初稿。
这是我的第一个pandoc过滤器(也是我第一次在lua编码)。

这是过滤器:

Note = function (elem)
  local textContent = {}
  local content = elem.content
  for i = 1, #content do
    textContent[2*i-1] = pandoc.Str(pandoc.utils.stringify(content[i]))
    if i < #content
    then
      textContent[2*i] = pandoc.LineBreak()
    end
  end
  return pandoc.Span(textContent, pandoc.Attr("", {"footnote"}, {}))
end

它适用于带有无格式文本的脚注(由于使用了stringify()函数,格式会丢失):简单的脚注和多个块脚注都可以很好地呈现。

为了保留格式,我尝试在Note元素的content上使用walk_block()函数,但是我无法获得任何结果。

我遇到了第二个问题: stringify()函数返回CodeBlock元素的void字符串。

所以,当我在以下markdown文本上使用此过滤器时:

Here is a footnote reference,[^1] and another.[^longnote]

[^1]: Here is the footnote.

[^longnote]: Here's one with multiple blocks.

    Subsequent paragraphs are indented to show that they
belong to the previous footnote.

        { some.code }

    The whole paragraph can be indented, or just the first
    line.  In this way, multi-paragraph footnotes work like
    multi-paragraph list items.

This paragraph won't be part of the note, because it
isn't indented.

我获得以下HTML片段:

<p>
  Here is a footnote reference,
  <span class="footnote">Here is the footnote.</span>
  and another.
  <span class="footnote">Here’s one with multiple blocks.
    <br />
    Subsequent paragraphs are indented to show that they belong to the previous footnote.
    <br />
    <br />
    The whole paragraph can be indented, or just the first line. In this way, multi-paragraph footnotes work like multi-paragraph list items.
  </span>
</p>
<p>This paragraph won’t be part of the note, because it isn’t indented.</p>

代码块丢失了。 有没有办法保持脚注的格式和代码块?

我找到了如何处理Note元素。

首先, Note元素是一个内联元素,因此我们可以使用walk_inline 奇怪的是, Note元素可以嵌入像ParaCodeBlock这样的块元素。

以下过滤器仅处理ParaCodeBlock元素。 格式化保留。

由于Para元素是内联元素的列表,因此很明显在Span元素中重用这些元素。
CodeBlock文本也可以CodeBlockCode元素中处理。

local List = require 'pandoc.List'

Note = function (elem)
  local inlineElems = List:new{} -- where we store all Inline elements of the footnote
  -- Note is an inline element, so we have to use walk_inline
  pandoc.walk_inline(elem, {
    -- Para is a list of Inline elements, so we can concatenate to inlineElems
    Para = function(el)
      inlineElems:extend(el.content)
      inlineElems:extend(List:new{pandoc.LineBreak()})
    end,
    -- CodeBlock is a block element. We have to store its text content in an inline Code element
    CodeBlock = function(el)
      inlineElems:extend(List:new{pandoc.Code(el.text, el.attr), pandoc.LineBreak()})
    end
  })
  table.remove(inlineElems) -- remove the extra LineBreak
  return pandoc.Span(inlineElems, pandoc.Attr("", {"footnote"}, {}))
end

如果Note元素嵌入其他类型的块元素(如BulletListTable ),则必须为walk_inline函数开发特定的过滤器。

暂无
暂无

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

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