Debian Codegraph @

texmf-dist/doc/support/cluttex/build.lua

raw symbols SCIP
1 --[[
2 Copyright 2016, 2018 ARATA Mizuki
3
4 This file is part of ClutTeX.
5
6 ClutTeX is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 ClutTeX is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with ClutTeX. If not, see <http://www.gnu.org/licenses/>.
18 ]]
19
20 local srcdir = "src/"
21 local mode
22 local default_os
23 if arg[1] == "--unix-shellscript" then
24 default_os, mode = "unix", "shellscript"
25 table.remove(arg, 1)
26 elseif arg[1] == "--windows-batchfile" then
27 default_os, mode = "windows", "batchfile"
28 table.remove(arg, 1)
29 end
30 local outfile = arg[1]
31 local preserve_location_info = false
32
33 local modules = {
34 {
35 name = "texrunner.pathutil",
36 path = "texrunner/pathutil.lua",
37 path_unix = "texrunner/pathutil_unix.lua",
38 path_windows = "texrunner/pathutil_windows.lua",
39 },
40 {
41 name = "texrunner.shellutil",
42 path = "texrunner/shellutil.lua",
43 path_unix = "texrunner/shellutil_unix.lua",
44 path_windows = "texrunner/shellutil_windows.lua",
45 },
46 {
47 name = "texrunner.fsutil",
48 path = "texrunner/fsutil.lua",
49 },
50 {
51 name = "texrunner.luatexinit",
52 path = "texrunner/luatexinit.lua",
53 },
54 {
55 name = "texrunner.isatty",
56 path = "texrunner/isatty.lua",
57 },
58 {
59 name = "texrunner.fswatcher_windows",
60 path = "texrunner/fswatcher_windows.lua",
61 },
62 }
63
64 local imported_globals = {"io", "os", "string", "table", "package", "require", "assert", "error", "ipairs", "type", "select", "arg"}
65
66 -- TODO: This code may interfere with the string literal embedded in luatexinit.lua
67 local function strip_global_imports(code)
68 local function repl(s1, s2)
69 if s1 == s2 then
70 for i, v in ipairs(imported_globals) do
71 if v == s1 then
72 return ""
73 end
74 end
75 end
76 return nil
77 end
78 return (code:gsub("local (%w+) = (%w+)\n", repl))
79 end
80
81 local function strip_test_code(code)
82 if preserve_location_info then
83 return (code:gsub("%-%- TEST CODE\n.-%-%- END TEST CODE\n", function(s)
84 return (s:gsub("[^\n]",""))
85 end))
86 else
87 return (code:gsub("%-%- TEST CODE\n(.-)%-%- END TEST CODE\n", ""))
88 end
89 end
90
91 local function load_module_code(path)
92 assert(loadfile(srcdir .. path)) -- Check syntax
93 return strip_test_code(assert(io.open(srcdir .. path, "r")):read("*a"))
94 end
95
96 assert(loadfile(srcdir .. "cluttex-ml.lua")) -- Check syntax
97
98 local shebang = "#!/usr/bin/env texlua\n"
99 local main = assert(io.open(srcdir .. "cluttex-ml.lua", "r")):read("*a")
100 --[[
101 if main:sub(1,2) == "#!" then
102 -- shebang
103 shebang,main = main:match("^([^\n]+\n)(.*)$")
104 end
105 ]]
106
107 local lines = {}
108 if mode == "batchfile" then
109 lines[1] = [=[
110 ::dummy:: --[[
111 @texlua "%~f0" %*
112 @goto :eof
113 ]]
114 ]=]
115 else
116 if shebang then
117 lines[1] = shebang
118 end
119 end
120
121 if not preserve_location_info then
122 table.insert(lines, string.format("local %s = %s\n", table.concat(imported_globals, ", "), table.concat(imported_globals, ", ")))
123 end
124
125 if default_os then
126 table.insert(lines, string.format("os.type = os.type or %q\n", default_os))
127 end
128
129 -- LuajitTeX doesn't seem to set package.loaded table...
130 table.insert(lines, "if lfs and not package.loaded['lfs'] then package.loaded['lfs'] = lfs end\n")
131 if preserve_location_info then
132 table.insert(lines, "local loadstring = loadstring or load\n")
133 for _,m in ipairs(modules) do
134 if m.path_windows or m.path_unix then
135 table.insert(lines, 'if os.type == "windows" then\n')
136 table.insert(lines, string.format("package.preload[%q] = assert(loadstring(%q, %q))\n", m.name, load_module_code(m.path_windows or m.path), "=" .. (m.path_windows or m.path)))
137 table.insert(lines, 'else\n')
138 table.insert(lines, string.format("package.preload[%q] = assert(loadstring(%q, %q))\n", m.name, load_module_code(m.path_unix or m.path), "=" .. (m.path_unix or m.path)))
139 table.insert(lines, 'end\n')
140 else
141 table.insert(lines, string.format("package.preload[%q] = assert(loadstring(%q, %q))\n", m.name, load_module_code(m.path), "=" .. m.path))
142 end
143 end
144 table.insert(lines, string.format("assert(loadstring(%q, %q))(...)\n", main, "=cluttex.lua"))
145 else
146 for _,m in ipairs(modules) do
147 if m.path_windows or m.path_unix then
148 table.insert(lines, 'if os.type == "windows" then\n')
149 table.insert(lines, string.format("package.preload[%q] = function(...)\n%send\n", m.name, load_module_code(m.path_windows or m.path)))
150 table.insert(lines, 'else\n')
151 table.insert(lines, string.format("package.preload[%q] = function(...)\n%send\n", m.name, load_module_code(m.path_unix or m.path)))
152 table.insert(lines, 'end\n')
153 else
154 table.insert(lines, string.format("package.preload[%q] = function(...)\n%send\n", m.name, load_module_code(m.path)))
155 end
156 end
157 table.insert(lines, strip_global_imports(main))
158 end
159
160 if outfile then
161 io.output(assert(io.open(outfile, "wb")))
162 end
163 io.write(table.concat(lines, ""))