Skip to content

Commit 4f3ace9

Browse files
committed
Fixed trailing newline problem #26 and #27
1 parent 2ff6b01 commit 4f3ace9

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

pyluatex.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ local function not_empty(str)
6767
return str ~= nil and str ~= ""
6868
end
6969

70+
local function split_lines(str)
71+
local lines = str:splitlines()
72+
if lines[#lines] == "" then
73+
table.remove(lines, #lines)
74+
end
75+
return lines
76+
end
77+
7078
function pyluatex.start(executable, local_imports)
7179
local script = file.join(folder, "pyluatex-interpreter.py")
7280

@@ -130,8 +138,8 @@ function pyluatex.execute(code, auto_print, write, repl_mode, store)
130138
repl_mode = repl_mode,
131139
ignore_errors = pyluatex.ignore_errors
132140
})
133-
local code_lines = code:splitlines()
134-
local output_lines = resp.output:splitlines()
141+
local code_lines = split_lines(code)
142+
local output_lines = split_lines(resp.output)
135143
if store then
136144
last_code = code_lines
137145
last_output = output_lines

test/test-cases/article_template.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
\documentclass{article}
1212
\pagenumbering{gobble}
1313
\usepackage[%s]{pyluatex}
14+
\usepackage{luacode}
1415

1516
\def\TESTID{%s}
1617
\typeout{TestID: \TESTID}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
tests:
2+
- code: |
3+
\begin{pythonrepl}
4+
def func(x):
5+
return 2 * x
6+
7+
func(7)
8+
\end{pythonrepl}
9+
\begin{luacode}
10+
local output = pyluatex.get_last_output()
11+
texio.write_nl("Last output line: |" .. output[#output] .. "|")
12+
\end{luacode}
13+
log: |
14+
Last output line: |14|
15+
- code: |
16+
\begin{python}
17+
a = 11
18+
print(a)
19+
\end{python}
20+
\begin{luacode}
21+
local code = pyluatex.get_last_code()
22+
texio.write_nl("Last code line: |" .. code[#code] .. "|")
23+
local output = pyluatex.get_last_output()
24+
texio.write_nl("Last output line: |" .. output[#output] .. "|")
25+
\end{luacode}
26+
log: |
27+
Last code line: |print(a)|
28+
Last output line: |11|
29+
pdf: 11

0 commit comments

Comments
 (0)