ConTeXt and Lua programming/Running Lua code externally
Normal Lua preloading
When Context starts up in normal (TeX) mode, it loads a number of LuaTeX libraries in addition to the format itself. In order to use these libraries outside the typesetting environment (e.g. when debugging Lua code etc.), scripts can be directly executed via mtxrun. Assuming that the Lua file called dummy.lua is in the working directory. the correct syntax would be
$ mtxrun --script dummy.lua
The libraries preloaded by mtxrun are a subset of those available during a Context run. In their loading order, they are
'l-string.lua', 'l-lpeg.lua', 'l-table.lua', 'l-io.lua', 'l-number.lua', 'l-set.lua', 'l-os.lua', 'l-file.lua', 'l-md5.lua', 'l-url.lua', 'l-dir.lua', 'l-boolean.lua', 'l-unicode.lua', 'l-math.lua', 'util-tab.lua', 'util-sto.lua', 'util-mrg.lua', 'util-lua.lua', 'util-prs.lua', 'util-fmt.lua', 'util-deb.lua', 'trac-inf.lua', 'trac-set.lua', 'trac-log.lua', 'trac-pro.lua', 'luat-env.lua', 'lxml-tab.lua', 'lxml-lpt.lua', 'lxml-mis.lua', 'lxml-aux.lua', 'lxml-xml.lua', 'data-ini.lua', 'data-exp.lua', 'data-env.lua', 'data-tmp.lua', 'data-met.lua', 'data-res.lua', 'data-pre.lua', 'data-inp.lua', 'data-out.lua', 'data-fil.lua', 'data-con.lua', 'data-use.lua', 'data-zip.lua', 'data-tre.lua', 'data-crl.lua', 'data-lua.lua', 'data-aux.lua', 'data-tmf.lua', 'data-lst.lua', 'luat-sta.lua', 'luat-fmt.lua',
Thus, Context’s elaborate string and table libraries, along with other useful stuff, are present as though it were an ordinary MkIV session.
Library dependencies
Context is huge. When quick-testing code that will eventually make it into your next module or something, a desired library may not be available with mtxrun --script. One workaround for missing functionality is -- relying on the filename database (as recommended by Hans) -- adding the file manually e.g.
dofile(resolvers.findfile("yourfile.lua"))
Only a small number of Context’s Lua libraries are self-contained. Most of them reference others, which is especially true of the utility functions of the l-* and luat-* namespaces. For instance, in order to get the module-related code from luat-lib.lua working, luat-cod.lua and luat-sto.lua have to be initialized first.
dofile(resolvers.findfile("luat-cod.lua")) dofile(resolvers.findfile("luat-sto.lua")) dofile(resolvers.findfile("luat-ini.lua"))
Standard library loading order
As it is not always clear which file relies on which to be present, it is natural to check in what order they are loaded by Context. The table below lists how the *.mkiv files that are called by the format (i.e. context.mkiv) reference their corresponding *.lua files.