[英]Pandoc Lua : how to add a markdown block around a header without losing the markdown syntax #
[英]How to add a page break before header automatically in pandoc
我需要將 Markdown 文檔轉換為 Word 文檔。 我需要它,以便在每個標題之前都有一個分頁符。
例如,在這里,將在# Heading 1
和# Heading 2
之前插入分頁# Heading 2
。 如果文檔中沒有內容,我不介意它跳過第一個。
# Heading 1
Hello
# Heading 2
World
我找到了這個 Lua 過濾器,但是,據我所知,它只是用於 Latex 而不是自動的。
我曾嘗試自己編寫 Lua 腳本,但失敗了,因為我不太了解 pandoc 的工作原理。
例如,我嘗試從Header
函數返回一個帶有前一個內容的Para
+ 一個分頁符,如下所示:
function Header(el)
if el.level == 1 then
local t = { pandoc.RawBlock('openxml', '<w:p><w:r><w:br w:type="page"/></w:r></w:p>'), el }
return pandoc.Para(t)
end
return el
end
但是,它最終會擦除所有標題。 我嘗試了相同想法的其他一些改動,但都沒有奏效。
Lua的問題主要是沒有靜態分析,沒有Intellisense。 我可能在這段代碼中做了一些完全不合適的事情,沒有任何編譯器錯誤,甚至沒有意識到。
我相信以下應該解決這個問題:
local pagebreak = '<w:p><w:r><w:br w:type="page"/></w:r></w:p>'
function Header(el)
if el.level == 1 then
-- return both the page break and the header as a list
return { pandoc.RawBlock('openxml', pagebreak), el }
end
-- No `return el` needed, as not returning any value is the same as
-- returning the original value.
end
至少可以說,在解組過程中 pandoc 的錯誤報告,即從 Lua 檢索對象時,並不是很好。 理想情況下, pandoc 應該報告由於在Para
包裝“塊”列表而產生的類型錯誤,因為該構造函數應該只接受“內聯”。
如果您好奇:我正在通過切換到不同的編組策略來解決此問題,請參閱此 PR 。 這需要一段時間,但我們會到達那里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.