-t plain
选项有自己的Writer吗? ,如果是的话,在源代码中没有看到这样的https://github.com/jgm/pandoc/tree/main/src/Text/Pandoc/Writers(没有plain.hs
或类似的东西)
无论如何,我想为我自己的语法或一些类似的功能创建一些自定义对象。我的语法与
plain
类似,但有些东西经过了改进,这就是为什么我使用过滤器在 plain
之上构建它。
为了构建新对象,我使用
RawBlock
pandoc 的内置对象,它允许您指定一个名为 format
的任意字符串作为其第一个参数。
以这个为例
input03.hs
[ Header
1
( "the-following-should-be-a-rawblock" , [] , [] )
[ Str "The"
, Space
, Str "following"
, Space
, Str "should"
, Space
, Str "be"
, Space
, Str "a"
, Space
, Str "RawBlock"
]
, RawBlock
(Format "html")
"<div class=\"raw-html-block\">\n This is a raw HTML block.\n</div>"
]
我在做
pandoc -f native -t plain input03.hs --verbose
据我所见
[INFO] Not rendering RawBlock (Format "html") "<div class=\"raw-html-block\">\n This is a raw HTML block.\n</div>"
The following should be a RawBlock
我只是想知道如何通过过滤器修改 Writer?
我尝试过失败
filter01.lua
Writer = pandoc.scaffolding.Writer
Writer.Block.RawBlock = function (rb, opts)
return { rb.text }
end
但是正在做
pandoc -f native -t plain input03.hs -L filter01.lua --verbose
不打印 RawBlock
Writer = pandoc.scaffolding.Writer
Writer.Block.RawBlock = function (rb, opts)
return { rb.text }
end
所以我做了我自己的Writer。
writer01.lua
Writer = pandoc.scaffolding.Writer
Writer.Block.Header = function (h, opts)
return { nil }
end
Writer.Block.RawBlock = function (rb, opts)
return { rb.text }
end
达到预期效果
pandoc -f native -t writer01.lua input03.hs --verbose
<div class="raw-html-block">
This is a raw HTML block.
</div>
我没有弄清楚的问题是:
Writer
的
Plain
-L lua-filter
稍微修改 Writer ? (看起来不可能,因为根据他们在 Reader 之后和 Writer 之前执行的文档)nil
吗?我想出了一个解决方案,在我所说的
writer-boilerplate.lua
上处理所有 pandoc 对象,这可能在某种程度上接近于 plain
作者的行为。
如果您需要创建您的 Custom Writer ,这是一个很好的起点。如果您需要修改任何对象的行为,只需使用修改后的代码在样板函数之后剪切并粘贴即可。
-- WRITER BOILTERPLATE
-----------------------------------------------------------
Writer = pandoc.scaffolding.Writer
-- First we define all the Block elements Handlers
-- https://hackage.haskell.org/package/pandoc-types-1.12.4.7/docs/Text-Pandoc-Definition.html#t:Block
-- • data Block
-- □ = Plain [Inline], Para [Inline], LineBlock [[Inline]] , CodeBlock Attr Text , RawBlock Format Text , BlockQuote [Block] , OrderedList ListAttributes [[Block]] , BulletList [[Block]] , DefinitionList [([Inline], [[Block]])] , Header Int Attr [Inline] , HorizontalRule , Table Attr Caption [ColSpec] TableHead [TableBody] TableFoot , Figure Attr Caption [Block] , Div Attr [Block]
Writer.Block.Plain = function(el)
return Writer.Inlines(el.content)
end
Writer.Block.Para = function(el)
return Writer.Inlines(el.content) .. "\n\n"
end
Writer.Block.LineBlock = function(el)
local lines = {}
for _, line in ipairs(el.content) do
table.insert(lines, Writer.Inlines(line))
end
return table.concat(lines, "\n") .. "\n\n"
end
Writer.Block.CodeBlock = function(el)
return el.text .. "\n\n"
end
Writer.Block.RawBlock = function(el)
return el.text
end
Writer.Block.BlockQuote = function(el)
return Writer.Blocks(el.content)
end
Writer.Block.OrderedList = function(el)
local items = {}
for _, item in ipairs(el.content) do
table.insert(items, Writer.Blocks(item))
end
return table.concat(items, "\n")
end
Writer.Block.BulletList = function(el)
local items = {}
for _, item in ipairs(el.content) do
table.insert(items, Writer.Blocks(item))
end
return table.concat(items, "\n")
end
Writer.Block.DefinitionList = function(el)
local items = {}
for _, pair in ipairs(el.content) do
table.insert(items, Writer.Inlines(pair[1]))
for _, block in ipairs(pair[2]) do
table.insert(items, Writer.Blocks(block))
end
end
return table.concat(items, "\n")
end
Writer.Block.Header = function(el)
return Writer.Inlines(el.content) .. "\n\n"
end
Writer.Block.HorizontalRule = function()
return "-----\n\n"
end
Writer.Block.Table = function(el)
return Writer.Blocks(el.caption.long) .. "\n\n"
end
Writer.Block.Figure = function(el)
return Writer.Blocks(el.content)
end
Writer.Block.Div = function(el)
return Writer.Blocks(el.content)
end
-- Then we define all the Inline Objects handlers
-- • data Inline
-- □ = Str Text , Emph [Inline] , Underline [Inline] , Strong [Inline] , Strikeout [Inline] , Superscript [Inline] , Subscript [Inline] , SmallCaps [Inline] , Quoted QuoteType [Inline] , Cite [Citation] [Inline] , Code Attr Text , Space , SoftBreak , LineBreak , Math MathType Text , RawInline Format Text , Link Attr [Inline] Target , Image Attr [Inline] Target , Note [Block] , Span Attr [Inline]
Writer.Inline.Str = function(el)
return el.text
end
Writer.Inline.Emph = function(el)
return Writer.Inlines(el.content)
end
Writer.Inline.Underline = function(el)
return Writer.Inlines(el.content)
end
Writer.Inline.Strong = function(el)
return Writer.Inlines(el.content)
end
Writer.Inline.Strikeout = function(el)
return Writer.Inlines(el.content)
end
Writer.Inline.Superscript = function(el)
return Writer.Inlines(el.content)
end
Writer.Inline.Subscript = function(el)
return Writer.Inlines(el.content)
end
Writer.Inline.SmallCaps = function(el)
return Writer.Inlines(el.content)
end
Writer.Inline.Quoted = function(el)
return Writer.Inlines(el.content)
end
Writer.Inline.Cite = function(el)
return Writer.Inlines(el.content)
end
Writer.Inline.Code = function(el)
return el.text
end
Writer.Inline.Space = function()
return " "
end
Writer.Inline.SoftBreak = function()
return " "
end
Writer.Inline.LineBreak = function()
return "\n"
end
Writer.Inline.Math = function(el)
return el.text
end
Writer.Inline.RawInline = function(el)
return el.text
end
Writer.Inline.Link = function(el)
return Writer.Inlines(el.content)
end
Writer.Inline.Image = function(el)
return el.src
end
Writer.Inline.Note = function(el)
return Writer.Blocks(el.content)
end
Writer.Inline.Span = function(el)
return Writer.Inlines(el.content)
end
-- EOF EOF EOF WRITER BOILTERPLATE
-----------------------------------------------------------