summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/pyluatex
diff options
context:
space:
mode:
Diffstat (limited to 'macros/luatex/latex/pyluatex')
-rw-r--r--macros/luatex/latex/pyluatex/pyluatex-interpreter.py37
-rw-r--r--macros/luatex/latex/pyluatex/pyluatex.lua17
-rw-r--r--macros/luatex/latex/pyluatex/pyluatex.pdfbin82073 -> 83049 bytes
-rw-r--r--macros/luatex/latex/pyluatex/pyluatex.sty2
-rw-r--r--macros/luatex/latex/pyluatex/pyluatex.tex19
5 files changed, 60 insertions, 15 deletions
diff --git a/macros/luatex/latex/pyluatex/pyluatex-interpreter.py b/macros/luatex/latex/pyluatex/pyluatex-interpreter.py
index 28ec28e12e..341c2ace04 100644
--- a/macros/luatex/latex/pyluatex/pyluatex-interpreter.py
+++ b/macros/luatex/latex/pyluatex/pyluatex-interpreter.py
@@ -34,7 +34,30 @@ import re
import os
import sys
+class PyLTTex:
+ def __init__(self):
+ self._log_buffer = []
+
+ def log(self, *objects, sep=' ', end='\n'):
+ try:
+ objects = [str(v) for v in objects]
+ except Exception as exc:
+ raise RuntimeError(
+ 'The object to log could not be transformed to a string.',
+ ) from exc
+ self._log_buffer.append(sep.join(objects))
+ self._log_buffer.append(end)
+
+ def _log_message(self):
+ msg = ''.join(self._log_buffer)
+ self._log_buffer = []
+ return msg
+
class Interpreter(InteractiveInterpreter):
+ def __init__(self):
+ self.tex = PyLTTex()
+ super().__init__({'tex': self.tex})
+
def execute_repl(self, code, ignore_errors):
self.success = True
output = ''
@@ -60,7 +83,7 @@ class Interpreter(InteractiveInterpreter):
output += out.getvalue()
if not ignore_errors and not self.success:
return False, output
- return self.success, output
+ return self.success, output, self.tex._log_message()
def execute(self, code):
with StringIO() as out, redirect_stdout(out), redirect_stderr(out):
@@ -75,7 +98,7 @@ class Interpreter(InteractiveInterpreter):
except:
traceback.print_exc()
self.success = False
- return self.success, out.getvalue()
+ return self.success, out.getvalue(), self.tex._log_message()
def showtraceback(self):
super().showtraceback()
@@ -95,13 +118,17 @@ class Handler(socketserver.StreamRequestHandler):
interpreter = interpreters[data['session']]
code = textwrap.dedent(data['code'])
if data['repl_mode']:
- success, output = interpreter.execute_repl(
+ success, output, log_msg = interpreter.execute_repl(
code,
data['ignore_errors']
)
else:
- success, output = interpreter.execute(code)
- response = { 'success': success, 'output': output }
+ success, output, log_msg = interpreter.execute(code)
+ response = {
+ 'success': success,
+ 'output': output,
+ 'log_msg': log_msg
+ }
self.wfile.write((json.dumps(response) + '\n').encode('utf-8'))
if __name__ == '__main__':
diff --git a/macros/luatex/latex/pyluatex/pyluatex.lua b/macros/luatex/latex/pyluatex/pyluatex.lua
index 9f7d5f7936..c12faa5cc8 100644
--- a/macros/luatex/latex/pyluatex/pyluatex.lua
+++ b/macros/luatex/latex/pyluatex/pyluatex.lua
@@ -118,8 +118,7 @@ end
local function request(data)
tcp:send(utilities.json.tostring(data) .. "\n")
local output = tcp:receive("*l")
- local response = utilities.json.tolua(output)
- return response.success, response.output
+ return utilities.json.tolua(output)
end
local function log_input(code)
@@ -152,7 +151,7 @@ function pyluatex.execute(code, auto_print, write, repl_mode, store)
if pyluatex.verbose then log_input(full_code) end
- local success, output = request(
+ local resp = request(
{
session = pyluatex.session,
code = full_code,
@@ -160,27 +159,29 @@ function pyluatex.execute(code, auto_print, write, repl_mode, store)
ignore_errors = pyluatex.ignore_errors
}
)
- local output_lines = split_lines(output)
+ local output_lines = split_lines(resp.output)
if store then
last_code = split_lines(code)
last_output = output_lines
end
- if success or pyluatex.ignore_errors then
- if pyluatex.verbose or not success then log_output(output) end
+ if resp.success or pyluatex.ignore_errors then
+ if pyluatex.verbose or not resp.success then log_output(resp.output) end
if write then
tex.print(output_lines)
end
else
if not pyluatex.verbose then log_input(full_code) end
- log_output(output)
+ log_output(resp.output)
if write then
tex.sprint(err_cmd("Python error (see above)"))
end
end
- return success
+ if resp.log_msg ~= "" then texio.write(resp.log_msg) end
+
+ return resp.success
end
function pyluatex.print_env()
diff --git a/macros/luatex/latex/pyluatex/pyluatex.pdf b/macros/luatex/latex/pyluatex/pyluatex.pdf
index 69fe67558e..bc41fa99e4 100644
--- a/macros/luatex/latex/pyluatex/pyluatex.pdf
+++ b/macros/luatex/latex/pyluatex/pyluatex.pdf
Binary files differ
diff --git a/macros/luatex/latex/pyluatex/pyluatex.sty b/macros/luatex/latex/pyluatex/pyluatex.sty
index a5adf62973..db5654e6b8 100644
--- a/macros/luatex/latex/pyluatex/pyluatex.sty
+++ b/macros/luatex/latex/pyluatex/pyluatex.sty
@@ -9,7 +9,7 @@
%% version 2005/12/01 or later.
\NeedsTeXFormat{LaTeX2e}
-\ProvidesPackage{pyluatex}[2023/01/03 v0.5.2 Execute Python code on the fly]
+\ProvidesPackage{pyluatex}[2023/01/14 v0.6.0 Execute Python code on the fly]
\RequirePackage{expl3}
\ExplSyntaxOn
diff --git a/macros/luatex/latex/pyluatex/pyluatex.tex b/macros/luatex/latex/pyluatex/pyluatex.tex
index 12c539d660..4dc0c09e35 100644
--- a/macros/luatex/latex/pyluatex/pyluatex.tex
+++ b/macros/luatex/latex/pyluatex/pyluatex.tex
@@ -15,7 +15,7 @@
\usepackage{url}
\title{The \emph{pyluatex} package}
\author{Tobias Enderle\\\url{https://github.com/tndrle/PyLuaTeX}}
-\date{v0.5.2 (2023/01/03)}
+\date{v0.6.0 (2023/01/14)}
\begin{document}
\maketitle
\raggedright
@@ -196,6 +196,23 @@ in your environment definition, e.g.
{\end{python}}
\end{tcblisting}
+\subsection{Logging from Python}
+\begin{tcblisting}{breakable,listing only,
+ size=fbox,colframe=black!8,boxrule=3pt,colback=black!8}
+tex.log(*objects, sep=' ', end='\n')
+\end{tcblisting}
+Writes \inlcode|objects| to the TeX log, separated by \inlcode|sep| and followed by \inlcode|end|.
+All elements in \inlcode|objects| are converted to strings using \inlcode|str()|.
+Both \inlcode|sep| and \inlcode|end| must be strings.
+
+\textit{Example:}
+\begin{tcblisting}{breakable,listing only,
+ size=fbox,colframe=black!8,boxrule=3pt,colback=black!8}
+\begin{python}
+tex.log('This text goes to the TeX log.')
+\end{python}
+\end{tcblisting}
+
\section{Requirements}
\begin{itemize}
\item Lua\LaTeX{}