Skip to content

Commit

Permalink
feat(core): Add ability to pass args to modules via \use and other co…
Browse files Browse the repository at this point in the history
…mmands that load modules
  • Loading branch information
alerque committed Jul 26, 2022
1 parent 0d530a5 commit e64ce0f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 33 deletions.
56 changes: 36 additions & 20 deletions classes/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ function base.declareSettings (_)
end

function base:loadPackage (packname, args)
local pack = require("packages." .. packname)
local pack = require(("packages.%s"):format(packname))
if pack.type == "package" then -- new package
self.packages[pack._name] = pack(self, args)
else -- legacay package
else -- legacy package
self:initPackage(pack, args)
end
end
Expand Down Expand Up @@ -226,6 +226,15 @@ function base:registerRawHandlers ()

end

local function optionsAsArgs (options)
local args = pl.tablex.copy(options)
args.src = nil
args.format = nil
args.module = nil
args.require = nil
return args
end

function base:registerCommands ()

local function replaceProcessBy(replacement, tree)
Expand Down Expand Up @@ -298,68 +307,75 @@ function base:registerCommands ()
end, "Within a macro definition, processes the contents of the macro body.")

self:registerCommand("script", function (options, content)
local args = optionsAsArgs(options)
if SU.hasContent(content) then
SILE.processString(content[1], options.format or "lua")
return SILE.processString(content[1], options.format or "lua", nil, args)
elseif options.src then
SILE.require(options.src)
return SILE.require(options.src)
else
SU.error("\\script funcion requires inline content or a src file path")
SU.error("\\script function requires inline content or a src file path")
return SILE.processString(content[1], options.format or "lua", nil, args)
end
end, "Runs lua code. The code may be supplied either inline or using src=...")

self:registerCommand("include", function (options, content)
local args = optionsAsArgs(options)
if SU.hasContent(content) then
SILE.processString(content[1], options.format)
return SILE.processString(content[1], options.format, nil, args)
elseif options.src then
SILE.processFile(options.src, options.format)
return SILE.processFile(options.src, options.format, args)
else
SU.error("\\include funcion requires inline content or a src file path")
SU.error("\\include function requires inline content or a src file path")
end
end, "Includes a content file for processing.")

self:registerCommand("lua", function (options, content)
local args = optionsAsArgs(options)
if SU.hasContent(content) then
SILE.processString(content[1], "lua")
return SILE.processString(content[1], "lua", nil, args)
elseif options.src then
SILE.processFile(options.src, "lua")
return SILE.processFile(options.src, "lua", args)
elseif options.require then
local module = SU.required(options, "require", "lua")
return require(module)
else
SU.error("\\lua funcion requires inline content or a src file path or a require module name")
SU.error("\\lua function requires inline content or a src file path or a require module name")
end
end, "Run Lua code. The code may be supplied either inline, using require=... for a Lua module, or using src=... for a file path")

self:registerCommand("sil", function (options, content)
local args = optionsAsArgs(options)
if SU.hasContent(content) then
SILE.processString(content[1], "sil")
return SILE.processString(content[1], "sil")
elseif options.src then
SILE.processFile(options.src, "sil")
return SILE.processFile(options.src, "sil", args)
else
SU.error("\\sil funcion requires inline content or a src file path")
SU.error("\\sil function requires inline content or a src file path")
end
end, "Process sil content. The content may be supplied either inline or using src=...")

self:registerCommand("xml", function (options, content)
local args = optionsAsArgs(options)
if SU.hasContent(content) then
SILE.processString(content[1], "xml")
return SILE.processString(content[1], "xml", nil, args)
elseif options.src then
SILE.processFile(options.src, "xml")
return SILE.processFile(options.src, "xml", args)
else
SU.error("\\xml funcion requires inline content or a src file path")
SU.error("\\xml function requires inline content or a src file path")
end
end, "Process xml content. The content may be supplied either inline or using src=...")

self:registerCommand("use", function (options, content)
local args = optionsAsArgs(options)
if content[1] and string.len(content[1]) > 0 then
SILE.processString(content[1], "lua")
SILE.processString(content[1], "lua", nil, args)
else
if options.src then
SU.warn("Use of 'src' with \\use is discouraged because some of it's path handling\n will eventually be deprecated. Use 'module' instead when possible.")
SILE.processFile(options.src, "lua")
SILE.processFile(options.src, "lua", args)
else
local module = SU.required(options, "module", "use")
SILE.use(module)
SILE.use(module, args)
end
end
end, "Load and initialize a SILE module (can be a package, a shaper, a typesetter, or whatever). Use module=... to specif what to load or include module code inline.")
Expand Down
18 changes: 9 additions & 9 deletions core/sile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ SILE.init = function ()
runEvals(SILE.input.evaluates, "evaluate")
end

SILE.use = function (module)
SILE.use = function (module, args)
local pack
if type(module) == "string" then
pack = require(module)
Expand All @@ -158,19 +158,19 @@ SILE.use = function (module)
-- all available modules, order is designated by the inputter module itself.
elseif pack.type == "outputter" then
SILE.outputters[name] = pack
SILE.outputter = pack()
SILE.outputter = pack(args)
elseif pack.type == "shaper" then
SILE.shapers[name] = pack
SILE.shaper = pack()
SILE.shaper = pack(args)
elseif pack.type == "typesetter" then
SILE.typesetters[name] = pack
SILE.typesetter = pack()
SILE.typesetter = pack(args)
elseif pack.type == "package" then
SILE.packages[name] = pack
if class then
pack(class)
pack(class, args)
else
table.insert(SILE.inputs.preambles, pack)
table.insert(SILE.inputs.preambles, { pack = pack, args = args })
end
end
end
Expand Down Expand Up @@ -261,7 +261,7 @@ local function detectFormat (doc, filename)
SU.error(("Unable to pick inputter to process input from '%s'"):format(filename))
end

function SILE.processString (doc, format, filename)
function SILE.processString (doc, format, filename, args)
local cpf
if not filename then
cpf = SILE.currentlyProcessingFile
Expand All @@ -277,7 +277,7 @@ function SILE.processString (doc, format, filename)
if cpf then SILE.currentlyProcessingFile = cpf end
end

function SILE.processFile (filename, format)
function SILE.processFile (filename, format, args)
local doc
if filename == "-" then
filename = "STDIN"
Expand All @@ -303,7 +303,7 @@ function SILE.processFile (filename, format)
end
SILE.currentlyProcessingFile = filename
local pId = SILE.traceStack:pushDocument(filename, doc)
local ret = SILE.processString(doc, format, filename)
local ret = SILE.processString(doc, format, filename, args)
SILE.traceStack:pop(pId)
return ret
end
Expand Down
4 changes: 3 additions & 1 deletion inputters/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ function base.preamble (_)
if type(preamble) == "string" then
SILE.processFile(preamble)
elseif type(preamble) == "table" then
local args = {}
if preamble.pack then preamble, args = preamble.pack, preamble.args end
if preamble.type == "package" then
SILE.documentState.documentClass:initPackage(preamble)
SILE.documentState.documentClass:initPackage(preamble, args)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion packages/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ base._name = "base"
base._initialized = false
base.class = nil

function base:_init (class)
function base:_init (class, args)
self.class = class
self:declareSettings()
self:registerRawHandlers()
Expand Down
6 changes: 4 additions & 2 deletions packages/masters/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ function package:_init (class, args)
SILE.scratch.masters = {}
end

defineMasters(class, args)

-- exports
class.switchMasterOnePage = switchMasterOnePage
class.switchMaster = switchMaster
class.defineMaster = defineMaster
class.defineMasters = defineMasters
class.currentMaster = currentMaster

if args then
class:defineMasters(args)
end

end

function package:registerCommands ()
Expand Down

0 comments on commit e64ce0f

Please sign in to comment.