11--- A base class for markdown command packages.
22--
3+ -- Derived from SILE's `packages.base`.
4+ --
35-- It abstracts the low-level details of feature detection and compatibility with resilient components.
46-- So the markdown.command packages can focus on their specific features and commands.
57--
8+ --
69-- @copyright License: MIT (c) 2024 Omikhleia, Didier Willis
7- -- @module packages.markdown.cmbase
10+ -- @classmod packages.markdown.cmbase
811--
9-
1012local base = require (" packages.base" )
1113
1214local package = pl .class (base )
@@ -16,9 +18,9 @@ package._name = "markdown.cmbase"
1618-- NOTE: The previous implementation was clever;
1719-- local ok, ResilientBase = pcall(require, 'classes.resilient.base')
1820-- return ok and self.class:is_a(ResilientBase)
19- -- However this *loads* the class, which loads all the silex extensions , even if
21+ -- However this *loads* the class, which loads all the SILE redefinitions , even if
2022-- the class is not actually used...
21- -- Enforcing the silex extensions is not what we wanted.
23+ -- Enforcing resilient's SILE overrides is not what we wanted (for now) .
2224-- So we are back to a more naive implementation, checking the class hierarchy
2325-- by name. This is perhaps lame and knows too much about internals, but heh.
2426local function isResilientClass (cl )
@@ -33,8 +35,10 @@ local function isResilientClass (cl)
3335end
3436
3537--- Load a package with a resilient variant.
36- --- @param resilientpack string The resilient package name.
37- --- @param legacypack string The legacy package name.
38+ -- In a resilient context, the resilient variant is loaded.
39+ -- Otherwise, the legacy variant is loaded.
40+ -- @tparam string resilientpack The resilient package name.
41+ -- @tparam string legacypack The legacy package name.
3842function package :loadAltPackage (resilientpack , legacypack )
3943 if not self .class .packages [resilientpack ] then
4044 -- In resilient context, try enforcing the use of resilient variants,
@@ -48,17 +52,23 @@ function package:loadAltPackage (resilientpack, legacypack)
4852 end
4953end
5054
51- --- Load an optional package if present
52- --- @param pack string The package name.
55+ --- Load an optional package, if available.
56+ -- @ tparam string pack string The package name.
5357function package :loadOptPackage (pack )
5458 local ok , _ = pcall (function () return self :loadPackage (pack ) end )
5559 SU .debug (" markdown.commands" , " Optional package " .. pack .. (ok and " loaded" or " not loaded" ))
5660end
5761
58- --- Feature detection, so we can see e.g. with self.hasPackageSupport.xxx if
59- --- a package is supported or not.
60- --- @param check function The feature detection function.
61- --- @return table The proxy object.
62+ --- (Internal) Feature detection proxy creation.
63+ -- Such proxy objects are used to implement:
64+ --
65+ -- - self.hasPackageSupport.xxx
66+ -- - self.hasCommandSupport.yyy
67+ -- - self.hasStyleSupport.zzz
68+ --
69+ -- So as to check if a package, command or style is supported or not.
70+ --- @tparam function check The feature detection function.
71+ --- @treturn table The proxy object.
6272function package :_createSupportProxy (check )
6373 return setmetatable ({}, {
6474 __index = check ,
@@ -68,7 +78,8 @@ function package:_createSupportProxy (check)
6878 })
6979end
7080
71- --- Package initialization.
81+ --- (Contructor) Package initialization.
82+ -- @tparam table _ Package options (not used here).
7283function package :_init (_ )
7384 base ._init (self )
7485
@@ -115,12 +126,16 @@ function package:_init (_)
115126 end )
116127end
117128
118- function package :registerCommand (name , func , help , pack )
129+ --- (Override) Register a command.
130+ -- It overrides the default package command registration, and tweaks known options.
131+ -- Typically, width and height in percentage are replaced by line and frame relative
132+ -- units, respectively, for compatibility with SILE units.
133+ -- @tparam string name Name of the command to register
134+ -- @tparam function func Command implementation function
135+ -- @tparam [opt] string help Short help string
136+ function package :registerCommand (name , func , help )
119137 local tweakCommandWithKnownOptions = function (options , content )
120138 options = options or {}
121- -- Tweak known options to be compatible with SILE units.
122- -- width and height in percentage are replaced by line and frame relative
123- -- units, respectively.
124139 if options .width and type (options .width ) == " string" then
125140 options .width = options .width :gsub (" %%$" , " %%lw" )
126141 end
@@ -129,16 +144,22 @@ function package:registerCommand(name, func, help, pack)
129144 end
130145 return func (options , content )
131146 end
132- base .registerCommand (self , name , tweakCommandWithKnownOptions , help , pack )
147+ base .registerCommand (self , name , tweakCommandWithKnownOptions , help )
133148end
134149
135150--- Register a style (as in resilient packages).
151+ -- For subclasses defining custom styles, this method is supposed to be used
152+ -- in an overridden registerStyles() method, so that the style is registered
153+ -- only in the resilient context.
154+ -- @tparam string name Style name
155+ -- @tparam table opts Style options
156+ -- @tparam table styledef Style definition
136157function package :registerStyle (name , opts , styledef )
137158 return self .styles :defineStyle (name , opts , styledef , self ._name )
138159end
139160
140- --- Register styles (as in resilient packages)
141- --- For overriding in subclass
161+ --- (Abstract) Register styles (as in resilient packages).
162+ -- For overriding in subclasses.
142163function package .registerStyles (_ ) end
143164
144165package .documentation = [[ \begin{document}
0 commit comments