Debian Codegraph @

texmf-dist/doc/support/cluttex/src/texrunner/luatexinit.lua

raw symbols SCIP
1 local function create_initialization_script(filename, options)
2 local initscript = assert(io.open(filename,"w"))
3 if type(options.file_line_error) == "boolean" then
4 initscript:write(string.format("texconfig.file_line_error = %s\n", options.file_line_error))
5 end
6 if type(options.halt_on_error) == "boolean" then
7 initscript:write(string.format("texconfig.halt_on_error = %s\n", options.halt_on_error))
8 end
9 initscript:write([==[
10 local print = print
11 local io_open = io.open
12 local io_write = io.write
13 local os_execute = os.execute
14 local texio_write = texio.write
15 local texio_write_nl = texio.write_nl
16 ]==])
17
18 -- Packages coded in Lua doesn't follow -output-directory option and doesn't write command to the log file
19 initscript:write(string.format("local output_directory = %q\n", options.output_directory))
20 -- tex.jobname may not be available when io.open is called for the first time
21 initscript:write(string.format("local jobname = %q\n", options.jobname))
22 initscript:write([==[
23 local luawritelog
24 local function openluawritelog()
25 if not luawritelog then
26 luawritelog = assert(io_open(output_directory .. "/" .. jobname .. ".cluttex-fls", "w"))
27 end
28 return luawritelog
29 end
30 io.open = function(fname, mode)
31 -- luatexja-ruby
32 if mode == "w" and fname == jobname .. ".ltjruby" then
33 fname = output_directory .. "/" .. fname
34 end
35 if type(mode) == "string" and string.find(mode, "w") ~= nil then
36 -- write mode
37 openluawritelog():write("OUTPUT " .. fname .. "\n")
38 end
39 return io_open(fname, mode)
40 end
41 os.execute = function(...)
42 texio_write_nl("log", string.format("CLUTTEX_EXEC %s", ...), "")
43 return os_execute(...)
44 end
45 ]==])
46
47 -- Silence some of the TeX output to the terminal.
48 initscript:write([==[
49 local function start_file_cb(category, filename)
50 if category == 1 then -- a normal data file, like a TeX source
51 texio_write_nl("log", "("..filename)
52 elseif category == 2 then -- a font map coupling font names to resources
53 texio_write("log", "{"..filename)
54 elseif category == 3 then -- an image file (png, pdf, etc)
55 texio_write("<"..filename)
56 elseif category == 4 then -- an embedded font subset
57 texio_write("<"..filename)
58 elseif category == 5 then -- a fully embedded font
59 texio_write("<<"..filename)
60 else
61 print("start_file: unknown category", category, filename)
62 end
63 end
64 callback.register("start_file", start_file_cb)
65 local function stop_file_cb(category)
66 if category == 1 then
67 texio_write("log", ")")
68 elseif category == 2 then
69 texio_write("log", "}")
70 elseif category == 3 then
71 texio_write(">")
72 elseif category == 4 then
73 texio_write(">")
74 elseif category == 5 then
75 texio_write(">>")
76 else
77 print("stop_file: unknown category", category)
78 end
79 end
80 callback.register("stop_file", stop_file_cb)
81 texio.write = function(...)
82 if select("#",...) == 1 then
83 -- Suppress luaotfload's message (See src/fontloader/runtime/fontload-reference.lua)
84 local s = ...
85 if string.match(s, "^%(using cache: ")
86 or string.match(s, "^%(using write cache: ")
87 or string.match(s, "^%(using read cache: ")
88 or string.match(s, "^%(load luc: ")
89 or string.match(s, "^%(load cache: ") then
90 return texio_write("log", ...)
91 end
92 end
93 return texio_write(...)
94 end
95 ]==])
96
97 -- Fix "arg" to make luamplib work
98 initscript:write([==[
99 if string.match(arg[0], "^%-%-lua=") then
100 local minindex = 0
101 while arg[minindex - 1] ~= nil do
102 minindex = minindex - 1
103 end
104 local arg2 = {}
105 for i = 0, #arg - minindex do
106 arg2[i] = arg[i + minindex]
107 end
108 arg = arg2
109 end
110 ]==])
111 initscript:close()
112 end
113
114 return {
115 create_initialization_script = create_initialization_script
116 }