Running Lua Code Externally

From ConTeXt wiki

Normal Lua preloading

When Context starts up in normal (TeX) mode, it loads a number of Lua 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.

n TeX-File Lua Dependencies
1 luat-bas.mkiv
2 luat-lib.mkiv
3 catc-ini.mkiv catc-ini.lua
4 cldf-ini.mkiv cldf-ini.lua
5 syst-lua.mkiv syst-lua.lua
6 syst-con.mkiv syst-con.lua
7 supp-fil.mkiv supp-fil.lua
8 char-ini.mkiv
9 char-utf.mkiv char-utf.lua
10 char-act.mkiv char-act.lua
11 mult-ini.mkiv mult-ini.lua
12 mult-chk.mkiv mult-chk.lua
13 mult-aux.mkiv mult-aux.lua
14 cldf-int.mkiv mult-clm.lua
15 toks-ini.mkiv toks-ini.lua
16 attr-ini.mkiv attr-ini.lua
17 node-ini.mkiv
18 node-fin.mkiv node-fin.lua
19 node-mig.mkiv node-mig.lua
20 node-par.mkiv node-par.lua
21 core-env.mkiv core-env.lua
22 back-ini.mkiv back-ini.lua
23 attr-col.mkiv attr-col.lua
24 attr-lay.mkiv attr-lay.lua
25 attr-neg.mkiv attr-neg.lua
26 attr-eff.mkiv attr-eff.lua
27 trac-tex.mkiv trac-tex.lua
28 trac-deb.mkiv
29 blob-ini.mkiv blob-ini.lua
30 supp-box.mkiv supp-box.lua
31 supp-ran.mkiv supp-ran.lua
32 core-con.mkiv core-con.lua
33 regi-ini.mkiv regi-ini.lua
34 lang-ini.mkiv
35 lang-lab.mkiv
36 core-uti.mkiv core-uti.lua
37 core-two.mkiv core-two.lua
38 colo-ini.mkiv
39 node-bck.mkiv node-bck.lua
40 lang-url.mkiv lang-url.lua
41 lang-wrd.mkiv lang-wrd.lua
42 symb-ini.mkiv symb-ini.lua
43 sort-ini.mkiv
44 pack-rul.mkiv pack-rul.lua
45 lxml-ini.mkiv
46 lxml-sor.mkiv lxml-sor.lua
47 strc-ini.mkiv strc-ini.lua
48 strc-tag.mkiv strc-tag.lua
49 strc-doc.mkiv strc-doc.lua
50 strc-num.mkiv strc-num.lua
51 strc-mar.mkiv strc-mar.lua
52 strc-prc.mkiv strc-prc.lua
53 strc-lst.mkiv strc-lst.lua
54 strc-pag.mkiv strc-pag.lua
55 strc-ref.mkiv
56 strc-reg.mkiv strc-reg.lua
57 strc-lev.mkiv strc-lev.lua
58 spac-hor.mkiv spac-hor.lua
59 spac-ver.mkiv spac-ver.lua
60 anch-pos.mkiv anch-pos.lua
61 pack-obj.mkiv pack-obj.lua
62 strc-itm.mkiv strc-itm.lua
63 strc-syn.mkiv strc-syn.lua
64 core-sys.mkiv core-sys.lua
65 page-flt.mkiv page-flt.lua
66 strc-flt.mkiv strc-flt.lua
67 page-lin.mkiv page-lin.lua
68 core-job.mkiv core-job.lua
69 buff-ini.mkiv buff-ini.lua
70 buff-ver.mkiv buff-ver.lua
71 buff-par.mkiv buff-par.lua
72 buff-imp-tex.mkiv buff-imp-tex.lua.lua
73 buff-imp-mp.mkiv buff-imp-mp.lua.lua
74 buff-imp-lua.mkiv buff-imp-lua.lua.lua
75 buff-imp-xml.mkiv buff-imp-xml.lua.lua
76 buff-imp-parsed-xml.mkiv buff-imp-parsed-xml.lua.lua
77 strc-blk.mkiv strc-blk.lua
78 scrn-int.mkiv scrn-int.lua
79 strc-bkm.mkiv strc-bkm.lua
80 tabl-tbl.mkiv tabl-tbl.lua.lua
81 java-ini.mkiv java-ini.lua
82 char-enc.mkiv char-enc.lua
83 font-ini.mkiv
84 font-col.mkiv font-col.lua
85 typo-cln.mkiv typo-cln.lua
86 typo-spa.mkiv typo-spa.lua
87 typo-krn.mkiv typo-krn.lua
88 typo-dir.mkiv typo-dir.lua
89 typo-brk.mkiv typo-brk.lua
90 typo-cap.mkiv typo-cap.lua
91 typo-dig.mkiv typo-dig.lua
92 typo-rep.mkiv typo-rep.lua
93 type-ini.mkiv type-ini.lua
94 scrp-ini.mkiv
95 mlib-ctx.mkiv
96 meta-ini.mkiv meta-ini.lua
97 meta-tex.mkiv meta-tex.lua
98 meta-fun.mkiv meta-fun.lua
99 page-str.mkiv page-str.lua
100 math-ini.mkiv
101 strc-mat.mkiv strc-mat.lua
102 chem-ini.mkiv chem-ini.lua
103 chem-str.mkiv chem-str.lua
104 node-rul.mkiv node-rul.lua
105 node-spl.mkiv node-spl.lua
106 strc-not.mkiv strc-not.lua
107 grph-inc.mkiv
108 grph-raw.mkiv grph-raw.lua
109 bibl-bib.mkiv bibl-bib.lua
110 bibl-tra.mkiv bibl-tra.lua
111 task-ini.mkiv task-ini.lua
112 cldf-ver.mkiv cldf-ver.lua
113 cldf-com.mkiv
114 core-ctx.mkiv core-ctx.lua
115 back-pdf.mkiv
116 mlib-pdf.mkiv mlib-pdf.lua
117 mlib-pps.mkiv mlib-pps.lua
118 grph-epd.mkiv grph-epd.lua
119 meta-pdf.mkiv meta-pdf.lua
120 back-exp.mkiv back-exp.lua.lua

See also