ConTeXt Hagen PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

The language mix VOORJAAR 2015 1

The language mix

Under the hood, the mkiv code base is becoming


Abstract quite a mix and once we have a more clear picture
During the third ConTEXt conference that ran in parallel
of where we’re heading, it might become even more
to EuroTEX 2009 in The Hague we had several sessions
where mkiv was discussed and a few upcoming features of a hybrid. Already for some time most of the font
were demonstrated. The next sections summarize some handling is done by Lua, and a bit more logic and
of that. It’s hard to predict the future, especially be- management might move to Lua as well. However,
cause new possibilities show up once LuaTEX is opened as we want to be downward compatible we cannot
up more, so remarks about the future are not definitive. go as far as we want (yet). This might change as
soon as more of the primitives have associated Lua
1 TEX functions. Even then it will be a trade off: calling
From now on, if I refer to TEX in the perspective Lua takes some time and it might not pay off at all.
of LuaTEX I mean “Good Old TEX”, the language Some of the more tricky components, like vertical
as well as the functionality. Although LuaTEX pro- spacing, grid snapping, balancing columns, etc. are
vides a couple of extensions it remains pretty close already in the process of being Luafied and their
to compatible to its ancestor, certainly from the hybrid form might turn into complete Lua driven
perspective of the end user. solutions eventually. Again, the compatibility issue
As most ConTEXt users code their documents forces us to follow a stepwise approach, but at the
in the TEX language, this will remain the focus of cost of (quite some) extra development time. But
mkiv. After all, there is no real reason to abandon whatever happens, the TEX input language as well
it. However, although ConTEXt already stimulates as machinery will be there.
users to use structure where possible and not to use
low level TEX commands in the document source, 2 MetaPost
we will add a few more structural variants. For I never regret integrating MetaPost support in
instance, we already introduced \startchapter and ConTEXt and a dream came true when mplib
\startitem in addition to \chapter and \item. became part of LuaTEX. Apart from a few minor
We even go further, by using key/value pairs for changes in the way text integrates into MetaPost
defining section titles, bookmarks, running headers, graphics the user interface in mkiv is the same as
references, bookmarks and list entries at the start of in mkii. Insofar as Lua is involved, this is hidden
a chapter. And, as we carry around much more in- from the user. We use Lua for managing runs and
formation in the (for TEX so typical) auxiliary data conversion of the result to pdf. Currently generating
files, we provide extensive control over rendering the MetaPost code by Lua is limited to assisting in the
numbers of these elements when they are recalled typesetting of chemical structure formulas which is
(like in tables of contents). So, if you really want now part of the core.
to use different texts for all references to a chapter When defining graphics we use the MetaPost
header, it can be done: language and not some TEX-like variant of it.
Information can be passed to MetaPost using special
\startchapter macros (like \MPcolor), but most relevant status
[label=emcsquare, information is passed automatically anyway.
title={About $e=mc^2$}, You should not be surprised if at some point we
bookmark={einstein}, can request information from TEX directly, because
list={About $e=mc^2$ (Einstein)}, after all this information is accessible. Think of
reference={$e=mc^2$}] something w := texdimen(0) ; being expanded at
the MetaPost end instead of w := \the\dimen0 ;
... content ... being passed to MetaPost from the TEX end.

\stopchapter
2 MAPS 50 Hans Hagen

3 Lua And, knowing Lua helps as well, since the following


What will the user see of Lua? First of all he or she is more efficient:
can use this scripting language to generate content.
But when making a format or by looking at the \startluacode
statistics printed at the end of a run, it will be clear local colored = {
that Lua is used all over the place. background = "color",
So how about Lua as a replacement for the TEX backgroundcolor = "bluegreen",
input language? Actually, it is already possible to foregroundcolor = "white",
make such “ConTEXt Lua Documents” using mkiv’s }
built in functions. Each ConTEXt command is also local basespec = {
available as a Lua function. framecolor = "bluered",
align= "middle",
\startluacode style = "type",
context.bTABLE { offset=".5ex",
framecolor = "blue", }
align= "middle", local bTR, eTR = context.bTR, context.eTR
style = "type", local bTD, eTD = context.bTD, context.eTD
offset=".5ex", context.bTABLE(basespec)
} for i=1,10 do
for i=1,10 do bTR()
context.bTR() for i=1,20 do
for i=1,20 do local r= math.random(99)
local r= math.random(99) bTD((r < 50 and colored) or nil)
if r < 50 then context("%#2i",r)
context.bTD { eTD()
background = "color", end
backgroundcolor = "blue" eTR()
} end
context(context.white("%#2i",r)) context.eTABLE()
else \stopluacode
context.bTD()
context("%#2i",r) Since in practice the speedup is negligible and
end the memory footprint is about the same, such
context.eTD() optimizations seldom make sense.
end At some point this interface will be extended, for
context.eTR() instance when we can use TEX’s main (scanning,
end parsing and processing) loop as a so-called corou-
context.eTABLE() tine and when we have opened up more of TEX’s
\stopluacode internals. Of course, instead of putting this in your
TEX source, you can as well keep the code at the
Of course it helps if you know ConTEXt a bit. For Lua end.
instance we can as well say:

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:

1 Similar methods exist for processing xml files.

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

\startluacode able to share the implementation with TEX input, it


function xml.functions.totalscore(root) also makes sense to stick to some hybrid approach.
local n = 0 In any case, more of the calculations and logic will
for e in xml.collected(root,"/outcome") do move to Lua, while TEX will deal with the content.
if xml.filter(e,"action[text()='add']") then A somewhat strange animal here is xsl-fo. We
local m = xml.filter do support it, but the mkii implementation was
(e,"xml:///score/text()") always somewhat limited and the code was quite
n = n + (tonumber(m or 0) or 0) complex. So, this needs a proper rewrite in mkiv,
end which will happen indeed. It’s mostly a nice exercise
end of hybrid technology but until now I never really
tex.write(n) needed it. Other bits and pieces of the current xml
end goodies might also get an upgrade.
\stopluacode There is already a bunch of functions and macros
to filter and manipulate xml content and currently
You can either use such a function in a filter or just the code involved is being cleaned up. What di-
use it as a TEX macro: rection we go also depends on users’ demands.
So, with respect to xml you can expect more
\startxmlsetups xml:question support, a better integration and an upgrade of
\blank some supported xml related standards.
\xmlfirst{#1}{wording}
\startitemize 5 Tools
\xmlfilter{#1} Some of the tools that ship with ConTEXt are also
{/answer/choice/command(xml:answer:choice)} examples of hybrid usage.
\stopitemize Take this:
\endgraf
score: \xmlfunction{#1}{totalscore} mtxrun --script server --auto
\blank
\stopxmlsetups On my machine this reports:

\startxmlsetups xml:answer:choice MTXrun | running at port: 31415


\startitem MTXrun | document root: c:/data/develop/context/
\xmlflush{#1} lua
\stopitem MTXrun | main index file: unknown
\stopxmlsetups MTXrun | scripts subpath: c:/data/develop/context
/lua
The filter variant is like this: MTXrun | context services: http://localhost:31415
/mtx-server-ctx-startup.lua
\xmlfilter{#1}{./function('totalscore')}
The mtxrun script is a Lua script that acts as a con-
So you can take your choice and make your source troller for other scripts, in this case mtx-server.lua
look more xml-ish, Lua-like or TEX-wise. A careful that is part of the regular distribution. As we use
reader might have noticed the peculiar xml:// in the LuaTEX as a Lua interpreter and since LuaTEX has
function code. When used inside mkiv, the serializer a socket library built in, it can act as a web server,
defaults to TEX so results are piped back into TEX. limited but quite right for our purpose.3
This prefix forced the regular serializer which keeps The web page that pops up when you enter the
the result at the Lua end. given address lets you currently choose between the
Currently some of the xml related modules, like ConTEXt help system and a font testing tool. In
mathml and handling of tables, are really a mix of figure ?? you seen an example of what the font
TEX code and Lua calls, but it makes sense to move testing tool does.
them completely to Lua. One reason is that their Here we have LuaTEX running a simple web
input (formulas and table content) is restricted to server but it’s not aware of having TEX on board.
non-TEX anyway. On the other hand, in order to be When you click on one of the buttons at the bottom

3 This application is not intentional but just a side effect.


The language mix VOORJAAR 2015 5

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

You might also like