Skip to content

Commit 5a7694d

Browse files
committed
feat(outputters): Add a method to handle empty PDFs vs. cleanup after error
1 parent 9df32f1 commit 5a7694d

5 files changed

Lines changed: 34 additions & 4 deletions

File tree

core/utilities/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ function utilities.error (message, isbug)
332332
utilities.warn(message, isbug)
333333
_skip_traceback_levels = 2
334334
io.stderr:flush()
335-
SILE.outputter:finish() -- Only really useful from the REPL but no harm in trying
335+
SILE.outputter:abort()
336336
SILE.scratch.caughterror = true
337337
error("", 2)
338338
end

outputters/base.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ end
2929

3030
function outputter.newPage () end
3131

32+
function outputter:abort ()
33+
return self:finish() -- unless otherwise defined
34+
end
35+
3236
function outputter:finish ()
3337
self:runHooks("prefinish")
3438
end

outputters/debug.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ function outputter:newPage ()
4646
self:_writeline("New page")
4747
end
4848

49+
function outputter:abort ()
50+
if started then
51+
self:_writeline("Aborted")
52+
outfile:close()
53+
started = false
54+
end
55+
end
56+
4957
function outputter:finish ()
5058
if SILE.status.unsupported then
5159
self:_writeline("UNSUPPORTED")
@@ -54,6 +62,7 @@ function outputter:finish ()
5462
self:runHooks("prefinish")
5563
self:_writeline("Finish")
5664
outfile:close()
65+
started = false
5766
end
5867

5968
function outputter.getCursor (_)

outputters/libtexpdf.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,23 @@ end
6767
-- pdf structure package needs a tie in here
6868
function outputter._endHook (_) end
6969

70+
function outputter.abort ()
71+
if started then
72+
pdf.endpage()
73+
pdf.finish()
74+
started = false
75+
lastkey = false
76+
end
77+
end
78+
7079
function outputter:finish ()
80+
-- allows generation of empty PDFs
7181
self:_ensureInit()
7282
pdf.endpage()
7383
self:runHooks("prefinish")
7484
pdf.finish()
7585
started = false
76-
lastkey = nil
86+
lastkey = false
7787
end
7888

7989
function outputter.getCursor (_)

outputters/text.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ outputter.extension = "txt"
1515
-- function outputter:_init () end
1616

1717
function outputter:_ensureInit ()
18-
if not outfile then
18+
if not started then
19+
started = true
1920
local fname = self:getOutputFilename()
2021
outfile = fname == "-" and io.stdout or io.open(fname, "w+")
2122
end
@@ -34,10 +35,17 @@ function outputter:newPage ()
3435
outfile:write(" ")
3536
end
3637

38+
function outputter.abort ()
39+
if started then
40+
outfile:close()
41+
started = false
42+
end
43+
end
3744
function outputter:finish ()
3845
self:_ensureInit()
3946
self:runHooks("prefinish")
4047
outfile:close()
48+
started = false
4149
end
4250

4351
function outputter.getCursor (_)
@@ -79,7 +87,6 @@ function outputter:drawHbox (value, width)
7987
end
8088
self:_writeline(value.text)
8189
if width > 0 then
82-
started = true
8390
cursorX = cursorX + width
8491
end
8592
end

0 commit comments

Comments
 (0)