Skip to content

Commit

Permalink
refactor: Styles should not invoke functions but use AST --WIP
Browse files Browse the repository at this point in the history
For case and sub/superscript to work better recursing the AST.
  • Loading branch information
Omikhleia authored and Didier Willis committed Jul 10, 2023
1 parent 2796468 commit fca2873
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 180 deletions.
25 changes: 13 additions & 12 deletions classes/resilient/book.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ function class:_init (options)
-- Package "folio" is loaded by the plain class.
self:registerCommand("foliostyle", function (_, content)
local styleName = SILE.documentState.documentClass:oddPage() and "folio-odd" or "folio-even"
SILE.call("style:apply:paragraph", { name = styleName }, function ()
SILE.call("style:apply:paragraph", { name = styleName }, {
-- Ensure proper baseline alignment with a strut rule.
-- The baseline placement depends on the line output algorithm, and we cannot
-- trust it if it just uses the line ascenders.
-- Typically, if folios use "old-style" numbers, 16 and 17 facing pages shall have
-- aligned folios, but the 1 is smaller than the 6 and 7, the former ascends above,
-- and the latter descends below the baseline).
SILE.call("strut", { method = "rule"})
SILE.process(content)
end)
utils.createCommand("strut", { method = "rule"}),
utils.subTreeContent(content)
})
end)

-- Override the standard urlstyle hook to rely on styles
Expand Down Expand Up @@ -374,6 +374,7 @@ function class:declareSettings ()
end

function class:runningHeaderSectionReference (options, content)
-- FIXME aren't we re-computing the counter here? Sounds like a waste.
if SU.boolean(options.numbering, true) then
local sty = self:resolveStyle(options.style)
local numsty = sty.sectioning and sty.sectioning.numberstyle
Expand All @@ -400,10 +401,10 @@ function class:registerCommands ()
local closure = SILE.settings:wrap()
SILE.scratch.headers.even = function ()
closure(function ()
SILE.call("style:apply:paragraph", { name = "header-even" }, function ()
SILE.call("strut", { method = "rule"})
SILE.process(content)
end)
SILE.call("style:apply:paragraph", { name = "header-even" }, {
utils.createCommand("strut", { method = "rule"}),
content
})
end)
end
end, "Text to appear on the top of the even page(s).")
Expand All @@ -412,10 +413,10 @@ function class:registerCommands ()
local closure = SILE.settings:wrap()
SILE.scratch.headers.odd = function ()
closure(function ()
SILE.call("style:apply:paragraph", { name = "header-odd" }, function ()
SILE.call("strut", { method = "rule"})
SILE.process(content)
end)
SILE.call("style:apply:paragraph", { name = "header-odd" }, {
utils.createCommand("strut", { method = "rule"}),
content
})
end)
end
end, "Text to appear on the top of the odd page(s).")
Expand Down
133 changes: 60 additions & 73 deletions classes/resilient/resume.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local class = pl.class(base)
class._name = "resilient.resume"

local utils = require("resilient.utils")
local C = utils.createStructuredCommand

SILE.scratch.resilient = SILE.scratch.resilient or {}
SILE.scratch.resilient.resume = SILE.scratch.resilient.resume or {}
Expand Down Expand Up @@ -117,12 +118,12 @@ function class:_init (options)
self:registerCommand("foliostyle", function (_, content)
SILE.call("hbox", {}, {}) -- for vfill to be effective
SILE.call("vfill")
SILE.call("rightalign", {}, function()
SILE.process(content)
SILE.typesetter:typeset("/")
SILE.call("pageref", { marker = "resilient.resume:end" })
end)
SILE.call("eject") -- for vfill to be effective
SILE.call("rightalign", {}, {
content,
"/",
C("pageref", { marker = "resilient.resume:end" })
})
SILE.call("eject") -- for vfill to be effective
end)

-- override default document.parindent, we do not want it.
Expand Down Expand Up @@ -230,27 +231,23 @@ end

-- RESUME PROCESSING

local C = utils.createStructuredCommand

local function doEntry (rows, _, content)
local topic = utils.extractFromTree(content, "topic")
local description = utils.extractFromTree(content, "description")
local titleRow = C("row", { }, {
C("cell", { valign = "top", padding = "4pt 4pt 0 4pt" }, { function ()
SILE.call("style:apply:paragraph", { name = "resume-topic" }, function ()
-- We are typesetting in a different style but want proper alignment
-- With the other style, so strut tweaking:
SILE.call("style:apply", { name = "resume-description" }, function ()
SILE.call("strut")
end)
-- Then go ahead.
SILE.process(topic)
end)
end
local titleRow = C("row", {}, {
C("cell", { valign = "top", padding = "4pt 4pt 0 4pt" }, {
-- We are typesetting in a different style but want proper alignment
-- With the other style, so strut tweaking:
C("style:apply:paragraph", { name = "resume-topic" }, {
C("style:apply", { name = "resume-description" }, {
C("strut"),
}),
-- Then go ahead.
utils.subTreeContent(topic)
})
}),
C("cell", { valign = "top", span = 2, padding = "4pt 4pt 0.33cm 0" }, { function ()
SILE.call("style:apply", { name = "resume-description" }, description)
end
C("cell", { valign = "top", span = 2, padding = "4pt 4pt 0.33cm 0" }, {
C("style:apply", { name = "resume-description" }, description)
})
})
for i = 0, #content do
Expand All @@ -263,16 +260,14 @@ end

local doSection = function (rows, _, content)
local title = utils.extractFromTree(content, "title")
local titleRow = C("row", { }, {
C("cell", { valign = "bottom", padding = "4pt 4pt 0 4pt" }, { function ()
SILE.call("style:apply", { name = "resume-section" }, function ()
SILE.call("hrule", { width = "100%fw", height= "1ex" })
end)
end
local titleRow = C("row", {}, {
C("cell", { valign = "bottom", padding = "4pt 4pt 0 4pt" }, {
C("style:apply", { name = "resume-section" }, {
C("hrule", { width = "100%fw", height= "1ex" })
})
}),
C("cell", { span = 2, padding = "4pt 4pt 0.33cm 0" }, { function ()
SILE.call("style:apply", { name = "resume-section" }, title)
end
C("cell", { span = 2, padding = "4pt 4pt 0.33cm 0" }, {
C("style:apply", { name = "resume-section" }, title)
})
})
table.insert(rows, titleRow)
Expand Down Expand Up @@ -304,16 +299,14 @@ function class:registerCommands ()
local jobtitle = utils.extractFromTree(content, "jobtitle") or SU.error("jobtitle is mandatory")
local headline = utils.extractFromTree(content, "headline") -- can be omitted

SILE.call("cv-footer", {}, function()
SILE.process({ contact })
end)
SILE.call("cv-header", {}, function ()
SILE.call("style:apply:paragraph", { name = "resume-header" }, function ()
SILE.call("style:apply", { name = "resume-firstname" }, firstname)
SILE.typesetter:typeset(" ")
SILE.call("style:apply", { name = "resume-lastname" }, lastname)
end)
end)
SILE.call("cv-footer", {}, { contact })
SILE.call("cv-header", {}, {
C("style:apply:paragraph", { name = "resume-header" }, {
C("style:apply", { name = "resume-firstname" }, { utils.subTreeContent(firstname) }),
" ",
C("style:apply", { name = "resume-lastname" }, { utils.subTreeContent(lastname) })
})
})

local rows = {}

Expand All @@ -325,28 +318,26 @@ function class:registerCommands ()
end)
end
}),
C("cell", { span = 2, border = "0 1pt 0 0", padding = "4pt 2pt 4pt 0", valign = "bottom" }, { function ()
SILE.call("style:apply:paragraph", { name = "resume-fullname" }, function ()
SILE.call("style:apply", { name = "resume-firstname" }, firstname)
SILE.typesetter:typeset(" ")
SILE.call("style:apply", { name = "resume-lastname" }, lastname)
end)
end
C("cell", { span = 2, border = "0 1pt 0 0", padding = "4pt 2pt 4pt 0", valign = "bottom" }, {
C("style:apply:paragraph", { name = "resume-fullname" }, {
C("style:apply", { name = "resume-firstname" }, { utils.subTreeContent(firstname) }),
" ",
C("style:apply", { name = "resume-lastname" }, { utils.subTreeContent(lastname) })
})
})
})
table.insert(rows, fullnameAndPictureRow)

local jobtitleRow = C("row", { }, {
C("cell", { span = 3 }, { function ()
SILE.call("style:apply:paragraph", { name = "resume-jobtitle" }, jobtitle)
end
local jobtitleRow = C("row", {}, {
C("cell", { span = 3 }, {
C("style:apply:paragraph", { name = "resume-jobtitle" }, jobtitle)
})
})
table.insert(rows, jobtitleRow)

-- NOTE: if headline is absent, no problem. We still insert a row, just for
-- vertical spacing.
local headlineRow = C("row", { }, {
local headlineRow = C("row", {}, {
C("cell", { span = 3 }, { function ()
SILE.call("center", {}, function ()
SILE.call("parbox", { width = "80%fw" }, function()
Expand Down Expand Up @@ -390,16 +381,12 @@ function class:registerCommands ()
self:registerCommand("ranking", function (options, _)
local value = SU.cast("integer", options.value or 0)
local scale = SU.cast("integer", options.scale or 5)
SILE.call("style:apply", { name = "resume-dingbats" }, function ()
for _ = 1, value do
SILE.typesetter:typeset(charFromUnicode("U+25CF"))
SILE.call("kern", { width = "0.1em" })
end
for _ = value + 1, scale do
SILE.typesetter:typeset(charFromUnicode("U+25CB"))
SILE.call("kern", { width = "0.1em" })
end
end)
local rank = {}
for i = 1, scale do
rank[#rank + 1] = i <= value and charFromUnicode("U+25CF") or charFromUnicode("U+25CB")
rank[#rank + 1] = utils.createCommand("kern", { width = "0.1em" })
end
SILE.call("style:apply", { name = "resume-dingbats" }, rank)
end)

self:registerCommand("cv-bullet", function (_, _)
Expand All @@ -419,15 +406,15 @@ function class:registerCommands ()
local phone = SILE.inputter:findInTree(content, "phone") or SU.error("phone is mandatory")
local email = SILE.inputter:findInTree(content, "email") or SU.error("email is mandatory")

SILE.call("style:apply:paragraph", { name = "resume-contact" }, function ()
SILE.call("cv-icon-text", { symbol="U+1F4CD" }, street)
SILE.call("cv-bullet")
SILE.process(city)
SILE.call("par")
SILE.process({ phone })
SILE.call("cv-bullet")
SILE.process({ email })
end)
SILE.call("style:apply:paragraph", { name = "resume-contact" }, {
C("cv-icon-text", { symbol="U+1F4CD" }, { utils.subTreeContent(street) }),
C("cv-bullet"),
utils.subTreeContent(city),
C("par"),
phone,
C("cv-bullet"),
email
})
end)

self:registerCommand("cv-icon-text", function (options, content)
Expand Down
12 changes: 3 additions & 9 deletions packages/resilient/abbr/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,10 @@ function package:registerCommands ()
SILE.process(content)
if SU.boolean(options.sq) then
-- Latin sequiturque ("and next page")
SILE.call("font", { style = "italic", language = "und" }, function ()
SILE.typesetter:typeset(" sq.")
end)
SILE.call("font", { style = "italic", language = "und" }, { " sq." })
elseif SU.boolean(options.sqq) then
-- Latin sequiturque, plural ("and following pages")
SILE.call("font", { style = "italic", language = "und" }, function ()
SILE.typesetter:typeset(" sqq.")
end)
SILE.call("font", { style = "italic", language = "und" }, { " sqq." })
elseif SU.boolean(options.suiv) then
-- French ("et suivant") for those finding the latin sequiturque pedantic
-- as Lacroux in his Orthotypographie book..
Expand All @@ -108,9 +104,7 @@ function package:registerCommands ()
elseif century:match("^[ivx]+$") == nil then
SU.error("Not a valid century '"..century.. "' in abbr:siecle")
end
SILE.call("font", { features = "+smcp" }, function ()
SILE.typesetter:typeset(century)
end)
SILE.call("font", { features = "+smcp" }, { century })
if century == "i" then
SILE.call("textsuperscript", {}, { "er" })
else
Expand Down
2 changes: 1 addition & 1 deletion packages/resilient/epigraph/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function package:registerCommands ()
-- HACK. Oh my. When left-aligned, the rule is seen as longer than The
-- line and a new line is inserted. Tweaking it by 0.05pt seems to avoid
-- it. Rounding issue somewhere? I feel tired.
SILE.call("hrule", {width = epigraphw - 0.05, height = rule })
SILE.call("hrule", { width = epigraphw - 0.05, height = rule })
end)
end
if source then
Expand Down
4 changes: 3 additions & 1 deletion packages/resilient/poetry/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ function package:registerCommands ()

self:registerCommand("resilient.poetry:prosody", function (options, content)
local prosodyBox
local vadjust
local vadjust -- FIXME whhy in style without absolute?

SILE.call("style:apply", { name = "poetry-prosody" }, function ()
-- FIXME bad style design
vadjust = options.lower or SILE.measurement()
prosodyBox = SILE.call("hbox", {}, function()
SILE.typesetter:typeset(options.name)
Expand Down Expand Up @@ -235,6 +236,7 @@ function package:registerCommands ()
local setback = SILE.length("1.75em"):absolute()
SILE.settings:temporarily(function ()
SILE.call("style:apply", { name = "poetry-verseno"}, function ()
-- FIXME: bad style and hbox design
local w = SILE.length("6em"):absolute()

local h = SILE.call("hbox", {}, { mark })
Expand Down
Loading

0 comments on commit fca2873

Please sign in to comment.