【发布时间】:2019-01-04 00:57:06
【问题描述】:
我尝试实现CSS Generated Content for Paged Media Module 中定义的脚注。
使用此定义,脚注必须内联spans。
我写了pandoclua 过滤器的初稿。
这是我的第一个 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 元素返回一个空字符串。
所以,当我对以下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>
代码块丢失。有什么办法可以同时保留脚注的格式和代码块?
【问题讨论】:
-
这永远不会如你所愿。请参阅p tag 的 HTML 规范。一旦找到任何块级元素的开始标签,
p元素就会关闭。因此,您不能将块级元素放在段落中。span也是如此,它是一个 phrasing content 元素,只能包含其他短语内容元素。我猜 Pandoc 的 HTML 渲染器知道这一点,并拒绝在不允许的地方输出块级标签。 -
您引用的规范仅在脚注示例中显示内联级别的内容。而footnote display property 提供了一种方法来指示内容是应该显示为“内联”还是“块”内容。这是必要的,因为脚注本身实际上不能是包含它的
span标记内的块内容。换句话说,规范只提供了一个脚注,其中包含不超过一个段落的内容,但只有 content ,而不是p本身。 -
有类似Bigfoot.js(网站当前关闭,请参阅GitHub project)的东西,它们显示脚注以便它们显示为内联,但实际上它们使用Markdown的常见脚注标记(所有脚注在末尾document) 并使用一些 JavaScript 和 CSS 使它们显示为内联。这与 CSS Generated Content for Paged Media Module 规范非常不同,它们实际上是内联的。
-
@Waylan 感谢您的 cmets!按照您的回复顺序:1/让我感到惊讶的是pandoc对
Note元素的定义hackage.haskell.org/package/pandoc-types-1.17.5.1/docs/…它是一个由块元素列表组成的内联元素(这对我的大脑来说很奇怪)。但是,我同意你的观点:我只能处理措辞内容。但是,我认为应该可以将一些简单的格式保留为粗体或 emph。 -
@Waylan 2/ 我同意。这就是为什么我尝试使用
<br/>(在 pandoc 中命名为LineBreak)。 3/ 我更喜欢避免使用 JS 进行任何 DOM 操作(或仅使用非常简单的脚本)。原因是我使用Prince制作pdf(html内容不打算在浏览器中使用)。
标签: html filter lua markdown pandoc