• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sile-typesetter / sile / 14284237390

05 Apr 2025 05:33PM UTC coverage: 63.158% (+31.8%) from 31.375%
14284237390

push

github

web-flow
Merge pull request #2248 from alerque/class-warfare

Normalize module layout across all module types

257 of 350 new or added lines in 14 files covered. (73.43%)

71 existing lines in 11 files now uncovered.

13670 of 21644 relevant lines covered (63.16%)

3070.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.12
/packages/base.lua
1
--- SILE package class.
2
-- @interfaces packages
3

4
local package = pl.class()
114✔
5
package.type = "package"
114✔
6
package._name = "base"
114✔
7

8
package._initialized = false
114✔
9
package.class = nil
114✔
10

11
-- For shimming packages that used to have legacy exports
12
package.exports = {}
114✔
13

14
local function script_path ()
15
   local src = debug.getinfo(3, "S").source:sub(2)
759✔
16
   local base = src:match("(.*[/\\])")
759✔
17
   return base
759✔
18
end
19

20
local settingDeclarations = {}
114✔
21
local rawhandlerRegistrations = {}
114✔
22
local commandRegistrations = {}
114✔
23

24
function package:_init (_, reload)
114✔
25
   SU._avoid_base_class_use(self)
759✔
26
   self.class = SILE.scratch.half_initialized_class or SILE.documentState.documentClass
759✔
27
   if not self.class then
759✔
28
      SU.error("Attempted to initialize package before class, should have been queued in the preamble", true)
×
29
   end
30
   self.basedir = script_path()
1,518✔
31
   -- Note string.format(%p) would be nicer than tostring() but only LuaJIT and Lua 5.4 support it
32
   local settingsDeclarator = tostring(self.declareSettings)
759✔
33
   if reload or not settingDeclarations[settingsDeclarator] then
759✔
34
      settingDeclarations[settingsDeclarator] = true
168✔
35
      self:declareSettings()
168✔
36
   end
37
   local rawhandlerRegistrator = tostring(self.registerRawHandlers)
759✔
38
   if reload or not rawhandlerRegistrations[rawhandlerRegistrator] then
759✔
39
      rawhandlerRegistrations[rawhandlerRegistrator] = true
126✔
40
      self:registerRawHandlers()
126✔
41
   end
42
   local commandRegistrator = tostring(self.registerCommands)
759✔
43
   if reload or not commandRegistrations[commandRegistrator] then
759✔
44
      commandRegistrations[commandRegistrator] = true
693✔
45
      self:registerCommands()
693✔
46
   end
47
end
48

49
function package:_post_init ()
114✔
50
   self._initialized = true
760✔
51
end
52

53
function package.declareSettings (_) end
234✔
54

55
function package.registerRawHandlers (_) end
233✔
56

57
function package:loadPackage (packname, options, reload)
114✔
58
   return self.class:loadPackage(packname, options, reload)
274✔
59
end
60

61
function package:reloadPackage (packname, options)
114✔
62
   return self.class:reloadPackage(packname, options)
×
63
end
64

65
function package.registerCommands (_) end
136✔
66

67
-- This gives us a hook to match commands with the packages that registered
68
-- them as opposed to core commands or class-provided commands
69

70
--- Register a function as a SILE command.
71
-- Takes any Lua function and registers it for use as a SILE command (which will in turn be used to process any content
72
-- nodes identified with the command name.
73
--
74
-- A similar method is available for classes, `classes:registerCommand`.
75
-- @tparam string name Name of cammand to register.
76
-- @tparam function func Callback function to use as command handler.
77
-- @tparam[opt] nil|string help User friendly short usage string for use in error messages, documentation, etc.
78
-- @tparam[opt] nil|string pack Information identifying the module registering the command for use in error and usage
79
-- messages. Usually auto-detected.
80
-- @see SILE.classes:registerCommand
81
function package:registerCommand (name, func, help, pack)
114✔
82
   self.class:registerCommand(name, func, help, pack)
3,063✔
83
end
84
function package:registerRawHandler (format, callback)
114✔
85
   self.class:registerRawHandler(format, callback)
7✔
86
end
87

88
-- Using this rather than doing the work directly will give us a way to
89
-- un-export them if we ever need to unload modules and revert functions
90
function package:export (name, func)
114✔
91
   self.class[name] = func
696✔
92
end
93

94
-- Shims for two possible kinds of legacy exports: blind direct stuffing into
95
-- the class but not expecting to be called as a method AND the exports table
96
-- to package modules...
97

98
local _deprecate_class_funcs = [[
99
   Please explicitly use functions provided by packages by referencing them in
100
   the document class's list of loaded packages rather than the legacy solution
101
   that added non-method functions to the class.
102
]]
114✔
103

104
local _deprecate_exports_table = [[
105
   Please explicitly use functions provided by packages by referencing them in
106
   the document class's list of loaded packages rather than the legacy solution
107
   of calling them from an exports table.
108
]]
114✔
109

110
function package:deprecatedExport (name, func, noclass, notable)
114✔
111
   if not noclass then
758✔
112
      self.class[name] = function (...)
758✔
113
         -- http://lua-users.org/wiki/VarargTheSecondClassCitizen
UNCOV
114
         local inputs = { ... }
×
115
         -- local inputs = table.unpack({...}, 1, select("#", ...))
UNCOV
116
         if type(inputs[1]) ~= "table" or inputs[1].type ~= "class" then
×
117
            table.insert(inputs, 1, self.class)
×
118
         end
UNCOV
119
         SU.deprecated(
×
UNCOV
120
            ("class.%s"):format(name),
×
UNCOV
121
            ("class.packages.%s:%s"):format(self._name, name),
×
122
            "0.14.0",
123
            "0.16.0",
124
            _deprecate_class_funcs
125
         )
UNCOV
126
         return func(pl.utils.unpack(inputs, 1, select("#", ...) + 1))
×
127
      end
128
   end
129

130
   if not notable then
758✔
131
      self.exports[name] = function (...)
758✔
132
         local inputs = { ... }
×
133
         if type(inputs[1]) ~= "table" or inputs[1].type ~= "package" then
×
134
            table.insert(inputs, 1, self)
×
135
         end
136
         SU.deprecated(
×
137
            ("require('packages.%s').exports.%s"):format(self._name, name),
×
138
            ("class.packages.%s:%s"):format(self._name, name),
×
139
            "0.14.0",
140
            "0.16.0",
141
            _deprecate_exports_table
142
         )
143
         return func(pl.utils.unpack(inputs, 1, select("#", ...) + 1))
×
144
      end
145
   end
146
end
147

148
return package
114✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc