You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was thinking about how to support the new table style in custom writers (#6573), the asymmetry between custom reader and writer, and how writers for non context-free languages sometimes require obscure hacks (e.g. https://groups.google.com/d/msgid/pandoc-discuss/e249e0e0-d71e-4476-9d1f-12f7d3f2acf5%40googlegroups.com). That's why I started to think about an alternative interface that could be setup in parallel to the existing one.
One idea I had was to check for the existence of a global Writer function and to use it if present. It would get two arguments, the Pandoc document and the writer options, along the lines of custom readers. The full responsibility of rendering the document would then be on that function.
As a convenience, we could add a helper pandoc.writer.make that works similar to a Lua filter, but expects functions to return strings (or Doc elements in case we decide to support that via a doclayout module). It would generate a function that converts any AST element to a string.
localwrite_htmlwrite_html=pandoc.writer.make{
-- expects string return valuesSpace=function (_) return'' end,
-- allows to use pandoc.writeStr=function (s) returnpandoc.write(pandoc.Pandoc(s.text), 'html') end,
-- recurse on element contentEmph=function (emph)
return ('<em>%s</em>'):format(write_html(emph.content))
end,
Para=function (p)
-- closing tag omitted intentionally, as suggested by-- some HTML style guides.return ('<p>%s'):format(write_html(p.content))
end,
-- Improved error handling by making this the default implementation for-- catch-all functions of a type.Block=function (b) error(('no converter for block of type %s')format(b.t)) end
}
functionWriter(doc, writer_options)
localhead= ('<head><title>%s</title>\n'):write_html(doc.meta.title)
localbody='<body>' ..write_html(doc.blocks)
return'<html>\n' ..head..bodyend
A possible downside that I see is that the additional capabilities could blur the distinction between filter and writer, but that could be a good thing in cases where a filter is coupled to a custom writer.
The text was updated successfully, but these errors were encountered:
One thing people have sometimes requested is the ability to just define certain functions in a custom writer, and have everything else "the same as usual." e.g. override the treatment of Link in HTML while keeping everything else the same.
This can be done using a filter that replaces a link with a RawInline in the output format. But perhaps there'd be a way of facilitating this in custom writers? e.g. make_variant("html", { Link = ... })? This would just be shorthand for "apply a filter then call the html writer". Just thinking aloud.
One thing people have sometimes requested is the ability to just define certain functions in a custom writer, and have everything else "the same as usual." e.g. override the treatment of Link in HTML while keeping everything else the same.
This can be done using a filter that replaces a link with a RawInline in the output format. But perhaps there'd be a way of facilitating this in custom writers? e.g. make_variant("html", { Link = ... })? This would just be shorthand for "apply a filter then call the html writer". Just thinking aloud.
Awesome idea. I've had a question about custom Haskell writers on the mailing list a few weeks ago. This here is the primary use case I've had in mind.
I was thinking about how to support the new table style in custom writers (#6573), the asymmetry between custom reader and writer, and how writers for non context-free languages sometimes require obscure hacks (e.g. https://groups.google.com/d/msgid/pandoc-discuss/e249e0e0-d71e-4476-9d1f-12f7d3f2acf5%40googlegroups.com). That's why I started to think about an alternative interface that could be setup in parallel to the existing one.
One idea I had was to check for the existence of a global
Writer
function and to use it if present. It would get two arguments, thePandoc
document and the writer options, along the lines of custom readers. The full responsibility of rendering the document would then be on that function.As a convenience, we could add a helper
pandoc.writer.make
that works similar to a Lua filter, but expects functions to return strings (orDoc
elements in case we decide to support that via a doclayout module). It would generate a function that converts any AST element to a string.A possible downside that I see is that the additional capabilities could blur the distinction between filter and writer, but that could be a good thing in cases where a filter is coupled to a custom writer.
The text was updated successfully, but these errors were encountered: