%%
%% This is file `lt3luabridge.tex',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% lt3luabridge.dtx  (with options: `generic-package')
%% 
%% Copyright (C) 2016-2024 Vít Starý Novotný
%% 
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%% 
%%    http://www.latex-project.org/lppl.txt
%% 
%% This work has the LPPL maintenance status `maintained'.
%% The Current Maintainer of this work is Vít Novotný.
%% 
%% Send bug reports, requests for additions and questions
%% either to the GitHub issue tracker at
%% 
%%   https://github.com/Witiko/lt3luabridge/issues
%% 
%% or to the e-mail address <witiko@mail.muni.cz>.
%% 
%% MODIFICATION ADVICE:
%% 
%% If you want to customize this file, it is best to make a copy of
%% the source file(s) from which it was produced.  Use a different
%% name for your copy(ies) and modify the copy(ies); this will ensure
%% that your modifications do not get overwritten when you install a
%% new release of the standard system.  You should also ensure that
%% your modified source file does not generate any modified file with
%% the same name as a standard file.
%% 
%% You will also need to produce your own, suitably named, .ins file to
%% control the generation of files from your source file; this file
%% should contain your own preambles for the files it generates, not
%% those in the standard .ins files.
%% 
%% The names of the source files used are shown above.
%% 
\expandafter\ifx\csname ExplSyntaxOn\endcsname\relax
  \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 }
  {
    \sys_if_platform_unix:TF
      {
        \str_const:Nn
          \c_luabridge_default_output_dirname_str
          { $TEXMF_OUTPUT_DIRECTORY }
      }
      {
        \sys_if_platform_windows:TF
          {
            \str_set:Nn
              \l_tmpa_str
              { TEXMF_OUTPUT_DIRECTORY }
            \str_put_left:NV
              \l_tmpa_str
              \c_percent_str
            \str_put_right:NV
              \l_tmpa_str
              \c_percent_str
            \str_const:NV
              \c_luabridge_default_output_dirname_str
              \l_tmpa_str
          }
          {
            \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
        \str_gset_eq: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_tl_set:Nn
      {
        \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:Nx
          \l_tmpa_tl
          { \tl_to_str:n { #2 } }
        \regex_replace_all:nnN
          { [\\"] }
          { \\\0 }
          \l_tmpa_tl
        \tl_set:Nx
          \l_tmpa_tl
          {
            local~ran_ok, err = pcall(function()
              local~ran_ok, kpse = pcall(require,~"kpse")
              if~ran_ok~then~kpse.set_program_name("luatex") end~
              assert(load(" \exp_not:V \l_tmpa_tl "))()
            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(err .. " \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 \\ csname~
                  msg_error:nnvv\iow_char:N \\ \iow_char:N \\ endcsname
                    { 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
          {
            \str_if_eq:NNTF
              \g_luabridge_output_dirname_str
              \c_luabridge_default_output_dirname_str
              {
                \sys_if_platform_windows:TF
                  {
                    if~not~defined~TEXMF_OUTPUT_DIRECTORY~(
                      texlua~
                        \g_luabridge_helper_script_filename_str
                    )~else~(
                      texlua~
                        \g_luabridge_output_dirname_str /
                        \g_luabridge_helper_script_filename_str
                    )
                  }
                  {
                    \sys_if_platform_unix:T
                      {
                        TEXMF_OUTPUT_DIRECTORY =
                          ${TEXMF_OUTPUT_DIRECTORY:-.} \iow_newline:
                      }
                    texlua~
                      \g_luabridge_output_dirname_str /
                      \g_luabridge_helper_script_filename_str
                  }
              }
              {
                texlua~
                  \g_luabridge_output_dirname_str /
                  \g_luabridge_helper_script_filename_str
              }
          }
          { }
          #1
          {
          }
          {
            \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_tl_set:Nn
      {
        \tl_set:Nn
          \l_tmpa_tl
          { #2 }
        \tl_set:Nx
          \l_tmpa_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
          }
        \tl_set:No
          #1
          {
            \directlua
              {
                \tl_use:N
                  \l_tmpa_tl
              }
          }
      }
    \cs_generate_variant:Nn
      \lua_now:n
      { V }
  }
\cs_new:Nn
  \luabridge_now:n
  {
    \luabridge_tl_set:Nn
      \l_tmpb_tl
      { #1 }
    \tl_use:N
      \l_tmpb_tl
  }
\cs_new_protected:Npn
  \luabridgeExecute
  #1
  {
    \luabridge_now:e
      { #1 }
  }
\cs_generate_variant:Nn
  \luabridge_now:n
  { e }
\ExplSyntaxOff
\endinput
%%
%% End of file `lt3luabridge.tex'.
