| 1 |
%% Copyright 2021-2026 Tobias Enderle |
| 2 |
%% |
| 3 |
%% This work may be distributed and/or modified under the |
| 4 |
%% conditions of the LaTeX Project Public License, either version 1.3c |
| 5 |
%% of this license or (at your option) any later version. |
| 6 |
%% The latest version of this license is in |
| 7 |
%% http://www.latex-project.org/lppl.txt |
| 8 |
%% and version 1.3c or later is part of all distributions of LaTeX |
| 9 |
%% version 2005/12/01 or later. |
| 10 |
|
| 11 |
\NeedsTeXFormat{LaTeX2e} |
| 12 |
\ProvidesExplPackage{pyluatex}{2026/04/17}{v0.7.0}{Execute Python code on the fly} |
| 13 |
|
| 14 |
\cs_new:Npn \__pyluatex_err:n #1 { \PackageError { PyLuaTeX } {#1} {} } |
| 15 |
\sys_if_engine_luatex:F { \__pyluatex_err:n { LuaTeX~is~required } } |
| 16 |
\sys_if_shell_unrestricted:F { |
| 17 |
\__pyluatex_err:n { Shell~escape~is~required~(add~--shell-escape~option) } |
| 18 |
} |
| 19 |
|
| 20 |
\directlua{ |
| 21 |
pyluatex = require("pyluatex") |
| 22 |
pyluatex.start() |
| 23 |
} |
| 24 |
|
| 25 |
\cs_new:Npn \__pyluatex_env:nn #1 #2 { |
| 26 |
\NewDocumentEnvironment {#1} { !O{} } { \directlua{ |
| 27 |
pyluatex.set_options([==[##1]==], {#2}) |
| 28 |
pyluatex.record_env("#1") |
| 29 |
} } { \directlua{pyluatex.execute_env()} } |
| 30 |
} |
| 31 |
\__pyluatex_env:nn { python } {} |
| 32 |
\__pyluatex_env:nn { pythonq } { quiet = true } |
| 33 |
\__pyluatex_env:nn { pythonrepl } { quiet = true, repl = true } |
| 34 |
|
| 35 |
\newcommand* { \PyLTVerbatimEnv } { |
| 36 |
\directlua{pyluatex.set_parent_env([==[\@currenvir]==])} |
| 37 |
} |
| 38 |
|
| 39 |
\cs_new:Npn \__pyluatex_cmd_inner:nn [#1] #2 { \directlua{ |
| 40 |
pyluatex.handle_cmd([==[#1]==], [==[#2]==]) |
| 41 |
} } |
| 42 |
\cs_new:Npn \__pyluatex_cmd:Nnn #1 #2 #3 { |
| 43 |
\newcommand {#1} { |
| 44 |
\directlua{pyluatex.prepare_cmd_handler({#2}, pyluatex.#3)} |
| 45 |
\exp_after:wN \__pyluatex_cmd_inner:nn \directlua{pyluatex.opt_arg()} |
| 46 |
} |
| 47 |
} |
| 48 |
\__pyluatex_cmd:Nnn \py {} { execute_with_options, true, -1 } |
| 49 |
\__pyluatex_cmd:Nnn \pyq { quiet = true } { execute_with_options, true, -1 } |
| 50 |
\__pyluatex_cmd:Nnn \pyc {} { execute_with_options, false, -1 } |
| 51 |
\__pyluatex_cmd:Nnn \pycq { quiet = true } { execute_with_options, false, -1 } |
| 52 |
\__pyluatex_cmd:Nnn \pyif { |
| 53 |
quiet = false, repl = false, store = false, ignoreerrors = false |
| 54 |
} { py_if } |
| 55 |
\__pyluatex_cmd:Nnn \pyfile {} { run_file } |
| 56 |
\__pyluatex_cmd:Nnn \pyfileq { quiet = true } { run_file } |
| 57 |
\__pyluatex_cmd:Nnn \pyfilerepl { quiet = true, repl = true } { run_file } |
| 58 |
|
| 59 |
\newcommand { \pyoptions } [1] { |
| 60 |
\directlua{pyluatex.set_options([==[#1]==], {}, true)} |
| 61 |
} |
| 62 |
\newcommand { \pyoption } [2] { \pyoptions { #1 = #2 } } |
| 63 |
\newcommand { \pysession } [1] { \pyoptions { session = #1 } } |
| 64 |
|
| 65 |
\let\@unprocessedoptions\relax % https://tex.stackexchange.com/a/615863 |
| 66 |
\endinput |