Difference between revisions of "Verbatim XML"

From Wiki
Jump to navigation Jump to search
(Created page with "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. Han...")
(No difference)

Revision as of 08:15, 13 February 2015

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)

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

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

\startxmlsetups xml:json
    \pushcatcodetable
    \setcatcodetable\ctxcatcodes
    \xmlfunction{#1}{processJSON}
    \popcatcodetable
\stopxmlsetups