Skip to content

Commit

Permalink
feat(outputters): Add a method to handle empty PDFs vs. cleanup after…
Browse files Browse the repository at this point in the history
… error
  • Loading branch information
alerque committed Dec 12, 2024
1 parent 9df32f1 commit 4730340
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion core/utilities/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ function utilities.error (message, isbug)
utilities.warn(message, isbug)
_skip_traceback_levels = 2
io.stderr:flush()
SILE.outputter:finish() -- Only really useful from the REPL but no harm in trying
SILE.outputter:abort()
SILE.scratch.caughterror = true
error("", 2)
end
Expand Down
4 changes: 4 additions & 0 deletions outputters/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ end

function outputter.newPage () end

function outputter:abort ()
self:finish() -- unless otherwise defined
end

function outputter:finish ()
self:runHooks("prefinish")
end
Expand Down
5 changes: 5 additions & 0 deletions outputters/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ function outputter:newPage ()
self:_writeline("New page")
end

function outputter:abort ()
self:_writeline("Aborted")
outfile:close()
end

function outputter:finish ()
if SILE.status.unsupported then
self:_writeline("UNSUPPORTED")
Expand Down
12 changes: 11 additions & 1 deletion outputters/libtexpdf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,23 @@ end
-- pdf structure package needs a tie in here
function outputter._endHook (_) end

function outputter:abort ()
if started then
pdf.endpage()
pdf.finish()
started = false
lastkey = false
end
end

function outputter:finish ()
-- allows generation of empty PDFs
self:_ensureInit()
pdf.endpage()
self:runHooks("prefinish")
pdf.finish()
started = false
lastkey = nil
lastkey = false
end

function outputter.getCursor (_)
Expand Down
11 changes: 9 additions & 2 deletions outputters/text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ outputter.extension = "txt"
-- function outputter:_init () end

function outputter:_ensureInit ()
if not outfile then
if not started then
started = true
local fname = self:getOutputFilename()
outfile = fname == "-" and io.stdout or io.open(fname, "w+")
end
Expand All @@ -34,10 +35,17 @@ function outputter:newPage ()
outfile:write(" ")
end

function outputter:abort ()
if started then
outfile:close()
started = false
end
end
function outputter:finish ()
self:_ensureInit()
self:runHooks("prefinish")
outfile:close()
started = false
end

function outputter.getCursor (_)
Expand Down Expand Up @@ -79,7 +87,6 @@ function outputter:drawHbox (value, width)
end
self:_writeline(value.text)
if width > 0 then
started = true
cursorX = cursorX + width
end
end
Expand Down

0 comments on commit 4730340

Please sign in to comment.