gpt4 book ai didi

html - Pandoc lua 过滤器 : How to preserve footnotes' formatting?

转载 作者:搜寻专家 更新时间:2023-10-31 08:09:15 25 4
gpt4 key购买 nike

我尝试实现 CSS Generated Content for Paged Media Module 中定义的脚注.
根据这个定义,脚注必须是内联的 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() 函数,格式会丢失):简单脚注和多 block 脚注可以很好地呈现。

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

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

因此,当我在以下 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 元素可以嵌入 block 元素,例如 ParaCodeBlock

下面的过滤器只处理ParaCodeBlock 元素。保留格式。

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

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 元素嵌入其他类型的 block 元素(如 BulletListTable),则必须开发一个特定的过滤器对于 walk_inline 函数。

关于html - Pandoc lua 过滤器 : How to preserve footnotes' formatting?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51548827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com