Changes

Jump to navigation Jump to search
966 bytes added ,  10:45, 28 March 2013
no edit summary
== 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:
end
</pre></code>
What does this do? As you can see, we mostly use the <code>context...</code> 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 information for our list. The functions for <code>item</code> and <code>model</code> 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 <code>price</code> 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 was is just one example of what you can do with Lua. Good luck 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.<pre><code>price = string.format("%08d", price)</code></pre>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):<pre><code>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</code></pre>  But where Lua really shines is when it comes to constructions such as loops and conditionals. Here is an easy example for a conditional:<pre><code> local price = tonumber(xml.text(t, "./")) context.bTD() if price > 50000 then context.color( { "red" }, price) else context(price) end</code></pre> Have fun finding other fascinating use casesmore ways to do interesting things with Lua and xml!
--[[User:Thomas|Thomas]] 09:35, 28 March 2013 (CET)
gardener
111

edits

Navigation menu