sile 0.15.5

Simon’s Improved Layout Engine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
--- SILE document class interface.
-- @interfaces classes

local class = pl.class()
class.type = "class"
class._name = "base"

class._initialized = false
class.deferredInit = {}
class.pageTemplate = { frames = {}, firstContentFrame = nil }
class.defaultFrameset = {}
class.firstContentFrame = "page"
class.options = setmetatable({}, {
   _opts = {},
   __newindex = function (self, key, value)
      local opts = getmetatable(self)._opts
      if type(opts[key]) == "function" then
         opts[key](class, value)
      elseif type(value) == "function" then
         opts[key] = value
      elseif type(key) == "number" then
         return
      else
         SU.error("Attempted to set an undeclared class option '" .. key .. "'")
      end
   end,
   __index = function (self, key)
      if key == "super" then
         return nil
      end
      if type(key) == "number" then
         return nil
      end
      local opt = getmetatable(self)._opts[key]
      if type(opt) == "function" then
         return opt(class)
      elseif opt then
         return opt
      else
         SU.error("Attempted to get an undeclared class option '" .. key .. "'")
      end
   end,
})
class.hooks = {
   newpage = {},
   endpage = {},
   finish = {},
}

class.packages = {}

function class:_init (options)
   SILE.scratch.half_initialized_class = self
   if self == options then
      options = {}
   end
   SILE.languageSupport.loadLanguage("und") -- preload for unlocalized fallbacks
   self:declareOptions()
   self:registerRawHandlers()
   self:declareSettings()
   self:registerCommands()
   self:setOptions(options)
   self:declareFrames(self.defaultFrameset)
   self:registerPostinit(function (self_)
      -- In the event no packages have called \language explicitly or otherwise triggerend the language loader, at this
      -- point we'll have a language *setting* but not actually have loaded the language. We put it off as long as we
      -- could in case the user changed the default document language and we didn't need to load the system default one,
      -- but that time has come at gone at this point. Make sure we've loaded somethnig...
      local lang = SILE.settings:get("document.language")
      SILE.languageSupport.loadLanguage(lang)
      if type(self.firstContentFrame) == "string" then
         self_.pageTemplate.firstContentFrame = self_.pageTemplate.frames[self_.firstContentFrame]
      end
      local frame = self_:initialFrame()
      SILE.typesetter = SILE.typesetters.base(frame)
      SILE.typesetter:registerPageEndHook(function ()
         SU.debug("frames", function ()
            for _, v in pairs(SILE.frames) do
               SILE.outputter:debugFrame(v)
            end
            return "Drew debug outlines around frames"
         end)
      end)
   end)
end

function class:_post_init ()
   SILE.documentState.documentClass = self
   self._initialized = true
   for i, func in ipairs(self.deferredInit) do
      func(self)
      self.deferredInit[i] = nil
   end
   SILE.scratch.half_initialized_class = nil
end

function class:setOptions (options)
   options = options or {}
   -- Classes that add options with dependencies should explicitly handle them, then exempt them from further processing.
   -- The landscape and crop related options are handled explicitly before papersize, then the "rest" of options that are not interdependent.
   self.options.landscape = SU.boolean(options.landscape, false)
   options.landscape = nil
   self.options.papersize = options.papersize or "a4"
   options.papersize = nil
   self.options.bleed = options.bleed or "0"
   options.bleed = nil
   self.options.sheetsize = options.sheetsize or nil
   options.sheetsize = nil
   for option, value in pairs(options) do
      self.options[option] = value
   end
end

function class:declareOption (option, setter)
   rawset(getmetatable(self.options)._opts, option, nil)
   self.options[option] = setter
end

function class:declareOptions ()
   self:declareOption("class", function (_, name)
      if name then
         if self._legacy then
            self._name = name
         elseif name ~= self._name then
            SU.error("Cannot change class name after instantiation, derive a new class instead.")
         end
      end
      return self._name
   end)
   self:declareOption("landscape", function (_, landscape)
      if landscape then
         self.landscape = landscape
      end
      return self.landscape
   end)
   self:declareOption("papersize", function (_, size)
      if size then
         self.papersize = size
         SILE.documentState.paperSize = SILE.papersize(size, self.options.landscape)
         SILE.documentState.orgPaperSize = SILE.documentState.paperSize
         SILE.newFrame({
            id = "page",
            left = 0,
            top = 0,
            right = SILE.documentState.paperSize[1],
            bottom = SILE.documentState.paperSize[2],
         })
      end
      return self.papersize
   end)
   self:declareOption("sheetsize", function (_, size)
      if size then
         self.sheetsize = size
         SILE.documentState.sheetSize = SILE.papersize(size, self.options.landscape)
         if
            SILE.documentState.sheetSize[1] < SILE.documentState.paperSize[1]
            or SILE.documentState.sheetSize[2] < SILE.documentState.paperSize[2]
         then
            SU.error("Sheet size shall not be smaller than the paper size")
         end
         if SILE.documentState.sheetSize[1] < SILE.documentState.paperSize[1] + SILE.documentState.bleed then
            SU.debug("frames", "Sheet size width augmented to take page bleed into account")
            SILE.documentState.sheetSize[1] = SILE.documentState.paperSize[1] + SILE.documentState.bleed
         end
         if SILE.documentState.sheetSize[2] < SILE.documentState.paperSize[2] + SILE.documentState.bleed then
            SU.debug("frames", "Sheet size height augmented to take page bleed into account")
            SILE.documentState.sheetSize[2] = SILE.documentState.paperSize[2] + SILE.documentState.bleed
         end
      else
         return self.sheetsize
      end
   end)
   self:declareOption("bleed", function (_, dimen)
      if dimen then
         self.bleed = dimen
         SILE.documentState.bleed = SU.cast("measurement", dimen):tonumber()
      end
      return self.bleed
   end)
end

function class.declareSettings (_)
   SILE.settings:declare({
      parameter = "current.parindent",
      type = "glue or nil",
      default = nil,
      help = "Glue at start of paragraph",
   })
   SILE.settings:declare({
      parameter = "current.hangIndent",
      type = "measurement or nil",
      default = nil,
      help = "Size of hanging indent",
   })
   SILE.settings:declare({
      parameter = "current.hangAfter",
      type = "integer or nil",
      default = nil,
      help = "Number of lines affected by handIndent",
   })
end

function class:loadPackage (packname, 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
   elseif type(packname) == "nil" or packname == "nil" or pl.stringx.strip(packname) == "" then
      SU.error(("Attempted to load package with an invalid packname '%s'"):format(packname))
   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
end

function class:reloadPackage (packname, options)
   return self:loadPackage(packname, options, true)
end

function class:initPackage (pack, options)
   SU.deprecated(
      "class:initPackage(options)",
      "package(options)",
      "0.14.0",
      "0.16.0",
      [[
  This package appears to be a legacy format package. It returns a table
  and expects SILE to guess about what to do. New packages inherit
  from the base class and have a constructor function (_init) that
  automatically handles setup.]]
   )
   if type(pack) == "table" then
      if pack.exports then
         pl.tablex.update(self, pack.exports)
      end
      if type(pack.declareSettings) == "function" then
         pack.declareSettings(self)
      end
      if type(pack.registerRawHandlers) == "function" then
         pack.registerRawHandlers(self)
      end
      if type(pack.registerCommands) == "function" then
         pack.registerCommands(self)
      end
      if type(pack.init) == "function" then
         self:registerPostinit(pack.init, options)
      end
   end
end

--- Register a callback function to be executed after the class initialization has completed.
-- Sometimes a class or package may want to run things after the class has been fully initialized. This can be useful
-- for setting document settings after packages and all their dependencies have been loaded. For example a package might
-- want to trigger something to happen after all frames have been defined, but the package itself doesn't know if it is
-- being loaded before or after the document options are processed, frame masters have been setup, etc. Rather than
-- relying on the user to load the package after these events, the package can use this callback to defer the action
-- until those things can be reasonable expected to have completed.
--
-- Functions in the deferred initialization queue are run on a first-set first-run basis.
--
-- Note the typesetter will *not* have been instantiated yet, so is not appropriate to try to output content at this
-- point. Injecting content to be processed at the start of a document should be done with preambles. The inputter
-- *will* be instantiated at this point, so adding a new preamble is viable.
-- If the class has already been initialized the callback function will be run immediately.
-- @tparam function func Callback function accepting up to two arguments.
-- @tparam[opt] table options Additional table passed as a second argument to the callback.
function class:registerPostinit (func, options)
   if self._initialized then
      return func(self, options)
   end
   table.insert(self.deferredInit, function (_)
      func(self, options)
   end)
end

function class:registerHook (category, func)
   for _, func_ in ipairs(self.hooks[category]) do
      if func_ == func then
         return
         --[[ See https://github.com/sile-typesetter/sile/issues/1531
      return SU.warn("Attempted to set the same function hook twice, probably unintended, skipping.")
      -- If the same function signature is already set a package is probably being
      -- re-initialized. Ditch the first instance of the hook so that it runs in
      -- the order of last initialization.
      self.hooks[category][_] = nil
      ]]
      end
   end
   table.insert(self.hooks[category], func)
end

function class:runHooks (category, options)
   for _, func in ipairs(self.hooks[category]) do
      SU.debug("classhooks", "Running hook from", category, options and "with options #" .. #options)
      func(self, options)
   end
end

--- Register a function as a SILE command.
-- Takes any Lua function and registers it for use as a SILE command (which will in turn be used to process any content
-- nodes identified with the command name.
--
-- Note that this should only be used to register commands supplied directly by a document class. A similar method is
-- available for packages, `packages:registerCommand`.
-- @tparam string name Name of cammand to register.
-- @tparam function func Callback function to use as command handler.
-- @tparam[opt] nil|string help User friendly short usage string for use in error messages, documentation, etc.
-- @tparam[opt] nil|string pack Information identifying the module registering the command for use in error and usage
-- messages. Usually auto-detected.
-- @see SILE.packages:registerCommand
function class.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 class.registerRawHandler (_, format, callback)
   SILE.rawHandlers[format] = callback
end

function class:registerRawHandlers ()
   self:registerRawHandler("text", function (_, content)
      SILE.settings:temporarily(function ()
         SILE.settings:set("typesetter.parseppattern", "\n")
         SILE.settings:set("typesetter.obeyspaces", true)
         SILE.typesetter:typeset(content[1])
      end)
   end)
end

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

function class:registerCommands ()
   local function replaceProcessBy (replacement, tree)
      if type(tree) ~= "table" then
         return tree
      end
      local ret = pl.tablex.deepcopy(tree)
      if tree.command == "process" then
         return replacement
      else
         for i, child in ipairs(tree) do
            ret[i] = replaceProcessBy(replacement, child)
         end
         return ret
      end
   end

   self:registerCommand("define", function (options, content)
      SU.required(options, "command", "defining command")
      if type(content) == "function" then
         -- A macro defined as a function can take no argument, so we register
         -- it as-is.
         self:registerCommand(options["command"], content)
         return
      elseif options.command == "process" then
         SU.warn("Did you mean to re-definine the `\\process` macro? That probably won't go well.")
      end
      self:registerCommand(options["command"], function (_, inner_content)
         SU.debug("macros", "Processing macro \\" .. options["command"])
         local macroArg
         if type(inner_content) == "function" then
            macroArg = inner_content
         elseif type(inner_content) == "table" then
            macroArg = pl.tablex.copy(inner_content)
            macroArg.command = nil
            macroArg.id = nil
         elseif inner_content == nil then
            macroArg = {}
         else
            SU.error(
               "Unhandled content type " .. type(inner_content) .. " passed to macro \\" .. options["command"],
               true
            )
         end
         -- Replace every occurrence of \process in `content` (the macro
         -- body) with `macroArg`, then have SILE go through the new `content`.
         local newContent = replaceProcessBy(macroArg, content)
         SILE.process(newContent)
         SU.debug("macros", "Finished processing \\" .. options["command"])
      end, options.help, SILE.currentlyProcessingFile)
   end, "Define a new macro. \\define[command=example]{ ... \\process }")

   -- A utility function that allows SILE.call() to be used as a noop wrapper.
   self:registerCommand("noop", function (_, content)
      SILE.process(content)
   end)

   -- The document (SIL) or sile (XML) command is always the sigular leaf at the
   -- top level of our AST. The work you might expect to see happen here is
   -- actually handled by SILE.inputter:classInit() before we get here, so these
   -- are just pass through functions. Theoretically, this could be a useful
   -- point to hook into-especially for included documents.
   self:registerCommand("document", function (_, content)
      SILE.process(content)
   end)
   self:registerCommand("sile", function (_, content)
      SILE.process(content)
   end)

   self:registerCommand("comment", function (_, _) end, "Ignores any text within this command's body.")

   self:registerCommand("process", function ()
      SU.error("Encountered unsubstituted \\process.")
   end, "Within a macro definition, processes the contents of the macro body.")

   self:registerCommand("script", function (options, content)
      local packopts = packOptions(options)
      local function _deprecated (original, suggested)
         SU.deprecated(
            "\\script",
            "\\lua or \\use",
            "0.15.0",
            "0.16.0",
            ([[
      The \script function has been deprecated. It was overloaded to mean
      too many different things and more targeted tools were introduced in
      SILE v0.14.0. To load 3rd party modules designed for use with SILE,
      replace \script[src=...] with \use[module=...]. To run arbitrary Lua
      code inline use \lua{}, optionally with a require= parameter to load
      a (non-SILE) Lua module using the Lua module path or src= to load a
      file by file path.

      For this use case consider replacing:

          %s

      with:

          %s
      ]]):format(original, suggested)
         )
      end
      if SU.ast.hasContent(content) then
         _deprecated("\\script{...}", "\\lua{...}")
         return SILE.processString(content[1], options.format or "lua", nil, packopts)
      elseif options.src then
         local module = options.src:gsub("%/", ".")
         local original = (("\\script[src=%s]"):format(options.src))
         local result = SILE.require(options.src)
         local suggested = (result._name and "\\use[module=%s]" or "\\lua[require=%s]"):format(module)
         _deprecated(original, suggested)
         return result
      else
         SU.error("\\script function requires inline content or a src file path")
         return SILE.processString(content[1], options.format or "lua", nil, packopts)
      end
   end, "Runs lua code. The code may be supplied either inline or using src=...")

   self:registerCommand("include", function (options, content)
      local packopts = packOptions(options)
      if SU.ast.hasContent(content) then
         local doc = SU.ast.contentToString(content)
         return SILE.processString(doc, options.format, nil, packopts)
      elseif options.src then
         return SILE.processFile(options.src, options.format, packopts)
      else
         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 packopts = packOptions(options)
         if SU.ast.hasContent(content) then
            local doc = SU.ast.contentToString(content)
            return SILE.processString(doc, "lua", nil, packopts)
         elseif options.src then
            return SILE.processFile(options.src, "lua", packopts)
         elseif options.require then
            local module = SU.required(options, "require", "lua")
            return require(module)
         else
            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 packopts = packOptions(options)
      if SU.ast.hasContent(content) then
         local doc = SU.ast.contentToString(content)
         return SILE.processString(doc, "sil")
      elseif options.src then
         return SILE.processFile(options.src, "sil", packopts)
      else
         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 packopts = packOptions(options)
      if SU.ast.hasContent(content) then
         local doc = SU.ast.contentToString(content)
         return SILE.processString(doc, "xml", nil, packopts)
      elseif options.src then
         return SILE.processFile(options.src, "xml", packopts)
      else
         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 packopts = packOptions(options)
         if content[1] and string.len(content[1]) > 0 then
            local doc = SU.ast.contentToString(content)
            SILE.processString(doc, "lua", nil, packopts)
         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", packopts)
            else
               local module = SU.required(options, "module", "use")
               SILE.use(module, packopts)
            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."
   )

   self:registerCommand("raw", function (options, content)
      local rawtype = SU.required(options, "type", "raw")
      local handler = SILE.rawHandlers[rawtype]
      if not handler then
         SU.error("No inline handler for '" .. rawtype .. "'")
      end
      handler(options, content)
   end, "Invoke a raw passthrough handler")

   self:registerCommand("pagetemplate", function (options, content)
      SILE.typesetter:pushState()
      SILE.documentState.thisPageTemplate = { frames = {} }
      SILE.process(content)
      SILE.documentState.thisPageTemplate.firstContentFrame = SILE.getFrame(options["first-content-frame"])
      SILE.typesetter:initFrame(SILE.documentState.thisPageTemplate.firstContentFrame)
      SILE.typesetter:popState()
   end, "Defines a new page template for the current page and sets the typesetter to use it.")

   self:registerCommand("frame", function (options, _)
      SILE.documentState.thisPageTemplate.frames[options.id] = SILE.newFrame(options)
   end, "Declares (or re-declares) a frame on this page.")

   self:registerCommand("penalty", function (options, _)
      if SU.boolean(options.vertical, false) and not SILE.typesetter:vmode() then
         SILE.typesetter:leaveHmode()
      end
      if SILE.typesetter:vmode() then
         SILE.typesetter:pushVpenalty({ penalty = tonumber(options.penalty) })
      else
         SILE.typesetter:pushPenalty({ penalty = tonumber(options.penalty) })
      end
   end, "Inserts a penalty node. Option is penalty= for the size of the penalty.")

   self:registerCommand("discretionary", function (options, _)
      local discretionary = SILE.types.node.discretionary({})
      if options.prebreak then
         local hbox = SILE.typesetter:makeHbox({ options.prebreak })
         discretionary.prebreak = { hbox }
      end
      if options.postbreak then
         local hbox = SILE.typesetter:makeHbox({ options.postbreak })
         discretionary.postbreak = { hbox }
      end
      if options.replacement then
         local hbox = SILE.typesetter:makeHbox({ options.replacement })
         discretionary.replacement = { hbox }
      end
      table.insert(SILE.typesetter.state.nodes, discretionary)
   end, "Inserts a discretionary node.")

   self:registerCommand("glue", function (options, _)
      local width = SU.cast("length", options.width):absolute()
      SILE.typesetter:pushGlue(width)
   end, "Inserts a glue node. The width option denotes the glue dimension.")

   self:registerCommand("kern", function (options, _)
      local width = SU.cast("length", options.width):absolute()
      SILE.typesetter:pushHorizontal(SILE.types.node.kern(width))
   end, "Inserts a glue node. The width option denotes the glue dimension.")

   self:registerCommand("skip", function (options, _)
      options.discardable = SU.boolean(options.discardable, false)
      options.height = SILE.types.length(options.height):absolute()
      SILE.typesetter:leaveHmode()
      if options.discardable then
         SILE.typesetter:pushVglue(options)
      else
         SILE.typesetter:pushExplicitVglue(options)
      end
   end, "Inserts vertical skip. The height options denotes the skip dimension.")

   self:registerCommand("par", function (_, _)
      SILE.typesetter:endline()
   end, "Ends the current paragraph.")
end

function class:initialFrame ()
   SILE.documentState.thisPageTemplate = pl.tablex.deepcopy(self.pageTemplate)
   SILE.frames = { page = SILE.frames.page }
   for k, v in pairs(SILE.documentState.thisPageTemplate.frames) do
      SILE.frames[k] = v
   end
   if not SILE.documentState.thisPageTemplate.firstContentFrame then
      SILE.documentState.thisPageTemplate.firstContentFrame = SILE.frames[self.firstContentFrame]
   end
   SILE.documentState.thisPageTemplate.firstContentFrame:invalidate()
   return SILE.documentState.thisPageTemplate.firstContentFrame
end

function class:declareFrame (id, spec)
   spec.id = id
   if spec.solve then
      self.pageTemplate.frames[id] = spec
   else
      self.pageTemplate.frames[id] = SILE.newFrame(spec)
   end
   --   next = spec.next,
   --   left = spec.left and fW(spec.left),
   --   right = spec.right and fW(spec.right),
   --   top = spec.top and fH(spec.top),
   --   bottom = spec.bottom and fH(spec.bottom),
   --   height = spec.height and fH(spec.height),
   --   width = spec.width and fH(spec.width),
   --   id = id
   -- })
end

function class:declareFrames (specs)
   if specs then
      for k, v in pairs(specs) do
         self:declareFrame(k, v)
      end
   end
end

-- WARNING: not called as class method
function class.newPar (typesetter)
   local parindent = SILE.settings:get("current.parindent") or SILE.settings:get("document.parindent")
   -- See https://github.com/sile-typesetter/sile/issues/1361
   -- The parindent *cannot* be pushed non-absolutized, as it may be evaluated
   -- outside the (possibly temporary) setting scope where it was used for line
   -- breaking.
   -- Early absolutization can be problematic sometimes, but here we do not
   -- really have the choice.
   -- As of problematic cases, consider a parindent that would be defined in a
   -- frame-related unit (%lw, %fw, etc.). If a frame break occurs and the next
   -- frame has a different width, the parindent won't be re-evaluated in that
   -- new frame context. However, defining a parindent in such a unit is quite
   -- unlikely. And anyway pushback() has plenty of other issues.
   typesetter:pushGlue(parindent:absolute())
   local hangIndent = SILE.settings:get("current.hangIndent")
   if hangIndent then
      SILE.settings:set("linebreak.hangIndent", hangIndent)
   end
   local hangAfter = SILE.settings:get("current.hangAfter")
   if hangAfter then
      SILE.settings:set("linebreak.hangAfter", hangAfter)
   end
end

-- WARNING: not called as class method
function class.endPar (typesetter)
   -- If we're already explicitly out of hmode don't do anything special in the way of skips or indents. Assume the user
   -- has handled that how they want, e.g. with a skip.
   local queue = typesetter.state.outputQueue
   local last_vbox = queue and queue[#queue]
   local last_is_vglue = last_vbox and last_vbox.is_vglue
   local last_is_vpenalty = last_vbox and last_vbox.is_penalty
   if typesetter:vmode() and (last_is_vglue or last_is_vpenalty) then
      return
   end
   SILE.settings:set("current.parindent", nil)
   typesetter:leaveHmode()
   typesetter:pushVglue(SILE.settings:get("document.parskip"))
end

function class:newPage ()
   SILE.outputter:newPage()
   self:runHooks("newpage")
   -- Any other output-routiney things will be done here by inheritors
   return self:initialFrame()
end

function class:endPage ()
   SILE.typesetter.frame:leave(SILE.typesetter)
   self:runHooks("endpage")
   -- I'm trying to call up a new frame here, don't cause a page break in the current one
   -- SILE.typesetter:leaveHmode()
   -- Any other output-routiney things will be done here by inheritors
end

function class:finish ()
   SILE.inputter:postamble()
   SILE.typesetter:endline()
   SILE.call("vfill")
   while not SILE.typesetter:isQueueEmpty() do
      SILE.call("supereject")
      SILE.typesetter:leaveHmode(true)
      SILE.typesetter:buildPage()
      if not SILE.typesetter:isQueueEmpty() then
         SILE.typesetter:initNextFrame()
      end
   end
   SILE.typesetter:runHooks("pageend") -- normally run by the typesetter
   self:endPage()
   if SILE.typesetter and not SILE.typesetter:isQueueEmpty() then
      SU.error("Queues are not empty as expected after ending last page", true)
   end
   SILE.outputter:finish()
   self:runHooks("finish")
end

return class