luat-fmt.lua /size: 13 Kb    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
luat-fmt
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to mtxrun
"
,
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
local
format
=
string
.
format
10
local
concat
=
table
.
concat
11
local
quoted
=
string
.
quoted
12
local
luasuffixes
=
utilities
.
lua
.
suffixes
13 14
local
report_format
=
logs
.
reporter
(
"
resolvers
"
,
"
formats
"
)
15 16
-- this is a bit messy: we also handle flags in mtx-context so best we
17
-- can combine this some day (all here)
18 19
local
function
primaryflags
(
arguments
)
20
local
flags
=
{
}
21
if
arguments
.
silent
then
22
flags
[
#
flags
+
1
]
=
"
--interaction=batchmode
"
23
end
24
return
concat
(
flags
,
"
"
)
25
end
26 27
local
function
secondaryflags
(
arguments
)
28
local
trackers
=
arguments
.
trackers
29
local
directives
=
arguments
.
directives
30
local
flags
=
{
}
31
if
trackers
and
trackers
~
=
"
"
then
32
flags
[
#
flags
+
1
]
=
"
--c:trackers=
"
.
.
quoted
(
trackers
)
33
end
34
if
directives
and
directives
~
=
"
"
then
35
flags
[
#
flags
+
1
]
=
"
--c:directives=
"
.
.
quoted
(
directives
)
36
end
37
if
arguments
.
silent
then
38
flags
[
#
flags
+
1
]
=
"
--c:silent
"
39
end
40
if
arguments
.
errors
then
41
flags
[
#
flags
+
1
]
=
"
--c:errors
"
42
end
43
if
arguments
.
ansi
then
44
flags
[
#
flags
+
1
]
=
"
--c:ansi
"
45
end
46
if
arguments
.
ansilog
then
47
flags
[
#
flags
+
1
]
=
"
--c:ansilog
"
48
end
49
if
arguments
.
strip
then
50
flags
[
#
flags
+
1
]
=
"
--c:strip
"
51
end
52
if
arguments
.
lmtx
then
53
flags
[
#
flags
+
1
]
=
"
--c:lmtx
"
54
end
55
return
concat
(
flags
,
"
"
)
56
end
57 58
-- The silent option is for Taco. It's a bit of a hack because we cannot yet mess
59
-- with directives. In fact, I could probably clean up the maker a bit by now.
60 61
local
template
=
[[
--ini %primaryflags% --lua=%luafile% %texfile% %secondaryflags% %dump% %redirect%
]]
62 63
local
checkers
=
{
64
primaryflags
=
"
verbose
"
,
-- "flags"
65
secondaryflags
=
"
verbose
"
,
-- "flags"
66
luafile
=
"
readable
"
,
-- "cache"
67
texfile
=
"
readable
"
,
-- "cache"
68
redirect
=
"
string
"
,
69
dump
=
"
string
"
,
70
binarypath
=
"
string
"
,
71
}
72 73
local
runners
=
{
74
luatex
=
sandbox
.
registerrunner
{
75
name
=
"
make luatex format
"
,
76
program
=
"
luatex
"
,
77
template
=
template
,
78
checkers
=
checkers
,
79
reporter
=
report_format
,
80
}
,
81
luametatex
=
sandbox
.
registerrunner
{
82
name
=
"
make luametatex format
"
,
83
program
=
"
luametatex
"
,
84
template
=
template
,
85
checkers
=
checkers
,
86
reporter
=
report_format
,
87
}
,
88
luajittex
=
sandbox
.
registerrunner
{
89
name
=
"
make luajittex format
"
,
90
program
=
"
luajittex
"
,
91
template
=
template
,
92
checkers
=
checkers
,
93
reporter
=
report_format
,
94
}
,
95
}
96 97
local
function
validbinarypath
(
)
98
-- if environment.arguments.addbinarypath then
99
if
not
environment
.
arguments
.
nobinarypath
then
100
local
path
=
environment
.
ownpath
or
file
.
dirname
(
environment
.
ownname
)
101
if
path
and
path
~
=
"
"
then
102
path
=
dir
.
expandname
(
path
)
103
if
path
~
=
"
"
and
lfs
.
isdir
(
path
)
then
104
return
path
105
end
106
end
107
end
108
end
109 110
function
environment
.
make_format
(
formatname
)
111
-- first we set up the engine and normally that information is provided
112
-- by the engine ... when we move to luametatex we could decide to simplfy
113
-- all the following
114
local
arguments
=
environment
.
arguments
115
local
engine
=
environment
.
ownmain
or
"
luatex
"
116
local
silent
=
arguments
.
silent
117
local
errors
=
arguments
.
errors
118
-- now we locate the to be used source files ... there are some variants that we
119
-- need to take care
120
local
texsourcename
=
"
"
121
local
texsourcepath
=
"
"
122
local
fulltexsourcename
=
"
"
123
if
engine
=
=
"
luametatex
"
then
124
texsourcename
=
file
.
addsuffix
(
formatname
,
"
mkxl
"
)
125
fulltexsourcename
=
resolvers
.
findfile
(
texsourcename
,
"
tex
"
)
or
"
"
126
end
127
if
fulltexsourcename
=
=
"
"
then
128
texsourcename
=
file
.
addsuffix
(
formatname
,
"
mkiv
"
)
129
fulltexsourcename
=
resolvers
.
findfile
(
texsourcename
,
"
tex
"
)
or
"
"
130
end
131
if
fulltexsourcename
=
=
"
"
then
132
texsourcename
=
file
.
addsuffix
(
formatname
,
"
tex
"
)
133
fulltexsourcename
=
resolvers
.
findfile
(
texsourcename
,
"
tex
"
)
or
"
"
134
end
135
if
fulltexsourcename
=
=
"
"
then
136
report_format
(
"
no tex source file with name %a (mkiv or tex)
"
,
formatname
)
137
return
138
end
139
report_format
(
"
using tex source file %a
"
,
fulltexsourcename
)
140
-- this is tricky: we normally have an expanded path but when we don't have one,
141
-- the current path gets appended
142
fulltexsourcename
=
dir
.
expandname
(
fulltexsourcename
)
143
texsourcepath
=
file
.
dirname
(
fulltexsourcename
)
144
if
not
lfs
.
isfile
(
fulltexsourcename
)
then
145
report_format
(
"
no accessible tex source file with name %a
"
,
fulltexsourcename
)
146
return
147
end
148
-- we're getting there, that is: we have a file that specifies the context format;
149
-- in addition to that file we need a stub for setting up lua as we start rather
150
-- minimalistic
151
local
specificationname
=
"
context.lus
"
152
local
specificationpath
=
"
"
153
local
fullspecificationname
=
resolvers
.
findfile
(
specificationname
)
or
"
"
154
if
fullspecificationname
=
=
"
"
then
155
report_format
(
"
unable to locate specification file %a
"
,
specificationname
)
156
return
157
end
158
report_format
(
"
using specification file %a
"
,
fullspecificationname
)
159
-- let's expand the found name and so an extra check
160
fullspecificationname
=
dir
.
expandname
(
fullspecificationname
)
161
specificationpath
=
file
.
dirname
(
fullspecificationname
)
162
if
texsourcepath
~
=
specificationpath
then
163
report_format
(
"
tex source file and specification file are on different paths
"
)
164
return
165
end
166
-- let's do an additional check here, if only because we then have a bit better
167
-- feedback on what goes wrong
168
if
not
lfs
.
isfile
(
fulltexsourcename
)
then
169
report_format
(
"
no accessible tex source file with name %a
"
,
fulltexsourcename
)
170
return
171
end
172
if
not
lfs
.
isfile
(
fullspecificationname
)
then
173
report_format
(
"
no accessible specification file with name %a
"
,
fulltexsourcename
)
174
return
175
end
176
-- we're still going strong
177
report_format
(
"
using tex source path %a
"
,
texsourcepath
)
178
-- we will change tot the format path because some local files will be created
179
-- in the process and we don't want clutter
180
local
validformatpath
=
caches
.
getwritablepath
(
"
formats
"
,
engine
)
or
"
"
181
local
startupdir
=
dir
.
current
(
)
182
if
validformatpath
=
=
"
"
then
183
report_format
(
"
invalid format path, insufficient write access
"
)
184
return
185
end
186
-- in case we have a qualified path, we need to do this before we change
187
-- because we can have half qualified paths (in lxc)
188
local
binarypath
=
validbinarypath
(
)
189
report_format
(
"
changing to format path %a
"
,
validformatpath
)
190
lfs
.
chdir
(
validformatpath
)
191
if
dir
.
current
(
)
~
=
validformatpath
then
192
report_format
(
"
unable to change to format path %a
"
,
validformatpath
)
193
return
194
end
195
-- we're now ready for making the format which we do on the first found
196
-- writable path
197
local
usedluastub
=
nil
198
local
usedlualibs
=
dofile
(
fullspecificationname
)
199
if
type
(
usedlualibs
)
=
=
"
string
"
then
200
usedluastub
=
file
.
join
(
specificationpath
,
usedlualibs
)
201
elseif
type
(
usedlualibs
)
=
=
"
table
"
then
202
report_format
(
"
using stub specification %a
"
,
fullspecificationname
)
203
local
texbasename
=
file
.
basename
(
name
)
204
local
luastubname
=
file
.
addsuffix
(
texbasename
,
luasuffixes
.
lua
)
205
local
lucstubname
=
file
.
addsuffix
(
texbasename
,
luasuffixes
.
luc
)
206
-- pack libraries in stub
207
report_format
(
"
creating initialization file %a
"
,
luastubname
)
208
utilities
.
merger
.
selfcreate
(
usedlualibs
,
specificationpath
,
luastubname
)
209
-- compile stub file (does not save that much as we don't use this stub at startup any more)
210
if
utilities
.
lua
.
compile
(
luastubname
,
lucstubname
)
and
lfs
.
isfile
(
lucstubname
)
then
211
report_format
(
"
using compiled initialization file %a
"
,
lucstubname
)
212
usedluastub
=
lucstubname
213
else
214
report_format
(
"
using uncompiled initialization file %a
"
,
luastubname
)
215
usedluastub
=
luastubname
216
end
217
else
218
report_format
(
"
invalid stub specification %a
"
,
fullspecificationname
)
219
lfs
.
chdir
(
startupdir
)
220
return
221
end
222
-- we're ready to go now but first we check if we actually do have a runner
223
-- for this engine ... we'd better have one
224
local
runner
=
runners
[
engine
]
225
if
not
runner
then
226
report_format
(
"
the format %a cannot be generated, no runner available for engine %a
"
,
name
,
engine
)
227
lfs
.
chdir
(
startupdir
)
228
return
229
end
230
-- now we can generate the format, where we use a couple of flags,
231
-- split into two categories
232
local
primaryflags
=
primaryflags
(
arguments
)
233
local
secondaryflags
=
secondaryflags
(
arguments
)
234
local
specification
=
{
235
binarypath
=
binarypath
,
236
primaryflags
=
primaryflags
,
237
secondaryflags
=
secondaryflags
,
238
luafile
=
quoted
(
usedluastub
)
,
239
texfile
=
quoted
(
fulltexsourcename
)
,
240
dump
=
os
.
platform
=
=
"
unix
"
and
"
\\\\dump
"
or
"
\\dump
"
,
241
}
242
if
silent
then
243
specification
.
redirect
=
"
> temp.log
"
244
end
245
statistics
.
starttiming
(
"
format
"
)
246
local
result
=
runner
(
specification
)
247
statistics
.
stoptiming
(
"
format
"
)
248
if
silent
then
249
os
.
remove
(
"
temp.log
"
)
250
end
251
-- some final report
252
report_format
(
)
253
if
binarypath
and
binarypath
~
=
"
"
then
254
report_format
(
"
binary path : %s
"
,
binarypath
or
"
?
"
)
255
end
256
report_format
(
"
format path : %s
"
,
validformatpath
)
257
report_format
(
"
luatex engine : %s
"
,
engine
)
258
report_format
(
"
lua startup file : %s
"
,
usedluastub
)
259
if
primaryflags
~
=
"
"
then
260
report_format
(
"
primary flags : %s
"
,
primaryflags
)
261
end
262
if
secondaryflags
~
=
"
"
then
263
report_format
(
"
secondary flags : %s
"
,
secondaryflags
)
264
end
265
report_format
(
"
context file : %s
"
,
fulltexsourcename
)
266
report_format
(
"
run time : %.3f seconds
"
,
statistics
.
elapsed
(
"
format
"
)
)
267
report_format
(
"
return value : %s
"
,
result
=
=
0
and
"
okay
"
or
"
error
"
)
268
report_format
(
)
269
-- last we go back to the home base
270
lfs
.
chdir
(
startupdir
)
271
end
272 273
local
template
=
[[
%primaryflags% --fmt=%fmtfile% --lua=%luafile% %texfile% %secondaryflags%
]]
274 275
local
checkers
=
{
276
primaryflags
=
"
verbose
"
,
277
secondaryflags
=
"
verbose
"
,
278
fmtfile
=
"
readable
"
,
-- "cache"
279
luafile
=
"
readable
"
,
-- "cache"
280
texfile
=
"
readable
"
,
-- "cache"
281
}
282 283
local
runners
=
{
284
luatex
=
sandbox
.
registerrunner
{
285
name
=
"
run luatex format
"
,
286
program
=
"
luatex
"
,
287
template
=
template
,
288
checkers
=
checkers
,
289
reporter
=
report_format
,
290
}
,
291
luametatex
=
sandbox
.
registerrunner
{
292
name
=
"
run luametatex format
"
,
293
program
=
"
luametatex
"
,
294
template
=
template
,
295
checkers
=
checkers
,
296
reporter
=
report_format
,
297
}
,
298
luajittex
=
sandbox
.
registerrunner
{
299
name
=
"
run luajittex format
"
,
300
program
=
"
luajittex
"
,
301
template
=
template
,
302
checkers
=
checkers
,
303
reporter
=
report_format
,
304
}
,
305
}
306 307
function
environment
.
run_format
(
formatname
,
scriptname
,
filename
,
primaryflags
,
secondaryflags
,
verbose
)
308
local
engine
=
environment
.
ownmain
or
"
luatex
"
309
if
not
formatname
or
formatname
=
=
"
"
then
310
report_format
(
"
missing format name
"
)
311
return
312
end
313
if
not
scriptname
or
scriptname
=
=
"
"
then
314
report_format
(
"
missing script name
"
)
315
return
316
end
317
if
not
lfs
.
isfile
(
formatname
)
or
not
lfs
.
isfile
(
scriptname
)
then
318
formatname
,
scriptname
=
resolvers
.
locateformat
(
formatname
)
319
end
320
if
not
formatname
or
formatname
=
=
"
"
then
321
report_format
(
"
invalid format name
"
)
322
return
323
end
324
if
not
scriptname
or
scriptname
=
=
"
"
then
325
report_format
(
"
invalid script name
"
)
326
return
327
end
328
local
runner
=
runners
[
engine
]
329
if
not
runner
then
330
report_format
(
"
format %a cannot be run, no runner available for engine %a
"
,
file
.
nameonly
(
name
)
,
engine
)
331
return
332
end
333
if
not
filename
then
334
filename
"
"
335
end
336
local
binarypath
=
validbinarypath
(
)
337
local
specification
=
{
338
binarypath
=
binarypath
,
339
primaryflags
=
primaryflags
or
"
"
,
340
secondaryflags
=
secondaryflags
or
"
"
,
341
fmtfile
=
quoted
(
formatname
)
,
342
luafile
=
quoted
(
scriptname
)
,
343
texfile
=
filename
~
=
"
"
and
quoted
(
filename
)
or
"
"
,
344
}
345
statistics
.
starttiming
(
)
346
local
result
=
runner
(
specification
)
347
local
runtime
=
statistics
.
stoptiming
(
)
348
if
verbose
then
349
report_format
(
)
350
if
binarypath
and
binarypath
~
=
"
"
then
351
report_format
(
"
binary path : %s
"
,
binarypath
)
352
end
353
report_format
(
"
luatex engine : %s
"
,
engine
)
354
report_format
(
"
lua startup file : %s
"
,
scriptname
)
355
report_format
(
"
tex format file : %s
"
,
formatname
)
356
if
filename
~
=
"
"
then
357
report_format
(
"
tex input file : %s
"
,
filename
)
358
end
359
if
primaryflags
~
=
"
"
then
360
report_format
(
"
primary flags : %s
"
,
primaryflags
)
361
end
362
if
secondaryflags
~
=
"
"
then
363
report_format
(
"
secondary flags : %s
"
,
secondaryflags
)
364
end
365
report_format
(
"
run time : %.3f seconds
"
,
runtime
)
366
report_format
(
"
return value : %s
"
,
result
=
=
0
and
"
okay
"
or
"
error
"
)
367
report_format
(
)
368
end
369
return
result
370
end
371