Skip to content

Commit

Permalink
chore(cli): Handle new -r, -E, -p, -P, -c, and -O flags
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Jul 2, 2022
1 parent f002300 commit 607ff97
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 20 deletions.
33 changes: 27 additions & 6 deletions core/cli.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local cli = pl.class()
local lpeg = require("lpeg")

cli.parseArguments = function ()
local cliargs = require("cliargs")
Expand Down Expand Up @@ -53,15 +54,19 @@ cli.parseArguments = function ()
if opts.backend then
SILE.backend = opts.backend
end
if opts.class then
SILE.input.class = opts.class
end
for _, flags in ipairs(opts.debug) do
for _, flag in ipairs(pl.stringx.split(flags, ",")) do
SILE.debugFlags[flag] = true
end
end
for _, statement in ipairs(opts.evaluate) do
local func, err = load(statement)
if err then SU.error(err) end
SILE.dolua[#SILE.dolua+1] = func
for _, statement in ipairs(opts["evaluate"]) do
table.insert(SILE.input.evaluate, statement)
end
for _, statement in ipairs(opts["evaluate-after"]) do
table.insert(SILE.input.evaluateAfter, statement)
end
if opts.fontmanager then
SILE.forceFontManager = opts.fontmanager
Expand All @@ -76,8 +81,24 @@ cli.parseArguments = function ()
end
SILE.outputFilename = opts.output
end
for _, include in ipairs(opts.include) do
SILE.preamble[#SILE.preamble+1] = include
for _, path in ipairs(opts.options) do
local params = table.concat(opts.options, ",")
local parameters = SILE.parserBits.parameters
local options = parameters:match(params)
SILE.input.options = options
end
for _, path in ipairs(opts.require) do
table.insert(SILE.input.require, path)
end
for _, path in ipairs(opts.preamble) do
table.insert(SILE.input.preamble, path)
end
for _, path in ipairs(opts.postamble) do
table.insert(SILE.input.postamble, path)
end
for _, path in ipairs(opts.include) do
SU.deprecated("-I/--include", "-r/--require or -p/--preamble", "0.14.0", "0.15.0")
table.insert(SILE.input.include, path)
end
-- http://lua-users.org/wiki/VarargTheSecondClassCitizen
local identity = function (...) return table.unpack({...}, 1, select('#', ...)) end
Expand Down
35 changes: 26 additions & 9 deletions core/sile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,18 @@ SILE.nodeMakers = {}
SILE.tokenizers = {}
SILE.status = {}
SILE.scratch = {}
SILE.dolua = {}
SILE.preamble = {}
SILE.documentState = {}

SILE.input = {
evaluate = {},
evaluateAfter = {},
include = {},
require = {},
options = {},
preamble = {},
postamble = {},
}

-- Internal functions / classes / factories
SILE.fluent = require("fluent")()
SILE.traceStack = require("core.tracestack")()
Expand Down Expand Up @@ -107,6 +115,19 @@ SILE.baseClass = setmetatable({}, {

local defaultinputters = { "xml", "lua", "sil" }

local function runEvals (evals, arg)
for _, snippet in ipairs(evals) do
local pId = SILE.traceStack:pushText(snippet)
local status, func = pcall(load, snippet)
if status then
func()
else
SU.error(("Error parsing code provided in --%s snippet: %s"):format(arg, func))
end
SILE.traceStack:pop(pId)
end
end

SILE.init = function ()
-- Set by def
if not SILE.backend then
Expand All @@ -132,10 +153,7 @@ SILE.init = function ()
SILE.shaper = SILE.shapers.harfbuzz()
SILE.outputter = SILE.outputters.dummy()
end
for _, func in ipairs(SILE.dolua) do
local _, err = pcall(func)
if err then error(err) end
end
runEvals(SILE.input.evaluate, "evaluate")
end

SILE.require = function (dependency, pathprefix, deprecation_ack)
Expand Down Expand Up @@ -297,9 +315,8 @@ function SILE.finish ()
if SILE.makeDeps then
SILE.makeDeps:write()
end
if SILE.preamble then
SILE.documentState.documentClass:finish()
end
SILE.documentState.documentClass:finish()
runEvals(SILE.input.evaluateAfter, "evaluate-after")
io.stderr:write("\n")
end

Expand Down
15 changes: 14 additions & 1 deletion inputters/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ local base = pl.class()
function base._init (_) end

function base.classInit (_, tree)
local class = tree.options.class or "plain"
-- pl.tablex.merge(tree.options, SILE.input.options)
local class = SILE.input.class or tree.options.class or "plain"
local constructor = SILE.require(class, "classes", true)
if constructor.id then
SU.deprecated("std.object", "pl.class", "0.13.0", "0.14.0", string.format(_deprecated, constructor.id))
Expand All @@ -27,4 +28,16 @@ function base.findInTree (_, tree, command)
end
end

function base:preamble ()
for _, path in ipairs(SILE.input.preamble) do
SILE.readFile(path)
end
end

function base:postamble ()
for _, path in ipairs(SILE.input.postamble) do
SILE.readFile(path)
end
end

return base
2 changes: 2 additions & 0 deletions inputters/xml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ function xml:process (doc)
if SILE.Commands[content.command] then
SILE.call(content.command, content.options, content)
else
self:preamble()
SILE.process(content)
self:postamble()
end
if root and not SILE.preamble then
SILE.documentState.documentClass:finish()
Expand Down
8 changes: 4 additions & 4 deletions sile.in
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ if SILE.masterFilename then
ProFi:start()
end

for _, preamble in pairs(SILE.preamble) do
io.stderr:write("Loading "..preamble.."\n")
local c = SILE.resolveFile(preamble, "classes")
for _, path in ipairs(SILE.input.include) do
io.stderr:write("Loading "..path.."\n")
local c = SILE.resolveFile(path, "classes")
if c then
SILE.readFile(c)
else
SILE.require(preamble, "classes")
SILE.require(path, "classes")
end
end

Expand Down

0 comments on commit 607ff97

Please sign in to comment.