Skip to content

Commit

Permalink
feat(inputters): Allow CLI to mandate inputter used for master document
Browse files Browse the repository at this point in the history
Will enable lots of things including passing options to the inputter,
processing files where the content detection might trip up, potentially
ambiguous formats, multiple inputters for the same format, etc.
  • Loading branch information
alerque committed Jul 26, 2022
1 parent e64ce0f commit 1b9009f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
5 changes: 3 additions & 2 deletions core/cli.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ cli.parseArguments = function ()
if opts.INPUT == "STDIO" then
opts.INPUT = "-"
end
SILE.input.filename = opts.INPUT
-- Turn slashes around in the event we get passed a path from a Windows shell
SILE.inputFile = opts.INPUT:gsub("\\", "/")
local filename = opts.INPUT:gsub("\\", "/")
-- Strip extension
SILE.masterFilename = string.match(SILE.inputFile, "(.+)%..-$") or SILE.inputFile
SILE.masterFilename = string.match(filename, "(.+)%..-$") or filename
SILE.masterDir = SILE.masterFilename:match("(.-)[^%/]+$")
end
if opts.backend then
Expand Down
25 changes: 19 additions & 6 deletions core/sile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ SILE.rawHandlers = {}
-- needed for a user to use a SILE-as-a-library verion to produce documents
-- programatically.
SILE.input = {
filename = "",
evaluates = {},
evaluateAfters = {},
includes = {},
Expand Down Expand Up @@ -154,8 +155,7 @@ SILE.use = function (module, args)
SILE.sratch.class_from_uses = pack
elseif pack.type == "inputter" then
SILE.inputters[name] = pack
-- nothing more to instantiate for inputters because format detection uses
-- all available modules, order is designated by the inputter module itself.
SILE.inputter = pack(args)
elseif pack.type == "outputter" then
SILE.outputters[name] = pack
SILE.outputter = pack(args)
Expand Down Expand Up @@ -268,11 +268,24 @@ function SILE.processString (doc, format, filename, args)
local caller = debug.getinfo(2, "Sl")
SILE.currentlyProcessingFile = caller.short_src..":"..caller.currentline
end
format = format or detectFormat(doc, filename)
io.stderr:write(("<%s> as %s\n"):format(SILE.currentlyProcessingFile, format))
SILE.inputter = SILE.inputters[format]()
-- In the event we're processing the master file *and* the user gave us
-- a specific inputter to use, use it at the exclusion of all content type
-- detection
local inputter
if filename and filename:gsub("STDIN", "-") == SILE.input.filename and SILE.inputter then
inputter = SILE.inputter
else
format = format or detectFormat(doc, filename)
io.stderr:write(("<%s> as %s\n"):format(SILE.currentlyProcessingFile, format))
inputter = SILE.inputters[format]()
-- If we did content detection *and* this is the master file, save the
-- inputter for posterity and postambles
if filename and filename:gsub("STDIN", "-") == SILE.input.filename then
SILE.inputter = inputter
end
end
local pId = SILE.traceStack:pushDocument(SILE.currentlyProcessingFile, doc)
SILE.inputter:process(doc)
inputter:process(doc)
SILE.traceStack:pop(pId)
if cpf then SILE.currentlyProcessingFile = cpf end
end
Expand Down
4 changes: 2 additions & 2 deletions sile.in
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ if SILE.masterFilename then
end

local main, err = xpcall(function()
return SILE.processFile(SILE.inputFile)
end, SILE.errorHandler)
return SILE.processFile(SILE.input.filename)
end, SILE.errorHandler)

if not main then
if type(err) == "string" and err:match(": interrupted!") then
Expand Down

0 comments on commit 1b9009f

Please sign in to comment.