Hi all,
There’s something which I don’t quite understand in with respect to the following:
As far as I can tell, lua filters and classic style writers are pretty much identical, basically defining Lua functions corresponding to node names, and adding the code inside these functions. The only notable difference I can think of is that classic style writers return raw output (i.e. strings) from these functions, whereas filters typically return some other pandoc object, or node in the AST, effectively modifying it during runtime.
Enter new style writers. The only example I have been able to find of these writers is the one in the main documentation, and it is super confusing:
function Writer (doc, opts)
local filter = {
CodeBlock = function (cb)
-- only modify if code block has no attributes
if cb.attr == pandoc.Attr() then
local delimited = '```\n' .. cb.text .. '\n```'
return pandoc.RawBlock('markdown', delimited)
end
end
}
return pandoc.write(doc:walk(filter), 'gfm', opts)
end
Here we have a global function called Writer
which takes in doc
, a pandoc.Pandoc
and then proceeds to defining a filter which it then applies to this document using the walk
method. It then uses the gfm
writer to actually generate the raw output. So basically, a writer within a writer.
I’m not sure if this is just a bad example, or whether I’m missing something. It would have been more useful to rewrite the example provided for classic style writers in the new style , in my opinion.
Are there any more down-to-earth examples for making writers in the new style? Why are we defining filters inside these new style writers? Couldn’t we just make a simple filter instead?
Regards,
Amine