%%
%% This is file `lt3luabridge.tex',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% lt3luabridge.dtx  (with options: `generic-package')
%% 
%% Copyright (C) 2016-2022 Vít 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.
%% 
\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
        \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_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:Nx
          \l_tmpa_tl
          { \tl_to_str:n { #1 } }
        \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
          {
            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_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
          }
        \lua_now:V
          \l_tmpa_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
\endinput
%%
%% End of file `lt3luabridge.tex'.
