Changes

Jump to navigation Jump to search
169 bytes added ,  08:44, 28 March 2013
m
small corrections
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 <tt>list</tt> 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 <ttcode><price></ttcode> element. We need to tell Lua that this content is not a string, but a number, hence the use of the <ttcode>tonumber</ttcode> function. Within a ConTeXt environment, we would use <ttcode>\xmltext{#1}{./}</ttcode>. The Lua equivalent is <ttcode>xml.text(t, "./")</ttcode>. 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 just one example of what you can do with Lua. Good luck finding other fascinating use cases!
--[[User:Thomas|Thomas]] 09:35, 28 March 2013 (CET)
gardener
111

edits

Navigation menu