summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2021-07-27 20:36:31 +0000
committerKarl Berry <karl@freefriends.org>2021-07-27 20:36:31 +0000
commitc5b5e3fd195d647f3107171ca73a29b83bf4d7b3 (patch)
tree01eb2e0fef4c2aea7fcd43dab80ee549d89851fb /Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua
parentd94a4be7eef3b68410940ceebbd4bd2401aebbce (diff)
pyluatex (27jul21)
git-svn-id: svn://tug.org/texlive/trunk@60095 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua')
-rw-r--r--Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua85
1 files changed, 56 insertions, 29 deletions
diff --git a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua
index 4f904afe157..506b8bf9cf8 100644
--- a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua
+++ b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua
@@ -35,10 +35,11 @@ local folder = file.pathpart(file.collapsepath(status.filename, true))
local script = file.join(folder, "pyluatex-interpreter.py")
local tcp = nil
-local python_lines = {}
-local python_output = nil
+local env_end = nil
+local env_lines = nil
-local env_end = "\\end{python}"
+local last_code = nil
+local last_output = nil
local function err_cmd(message)
return "\\PackageError{PyLuaTeX}{" .. message .. "}{}"
@@ -80,74 +81,100 @@ 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)
+local function split_lines(str)
+ if str:sub(-1) ~= "\n" then
+ str = str .. "\n"
end
+
+ local t = {}
+ for s in str:gmatch("(.-)\r?\n") do
+ table.insert(t, s)
+ end
+ return t
end
-function pyluatex.execute(code, write)
- if pyluatex.verbose then log_input(code) end
+function pyluatex.execute(code, auto_print, write)
+ local full_code
+ if auto_print then
+ full_code = "print(str(" .. code .. "), end='')"
+ else
+ full_code = code
+ end
+
+ if pyluatex.verbose then log_input(full_code) end
+
+ local success, output = request({ session = pyluatex.session, code = full_code })
+ last_code = split_lines(code)
+ last_output = split_lines(output)
- local success, output = request({ session = pyluatex.session, code = code })
if success then
if pyluatex.verbose then log_output(output) end
+
if write then
- print_lines(output)
- else
- return output
+ tex.print(last_output)
end
else
- if not pyluatex.verbose then log_input(code) end
+ if not pyluatex.verbose then log_input(full_code) end
log_output(output)
if write then
tex.sprint(err_cmd("Python error (see above)"))
end
end
- return nil
+
+ return success
end
function pyluatex.print_env()
- if python_output ~= nil then
- print_lines(python_output)
- python_output = nil
+ if last_output ~= nil then
+ tex.print(last_output)
end
end
local function record_line(line)
- local s, e = line:find(env_end)
+ local s, e = line:find(env_end, 1, true)
if s ~= nil then
luatexbase.remove_from_callback("process_input_buffer", "pyluatex_record_line")
- table.insert(python_lines, line:sub(1, s - 1))
- local code = table.concat(python_lines, "\n")
- local output = pyluatex.execute(code, false)
- if output ~= nil then
- python_output = output
+ table.insert(env_lines, line:sub(1, s - 1))
+ local code = table.concat(env_lines, "\n")
+ local success = pyluatex.execute(code, false, false)
+ if success then
return line:sub(s)
else
return env_end .. err_cmd("Python error (see above)") .. line:sub(e + 1)
end
else
- table.insert(python_lines, line)
+ table.insert(env_lines, line)
return ""
end
end
-function pyluatex.record_env()
- python_lines = {}
- python_output = nil
+function pyluatex.record_env(quiet)
+ if quiet then
+ env_end = "\\end{pythonq}"
+ else
+ env_end = "\\end{python}"
+ end
+ env_lines = {}
luatexbase.add_to_callback("process_input_buffer", record_line, "pyluatex_record_line")
end
-function pyluatex.run_file(path)
+function pyluatex.run_file(path, write)
local f = io.open(path, "r")
if f then
local code = f:read("*a")
f:close()
- pyluatex.execute(code, true)
+ pyluatex.execute(code, false, write)
else
tex.sprint(err_cmd("File not found: " .. path))
end
end
+function pyluatex.get_last_code()
+ return last_code
+end
+
+function pyluatex.get_last_output()
+ return last_output
+end
+
return pyluatex