summaryrefslogtreecommitdiff
path: root/macros/generic/lt3luabridge/lt3luabridge.dtx
blob: 39e353a1232f9cbc17bcb86d705bc9eef37847e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
% \iffalse
%<*driver>
\documentclass[full]{l3doc}
\usepackage{hologo}
\begin{filecontents}[overwrite,nosearch,noheader]{\jobname.bib}

@online{expl3,
  title = {expl3},
  subtitle = {Wrapper package for experimental \LaTeX3},
  author = {{The \LaTeX{} Team}},
  publisher = {{CTAN}},
  date = {2022-06-16},
  urldate = {2022-06-26},
  url = {https://ctan.org/pkg/expl3},
}

@online{markdown,
  title = {Markdown},
  subtitle = {A package for converting and rendering markdown documents inside \TeX},
  author = {Vít Novotný},
  publisher = {{CTAN}},
  date = {2022-05-31},
  urldate = {2022-06-26},
  url = {https://ctan.org/pkg/markdown},
  note = {Version 2.15.2-0-gb238dbc},
}

\end{filecontents}
\usepackage{biblatex}
\addbibresource{\jobname.bib}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{^^A
%   The \textsf{lt3luabridge} package: \Lua{} without \Lua\TeX^^A
% }
%
% \author{^^A
%  Vít Novotný\thanks
%    {^^A
%      E-mail:
%        \href{mailto:witiko@mail.muni.cz}
%          {witiko@mail.muni.cz}^^A
%    }^^A
% }
%
% \date{Released 2022-08-08}
%
% \maketitle
%
% \begin{documentation}
%
% The \pkg{lt3luabridge} expl3~\cite{expl3} package provides support for
% executing \Lua{} code in \Lua\TeX{} or any other \TeX{} engine that exposes
% the shell. The package provides interfaces to plain \TeX, \LaTeX, and
% \Hologo{ConTeXt}
% formats:
% \begin{verbatim}
% \documentclass{standalone}
% \usepackage{lt3luabridge}
% \begin{document}
% $ 1 + 2 = \luabridgeExecute{ print(1 + 2) } $
% \end{document}
% \end{verbatim}
% The package was previously part of the Markdown package~\cite{markdown},
% where it has been battle-tested since 2016.  Since 2022, lt3luabridge has
% also been available as a separate package.
%
% \section{Loading the package}
%
% Use the |\input lt3luabridge\relax| command to load the package from plain \TeX,
% use the |\usepackage{lt3luabridge}| command to load the package from \LaTeX, and
% use the |\usemodule[t][lt3luabridge]| command to load the package from \Hologo{ConTeXt}.
%
% \section{Executing \Lua{} code}
%
% The interface for executing \Lua{} code mimics the \cs{lua_now:n} function
% from \pkg{l3luatex}.
%
% \begin{function}[added = 2022-06-26, updated=2022-07-31]{\luabridge_now:n, \luabridge_now:e}
%   \begin{syntax}
%     \cs{luabridge_now:n} \Arg{token list}
%   \end{syntax}
%   The \meta{token list} is first tokenized by \TeX{}, which includes
%   converting line ends to spaces in the usual \TeX{} manner and which
%   respects currently-applicable \TeX{} category codes. The resulting
%   \meta{\Lua{} input} is passed to the \Lua{} interpreter for processing.
%   Each \cs{luabridge_now:n} block is treated by \Lua{} as a separate chunk.
%   The \Lua{} interpreter executes the \meta{\Lua{} input} immediately,
%   and in an expandable manner.
%
%   Unlike \cs{lua_now:n}, \cs{luabridge_now:n} may execute \meta{\Lua{} input}
%   in a separate process from \TeX. Therefore, you should not interact with
%   \TeX{} from \meta{\Lua{} input} or create global variables. The only
%   exception is the standard output produced by the |print()| \Lua{} function
%   like in the example at the top of this page. The standard output of
%   |print()| will be inserted into \TeX's input stream.
% \end{function}
%
% \begin{function}[added = 2022-06-26, updated=2022-07-31]{\luabridgeExecute}
%   \begin{syntax}
%     \cs{luabridgeExecute} \Arg{token list}
%   \end{syntax}
%   The \cs{luabridgeExecute} document command aliases
%   the \cs{luabridge_now:e} function.
% \end{function}
%
% \section{Setting and getting the method to execute \Lua{} code}
%
% There are several methods that can be used to execute \Lua{} code. This
% section describes the interface that the package provides to set the
% preferred method or to determine which method was used.
%
% \begin{variable}[added = 2022-06-26]{\g_luabridge_method_int}
%   This variable controls the method used to execute \Lua{} code.
%   The variable is set automatically when the package is loaded
%   and changing the value of the variable afterwards has no effect.
%   However, we can set the value of the variable before loading the
%   package to one of the constants described below.
% \end{variable}
%
% \begin{variable}[added = 2022-07-31]{\c_luabridge_method_shell_int}
%   Use shell escape through the \cs{write18} \TeX{} command to execute \Lua{} code.
% \end{variable}
%
% \begin{variable}[added = 2022-06-26]{\c_luabridge_method_directlua_int}
%   Use the \cs{directlua} primitive of Lua\TeX{} to execute \Lua{} code.
% \end{variable}
%
% \section{Setting and getting the filenames of helper files}
%
% When shell escape is used to execute \Lua{} code, several helper files are
% needed to shuffle around code and output. The following variables and
% constants are undefined when the \cs{directlua} primitive of Lua\TeX{} is
% used to execute \Lua{} code.
%
% \begin{variable}[added = 2022-06-26]{\g_luabridge_output_dirname_str}
%   This variable controls the output directory that will store the helper
%   files. The variable should be set to the same value as the
%   \texttt{-output-directory} parameter of the \TeX{} engine.
% \end{variable}
%
% \begin{variable}[added = 2022-06-26]{\c_luabridge_default_output_dirname_str}
%   This constant is the default value of \cs{g_luabridge_output_dirname_str}.
% \end{variable}
%
% \begin{variable}[added = 2022-06-26]{\g_luabridge_helper_script_filename_str}
%   This variable controls the filename of a helper \Lua{} script that will be
%   executed from the shell using the \TeX{} \Lua{} interpreter.
% \end{variable}
%
% \begin{variable}[added = 2022-06-26]{\c_luabridge_default_helper_script_filename_str}
%   This constant is the default value of
%   \cs{g_luabridge_helper_script_filename_str}.
% \end{variable}
%
% \begin{variable}[added = 2022-06-26]{\g_luabridge_error_output_filename_str}
%   This variable controls the filename of a helper file that will contain the
%   error output produced by the |texlua| interpreter (if any).
% \end{variable}
%
% \begin{variable}[added = 2022-06-26]{\c_luabridge_default_error_output_filename_str}
%   This constant is the default value of
%   \cs{g_luabridge_error_output_filename_str}.
% \end{variable}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{Plain \TeX{} implementation}
%
% This section contains the implementation for plain \TeX{} using generic expl3.
%
%    \begin{macrocode}
%<@@=luabridge>
%<*generic-package>
\ifx\ExplSyntaxOn\undefined
  \input expl3-generic\relax
\fi
\ExplSyntaxOn
\int_const:Nn
  \c_luabridge_method_directlua_int
  { 0 }
\int_const:Nn
  \c_luabridge_method_shell_int
  { 1 }
\int_if_exist:NF
  \g_luabridge_method_int
  {
    \int_new:N
      \g_luabridge_method_int
      \sys_if_engine_luatex:TF
        {
          \int_gset_eq:NN
            \g_luabridge_method_int
            \c_luabridge_method_directlua_int
        }
        {
          \int_gset_eq:NN
            \g_luabridge_method_int
            \c_luabridge_method_shell_int
        }
  }
\msg_new:nnn
  { luabridge }
  { method-shell }
  {
    Using~shell~escape~as~the~bridging~method
  }
\msg_new:nnn
  { luabridge }
  { method-directlua }
  {
    Using~direct~Lua~access~as~the~bridging~method
  }
\msg_new:nnn
  { luabridge }
  { unknown-method }
  {
    Unknown~bridging~method:~#1
  }
\int_case:nnF
  { \g_luabridge_method_int }
  {
    { \c_luabridge_method_shell_int }
      {
        \msg_info:nn
          { luabridge }
          { method-shell }
      }
    { \c_luabridge_method_directlua_int }
      {
        \msg_info:nn
          { luabridge }
          { method-directlua }
      }
  }
  {
    \cs_generate_variant:Nn
      \msg_error:nnn
      { nnV }
    \msg_error:nnV
      { luabridge }
      { unknown-method }
      \g_luabridge_method_int
  }
\int_compare:nNnT
  { \g_luabridge_method_int }
  =
  { \c_luabridge_method_shell_int }
  {
    \str_const:Nn
      \c_luabridge_default_output_dirname_str
      { . }
    \str_const:Nx
      \c_luabridge_default_helper_script_filename_str
      { \jobname.luabridge.lua }
    \str_const:Nx
      \c_luabridge_default_error_output_filename_str
      { \jobname.luabridge.err }
    \str_if_exist:NF
      \g_luabridge_output_dirname_str
      {
        \str_new:N
          \g_luabridge_output_dirname_str
        \tl_gset:Nn
          \g_luabridge_output_dirname_str
          \c_luabridge_default_output_dirname_str
      }
    \str_if_exist:NF
      \g_luabridge_helper_script_filename_str
      {
        \str_gset_eq:NN
          \g_luabridge_helper_script_filename_str
          \c_luabridge_default_helper_script_filename_str
      }
    \str_if_exist:NF
      \g_luabridge_error_output_filename_str
      {
        \str_gset_eq:NN
          \g_luabridge_error_output_filename_str
          \c_luabridge_default_error_output_filename_str
      }
    \cs_new:Nn
      \luabridge_now:n
      {
        \iow_open:NV
          \g_tmpa_iow
          \g_luabridge_helper_script_filename_str
        \msg_info:nnV
          { luabridge }
          { writing-helper-script }
          \g_luabridge_helper_script_filename_str
        \tl_set:Nn
          \l_tmpa_tl
          { #1 }
        \tl_set:Nx
          \l_tmpb_tl
          {
            local~ran_ok, error = pcall(function()
              local~ran_ok, kpse = pcall(require,~"kpse")
              if~ran_ok~then~kpse.set_program_name("luatex") end~
              \exp_not:V \l_tmpa_tl
              \iow_newline:
            end)
            if~not~ran_ok~then~
              local~file = io.open("
                \g_luabridge_output_dirname_str /
                \g_luabridge_error_output_filename_str
              ", "w")
              if~file~then~
                file:write(error .. " \iow_char:N \\ n ")
                file:close()
              end~
              print('
                \iow_char:N \\ \iow_char:N \\ begingroup
                  \iow_char:N \\ \iow_char:N \\ ExplSyntaxOn
                  \iow_char:N \\ \iow_char:N \\ msg_error:nnvv
                    { luabridge }
                    { failed-to-execute }
                    { g_luabridge_output_dirname_str }
                    { g_luabridge_error_output_filename_str }
                \iow_char:N \\ \iow_char:N \\ endgroup
              ')
            end
          }
        \iow_now:NV
          \g_tmpa_iow
          \l_tmpa_tl
        \iow_close:N
          \g_tmpa_iow
        \msg_info:nnV
          { luabridge }
          { executing-helper-script }
          \g_luabridge_helper_script_filename_str
        \sys_get_shell:xnNTF
          {
            texlua~
              \g_luabridge_output_dirname_str /
              \g_luabridge_helper_script_filename_str
          }
          { }
          \l_tmpa_tl
          {
            \l_tmpa_tl
          }
          {
            \msg_error:nn
              { luabridge }
              { level-disabled }
          }
      }
    \prg_generate_conditional_variant:Nnn
      \sys_get_shell:nnN
      { xnN }
      { TF }
    \cs_generate_variant:Nn
      \msg_info:nnn
      { nnV }
    \cs_generate_variant:Nn
      \msg_error:nnnn
      { nnvv }
    \cs_generate_variant:Nn
      \iow_open:Nn
      { NV }
    \cs_generate_variant:Nn
      \iow_now:Nn
      { NV }
    \msg_new:nnn
      { luabridge }
      { writing-helper-script }
      {
        Writing~a~helper~Lua~script~to~file~#1
      }
    \msg_new:nnn
      { luabridge }
      { executing-helper-script }
      {
        Executing~a~helper~Lua~script~from~file~#1
      }
    \msg_new:nnnn
      { luabridge }
      { failed-to-execute }
      {
        An~error~was~encountered~while~executing~Lua~code
      }
      {
        For further clues, examine file #1 / #2
      }
    \msg_new:nnnn
      { luabridge }
      { level-disabled }
      {
        Shell~escape~seems~to~be~disabled
      }
      {
        You~may~need~to~run~TeX~with~the~--shell-escape~or~the~
        --enable-write18~flag,~or~write~shell_escape=t~in~the~
        texmf.cnf~file.
      }
  }
\int_compare:nNnT
  { \g_luabridge_method_int }
  =
  { \c_luabridge_method_directlua_int }
  {
    \cs_new:Nn
      \luabridge_now:n
      {
        \tl_set:Nn
          \l_tmpa_tl
          { #1 }
        \tl_set:Nx
          \l_tmpb_tl
          {
            _ENV = setmetatable({}, {__index = _ENV})
            local~function~print(input)
              input = tostring(input)
              local~output = {}
              for~line~in~input:gmatch("[^
                    \iow_char:N \\ r
                    \iow_char:N \\ n
                  ]+") do~
                table.insert(output, line)
              end~
              tex.print(output)
            end~
            \exp_not:V \l_tmpa_tl
          }
        \lua_now:V
          \l_tmpb_tl
      }
    \cs_generate_variant:Nn
      \lua_now:n
      { V }
  }
\cs_new_protected:Npn
  \luabridgeExecute
  #1
  {
    \luabridge_now:e
      { #1 }
  }
\cs_generate_variant:Nn
  \luabridge_now:n
  { e }
\ExplSyntaxOff
%</generic-package>
%    \end{macrocode}
%
% \section{\LaTeX{} implementation}
%
% This section contains the implementation for \LaTeX.
%
%    \begin{macrocode}
%<*latex-package>
\RequirePackage{expl3}
\ProvidesExplPackage
  {lt3luabridge}%
  {2022-08-08}%
  {2.0.0}%
  {An expl3 package that allows you to execute Lua code in LuaTeX or any other
   TeX engine that exposes the shell}
\input lt3luabridge\relax
%</latex-package>
%    \end{macrocode}
%
% \section{\Hologo{ConTeXt} implementation}
%
% This section contains the implementation for \Hologo{ConTeXt}.
% \Hologo{ConTeXt} MkII, MkIV, and later formats are supported.
%
%    \begin{macrocode}
%<*context-package>
\writestatus{loading}{ConTeXt User Module / lt3luabridge}
\startmodule[lt3luabridge]
\unprotect
\input lt3luabridge\relax
%</context-package>
%    \end{macrocode}
%
% \end{implementation}
%
% \printbibliography
% \PrintIndex