Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
Omikhleia authored and Didier Willis committed Sep 8, 2023
1 parent 2157be4 commit 573666c
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 106 deletions.
71 changes: 70 additions & 1 deletion examples/sile-resilient-manual-styles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,75 @@ blockquote:
before:
skip: "smallskip"

bookmatter-author:
inherit: "bookmatter-titlepage"
origin: "resilient.bookmatters"
style:
font:
size: "16pt"
properties:
case: "upper"

bookmatter-copyright:
origin: "resilient.bookmatters"
style:
paragraph:
align: "noparindent"

bookmatter-halftitle:
origin: "resilient.bookmatters"
style:
font:
size: "14pt"
paragraph:
after:
skip: "5%fh"
align: "center"
before:
skip: "25%fh"
properties:
case: "upper"

bookmatter-legal:
origin: "resilient.bookmatters"
style:
font:
size: "0.8em"
paragraph:
before:
skip: "smallskip"

bookmatter-publisher:
inherit: "bookmatter-titlepage"
origin: "resilient.bookmatters"
style:

bookmatter-subtitle:
inherit: "bookmatter-titlepage"
origin: "resilient.bookmatters"
style:
font:
size: "16pt"
paragraph:
after:
skip: "5%fh"

bookmatter-title:
inherit: "bookmatter-titlepage"
origin: "resilient.bookmatters"
style:
font:
size: "20pt"
paragraph:
after:
skip: "5%fh"
before:
skip: "30%fh"

bookmatter-titlepage:
origin: "resilient.bookmatters"
style:

epigraph:
origin: "resilient.epigraph"
style:
Expand Down Expand Up @@ -474,9 +543,9 @@ sectioning-chapter-main-number:
inherit: "sectioning-chapter-base-number"
origin: "resilient.book"
style:
color: "#66a0b3"
font:
size: "3em"
color: "#66a0b3"
numbering:
after:
kern: "3spc"
Expand Down
2 changes: 1 addition & 1 deletion examples/sile-resilient-manual.silm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ metadata:
publisher: Omikhleia
pubdate: 2023-10-31
url: https://github.com/Omikhleia/resilient.sile
copyright: © 2021–2023 Didier Willis.
copyright: © 2021–2023, Didier Willis.
legal: |
This material may be distributed only subject to the terms and conditions
set forth in the Creative Commons Attribution, Share-Alike License,
Expand Down
18 changes: 2 additions & 16 deletions inputters/silm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -465,25 +465,11 @@ function inputter:parse (doc)
content[#content+1] = function ()
-- FIXME PLAY FAIR
local class = SILE.documentState.documentClass
class:registerCommand("InitialJoined", function (_, content)
local letter = content[1]
class:registerCommand("InitialJoined", function (_, xcontent)
local letter = xcontent[1]
SILE.call("use", { module = "packages.dropcaps" })
SILE.call("dropcap", { family = "Zallman Caps", lines = 2, join = true }, { letter })
end)
class:registerCommand("noindenting", function (_, content)
SILE.settings:temporarily(function ()
SILE.settings:set("document.parindent", SILE.nodefactory.glue())
SILE.settings:set("current.parindent", SILE.nodefactory.glue())
SILE.process(content)
SILE.call("par")
end)
end)
SILE.scratch.styles.alignments["noindent"] = "noindenting"
class:registerStyle("full", {}, {
paragraph = {
align = "noindent",
}
})
end
-- END FIXME MOVE THIS ELSEWHERE

Expand Down
194 changes: 129 additions & 65 deletions packages/resilient/bookmatters/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,84 +6,67 @@
local ast = require("silex.ast")
local createCommand = ast.createCommand
local layoutParser = require("resilient.layoutparser")
local loadkit = require("loadkit")
local templateLoader = loadkit.make_loader("djt")

local base = require("packages.resilient.base")

local package = pl.class(base)
package._name = "resilient.bookmatters"

local extraRequestedPackages = {
"masters",
"twoside",
"resilient.sectioning",
"resilient.headers"
}

function package:_init (options)
base._init(self, options)

self:loadPackage("barcodes.ean13")
self:loadPackage("qrcode")
self:loadPackage("background")
self:loadPackage("framebox")
-- There are other package assumptions for some commands,
-- that we are not going to handle here (masters, resilient.headers, etc.)
-- They ought to be already loaded by the document class, as "resilient.book",
-- with proper hooks etc.
-- Ensuring everything works with other classes (fully resilient-aware or
-- not) is beyond the scope of this package.
local missing = false
for _, p in ipairs(extraRequestedPackages) do
if not self.class.packages[p] then
missing = true
end
end
if missing then
SU.error([[Book matters require packages not loaded by the current document class.
Either you are not using a resilient class, or your class lacks the appropriate
support.
Please consider using a resilient-compatible class!]])
end

local layout = layoutParser:match("geometry 0.5in 0.5in")
local oddFrameset, evenFrameset = layout:frameset()
self.class:defineMaster({
id = "front-cover",
id = "bookmatters-front-cover",
firstContentFrame = "content",
frames = oddFrameset
})
self.class:defineMaster({
id = "back-cover",
id = "bookmatters-back-cover",
firstContentFrame = "content",
frames = evenFrameset
})
end

-- BEGIN FIXME MOVE TO A MORE APPROPRIATE PLACE
local function nodesToText (nodes)
local spc = SILE.measurement("0.8spc"):tonumber() -- approx. see below.
local string = ""
for i = 1, #nodes do
local node = nodes[i]
if node.is_nnode or node.is_unshaped then
string = string .. node:toText()
elseif node.is_glue or node.is_kern then
-- What we want to avoid is "small" glues or kerns to be expanded as
-- full spaces. Comparing to a "decent" ratio of a space is fragile and
-- empirical: the content could contain font changes, so the comparison
-- is wrong in the general case. It's still better than nothing.
-- (That's what the debug text outputter does to, by the way).
if node.width:tonumber() > spc then
string = string .. " "
end
elseif not (node.is_zerohbox or node.is_migrating) then
-- Here, typically, the main case is an hbox.
-- Even if extracting its content could be possible in regular cases
-- (e.g. \raise), we cannot take a general decision, as it is a versatile
-- object (e.g. \rebox) and its outputYourself could moreover have been
-- redefine to do fancy things. Better warn and skip.
SU.warn("Some content could not be converted to text: " .. node)
end
end
-- Trim leading and trailing spaces, and simplify internal spaces.
return string:match("^%s*(.-)%s*$"):gsub("%s+", " ")
end

local function toText (content)
SILE.typesetter:pushState()
-- Temporarilly kill footnotes and labels (fragile)
local oldFt = SILE.Commands["footnote"]
SILE.Commands["footnote"] = function () end
local oldLbl = SILE.Commands["label"]
SILE.Commands["label"] = function () end
SILE.process(content)
SILE.Commands["footnote"] = oldFt
SILE.Commands["label"] = oldLbl
local text = nodesToText(SILE.typesetter.state.nodes)
SILE.typesetter:popState()
return text
end
-- END FIXME MOVE TO A MORE APPROPRIATE PLACE

local loadkit = require("loadkit")
local loader = loadkit.make_loader("djt")

-- Look for templates explicitly in a templates/ subdirectory
-- as per other document resources, and if not found, try to
-- load them from module locations.
local function getTemplateInclude(res, metaopts)
local tpl = SILE.resolveFile("templates/" .. res .. ".djt") or loader("templates." .. res)
local tpl = SILE.resolveFile("templates/" .. res .. ".djt")
or templateLoader("templates." .. res)
if not tpl then
SU.error("Cannot find template '" .. res .. "'")
end
Expand All @@ -93,12 +76,13 @@ end

function package:registerCommands ()

-- Book matter support commands

self:registerCommand("bookmatters:front-cover", function (options, _)
local image = SU.required(options, "image", "bookmatters:front-cover")
-- local metadata = options.metadata or {} -- Unusual: table of metadata options

SILE.call("switch-master-one-page", { id = "front-cover" })

SILE.call("switch-master-one-page", { id = "bookmatters-front-cover" })
SILE.call("hbox") -- To ensure some content
SILE.call("background", {
src = SILE.resolveFile(image),
Expand All @@ -115,8 +99,7 @@ function package:registerCommands ()
local metadata = options.metadata or {} -- Unusual: table of metadata options

SILE.call("open-on-even-page")

SILE.call("switch-master-one-page", { id = "back-cover" })
SILE.call("switch-master-one-page", { id = "bookmatters-back-cover" })
SILE.call("hbox") -- To ensure some content
SILE.call("background", {
src = SILE.resolveFile(image),
Expand Down Expand Up @@ -152,20 +135,34 @@ function package:registerCommands ()
SILE.call("framebreak")
end, "Create templated recto-verso pages")

self:registerCommand("Ean13", function (_, content)
-- Book matter helpful default pseudo-styles
-- (for easier Djot custom styling)

self:registerCommand("noparindent", function (_, content)
SILE.settings:temporarily(function ()
SILE.settings:set("document.parindent", SILE.nodefactory.glue())
SILE.settings:set("current.parindent", SILE.nodefactory.glue())
SILE.process(content)
SILE.call("par")
end)
end, "Typeset its contents without paragraph indentation.")

SILE.scratch.styles.alignments["noparindent"] = "noparindent"

self:registerCommand("bookmatter-ean13", function (_, content)
local code = content[1]
-- Markdown/Djot parser may interpret a dash between digits as smart typography for en-dash.
-- Let's remove those.
code = code:gsub("[–-]","")
SILE.call("smallskip")
SILE.call("medskip")
SILE.call("ean13", { code = code })
SILE.call("medskip")
end, "EAN-13 convenience for Djot custom styling")

self:registerCommand("QRCode", function (_, content)
local url = toText(content)
self:registerCommand("bookmatter-qrcode", function (_, content)
local url = content[1]
if not url:match("^https?://") then
SU.warn("QRCode content is not a URL: " .. url .. " (skipped)")
SU.warn("bookmatter-qrcode content is not a URL: " .. url .. " (skipped)")
else
SILE.call("href", { src = url }, function ()
SILE.call("qrcode", { code = url })
Expand All @@ -176,11 +173,78 @@ function package:registerCommands ()
end

function package:registerStyles ()
-- Enumerate style
-- self:registerStyle("lists-enumerate1", { inherit = "lists-enumerate-base" }, {
-- numbering = { display = "arabic", after = { text = "." } }
-- })
-- Some default styles for (usually) half-title page recto
self:registerStyle("bookmatter-halftitle", {}, {
font = {
size = "14pt"
},
paragraph = {
after = {
skip = "5%fh"
},
align = "center",
before = {
skip = "25%fh"
}
},
properties = {
case = "upper"
}
})

-- Some default styles for (usually) title page recto
self:registerStyle("bookmatter-titlepage", {}, {
})
self:registerStyle("bookmatter-title", { inherit = "bookmatter-titlepage" }, {
font = {
size = "20pt"
},
paragraph = {
after = {
skip = "5%fh"
},
before = {
skip = "30%fh"
}
},
})
self:registerStyle("bookmatter-subtitle", { inherit = "bookmatter-titlepage" }, {
font = {
size = "16pt"
},
paragraph = {
after = {
skip = "5%fh"
}
}
})
self:registerStyle("bookmatter-author", { inherit = "bookmatter-titlepage" }, {
font = {
size = "16pt"
},
properties = {
case = "upper"
},
})
self:registerStyle("bookmatter-publisher", { inherit = "bookmatter-titlepage" }, {
})

-- Styles for (usually) title page recto
self:registerStyle("bookmatter-copyright", {}, {
paragraph = {
align = "noparindent"
}
})
self:registerStyle("bookmatter-legal", {}, {
font = {
size = "0.8em"
},
paragraph = {
before = {
skip = "smallskip"
}
}
})
end

package.documentation = [[\begin{document}
Expand Down
Loading

0 comments on commit 573666c

Please sign in to comment.