Debian Codegraph @

texmf-dist/source/generic/lt3luabridge/lt3luabridge.dtx

raw
1 % \iffalse
2 %<*driver>
3 \documentclass[full]{l3doc}
4 \usepackage{hologo}
5 \begin{filecontents}[overwrite,nosearch,noheader]{\jobname.bib}
6
7 @online{expl3,
8 title = {expl3},
9 subtitle = {Wrapper package for experimental \LaTeX3},
10 author = {{The \LaTeX{} Team}},
11 publisher = {{CTAN}},
12 date = {2022-06-16},
13 urldate = {2022-06-26},
14 url = {https://ctan.org/pkg/expl3},
15 }
16
17 @online{markdown,
18 title = {Markdown},
19 subtitle = {A package for converting and rendering markdown documents inside \TeX},
20 author = {Vít Novotný},
21 publisher = {{CTAN}},
22 date = {2022-05-31},
23 urldate = {2022-06-26},
24 url = {https://ctan.org/pkg/markdown},
25 note = {Version 2.15.2-0-gb238dbc},
26 }
27
28 \end{filecontents}
29 \usepackage{biblatex}
30 \addbibresource{\jobname.bib}
31 \begin{document}
32 \DocInput{\jobname.dtx}
33 \end{document}
34 %</driver>
35 % \fi
36 %
37 % \title{^^A
38 % The \textsf{lt3luabridge} package: \Lua{} without \Lua\TeX^^A
39 % }
40 %
41 % \author{^^A
42 % Vít Starý Novotný\thanks
43 % {^^A
44 % E-mail:
45 % \href{mailto:[email protected]}
46 % {[email protected]}^^A
47 % }^^A
48 % }
49 %
50 % \date{Released 2025-06-24}
51 %
52 % \maketitle
53 %
54 % \begin{documentation}
55 %
56 % The \pkg{lt3luabridge} expl3~\cite{expl3} package provides support for
57 % executing \Lua{} code in \Lua\TeX{} or any other \TeX{} engine that exposes
58 % the shell. The package provides interfaces to plain \TeX, \LaTeX, and
59 % \Hologo{ConTeXt}
60 % formats:
61 % \begin{verbatim}
62 % \documentclass{standalone}
63 % \usepackage{lt3luabridge}
64 % \begin{document}
65 % $ 1 + 2 = \luabridgeExecute{ print(1 + 2) } $
66 % \end{document}
67 % \end{verbatim}
68 % The package was previously part of the Markdown package~\cite{markdown},
69 % where it has been battle-tested since 2016. Since 2022, lt3luabridge has
70 % also been available as a separate package.
71 %
72 % \section{Loading the package}
73 %
74 % Use the |\input lt3luabridge\relax| command to load the package from plain \TeX,
75 % use the |\usepackage{lt3luabridge}| command to load the package from \LaTeX, and
76 % use the |\usemodule[t][lt3luabridge]| command to load the package from \Hologo{ConTeXt}.
77 %
78 % \section{Executing \Lua{} code}
79 %
80 % The interface for executing \Lua{} code mimics the \cs{lua_now:n} function
81 % from \pkg{l3luatex}.
82 %
83 % \begin{function}[added = 2022-06-26, updated=2022-07-31]{\luabridge_now:n, \luabridge_now:e}
84 % \begin{syntax}
85 % \cs{luabridge_now:n} \Arg{token list}
86 % \end{syntax}
87 % The \meta{token list} is first tokenized by \TeX{}, which includes
88 % converting line ends to spaces in the usual \TeX{} manner and which
89 % respects currently-applicable \TeX{} category codes. The resulting
90 % \meta{\Lua{} input} is passed to the \Lua{} interpreter for processing.
91 % Each \cs{luabridge_now:n} block is treated by \Lua{} as a separate chunk.
92 % The \Lua{} interpreter executes the \meta{\Lua{} input} immediately,
93 % and in an expandable manner.
94 %
95 % Unlike \cs{lua_now:n}, \cs{luabridge_now:n} may execute \meta{\Lua{} input}
96 % in a separate process from \TeX. Therefore, you should not interact with
97 % \TeX{} from \meta{\Lua{} input} or create global variables. The only
98 % exception is the standard output produced by the |print()| \Lua{} function
99 % like in the example at the top of this page. The standard output of
100 % |print()| will be inserted into \TeX's input stream.
101 % \end{function}
102 %
103 % \begin{function}[added = 2022-06-26, updated=2022-07-31]{\luabridgeExecute}
104 % \begin{syntax}
105 % \cs{luabridgeExecute} \Arg{token list}
106 % \end{syntax}
107 % The \cs{luabridgeExecute} document command aliases
108 % the \cs{luabridge_now:e} function.
109 % \end{function}
110 %
111 % \begin{function}[added = 2024-02-14]{\luabridge_tl_set:Nn}
112 % \begin{syntax}
113 % \cs{luabridge_tl_set:Nn} \meta{tl var} \Arg{token list}
114 % \end{syntax}
115 % Like \cs{lua_now:n} but the result of executing the Lua code is stored in
116 % \meta{tl var} instead of being inserted into \TeX's input stream.
117 % \end{function}
118 %
119 % \section{Setting and getting the method to execute \Lua{} code}
120 %
121 % There are several methods that can be used to execute \Lua{} code. This
122 % section describes the interface that the package provides to set the
123 % preferred method or to determine which method was used.
124 %
125 % \begin{variable}[added = 2022-06-26]{\g_luabridge_method_int}
126 % This variable controls the method used to execute \Lua{} code.
127 % The variable is set automatically when the package is loaded
128 % and changing the value of the variable afterwards has no effect.
129 % However, we can set the value of the variable before loading the
130 % package to one of the constants described below.
131 % \end{variable}
132 %
133 % \begin{variable}[added = 2022-07-31]{\c_luabridge_method_shell_int}
134 % Use shell escape through the \cs{write18} \TeX{} command to execute \Lua{} code.
135 % \end{variable}
136 %
137 % \begin{variable}[added = 2022-06-26]{\c_luabridge_method_directlua_int}
138 % Use the \cs{directlua} primitive of Lua\TeX{} to execute \Lua{} code.
139 % \end{variable}
140 %
141 % \section{Setting and getting the filenames of helper files}
142 %
143 % When shell escape is used to execute \Lua{} code, several helper files are
144 % needed to shuffle around code and output. The following variables and
145 % constants are undefined when the \cs{directlua} primitive of Lua\TeX{} is
146 % used to execute \Lua{} code.
147 %
148 % \begin{variable}[added = 2022-06-26]{\g_luabridge_output_dirname_str}
149 % This variable controls the output directory that will store the helper
150 % files. The variable should be set to the same value as the
151 % \texttt{-output-directory} parameter of the \TeX{} engine.
152 % \end{variable}
153 %
154 % \begin{variable}[added = 2022-06-26, updated = 2024-07-03]{\c_luabridge_default_output_dirname_str}
155 % This constant is the default value of \cs{g_luabridge_output_dirname_str}.
156 % \end{variable}
157 %
158 % \begin{variable}[added = 2022-06-26]{\g_luabridge_helper_script_filename_str}
159 % This variable controls the filename of a helper \Lua{} script that will be
160 % executed from the shell using the \TeX{} \Lua{} interpreter.
161 % \end{variable}
162 %
163 % \begin{variable}[added = 2022-06-26]{\c_luabridge_default_helper_script_filename_str}
164 % This constant is the default value of
165 % \cs{g_luabridge_helper_script_filename_str}.
166 % \end{variable}
167 %
168 % \begin{variable}[added = 2022-06-26]{\g_luabridge_error_output_filename_str}
169 % This variable controls the filename of a helper file that will contain the
170 % error output produced by the |texlua| interpreter (if any).
171 % \end{variable}
172 %
173 % \begin{variable}[added = 2022-06-26]{\c_luabridge_default_error_output_filename_str}
174 % This constant is the default value of
175 % \cs{g_luabridge_error_output_filename_str}.
176 % \end{variable}
177 %
178 % None of the variables from this section may contain spaces.
179 %
180 % \end{documentation}
181 %
182 % \begin{implementation}
183 %
184 % \section{Plain \TeX{} implementation}
185 %
186 % This section contains the implementation for plain \TeX{} using generic expl3.
187 %
188 % \begin{macrocode}
189 %<@@=luabridge>
190 %<*generic-package>
191 \expandafter\ifx\csname ExplSyntaxOn\endcsname\relax
192 \input expl3-generic\relax
193 \fi
194 \ExplSyntaxOn
195 \int_const:Nn
196 \c_luabridge_method_directlua_int
197 { 0 }
198 \int_const:Nn
199 \c_luabridge_method_shell_int
200 { 1 }
201 \int_if_exist:NF
202 \g_luabridge_method_int
203 {
204 \int_new:N
205 \g_luabridge_method_int
206 \sys_if_engine_luatex:TF
207 {
208 \int_gset_eq:NN
209 \g_luabridge_method_int
210 \c_luabridge_method_directlua_int
211 }
212 {
213 \int_gset_eq:NN
214 \g_luabridge_method_int
215 \c_luabridge_method_shell_int
216 }
217 }
218 \msg_new:nnn
219 { luabridge }
220 { method-shell }
221 {
222 Using~shell~escape~as~the~bridging~method
223 }
224 \msg_new:nnn
225 { luabridge }
226 { method-directlua }
227 {
228 Using~direct~Lua~access~as~the~bridging~method
229 }
230 \msg_new:nnn
231 { luabridge }
232 { unknown-method }
233 {
234 Unknown~bridging~method:~#1
235 }
236 \int_case:nnF
237 { \g_luabridge_method_int }
238 {
239 { \c_luabridge_method_shell_int }
240 {
241 \msg_info:nn
242 { luabridge }
243 { method-shell }
244 }
245 { \c_luabridge_method_directlua_int }
246 {
247 \msg_info:nn
248 { luabridge }
249 { method-directlua }
250 }
251 }
252 {
253 \cs_generate_variant:Nn
254 \msg_error:nnn
255 { nnV }
256 \msg_error:nnV
257 { luabridge }
258 { unknown-method }
259 \g_luabridge_method_int
260 }
261 \int_compare:nNnT
262 { \g_luabridge_method_int }
263 =
264 { \c_luabridge_method_shell_int }
265 {
266 % \end{macrocode}
267 %
268 % Instead of assuming the current working directory as the output directory,
269 % try to determine the value of `-output-directory` from the environmental
270 % variable |TEXMF_OUTPUT_DIRECTORY|, which is automatically defined by
271 % \TeX{} engines and accessible from child processes.
272 %
273 % Note that this environmental variable is
274 % \href{https://github.com/MiKTeX/miktex/issues/1630}{not currently set by
275 % Mik\TeX{}}. Therefore, when shell escape is used to execute \Lua{} code,
276 % the value of the variable \cs{g_luabridge_output_dirname_str} won't be
277 % correctly determined by Mik\TeX{}, unless you a) manually set the
278 % environmental variable |TEXMF_OUTPUT_DIRECTORY| to the same value as
279 % |-output-directory| or b) do not set |-output-directory|. If neither a) or b)
280 % is an option for you, you may also c) manually set the variable
281 % \cs{g_luabridge_output_dirname_str} to the same value as |-output-directory|.
282 %
283 % \begin{macrocode}
284 \sys_if_platform_unix:TF
285 {
286 \str_const:Nn
287 \c_luabridge_default_output_dirname_str
288 { $TEXMF_OUTPUT_DIRECTORY }
289 }
290 {
291 \sys_if_platform_windows:TF
292 {
293 \str_set:Nn
294 \l_tmpa_str
295 { TEXMF_OUTPUT_DIRECTORY }
296 \str_put_left:NV
297 \l_tmpa_str
298 \c_percent_str
299 \str_put_right:NV
300 \l_tmpa_str
301 \c_percent_str
302 \str_const:NV
303 \c_luabridge_default_output_dirname_str
304 \l_tmpa_str
305 }
306 {
307 \str_const:Nn
308 \c_luabridge_default_output_dirname_str
309 { . }
310 }
311 }
312 % \end{macrocode}
313 %
314 % The constants \cs{c_luabridge_default_helper_script_filename_str} and
315 % \cs{c_luabridge_default_error_output_filename_str} are derived from
316 % \cs{jobname} by default. Since these constants may not contain spaces,
317 % \cs{jobname} must also not contain spaces, i.e. the filenames of your .tex
318 % files must not contain spaces.
319 %
320 % \begin{macrocode}
321 \str_const:Nx
322 \c_luabridge_default_helper_script_filename_str
323 { \jobname.luabridge.lua }
324 \str_const:Nx
325 \c_luabridge_default_error_output_filename_str
326 { \jobname.luabridge.err }
327 \str_if_exist:NF
328 \g_luabridge_output_dirname_str
329 {
330 \str_new:N
331 \g_luabridge_output_dirname_str
332 \str_gset_eq:NN
333 \g_luabridge_output_dirname_str
334 \c_luabridge_default_output_dirname_str
335 }
336 \str_if_exist:NF
337 \g_luabridge_helper_script_filename_str
338 {
339 \str_gset_eq:NN
340 \g_luabridge_helper_script_filename_str
341 \c_luabridge_default_helper_script_filename_str
342 }
343 \str_if_exist:NF
344 \g_luabridge_error_output_filename_str
345 {
346 \str_gset_eq:NN
347 \g_luabridge_error_output_filename_str
348 \c_luabridge_default_error_output_filename_str
349 }
350 \cs_new:Nn
351 \luabridge_tl_set:Nn
352 {
353 \iow_open:NV
354 \g_tmpa_iow
355 \g_luabridge_helper_script_filename_str
356 \msg_info:nnV
357 { luabridge }
358 { writing-helper-script }
359 \g_luabridge_helper_script_filename_str
360 % \end{macrocode}
361 %
362 % Escape |"| and |\| in the Lua code, so that we can represent it as a
363 % double-quoted string that we can pass into the |load()| Lua built-in
364 % and fail gracefully if the Lua code fails to compile.
365 %
366 % \begin{macrocode}
367 \tl_set:Nx
368 \l_tmpa_tl
369 { \tl_to_str:n { #2 } }
370 \regex_replace_all:nnN
371 { [\\"] }
372 { \\\0 }
373 \l_tmpa_tl
374 \tl_set:Nx
375 \l_tmpa_tl
376 {
377 local~ran_ok, err = pcall(function()
378 local~ran_ok, kpse = pcall(require,~"kpse")
379 if~ran_ok~then~kpse.set_program_name("luatex") end~
380 assert(load(" \exp_not:V \l_tmpa_tl "))()
381 end)
382 if~not~ran_ok~then~
383 local~file = io.open("
384 \g_luabridge_output_dirname_str /
385 \g_luabridge_error_output_filename_str
386 ", "w")
387 if~file~then~
388 file:write(err .. " \iow_char:N \\ n ")
389 file:close()
390 end~
391 print('
392 \iow_char:N \\ \iow_char:N \\ begingroup
393 \iow_char:N \\ \iow_char:N \\ ExplSyntaxOn
394 \iow_char:N \\ \iow_char:N \\ csname~
395 msg_error:nnvv \iow_char:N \\ \iow_char:N \\ endcsname
396 { luabridge }
397 { failed-to-execute }
398 { g_luabridge_output_dirname_str }
399 { g_luabridge_error_output_filename_str }
400 \iow_char:N \\ \iow_char:N \\ endgroup
401 ')
402 end
403 }
404 \iow_now:NV
405 \g_tmpa_iow
406 \l_tmpa_tl
407 \iow_close:N
408 \g_tmpa_iow
409 \msg_info:nnV
410 { luabridge }
411 { executing-helper-script }
412 \g_luabridge_helper_script_filename_str
413 \sys_get_shell:xnNTF
414 {
415 % \end{macrocode}
416 %
417 % If the environmental variable \texttt{TEXMF_OUTPUT_DIRECTORY} is undefined,
418 % use the current working directory (\texttt{.}) instead.
419 %
420 % \begin{macrocode}
421 \str_if_eq:NNTF
422 \g_luabridge_output_dirname_str
423 \c_luabridge_default_output_dirname_str
424 {
425 \sys_if_platform_windows:TF
426 {
427 if~not~defined~TEXMF_OUTPUT_DIRECTORY~(
428 texlua~
429 \g_luabridge_helper_script_filename_str
430 )~else~(
431 texlua~
432 \g_luabridge_output_dirname_str /
433 \g_luabridge_helper_script_filename_str
434 )
435 }
436 {
437 \sys_if_platform_unix:T
438 {
439 TEXMF_OUTPUT_DIRECTORY =
440 ${TEXMF_OUTPUT_DIRECTORY:-.} % noqa: S204
441 \iow_newline:
442 }
443 texlua~
444 \g_luabridge_output_dirname_str /
445 \g_luabridge_helper_script_filename_str
446 }
447 }
448 {
449 texlua~
450 \g_luabridge_output_dirname_str /
451 \g_luabridge_helper_script_filename_str
452 }
453 }
454 { }
455 #1
456 {
457 }
458 {
459 \msg_error:nn
460 { luabridge }
461 { level-disabled }
462 }
463 }
464 \prg_generate_conditional_variant:Nnn
465 \sys_get_shell:nnN
466 { xnN }
467 { TF }
468 \cs_generate_variant:Nn
469 \msg_info:nnn
470 { nnV }
471 \cs_generate_variant:Nn
472 \msg_error:nnnn
473 { nnvv }
474 \cs_generate_variant:Nn
475 \iow_open:Nn
476 { NV }
477 \cs_generate_variant:Nn
478 \iow_now:Nn
479 { NV }
480 \msg_new:nnn
481 { luabridge }
482 { writing-helper-script }
483 {
484 Writing~a~helper~Lua~script~to~file~#1
485 }
486 \msg_new:nnn
487 { luabridge }
488 { executing-helper-script }
489 {
490 Executing~a~helper~Lua~script~from~file~#1
491 }
492 \msg_new:nnnn
493 { luabridge }
494 { failed-to-execute }
495 {
496 An~error~was~encountered~while~executing~Lua~code
497 }
498 {
499 For~further~clues,~examine~file~#1 / #2
500 }
501 \msg_new:nnnn
502 { luabridge }
503 { level-disabled }
504 {
505 Shell~escape~seems~to~be~disabled
506 }
507 {
508 You~may~need~to~run~TeX~with~the~--shell-escape~or~the~
509 --enable-write18~flag,~or~write~shell_escape=t~in~the~
510 texmf.cnf~file.
511 }
512 }
513 \int_compare:nNnT
514 { \g_luabridge_method_int }
515 =
516 { \c_luabridge_method_directlua_int }
517 {
518 \cs_new:Nn
519 \luabridge_tl_set:Nn
520 {
521 \tl_set:Nn
522 \l_tmpa_tl
523 { #2 }
524 \tl_set:Nx
525 \l_tmpa_tl
526 {
527 _ENV = setmetatable({}, {__index = _ENV}) % noqa: S204
528 local~function~print(input)
529 input = tostring(input)
530 local~output = {}
531 for~line~in~input:gmatch("[^
532 \iow_char:N \\ r
533 \iow_char:N \\ n
534 ]+") do~
535 table.insert(output, line)
536 end~
537 tex.print(output)
538 end~
539 \exp_not:V \l_tmpa_tl
540 }
541 \tl_set:No
542 #1
543 {
544 \directlua
545 {
546 \tl_use:N
547 \l_tmpa_tl
548 }
549 }
550 }
551 \cs_generate_variant:Nn
552 \lua_now:n
553 { V }
554 }
555 \cs_new:Nn
556 \luabridge_now:n
557 {
558 \luabridge_tl_set:Nn
559 \l_tmpb_tl
560 { #1 }
561 \tl_use:N
562 \l_tmpb_tl
563 }
564 \cs_new_protected:Npn
565 \luabridgeExecute
566 #1
567 {
568 \luabridge_now:e
569 { #1 }
570 }
571 \cs_generate_variant:Nn
572 \luabridge_now:n
573 { e }
574 \ExplSyntaxOff
575 %</generic-package>
576 % \end{macrocode}
577 %
578 % \section{\LaTeX{} implementation}
579 %
580 % This section contains the implementation for \LaTeX.
581 %
582 % \begin{macrocode}
583 %<*latex-package>
584 \RequirePackage{expl3}
585 \ProvidesExplPackage
586 {lt3luabridge}%
587 {2025-06-24}%
588 {2.2.2}%
589 {An expl3 package that allows you to execute Lua code in LuaTeX or any other
590 TeX engine that exposes the shell}
591 \input lt3luabridge\relax
592 %</latex-package>
593 % \end{macrocode}
594 %
595 % \section{\Hologo{ConTeXt} implementation}
596 %
597 % This section contains the implementation for \Hologo{ConTeXt}.
598 % \Hologo{ConTeXt} MkII, MkIV, and later formats are supported.
599 %
600 % \begin{macrocode}
601 %<*context-package>
602 \writestatus{loading}{ConTeXt User Module / lt3luabridge}
603 \startmodule[lt3luabridge]
604 \unprotect
605 \input lt3luabridge\relax
606 %</context-package>
607 % \end{macrocode}
608 %
609 % \end{implementation}
610 %
611 % \printbibliography
612 % \PrintIndex