Open main menu

Changes

1,145 bytes added ,  08:15, 13 February 2015
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..."
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 [http://www.ntg.nl/pipermail/ntg-context/2015/080914.html 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)

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

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

<texcode>
\startxmlsetups xml:json
\pushcatcodetable
\setcatcodetable\ctxcatcodes
\xmlfunction{#1}{processJSON}
\popcatcodetable
\stopxmlsetups
</texcode>
3

edits