Difference between revisions of "XML Lua"

From Wiki
Jump to navigation Jump to search
Line 113: Line 113:
 
end
 
end
 
</pre></code>
 
</pre></code>
What does this do? As you can see, we mostly use the <tt>context...</tt> 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 information. The functions for <tt>item</tt> and <tt>model</tt> do nothing more than print the argument of the connected elements as table rows and the first table column. Things get a bit more interesting for the <tt>price</tt> 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 <xmlcode>price</xmlcode>
+
What does this do? As you can see, we mostly use the <tt>context...</tt> 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 information. The functions for <tt>item</tt> and <tt>model</tt> do nothing more than print the argument of the connected elements as table rows and the first table column. Things get a bit more interesting for the <tt>price</tt> 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 <tt><price></tt> element. We need to tell Lua that this content is not a string, but a number, hence the use of the <tt>tonumber</tt> function. As you can see, the equivalent of <pre>\xmltext{#1}{./}</pre> in Lua is <pre>xml.text(t, "./")</pre>.

Revision as of 08:32, 28 March 2013

General

If you process xml with ConTeXt Mkiv, you have the power of the Lua language at your fingertips. All \xml... 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 Mkiv manual.

A few general observations to help you getting started:

  • There are two sets of Lua commands: the xml... commands allow you to work with the content of your xml elements within Lua; lxml... commands pass the content to ConTeXt and typeset it.
  • Both commands receive the content as a Lua table.

Now let's get started with a silly example to show you what we can do in Lua.

The xml file

Here is the prices.xml file that we will be processing, a price list for cars. Prices are given in Euros; this used to be a currency in some countries in Europe:

<list>
  <item>
    <model>Volkswagen</model>
    <price>12000</price>
  </item>
  <item>
    <model>Mercedes</model>
    <price>35000</price>
  </item>
  <item>
    <model>Ferrari</model>
    <price>150000</price>
  </item>
</list>

The ConTeXt style file

Next, we write the environment file prices-style.tex which we will use to process our list. 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 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 table, since this is how we want to display the information:

\registerctxluafile{l-prices}{1.001}

\startxmlsetups xml:pricesetups
	\xmlsetsetup{#1}{list|
			 item|
			 model|
			 price}{xml:*}
\stopxmlsetups

\xmlregistersetup{xml:pricesetups}

\startxmlsetups xml:list
	\xmlfunction{#1}{list}
\stopxmlsetups

\startxmlsetups xml:item
	\xmlfunction{#1}{item}
\stopxmlsetups

\startxmlsetups xml:model
	\xmlfunction{#1}{model}
\stopxmlsetups

\startxmlsetups xml:price
	\xmlfunction{#1}{price}
\stopxmlsetups

\setupTABLE [column] [1]         [style=bold,width=0.3\textwidth]
\setupTABLE [column] [2,3,4]     [style=italic,width=0.2\textwidth]

The Lua file

And finally, the Lua file l-prices.lua, which will hold all the lua functions we use to process the xml:

function xml.functions.list(t)
 context.bTABLE()
 context.bTR()
 context.bTD()
 context("Model")
 context.eTD()
 context.bTD()
 context("Euros")
 context.eTD()
 context.bTD()
 context("Dollars")
 context.eTD()
 context.bTD()
 context("Yen")
 context.eTD()
 context.eTR()
 lxml.flush(t)
 context.eTABLE()
end

function xml.functions.item(t)
 context.bTR()
 lxml.flush(t)
 context.eTR()
end

function xml.functions.model(t)
 context.bTD()
 lxml.flush(t)
 context.eTD()
end

function xml.functions.price(t)
 local price = tonumber(xml.text(t, "./"))
 local dollar_price = price * 1.28
 local yen_price = price * 120.4
 context.bTD()
 context(price)
 context.eTD()
 context.bTD()
 context(dollar_price)
 context.eTD()
 context.bTD()
 context(yen_price)
 context.eTD()
end

What does this do? As you can see, we mostly use the context... commands which are described in the cld manual. They are Lua functions which print to ConTeXt. With the list function, we start a ConTeXt table and typeset a first row with the meta information. The functions for item and model do nothing more than print the argument of the connected elements as table rows and the first table column. Things get a bit more interesting for the price 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 <price> element. We need to tell Lua that this content is not a string, but a number, hence the use of the tonumber function. As you can see, the equivalent of

\xmltext{#1}{./}

in Lua is

xml.text(t, "./")

.