Skip to content

Commit

Permalink
refactor(core)!: Move registerCommand() out of global to classes
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The role of document commands has always been tightly
scoped to classes. For example the *book* class has a `\footnote`
command while *plain* does not—unless you manually load the package and setup the
frames. In spite of this obvious functional scope, registering commands
has been a global operation that stored them in a global registry. In
order to allow SILE to be used more programmatically as a library with
potentially more than one document and class being processed at at once,
these need to be moved out of the global scope. This will also
facilitate things like being able *unload* packages and revert to
previous functionality for anything they over-rood on load. For now the
functionality is shimmed, but code using the `SILE.registerCommand()`
function should switch to the method of the same name on the current
class, i.e. `class:registerCommand()`.
  • Loading branch information
alerque committed Jul 16, 2022
1 parent 8704308 commit bc527ea
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
13 changes: 13 additions & 0 deletions classes/base.lua
Expand Up @@ -181,6 +181,19 @@ function base:runHooks (category, args)
end
end

function base.registerCommand (_, name, func, help, pack)
SILE.Commands[name] = func
if not pack then
local where = debug.getinfo(2).source
pack = where:match("(%w+).lua")
end
--if not help and not pack:match(".sil") then SU.error("Could not define command '"..name.."' (in package "..pack..") - no help text" ) end
SILE.Help[name] = {
description = help,
where = pack
}
end

function base:registerCommands ()

local function replaceProcessBy(replacement, tree)
Expand Down
2 changes: 1 addition & 1 deletion core/font.lua
Expand Up @@ -57,7 +57,7 @@ SILE.registerCommand("font", function (options, content)
lastshaper, SILE.shaper = nil, lastshaper
end
end
end, "Set current font family, size, weight, style, variant, script, direction and language")
end, "Set current font family, size, weight, style, variant, script, direction and language", nil, true)

SILE.settings:declare({ parameter = "font.family", type = "string or nil", default = "Gentium Plus" })
SILE.settings:declare({ parameter = "font.size", type = "number or integer", default = 10 })
Expand Down
2 changes: 1 addition & 1 deletion core/hyphenator-liang.lua
Expand Up @@ -164,4 +164,4 @@ SILE.registerCommand("hyphenator:add-exceptions", function (options, content)
registerException(SILE._hyphenators[language], token.string)
end
end
end)
end, nil, nil, true)
6 changes: 3 additions & 3 deletions core/languages.lua
Expand Up @@ -47,7 +47,7 @@ SILE.registerCommand("language", function (options, content)
else
SILE.settings:set("document.language", main)
end
end)
end, nil, nil, true)

SILE.registerCommand("fluent", function (options, content)
local key = content[1]
Expand All @@ -67,7 +67,7 @@ SILE.registerCommand("fluent", function (options, content)
SU.warn(string.format("No localized message for %s found in locale %s", key, locale))
end
SILE.process({ message })
end)
end, nil, nil, true)

SILE.registerCommand("ftl", function (options, content)
local locale = options.locale or SILE.settings:get("document.language")
Expand All @@ -79,7 +79,7 @@ SILE.registerCommand("ftl", function (options, content)
local input = content[1]
fluent:add_messages(input, locale)
end
end)
end, nil, nil, true)

require("languages.unicode")

Expand Down
2 changes: 1 addition & 1 deletion core/settings.lua
Expand Up @@ -91,7 +91,7 @@ function settings:_init()
else
self:set(parameter, value, makedefault, reset)
end
end, "Set a SILE parameter <parameter> to value <value> (restoring the value afterwards if <content> is provided)")
end, "Set a SILE parameter <parameter> to value <value> (restoring the value afterwards if <content> is provided)", nil, true)

end

Expand Down
20 changes: 10 additions & 10 deletions core/sile.lua
Expand Up @@ -306,17 +306,17 @@ function SILE.call (command, options, content)
return result
end

function SILE.registerCommand (name, func, help, pack)
SILE.Commands[name] = func
if not pack then
local where = debug.getinfo(2).source
pack = where:match("(%w+).lua")
function SILE.registerCommand (name, func, help, pack, cheat)
if not cheat then
SU.deprecated("SILE.registerCommand", "class:registerCommand", "0.14.0", "0.16.0",
[[Commands are being scoped to the document classes they are loaded into rather than being globals.]])
local class = SILE.documentState.documentClass
if not class then SU.error("Can't register command "..name.." before a document class is loaded") end
class:registerCommand(name, func, help, pack)
else
-- Shimming until we have all scope cheating removed from core
SILE.classes.base.registerCommand(nil, name, func, help, pack)
end
--if not help and not pack:match(".sil") then SU.error("Could not define command '"..name.."' (in package "..pack..") - no help text" ) end
SILE.Help[name] = {
description = help,
where = pack
}
end

function SILE.setCommandDefaults (command, defaults)
Expand Down

0 comments on commit bc527ea

Please sign in to comment.