From 47303402e387e0af44d49b27052d16a83a31de07 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Thu, 12 Dec 2024 17:47:04 +0300 Subject: [PATCH] feat(outputters): Add a method to handle empty PDFs vs. cleanup after error --- core/utilities/init.lua | 2 +- outputters/base.lua | 4 ++++ outputters/debug.lua | 5 +++++ outputters/libtexpdf.lua | 12 +++++++++++- outputters/text.lua | 11 +++++++++-- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/core/utilities/init.lua b/core/utilities/init.lua index e56b26265..9c86b3322 100644 --- a/core/utilities/init.lua +++ b/core/utilities/init.lua @@ -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 diff --git a/outputters/base.lua b/outputters/base.lua index 3ea3335e1..c675958b1 100644 --- a/outputters/base.lua +++ b/outputters/base.lua @@ -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 diff --git a/outputters/debug.lua b/outputters/debug.lua index c731d4172..af9278ce1 100644 --- a/outputters/debug.lua +++ b/outputters/debug.lua @@ -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") diff --git a/outputters/libtexpdf.lua b/outputters/libtexpdf.lua index c7f7d42bf..ed549de44 100644 --- a/outputters/libtexpdf.lua +++ b/outputters/libtexpdf.lua @@ -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 (_) diff --git a/outputters/text.lua b/outputters/text.lua index 786d2fd36..445a186e3 100644 --- a/outputters/text.lua +++ b/outputters/text.lua @@ -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 @@ -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 (_) @@ -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