Pandoc lua filter: How to preserve footnotes' formatting?

RLesur

I try to implement footnotes as defined in CSS Generated Content for Paged Media Module.
With this definition, footnotes have to be inline spans. I wrote a first draft of a pandoc lua filter.
It is my first pandoc filter (and also the first time I code in lua).

Here's the filter:

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

It works well for footnotes with unformatted text (formatting is lost due to the use of the stringify() function): simple footnotes and multiple blocks footnotes are well rendered.

In order to preserve formatting, I tried to use the walk_block() function on the content of the Note element, but I cannot obtain any result.

I got a second problem: the stringify() function returns a void character string for CodeBlock elements.

So, when I use this filter on the following markdown text:

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.

I obtain the following HTML fragment:

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

The code block is lost. Is there any way to keep both footnotes' formatting and code blocks?

RLesur

I found how to process the Note element.

First, the Note element is an inline element, so we can use walk_inline. The weird thing is that a Note element can embed block elements like Para or CodeBlock.

The following filter deal only with Para and CodeBlock elements. Formatting is kept.

Since a Para element is a list of inline elements, it is obvious to reuse those elements in a Span element.
The CodeBlock text can also be processed in an inline Code element.

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

If the Note element embeds other types of block elements (like a BulletList or a Table), one has to develop a specific filter for the walk_inline function.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can I copy each worksheet into a new workbook and preserve formatting

分類Dev

How to protect Word footnotes or endnotes from accidental deletion?

分類Dev

Filter cascading CheckedListBox and preserve check state of the items

分類Dev

Returning two elements on a filter return Pandoc-filter

分類Dev

Pandocのlua-filter関数でリストアイテムを反復処理するにはどうすればよいですか?

分類Dev

How to run pandoc with a keybinding in Vim?

分類Dev

Date Filter incorrectly formatting the date value

分類Dev

How to achieve this formatting

分類Dev

How to formatting accounting code

分類Dev

Pandoc Luaフィルター:Span要素の属性を指定する方法

分類Dev

luaとwalk_blockのpandocフィルター

分類Dev

htmlを追加するpandocのLuaフィルター

分類Dev

How to preserve attachment content between render passes

分類Dev

How to preserve initial state in React Component?

分類Dev

how to preserve selection when sorting QTableView

分類Dev

How to specify the font used for word doc exported using pandoc?

分類Dev

How to add an image with alt tag but without caption in Pandoc?

分類Dev

How to register member function to lua without lua bind in c++

分類Dev

How does "isalpha" take its formatting?

分類Dev

How to reduce the number of repetitions for formatting many tables?

分類Dev

How do I conditionally parse formatting with Thymeleaf?

分類Dev

How to display a repeated list with angularjs and html formatting?

分類Dev

How to preserve shell script line spacing when reading stdout?

分類Dev

How do I preserve event listeners after sort?

分類Dev

IIS URLrewrite: How to preserve original host name for internal redirects?

分類Dev

How can I preserve entered indent spaces in batch on user prompt?

分類Dev

How can I preserve padding as an element boundary in a contenteditable div?

分類Dev

How can I preserve padding as an element boundary in a contenteditable div?

分類Dev

How can I preserve certain files when compiling as a single .exe?

Related 関連記事

  1. 1

    How can I copy each worksheet into a new workbook and preserve formatting

  2. 2

    How to protect Word footnotes or endnotes from accidental deletion?

  3. 3

    Filter cascading CheckedListBox and preserve check state of the items

  4. 4

    Returning two elements on a filter return Pandoc-filter

  5. 5

    Pandocのlua-filter関数でリストアイテムを反復処理するにはどうすればよいですか?

  6. 6

    How to run pandoc with a keybinding in Vim?

  7. 7

    Date Filter incorrectly formatting the date value

  8. 8

    How to achieve this formatting

  9. 9

    How to formatting accounting code

  10. 10

    Pandoc Luaフィルター:Span要素の属性を指定する方法

  11. 11

    luaとwalk_blockのpandocフィルター

  12. 12

    htmlを追加するpandocのLuaフィルター

  13. 13

    How to preserve attachment content between render passes

  14. 14

    How to preserve initial state in React Component?

  15. 15

    how to preserve selection when sorting QTableView

  16. 16

    How to specify the font used for word doc exported using pandoc?

  17. 17

    How to add an image with alt tag but without caption in Pandoc?

  18. 18

    How to register member function to lua without lua bind in c++

  19. 19

    How does "isalpha" take its formatting?

  20. 20

    How to reduce the number of repetitions for formatting many tables?

  21. 21

    How do I conditionally parse formatting with Thymeleaf?

  22. 22

    How to display a repeated list with angularjs and html formatting?

  23. 23

    How to preserve shell script line spacing when reading stdout?

  24. 24

    How do I preserve event listeners after sort?

  25. 25

    IIS URLrewrite: How to preserve original host name for internal redirects?

  26. 26

    How can I preserve entered indent spaces in batch on user prompt?

  27. 27

    How can I preserve padding as an element boundary in a contenteditable div?

  28. 28

    How can I preserve padding as an element boundary in a contenteditable div?

  29. 29

    How can I preserve certain files when compiling as a single .exe?

ホットタグ

アーカイブ