Verbatim XML

From ConTeXt wiki

Processing elements in XML that should be typeset verbatim (for example code snippets) is a tricky matter, since during XML processing, the catcode table is a different one.

Hans came up with a quick but powerful solution, that demonstrates two important things:

  • Processing a buffer from XML content in Lua
  • Temporarily (re)setting the catcode table to allow buffers to work.

This can come in handy in more situations.

First, define a xml function to process the xml element manually. (In this example, vimtyping is used to format a JSON buffer)

    function xml.functions.processJSON(t)
        buffers.assign("foo","\\startJSON\n" .. tostring(xml.text(t)) .. "\n\\stopJSON")
        context.getbuffer { "foo" }

The actual XML setup to process the <json>...</json> element looks as follows. This also handles the catcode table manipulation.

\startxmlsetups xml:json