summaryrefslogtreecommitdiff
path: root/macros/generic/lt3luabridge/lt3luabridge.tex
diff options
context:
space:
mode:
Diffstat (limited to 'macros/generic/lt3luabridge/lt3luabridge.tex')
-rw-r--r--macros/generic/lt3luabridge/lt3luabridge.tex407
1 files changed, 407 insertions, 0 deletions
diff --git a/macros/generic/lt3luabridge/lt3luabridge.tex b/macros/generic/lt3luabridge/lt3luabridge.tex
new file mode 100644
index 0000000000..0c2b8f8ddd
--- /dev/null
+++ b/macros/generic/lt3luabridge/lt3luabridge.tex
@@ -0,0 +1,407 @@
+\ifx\ExplSyntaxOn\undefined
+ \input expl3-generic\relax
+\fi
+\ExplSyntaxOn
+\int_const:Nn \c__luabridge_method_write_eighteen_int { 0 }
+\int_const:Nn \c__luabridge_method_os_execute_int { 1 }
+\int_const:Nn \c__luabridge_method_directlua_int { 2 }
+\int_if_exist:NF
+ \g__luabridge_method_int
+ {
+ \int_new:N
+ \g__luabridge_method_int
+ \cs_if_exist:NTF
+ \directlua
+ {
+ \int_gset_eq:NN
+ \g__luabridge_method_int
+ \c__luabridge_method_directlua_int
+ }
+ {
+ \int_gset_eq:NN
+ \g__luabridge_method_int
+ \c__luabridge_method_write_eighteen_int
+ }
+ }
+\msg_new:nnn
+ { luabridge }
+ { unknown-method }
+ {
+ Unknown~bridging~method:~#1
+ }
+\msg_new:nnn
+ { luabridge }
+ { method-write-eighteen }
+ {
+ Using~shell~escape~via~write18~as~the~bridging~method
+ }
+\msg_new:nnn
+ { luabridge }
+ { method-os-execute }
+ {
+ Using~shell~escape~via~os.execute()~as~the~bridging~method
+ }
+\msg_new:nnn
+ { luabridge }
+ { method-directlua }
+ {
+ Using~direct~Lua~access~as~the~bridging~method
+ }
+\int_case:nnF
+ { \g__luabridge_method_int }
+ {
+ { \c__luabridge_method_write_eighteen_int }
+ {
+ \msg_info:nn
+ { luabridge }
+ { method-write-eighteen }
+ }
+ { \c__luabridge_method_os_execute_int }
+ {
+ \msg_info:nn
+ { luabridge }
+ { method-os-execute }
+ }
+ { \c__luabridge_method_directlua_int }
+ {
+ \msg_info:nn
+ { luabridge }
+ { method-directlua }
+ }
+ }
+ {
+ \msg_error:nnx
+ { luabridge }
+ { unknown-method }
+ { \g__luabridge_method_int }
+ }
+\bool_if:nTF
+ {
+ \int_compare_p:nNn
+ { \g__luabridge_method_int }
+ =
+ { \c__luabridge_method_write_eighteen_int } ||
+ \int_compare_p:nNn
+ { \g__luabridge_method_int }
+ =
+ { \c__luabridge_method_os_execute_int }
+ }
+ {
+ \int_const:Nn \c__luabridge_level_disabled_int { 0 }
+ \int_const:Nn \c__luabridge_level_enabled_int { 1 }
+ \int_const:Nn \c__luabridge_level_restricted_int { 2 }
+ \int_new:N
+ \g__luabridge_level_int
+ \cs_if_exist:NTF
+ \pdfshellescape
+ {
+ \int_gset:Nn
+ \g__luabridge_level_int
+ { \pdfshellescape }
+ }
+ {
+ \cs_if_exist:NTF
+ \shellescape
+ {
+ \int_gset:Nn
+ \g__luabridge_level_int
+ { \shellescape }
+ }
+ {
+ \int_case:nnF
+ { \g__luabridge_method_int }
+ {
+ { \c__luabridge_method_write_eighteen_int }
+ {
+ \int_gset_eq:NN
+ \g__luabridge_level_int
+ \c__luabridge_level_enabled_int
+ }
+ }
+ {
+ \int_gset:Nn
+ \g__luabridge_level_int
+ {
+ \directlua
+ {
+ tex.sprint(status.shell_escape or "1")
+ }
+ }
+ }
+ }
+ }
+ \msg_new:nnn
+ { luabridge }
+ { unknown-level }
+ {
+ Unknown~shell~escape~level:~#1
+ }
+ \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.
+ }
+ \msg_new:nnn
+ { luabridge }
+ { level-enabled }
+ {
+ Shell~escape~seems~to~be~enabled
+ }
+ \msg_new:nnnn
+ { luabridge }
+ { level-restricted }
+ {
+ Shell~escape~seems~to~be~restricted
+ }
+ {
+ 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.
+ }
+ \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_const:Nx \c__luabridge_default_standard_output_filename_str { \jobname.luabridge.out }
+ \int_case:nnF
+ { \g__luabridge_level_int }
+ {
+ { \c__luabridge_level_disabled_int }
+ {
+ \msg_warning:nn
+ { luabridge }
+ { level-disabled }
+ }
+ { \c__luabridge_level_enabled_int }
+ {
+ \msg_info:nn
+ { luabridge }
+ { level-enabled }
+ }
+ { \c__luabridge_level_restricted_int }
+ {
+ \msg_warning:nn
+ { luabridge }
+ { level-restricted }
+ }
+ }
+ {
+ \msg_error:nnx
+ { luabridge }
+ { unknown-level }
+ { \g__luabridge_level_int }
+ }
+ \cs_new:Nn
+ \_luabridge_assert_shell_escape:
+ {
+ \int_case:nnF
+ { \g__luabridge_level_int }
+ {
+ { \c__luabridge_level_disabled_int }
+ {
+ \msg_error:nn
+ { luabridge }
+ { level-disabled }
+ }
+ }
+ }
+ \int_case:nn
+ { \g__luabridge_method_int }
+ {
+ { \c__luabridge_method_write_eighteen_int }
+ {
+ \cs_new:Nn
+ \_luabridge_execute_shell:n
+ {
+ \_luabridge_assert_shell_escape:
+ \immediate
+ \write 18
+ { #1 }
+ }
+ }
+ { \c__luabridge_method_os_execute_int }
+ {
+ \cs_new:Nn
+ \_luabridge_execute_shell:n
+ {
+ \_luabridge_assert_shell_escape:
+ \directlua
+ {
+ os.execute(
+ " \luaescapestring { #1 } "
+ )
+ }
+ }
+ }
+ }
+ \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
+ }
+ \str_if_exist:NF
+ \g__luabridge_standard_output_filename_str
+ {
+ \str_gset_eq:NN
+ \g__luabridge_standard_output_filename_str
+ \c__luabridge_default_standard_output_filename_str
+ }
+ \cs:w newwrite \cs_end:
+ \l__luabridge_output_stream
+ \cs_new:Nn
+ \_luabridge_execute:n
+ {
+ \immediate \openout
+ \l__luabridge_output_stream
+ \g__luabridge_helper_script_filename_str
+ \msg_info:nnV
+ { luabridge }
+ { writing-helper-script }
+ \g__luabridge_helper_script_filename_str
+ \tl_set:Nx
+ \l_tmpa_tl
+ {
+ local~ran_ok,~error~=~pcall(function()~
+ local~ran_ok,~kpse~=~pcall(require,~"kpse")~
+ if~ran_ok~then~kpse.set_program_name("luatex")~end~
+ #1~
+ 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_output_dirname_str }
+ \iow_char:N \\ \iow_char:N \\ endgroup
+ ')~
+ end
+ }
+ \immediate \write
+ \l__luabridge_output_stream
+ { \exp_not:V \l_tmpa_tl }
+ \immediate \closeout
+ \l__luabridge_output_stream
+ \msg_info:nnVV
+ { luabridge }
+ { executing-helper-script }
+ \g__luabridge_helper_script_filename_str
+ \g__luabridge_standard_output_filename_str
+ \tl_set:Nx
+ \l_tmpa_tl
+ {
+ texlua~"
+ \g__luabridge_output_dirname_str /
+ \g__luabridge_helper_script_filename_str
+ "~>~"
+ \g__luabridge_output_dirname_str /
+ \g__luabridge_standard_output_filename_str
+ "
+ }
+ \_luabridge_execute_shell:V
+ \l_tmpa_tl
+ \file_if_exist_input:VF
+ \g__luabridge_standard_output_filename_str
+ {
+ \msg_error:nn
+ { luabridge }
+ { level-disabled }
+ }
+ }
+ \cs_generate_variant:Nn
+ \msg_info:nnn
+ { nnV }
+ \cs_generate_variant:Nn
+ \msg_info:nnnn
+ { nnVV }
+ \cs_generate_variant:Nn
+ \msg_error:nnnn
+ { nnvv }
+ \cs_generate_variant:Nn
+ \_luabridge_execute_shell:n
+ { V }
+ \prg_generate_conditional_variant:Nnn
+ \file_if_exist_input:n
+ { V }
+ { F }
+ \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~
+ and~storing~the~result~in~file~#2
+ }
+ \msg_new:nnnn
+ { luabridge }
+ { failed-to-execute }
+ {
+ An~error~was~encountered~while~executing~Lua~code
+ }
+ {
+ For further clues, examine file #1/#2
+ }
+ }
+ {
+ \cs_new:Nn
+ \_luabridge_execute:n
+ {
+ \directlua
+ {
+ 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~
+ #1
+ }
+ }
+ }
+\cs_new_protected:Npn
+ \luabridgeExecute
+ { \_luabridge_execute:n }
+\ExplSyntaxOff