Skip to content

Commit

Permalink
Merge pull request #1979 from alerque/once-upon-a-package
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Jan 31, 2024
2 parents f29c6e8 + 3c3fada commit 502a1d9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
29 changes: 26 additions & 3 deletions classes/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,32 @@ function class.declareSettings (_)
end

function class:loadPackage (packname, options, reload)
local pack = require(("packages.%s"):format(packname))
if type(pack) == "table" and pack.type == "package" then -- new package
self.packages[pack._name] = pack(options, reload)
local pack
-- Allow loading by injecting whole packages as-is, otherwise try to load it with the usual packages path.
if type(packname) == "table" then
pack, packname = packname, packname._name
else
pack = require(("packages.%s"):format(packname))
if pack._name ~= packname then
SU.error(("Loaded module name '%s' does not match requested name '%s'"):format(pack._name, packname))
end
end
SILE.packages[packname] = pack
if type(pack) == "table" and pack.type == "package" then -- current package api
if self.packages[packname] then
-- If the same package name has been loaded before, we might be loading a modified version of the same package or
-- we might be re-loading the same package, or we might just be doubling up work because somebody called us twice.
-- The package itself should take care of the difference between load and reload based on the reload flag here,
-- but in addition to that we also want to avoid creating a new instance. We want to run the intitializer from the
-- (possibly different) new module, but not create a new instance ID and loose any connections it made.
-- To do this we add a create function that returns the current instance. This brings along the _initialized flag
-- and of course anything else already setup and running.
local current_instance = self.packages[packname]
pack._create = function () return current_instance end
pack(options, true)
else
self.packages[packname] = pack(options, reload)
end
else -- legacy package
self:initPackage(pack, options)
end
Expand Down
6 changes: 3 additions & 3 deletions core/sile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ local function suggest_luarocks (module)
)
end

SILE.use = function (module, options)
SILE.use = function (module, options, reload)
local status, pack
if type(module) == "string" then
status, pack = pcall(require, module)
Expand Down Expand Up @@ -211,9 +211,9 @@ SILE.use = function (module, options)
SILE.pagebuilders[name] = pack
SILE.pagebuilder = pack(options)
elseif pack.type == "package" then
SILE.packages[name] = pack
SILE.packages[pack._name] = pack
if class then
pack(options)
class:loadPackage(pack, options, reload)
else
table.insert(SILE.input.preambles, { pack = pack, options = options })
end
Expand Down
3 changes: 2 additions & 1 deletion inputters/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ local function process_ambles (ambles)
local options = {}
if amble.pack then amble, options = amble.pack, amble.options end
if amble.type == "package" then
amble(options)
local class = SILE.documentState.documentClass
class:loadPackage(amble.pack, options)
else
SILE.documentState.documentClass:initPackage(amble, options)
end
Expand Down

0 comments on commit 502a1d9

Please sign in to comment.