Changes

Jump to navigation Jump to search
3 bytes removed ,  08:27, 2 July 2022
m
Text replacement - "pragma-ade.com" to "pragma-ade.nl"
== 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.comnl/show-man-43.htm Mkiv manual].
A few general observations to help you getting started:
== 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.comnl/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 <code>context...</code> commands which are described in the [http://www.pragma-ade.comnl/general/manuals/cld-mkiv.pdf cld manual]. They are Lua functions which print to ConTeXt. With the <code>list</code> 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 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:

Navigation menu