summaryrefslogtreecommitdiff
path: root/macros/luatex/latex/pyluatex/pyluatex.lua
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2021-07-16 03:01:07 +0000
committerNorbert Preining <norbert@preining.info>2021-07-16 03:01:07 +0000
commit01b14a20f8942ab0c52aeb8abffa85b811c7a95a (patch)
tree2db57d63324f0141577f40d9e8a8a035b131e176 /macros/luatex/latex/pyluatex/pyluatex.lua
parent55ae461252a599d6bc951065a37c16a8971fc560 (diff)
CTAN sync 202107160301
Diffstat (limited to 'macros/luatex/latex/pyluatex/pyluatex.lua')
-rw-r--r--macros/luatex/latex/pyluatex/pyluatex.lua32
1 files changed, 24 insertions, 8 deletions
diff --git a/macros/luatex/latex/pyluatex/pyluatex.lua b/macros/luatex/latex/pyluatex/pyluatex.lua
index 07d8ea49d3..4f904afe15 100644
--- a/macros/luatex/latex/pyluatex/pyluatex.lua
+++ b/macros/luatex/latex/pyluatex/pyluatex.lua
@@ -36,6 +36,7 @@ local script = file.join(folder, "pyluatex-interpreter.py")
local tcp = nil
local python_lines = {}
+local python_output = nil
local env_end = "\\end{python}"
@@ -71,28 +72,34 @@ local function request(data)
return response.success, response.output
end
-local function print_input(code)
+local function log_input(code)
texio.write_nl("PyLuaTeX input for session \"" .. pyluatex.session .. "\": " .. code)
end
-local function print_output(code)
+local function log_output(code)
texio.write_nl("PyLuaTeX output: " .. code)
end
+local function print_lines(str)
+ for s in str:gmatch("[^\r\n]+") do
+ tex.sprint(s)
+ end
+end
+
function pyluatex.execute(code, write)
- if pyluatex.verbose then print_input(code) end
+ if pyluatex.verbose then log_input(code) end
local success, output = request({ session = pyluatex.session, code = code })
if success then
- if pyluatex.verbose then print_output(output) end
+ if pyluatex.verbose then log_output(output) end
if write then
- tex.sprint(output)
+ print_lines(output)
else
return output
end
else
- if not pyluatex.verbose then print_input(code) end
- print_output(output)
+ if not pyluatex.verbose then log_input(code) end
+ log_output(output)
if write then
tex.sprint(err_cmd("Python error (see above)"))
end
@@ -100,6 +107,13 @@ function pyluatex.execute(code, write)
return nil
end
+function pyluatex.print_env()
+ if python_output ~= nil then
+ print_lines(python_output)
+ python_output = nil
+ end
+end
+
local function record_line(line)
local s, e = line:find(env_end)
if s ~= nil then
@@ -108,7 +122,8 @@ local function record_line(line)
local code = table.concat(python_lines, "\n")
local output = pyluatex.execute(code, false)
if output ~= nil then
- return output .. line:sub(s)
+ python_output = output
+ return line:sub(s)
else
return env_end .. err_cmd("Python error (see above)") .. line:sub(e + 1)
end
@@ -120,6 +135,7 @@ end
function pyluatex.record_env()
python_lines = {}
+ python_output = nil
luatexbase.add_to_callback("process_input_buffer", record_line, "pyluatex_record_line")
end