Changes

Jump to navigation Jump to search
3,108 bytes added ,  11:57, 24 April 2021
m
xml.preprocessor tracing
== General ==
If you process xml with ConTeXt Mkiv, you have the power of the Lua language at your fingertips. All <code>\xml...</code> commands have their Lua counterparts. Unfortunately, this is not yet fully documented, so this wiki page is a place-holder until Hans finishes this section in the [http://pragma-ade.com/show-man-43.htm Mkiv manual].
A few general observations to help you getting started:
* There are two sets of Lua commands: the <code>xml... </code> commands allow you to work with the content of your xml elements within Lua; <code>lxml... </code> commands pass the content to ConTeXt and typeset it.
* Both commands receive the content as a Lua table.
== The ConTeXt style file ==
Next, we write the environment file <tt>prices-style.tex</tt> which we will use to process our list; on the command line, you use <tt>context --environment=prices-style prices.xml</tt>. As an exercise, we will use Lua for all xml elements (though this doesn't make sense in most cases). If you have already read the [http://pragma-ade.com/show-man-43.htm manual], most of this will be familiar. The first line connects our environment with the Lua file where all the Lua code will go. The last lines set up a ConTeXt table, since this is how we want to display the information:
<texcode>
end
</pre></code>
What does this do? As you can see, we mostly use the <ttcode>context...</ttcode> commands which are described in the [http://www.pragma-ade.com/general/manuals/cld-mkiv.pdf cld manual]. They are Lua functions which print to ConTeXt. With the <ttcode>list</ttcode> function, we start a ConTeXt table and typeset a first row with the meta informationfor our list. The functions for <ttcode>item</ttcode> and <ttcode>model</ttcode> do nothing more than print the argument of the connected elements as table rows and as the first table column. Things get a bit more interesting for the <ttcode>price</ttcode> function: this is where we use the power of Lua to do some easy calculations and convert the Euros to other currencies. First, we extract the content of the <code><price></code> element. We need to tell Lua that this content is not a string, but a number, hence the use of the <code>tonumber</code> function. Within a ConTeXt environment, we would use <code>\xmltext{#1}{./}</code>. The Lua equivalent is <code>xml.text(t, "./")</code>. Now that we have this content as a number, we can perform all kinds of arithmetic operations on it. Likewise, if it were a string, we could use Lua string manipulations on it – that's the good thing about using Lua, you have the full power of the language at your disposal. And finally, we typeset the results of our arithmetic operations in table cells. This is just one example of what you can do with Lua. Here are a few more suggestions for things which are easier to do in Lua than in pure ConTeXt: If you want to format your numbers to make the table easier to understand, you could use the Lua <code>string.format</code> function, e.g.<code><pre>price = string.format("%08d", price)</pre></code>which will left-pad your price with 0s. Another easy thing is string manipulation (OK, the example is very silly, but you see what is meant):<code><pre>function xml.functions.model(t) model_name = xml.text(t, "./") model_name = model_name:gsub("o", "x") context.bTD() context(model_name) context.eTD()end</pre></code> But where Lua really shines is when it comes to constructions such as loops and conditionals. Here is an easy example for a conditional:<code><pre> local price = tonumber(xml.text(t, "./")) context.bTD() if price > 50000 then context.color( { "red" }, price) else context(price) end</pre></code> Have fun finding more ways to do interesting things with Lua and xml!  --[[User:Thomas|Thomas]] 09:35, 28 March 2013 (CET) == The XML node at Lua end == In the example above the XML node <tt>id</tt> (<tt>#1</tt>) is passed to Lua by {{cmd|xmlfunction}} command, being transformed from node string <tt>id</tt> (e.g. <tt>xml:name::6</tt>) to Lua table representing the xml elements in the xml tree. To get this table at Lua end <tt>lxml.getid()</tt> function is used (e.g. <tt>xml.text(lxml.getid("#1"), "/price")</tt>). There is no performance gain in doing so. == XML preprocessor == Sometimes the XML source is not in the best shape and changing it at the originator side is not possible. You can preprocess it during ConTeXt run to the better shaped XML before it is used. <xmlcode><?xml version "1.0"?><document> <p>Altitude 60&amp;nbsp;m</p></document></xmlcode> will be transformed to the new xml <xmlcode><?xml version "1.0"?><document> <p>Altitude 60<nbsp/>m</p></document></xmlcode> with <tt>lxml.preprocessor</tt> function <xmlcode>price\startluacodefunction lxml.preprocessor(data) data = string.gsub(data, "&amp;nbsp;", "<nbsp/>") return dataend\stopluacode</xmlcode> You can check the result of this transformation with {{cmd|xmlshow}} command. [[Category:XML]][[Category:Programming and Databases]]
138

edits

Navigation menu