簡體   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