[英]How to refresh/preserve HTML formatting after javascript edit using JQuery Mobile
[英]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
元素可以嵌入像Para
或CodeBlock
這樣的塊元素。
以下過濾器僅處理Para
和CodeBlock
元素。 格式化保留。
由於Para
元素是內聯元素的列表,因此很明顯在Span
元素中重用這些元素。
CodeBlock
文本也可以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
元素嵌入其他類型的塊元素(如BulletList
或Table
),則必須為walk_inline
函數開發特定的過濾器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.