Professional Documents
Culture Documents
ConTeXt Hagen PDF
ConTeXt Hagen PDF
ConTeXt Hagen PDF
\stopchapter
2 MAPS 50 Hans Hagen
if r < 50 then 42 36 59 8 45 50 18 47 62 31 56 9 31 21 12 42 97 25 53 92
context.bTD { 41 82 76 74 60 69 38 97 23 97 61 59 42 31 55 17 48 36 10 40
background = "color", 5 45 56 52 54 21 12 60 60 95 42 60 23 54 82 77 45 2 50 33
backgroundcolor = "blue", 34 38 10 77 46 99 37 10 84 39 99 54 22 72 68 64 79 93 4 28
foregroundcolor = "white", 26 8 67 7 46 95 69 60 64 9 59 45 61 72 24 42 42 78 23 38
} 72 22 57 96 2 29 17 47 27 70 95 58 89 95 75 27 93 7 12 3
else 90 84 90 42 97 74 60 10 81 26 39 10 31 33 75 18 23 96 6 31
context.bTD() 5 95 36 81 50 25 62 21 31 70 46 55 29 77 34 97 17 1 10 64
end 5 70 96 79 40 65 23 97 78 54 72 23 84 59 3 23 65 44 47 32
context("%#2i",r) 38 71 83 70 70 22 86 47 4 16 92 32 48 48 11 4 35 6 99 42
context.eTD()
Figure 1. The result of the displayed Lua code.
The language mix VOORJAAR 2015 3
The script that manages a ConTEXt run (also called way) due to the way the content is organized or
context) will process files with that consist of such elements get abused.
commands directly if they have a cld suffix or when There is a dedicated manual for dealing with xml
you provide the flag --forcecld.1 in mkiv, so we only show a simple example here. The
documents to be processed are loaded in memory
context yourfile.cld and serialized using setups that are associated to
elements. We keep track of documents and nodes in
But will this replace TEX as an input language? a way that permits multipass data handling (rather
This is quite unlikely because coding documents in usual in TEX). Say that we have a document that
TEX is so convenient and there is not much to gain contains questions. The following definitions will
here. Of course in a pure Lua based workflow (for flush the (root element) questions:
instance publishing information from databases) it
would be nice to code in Lua, but even then it’s \startxmlsetups xml:mysetups
mostly syntactic sugar, as TEX has to do the job \xmlsetsetup{#1}{questions}{xml:questions}
anyway. However, eventually we will have a quite \stopxmlsetups
mature Lua counterpart.
\xmlregistersetup{xml:mysetups}
4 XML
This is not so much a programming language but \startxmlsetups xml:questions
more a method of tagging your document content \xmlflush{#1}
(or data). As structure is rather dominant in xml, it \stopxmlsetups
is quite handy for situations where we need different
output formats and multiple tools need to process \xmlprocessfile{main}{somefile.xml}{}
the same data. It’s also a standard, although this
does not mean that all documents you see are Here the #1 represents the current xml element. Of
properly structured. This in turn means that we course we need more associations in order to get
need some manipulative power in ConTEXt, and something meaningful. If we just serialize then we
that happens to be easier to do in mkiv than in have mappings like:
mkii.
In ConTEXt we have been supporting xml for \xmlsetsetup{#1}{question|answer}{xml:*}
a long time, and in mkiv we made the switch
from stream based to tree based processing. The So, questions and answers are mapped onto their
current implementation is mostly driven by what own setup which flushes them, probably with some
has been possible so far but as LuaTEX becomes numbering done at the spot.
more mature, bits and pieces will be reimplemented In this mechanism Lua is sort of invisible but
(or at least cleaned up and brought up to date with quite busy as it is responsible for loading, filtering,
developments in LuaTEX). accessing and serializing the tree. In this case TEX
One could argue that it makes more sense to and Lua hand over control in rapid succession.
use xslt for converting xml into something TEX, You can hook in your own functions, like:
but in most of the cases that I have to deal with
much effort goes into mapping structure onto a given \xmlfilter{#1}
layout specification. Adding a bit of xml to TEX {(wording|feedback|choice)/function(cleanup)}
mapping to that directly is quite convenient. The
total amount of code is probably smaller and it saves In this case the function cleanup is applied to
a processing step. elements with names that match one of the three
We’re mostly dealing with education-related doc- given.2
uments and these tend to have a more complex Of course, once you start mixing in Lua in this
structure than the final typeset result shows. Also, way, you need to know how we deal with xml at
readability of code is not served with such a split as the Lua end. The following function show how we
most mappings look messy anyway (or evolve that calculate scores:
2 This example is inspired by one of our projects where the cleanup involves
sanitizing (highly invalid) html data that is embedded as a CDATA stream, a trick to prevent the xml file to be invalid.
4 MAPS 50 Hans Hagen
of the screen, the server will load and execute a pdf file. So, we have Lua managing a run and mkiv
script related to the request and in this case that managing with help of Lua reading from zip files
script will create a TEX file and call LuaTEX with and converting xml into something that TEX is
ConTEXt to process that file. The result is piped happy with. As there is no standard with respect
back to the browser. to the content itself, i.e. the rendering is driven by
You can use this tool to investigate fonts (their whatever kind of structure is used and whatever the
bad and good habits) as well as to test the currently css file is able to map it onto, in practice we need
available OpenType functionality in mkiv (bugs as an additional style for this class of documents. But
well as goodies). anyway it’s a good example of integration.
So again we have a hybrid usage although in this
case the user is not confronted with Lua and/or TEX 6 The future
at all. The same is true for the other goodie, shown Apart from these language related issues, what more
in figure ??. Actually, such a goodie has always been is on the agenda? To mention a few integration
part of the ConTEXt distribution but it has been related thoughts:
rewritten in Lua.
The ConTEXt user interface is defined in an At some point I want to explore the possibility
xml file, and this file is used for several purposes: to limit processing to just one run, for instance
initializing the user interfaces at format generation by doing trial runs without outputting anything
time, typesetting the formal command references but still collecting multipass information. This
(for all relevant interface languages), for the wiki, might save some runtime in demanding work-
and for the mentioned help goodie. flows especially when we keep extensive font
Using the mix of languages permits us to provide loading and image handling in mind.
convenient processing of documents that otherwise Related to this is the ability to run mkiv as a
would demand more from the user than it does now. service but that demands that we can reset the
For instance, imagine that we want to process a state of LuaTEX and actually it might not be
series of documents in the so-called epub format. worth the trouble at all given faster processors
Such a document is a zipped file that has a descrip- and disks. Also, it might not save much runtime
tion and resources. As the content of this archive is on larger jobs.
prescribed it’s quite easy to process it: More interesting can be to continue experiment-
ing with isolating parts of ConTEXt in such a
context --ctx=x-epub.ctx yourfile.epub way that one can construct a specialized sub-
set of functionality. Of course the main body of
This is equivalent to: code will always be loaded as one needs basic
typesetting anyway.
texlua mtxrun.lua --script context --ctx=x-epub.
ctx yourfile.epub Of course we keep improving existing mechanisms
and improve solutions using a mix of TEX and Lua,
So, here we have LuaTEX running a script that itself using each language (and system) for what it can do
(locates and) runs a script context. That script best.
loads a ConTEXt job description file (with suffix
ctx). This file tells what styles to load and might 7 Notes
have additional directives but none of that has to
bother the end user. In the automatically loaded Hans Hagen
style we take care of reading the xml files from Pragma ADE, Hasselt
the zipped file and eventually map the embedded pragma (at) wxs dot nl
html like files onto style elements and produce a