summaryrefslogtreecommitdiff
path: root/scripts/context/lua/mtx-plain.lua
blob: 663664bae148275b6e016dcdb2ddb0ad7d9f167d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
if not modules then modules = { } end modules ['mtx-plain'] = {
    version   = 1.002,
    comment   = "companion to mtxrun.lua",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- something fishy ... different than the texmf.cnf suggests .. hardcoded ?

-- table={
--  ".",
--  "c:/data/develop/tex-context/tex/texmf-local/web2c/luatex",
--  "c:/data/develop/tex-context/tex/texmf-local/web2c",
--  "c:/data/develop/tex-context/tex/texmf-context/web2c",
--  "c:/data/develop/tex-context/tex/texmf-mswin/web2c",
--  "c:/data/develop/tex-context/tex/texmf/web2c",
-- }

local helpinfo = [[
<?xml version="1.0"?>
<application>
 <metadata>
  <entry name="name">mtx-plain</entry>
  <entry name="detail">Plain TeX Runner</entry>
  <entry name="version">1.00</entry>
 </metadata>
 <flags>
  <category name="basic">
   <subcategory>
    <flag name="make"><short>create format file</short></flag>
    <flag name="run"><short>process file</short></flag>
    <flag name="format" value="string"><short>format name (default: luatex-plain)</short></flag>
    <flag name="engine" value="string"><short>engine to use (default: luatex)</short></flag>
    <flag name="jit"><short>use luajittex</short></flag>
   </subcategory>
  </category>
 </flags>
</application>
]]

local application = logs.application {
    name     = "mtx-plain",
    banner   = "Plain TeX Runner 1.00",
    helpinfo = helpinfo,
}

local report = application.report

scripts       = scripts       or { }
scripts.plain = scripts.plain or { }

local function execute(...)
    local command = string.format(...)
    report("running command %a",command)
    report()
    os.execute(command)
    report()
end

local function resultof(...)
    local command = string.format(...)
    report("running command %a",command)
    return string.strip(os.resultof(command) or "")
end

function scripts.plain.make(texengine,texformat)
    report("generating kpse file database")
    execute("mktexlsr") -- better play safe and use this one
    local fmtpathspec = resultof("kpsewhich --var-value=TEXFORMATS --engine=%s",texengine)
    if fmtpathspec ~= "" then
        report("using path specification %a",fmtpathspec)
        fmtpathspec = resultof('kpsewhich -expand-path="%s"',fmtpathspec)
    end
    if fmtpathspec ~= "" then
        report("using path expansion %a",fmtpathspec)
    else
        report("no valid path reported, trying alternative")
        fmtpathspec = resultof("kpsewhich --show-path=fmt --engine=%s",texengine)
        if fmtpathspec ~= "" then
            report("using path expansion %a",fmtpathspec)
        else
            report("no valid path reported, falling back to current path")
            fmtpathspec = "."
        end
    end
    fmtpathspec = string.splitlines(fmtpathspec)[1] or fmtpathspec
    fmtpathspec = file.splitpath(fmtpathspec)
    local fmtpath = nil
    for i=1,#fmtpathspec do
        local path = fmtpathspec[i]
        if path ~= "." and lfs.isdir(path) and file.is_writable(path) then
            fmtpath = path
            break
        end
    end
    if not fmtpath then
        -- message
    else
        lfs.chdir(fmtpath)
        execute('%s --ini %s',texengine,file.addsuffix(texformat,"tex"))
        report("generating kpse file database")
        execute("mktexlsr")
    end
    report("format saved on path %a",fmtpath)
end

function scripts.plain.run(texengine,texformat,filename)
    execute('%s --fmt=%s "%s"',texengine,file.removesuffix(texformat),filename)
end

local texformat = environment.arguments.texformat or environment.arguments.format
local texengine = environment.arguments.texengine or environment.arguments.engine

if type(texengine) ~= "string" or texengine == "" then
    texengine = environment.arguments.jit and "luajittex" or"luatex"
end

if type(texformat) ~= "string" or texformat == "" then
    texformat = "luatex-plain"
end

local filename = environment.files[1]

if environment.arguments.exporthelp then
    application.export(environment.arguments.exporthelp,filename)
elseif environment.arguments.make then
    scripts.plain.make(texengine,texformat)
elseif filename then
    scripts.plain.run(texengine,texformat,filename)
else
    application.help()
end