Debian Codegraph @

texmf-dist/tex/generic/lt3luabridge/lt3luabridge.tex

raw
1 %%
2 %% This is file `lt3luabridge.tex',
3 %% generated with the docstrip utility.
4 %%
5 %% The original source files were:
6 %%
7 %% lt3luabridge.dtx (with options: `generic-package')
8 %%
9 %% Copyright (C) 2016-2024 Vít Starý Novotný
10 %%
11 %% This work may be distributed and/or modified under the
12 %% conditions of the LaTeX Project Public License, either version 1.3c
13 %% of this license or (at your option) any later version.
14 %% The latest version of this license is in
15 %%
16 %% http://www.latex-project.org/lppl.txt
17 %%
18 %% This work has the LPPL maintenance status `maintained'.
19 %% The Current Maintainer of this work is Vít Novotný.
20 %%
21 %% Send bug reports, requests for additions and questions
22 %% either to the GitHub issue tracker at
23 %%
24 %% https://github.com/Witiko/lt3luabridge/issues
25 %%
26 %% or to the e-mail address <[email protected]>.
27 %%
28 %% MODIFICATION ADVICE:
29 %%
30 %% If you want to customize this file, it is best to make a copy of
31 %% the source file(s) from which it was produced. Use a different
32 %% name for your copy(ies) and modify the copy(ies); this will ensure
33 %% that your modifications do not get overwritten when you install a
34 %% new release of the standard system. You should also ensure that
35 %% your modified source file does not generate any modified file with
36 %% the same name as a standard file.
37 %%
38 %% You will also need to produce your own, suitably named, .ins file to
39 %% control the generation of files from your source file; this file
40 %% should contain your own preambles for the files it generates, not
41 %% those in the standard .ins files.
42 %%
43 %% The names of the source files used are shown above.
44 %%
45 \expandafter\ifx\csname ExplSyntaxOn\endcsname\relax
46 \input expl3-generic\relax
47 \fi
48 \ExplSyntaxOn
49 \int_const:Nn
50 \c_luabridge_method_directlua_int
51 { 0 }
52 \int_const:Nn
53 \c_luabridge_method_shell_int
54 { 1 }
55 \int_if_exist:NF
56 \g_luabridge_method_int
57 {
58 \int_new:N
59 \g_luabridge_method_int
60 \sys_if_engine_luatex:TF
61 {
62 \int_gset_eq:NN
63 \g_luabridge_method_int
64 \c_luabridge_method_directlua_int
65 }
66 {
67 \int_gset_eq:NN
68 \g_luabridge_method_int
69 \c_luabridge_method_shell_int
70 }
71 }
72 \msg_new:nnn
73 { luabridge }
74 { method-shell }
75 {
76 Using~shell~escape~as~the~bridging~method
77 }
78 \msg_new:nnn
79 { luabridge }
80 { method-directlua }
81 {
82 Using~direct~Lua~access~as~the~bridging~method
83 }
84 \msg_new:nnn
85 { luabridge }
86 { unknown-method }
87 {
88 Unknown~bridging~method:~#1
89 }
90 \int_case:nnF
91 { \g_luabridge_method_int }
92 {
93 { \c_luabridge_method_shell_int }
94 {
95 \msg_info:nn
96 { luabridge }
97 { method-shell }
98 }
99 { \c_luabridge_method_directlua_int }
100 {
101 \msg_info:nn
102 { luabridge }
103 { method-directlua }
104 }
105 }
106 {
107 \cs_generate_variant:Nn
108 \msg_error:nnn
109 { nnV }
110 \msg_error:nnV
111 { luabridge }
112 { unknown-method }
113 \g_luabridge_method_int
114 }
115 \int_compare:nNnT
116 { \g_luabridge_method_int }
117 =
118 { \c_luabridge_method_shell_int }
119 {
120 \sys_if_platform_unix:TF
121 {
122 \str_const:Nn
123 \c_luabridge_default_output_dirname_str
124 { $TEXMF_OUTPUT_DIRECTORY }
125 }
126 {
127 \sys_if_platform_windows:TF
128 {
129 \str_set:Nn
130 \l_tmpa_str
131 { TEXMF_OUTPUT_DIRECTORY }
132 \str_put_left:NV
133 \l_tmpa_str
134 \c_percent_str
135 \str_put_right:NV
136 \l_tmpa_str
137 \c_percent_str
138 \str_const:NV
139 \c_luabridge_default_output_dirname_str
140 \l_tmpa_str
141 }
142 {
143 \str_const:Nn
144 \c_luabridge_default_output_dirname_str
145 { . }
146 }
147 }
148 \str_const:Nx
149 \c_luabridge_default_helper_script_filename_str
150 { \jobname.luabridge.lua }
151 \str_const:Nx
152 \c_luabridge_default_error_output_filename_str
153 { \jobname.luabridge.err }
154 \str_if_exist:NF
155 \g_luabridge_output_dirname_str
156 {
157 \str_new:N
158 \g_luabridge_output_dirname_str
159 \str_gset_eq:NN
160 \g_luabridge_output_dirname_str
161 \c_luabridge_default_output_dirname_str
162 }
163 \str_if_exist:NF
164 \g_luabridge_helper_script_filename_str
165 {
166 \str_gset_eq:NN
167 \g_luabridge_helper_script_filename_str
168 \c_luabridge_default_helper_script_filename_str
169 }
170 \str_if_exist:NF
171 \g_luabridge_error_output_filename_str
172 {
173 \str_gset_eq:NN
174 \g_luabridge_error_output_filename_str
175 \c_luabridge_default_error_output_filename_str
176 }
177 \cs_new:Nn
178 \luabridge_tl_set:Nn
179 {
180 \iow_open:NV
181 \g_tmpa_iow
182 \g_luabridge_helper_script_filename_str
183 \msg_info:nnV
184 { luabridge }
185 { writing-helper-script }
186 \g_luabridge_helper_script_filename_str
187 \tl_set:Nx
188 \l_tmpa_tl
189 { \tl_to_str:n { #2 } }
190 \regex_replace_all:nnN
191 { [\\"] }
192 { \\\0 }
193 \l_tmpa_tl
194 \tl_set:Nx
195 \l_tmpa_tl
196 {
197 local~ran_ok, err = pcall(function()
198 local~ran_ok, kpse = pcall(require,~"kpse")
199 if~ran_ok~then~kpse.set_program_name("luatex") end~
200 assert(load(" \exp_not:V \l_tmpa_tl "))()
201 end)
202 if~not~ran_ok~then~
203 local~file = io.open("
204 \g_luabridge_output_dirname_str /
205 \g_luabridge_error_output_filename_str
206 ", "w")
207 if~file~then~
208 file:write(err .. " \iow_char:N \\ n ")
209 file:close()
210 end~
211 print('
212 \iow_char:N \\ \iow_char:N \\ begingroup
213 \iow_char:N \\ \iow_char:N \\ ExplSyntaxOn
214 \iow_char:N \\ \iow_char:N \\ csname~
215 msg_error:nnvv \iow_char:N \\ \iow_char:N \\ endcsname
216 { luabridge }
217 { failed-to-execute }
218 { g_luabridge_output_dirname_str }
219 { g_luabridge_error_output_filename_str }
220 \iow_char:N \\ \iow_char:N \\ endgroup
221 ')
222 end
223 }
224 \iow_now:NV
225 \g_tmpa_iow
226 \l_tmpa_tl
227 \iow_close:N
228 \g_tmpa_iow
229 \msg_info:nnV
230 { luabridge }
231 { executing-helper-script }
232 \g_luabridge_helper_script_filename_str
233 \sys_get_shell:xnNTF
234 {
235 \str_if_eq:NNTF
236 \g_luabridge_output_dirname_str
237 \c_luabridge_default_output_dirname_str
238 {
239 \sys_if_platform_windows:TF
240 {
241 if~not~defined~TEXMF_OUTPUT_DIRECTORY~(
242 texlua~
243 \g_luabridge_helper_script_filename_str
244 )~else~(
245 texlua~
246 \g_luabridge_output_dirname_str /
247 \g_luabridge_helper_script_filename_str
248 )
249 }
250 {
251 \sys_if_platform_unix:T
252 {
253 TEXMF_OUTPUT_DIRECTORY =
254 ${TEXMF_OUTPUT_DIRECTORY:-.} % noqa: S204
255 \iow_newline:
256 }
257 texlua~
258 \g_luabridge_output_dirname_str /
259 \g_luabridge_helper_script_filename_str
260 }
261 }
262 {
263 texlua~
264 \g_luabridge_output_dirname_str /
265 \g_luabridge_helper_script_filename_str
266 }
267 }
268 { }
269 #1
270 {
271 }
272 {
273 \msg_error:nn
274 { luabridge }
275 { level-disabled }
276 }
277 }
278 \prg_generate_conditional_variant:Nnn
279 \sys_get_shell:nnN
280 { xnN }
281 { TF }
282 \cs_generate_variant:Nn
283 \msg_info:nnn
284 { nnV }
285 \cs_generate_variant:Nn
286 \msg_error:nnnn
287 { nnvv }
288 \cs_generate_variant:Nn
289 \iow_open:Nn
290 { NV }
291 \cs_generate_variant:Nn
292 \iow_now:Nn
293 { NV }
294 \msg_new:nnn
295 { luabridge }
296 { writing-helper-script }
297 {
298 Writing~a~helper~Lua~script~to~file~#1
299 }
300 \msg_new:nnn
301 { luabridge }
302 { executing-helper-script }
303 {
304 Executing~a~helper~Lua~script~from~file~#1
305 }
306 \msg_new:nnnn
307 { luabridge }
308 { failed-to-execute }
309 {
310 An~error~was~encountered~while~executing~Lua~code
311 }
312 {
313 For~further~clues,~examine~file~#1 / #2
314 }
315 \msg_new:nnnn
316 { luabridge }
317 { level-disabled }
318 {
319 Shell~escape~seems~to~be~disabled
320 }
321 {
322 You~may~need~to~run~TeX~with~the~--shell-escape~or~the~
323 --enable-write18~flag,~or~write~shell_escape=t~in~the~
324 texmf.cnf~file.
325 }
326 }
327 \int_compare:nNnT
328 { \g_luabridge_method_int }
329 =
330 { \c_luabridge_method_directlua_int }
331 {
332 \cs_new:Nn
333 \luabridge_tl_set:Nn
334 {
335 \tl_set:Nn
336 \l_tmpa_tl
337 { #2 }
338 \tl_set:Nx
339 \l_tmpa_tl
340 {
341 _ENV = setmetatable({}, {__index = _ENV}) % noqa: S204
342 local~function~print(input)
343 input = tostring(input)
344 local~output = {}
345 for~line~in~input:gmatch("[^
346 \iow_char:N \\ r
347 \iow_char:N \\ n
348 ]+") do~
349 table.insert(output, line)
350 end~
351 tex.print(output)
352 end~
353 \exp_not:V \l_tmpa_tl
354 }
355 \tl_set:No
356 #1
357 {
358 \directlua
359 {
360 \tl_use:N
361 \l_tmpa_tl
362 }
363 }
364 }
365 \cs_generate_variant:Nn
366 \lua_now:n
367 { V }
368 }
369 \cs_new:Nn
370 \luabridge_now:n
371 {
372 \luabridge_tl_set:Nn
373 \l_tmpb_tl
374 { #1 }
375 \tl_use:N
376 \l_tmpb_tl
377 }
378 \cs_new_protected:Npn
379 \luabridgeExecute
380 #1
381 {
382 \luabridge_now:e
383 { #1 }
384 }
385 \cs_generate_variant:Nn
386 \luabridge_now:n
387 { e }
388 \ExplSyntaxOff
389 \endinput
390 %%
391 %% End of file `lt3luabridge.tex'.