I have a not-so-old "pseudo-writer" implemented as a filter which mostly injects tons of raw markup into the document tree so that the output of the plain writer looks like my target format, returning a table of filters at the end like this
``````lua
return {
{
Meta = get_config
},
{
SmallCaps = SmallCaps,
Str = Str
},
note_filter,
main_filter
}
``````
where `get_config` just extracts some info from the metadata and populates a table declared at the top of the script which the functions in the other filters refer to.
Am I right that I in principle could convert this into a new style custom writer by removing the Meta filter, replacing `return {<filters>}` above with `local filters = {<filters>}` and adding the following at the bottom of the script?
``````lua
function Writer (doc, opts)
get_config(doc.meta)
for _,filter in ipairs(filters) do
doc = doc:walk(filter)
end
doc.blocks = blocks
return pandoc.write(doc, 'plain', opts)
``````
(This got me thinking: what if `walk` accepted one more argument which will be passed as an extra argument to the functions in the filters, which could be used to pass config and/or maintain state? It somehow feels more intuitive than defining a table outside the metadata handler which all handlers close around.)
(BTW the main reason that the `note_filter` is separate is that it needs to be topdown because it counts and collects the notes, while `main_filter` needs to be bottomup because it sometimes relies on the content of elements already having been processed. I was very happy when I realized that this would work!)