mtx-plain.lua /size: 5352 b    last modification: 2020-07-01 14:35
1if not modules then modules = { } end modules ['mtx-plain'] = {
2    version   = 1.002,
3    comment   = "companion to mtxrun.lua",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9-- Future version will use the texmf-cache/generic/formats/<engine> path
10-- instead because then we can use some more of the generic context
11-- initializers ... in that case we will also use the regular database
12-- instead of kpse here, just like with the font database code (as that
13-- one also works with kpse runtime).
14
15-- Maybe I have to update this one to use more recent ways to run programs.
16
17local format = string.format
18
19local helpinfo = [[
20<?xml version="1.0"?>
21<application>
22 <metadata>
23  <entry name="name">mtx-plain</entry>
24  <entry name="detail">Plain TeX Runner</entry>
25  <entry name="version">1.00</entry>
26 </metadata>
27 <flags>
28  <category name="basic">
29   <subcategory>
30    <flag name="make"><short>create format file</short></flag>
31    <flag name="fonts"><short>create plain font database</short></flag>
32    <flag name="run"><short>process file</short></flag>
33    <flag name="format" value="string"><short>format name (default: luatex-plain)</short></flag>
34    <flag name="engine" value="string"><short>engine to use (default: luatex)</short></flag>
35    <flag name="jit"><short>use luajittex</short></flag>
36   </subcategory>
37  </category>
38 </flags>
39</application>
40]]
41
42local application = logs.application {
43    name     = "mtx-plain",
44    banner   = "Plain TeX Runner 1.00",
45    helpinfo = helpinfo,
46}
47
48local report = application.report
49
50scripts       = scripts       or { }
51scripts.plain = scripts.plain or { }
52
53local passed_options = table.tohash {
54    "utc",
55    "synctex",
56}
57
58local function execute(...)
59    local command = format(...)
60    report("running command %a\n",command)
61    statistics.starttiming()
62    local status = os.execute(command)
63    statistics.stoptiming()
64    report("runtime %s seconds",statistics.elapsedtime())
65    return status
66end
67
68local function resultof(...)
69    local command = format(...)
70    report("running command %a",command)
71    local result = os.resultof(command) or ""
72    result = string.gsub(result,"[\n\r]+","")
73    return result
74end
75
76function scripts.plain.make(texengine,texformat)
77    report("generating kpse file database")
78    execute("mktexlsr") -- better play safe and use this one
79    local fmtpathspec = resultof("kpsewhich --var-value=TEXFORMATS --engine=%s",texengine)
80    if fmtpathspec ~= "" then
81        report("using path specification %a",fmtpathspec)
82        fmtpathspec = resultof('kpsewhich --expand-braces="%s"',fmtpathspec)
83    end
84    if fmtpathspec ~= "" then
85        report("using path expansion %a",fmtpathspec)
86    else
87        report("no valid path reported, trying alternative")
88     -- fmtpathspec = resultof("kpsewhich --show-path=fmt --engine=%s",texengine)
89        if fmtpathspec ~= "" then
90            report("using path expansion %a",fmtpathspec)
91        else
92            report("no valid path reported, falling back to current path")
93            fmtpathspec = "."
94        end
95    end
96    fmtpathspec = string.splitlines(fmtpathspec)[1] or fmtpathspec
97    fmtpathspec = file.splitpath(fmtpathspec)
98    local fmtpath = nil
99    for i=1,#fmtpathspec do
100        local path = fmtpathspec[i]
101        if path ~= "." then
102            dir.makedirs(path)
103            if lfs.isdir(path) and file.is_writable(path) then
104                fmtpath = path
105                break
106            end
107        end
108    end
109--  local fmtpath = resultof("kpsewhich --expand-path $safe-out-name=$TEXFORMATS")
110    if not fmtpath or fmtpath == "" then
111        fmtpath = "."
112    else
113        lfs.chdir(fmtpath)
114    end
115    execute('%s --ini %s \\dump',texengine,file.addsuffix(texformat,"tex"))
116    report("generating kpse file database")
117    execute("mktexlsr")
118    report("format %a saved on path %a",texformat,fmtpath)
119end
120
121function scripts.plain.run(texengine,texformat,filename)
122    local t = { }
123    for k, v in next, environment.arguments do
124        local m = passed_options[k] and "" or "mtx:"
125        if type(v) == "string" and v ~= "" then
126            v = format("--%s%s=%s",m,k,v)
127        elseif v then
128            v = format("--%s%s",m,k)
129        end
130        t[#t+1] = v
131    end
132    execute('%s --fmt=%s %s "%s"',texengine,file.removesuffix(texformat),table.concat(t," "),filename)
133end
134
135function scripts.plain.fonts()
136    execute('mtxrun --script fonts --reload --simple --typeone')
137end
138
139local texformat = environment.arguments.texformat or environment.arguments.format
140local texengine = environment.arguments.texengine or environment.arguments.engine
141
142if type(texengine) ~= "string" or texengine == "" then
143    texengine = (jit or environment.arguments.jit) and "luajittex" or "luatex"
144end
145
146if type(texformat) ~= "string" or texformat == "" then
147    texformat = "luatex-plain"
148end
149
150local filename = environment.files[1]
151
152if environment.arguments.exporthelp then
153    application.export(environment.arguments.exporthelp,filename)
154elseif environment.arguments.make then
155    scripts.plain.make(texengine,texformat)
156elseif environment.arguments.fonts then
157    scripts.plain.fonts()
158elseif filename then
159    scripts.plain.run(texengine,texformat,filename)
160else
161    application.help()
162end
163