Skip to content

Commit daa8a7e

Browse files
padding shorthand for typst logo
and individual control of each side's padding
1 parent 090fbb5 commit daa8a7e

File tree

10 files changed

+149
-35
lines changed

10 files changed

+149
-35
lines changed

src/resources/filters/modules/typst.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,40 @@ local function _main()
7575
return result
7676
end
7777
end
78+
79+
local function sortedPairs(t, f)
80+
local a = {}
81+
for n in pairs(t) do table.insert(a, n) end
82+
table.sort(a, f)
83+
local i = 0 -- iterator variable
84+
local iter = function() -- iterator function
85+
i = i + 1
86+
if a[i] == nil then return nil
87+
else return a[i], t[a[i]]
88+
end
89+
end
90+
return iter
91+
end
92+
93+
local function as_typst_dictionary(tab)
94+
local entries = {}
95+
for k, v in sortedPairs(tab) do
96+
if type(v) == 'table' then
97+
v = as_typst_dictionary(v)
98+
end
99+
if k and v then
100+
table.insert(entries, k .. ': ' .. v)
101+
end
102+
end
103+
if #entries == 0 then return nil end
104+
return '(' .. table.concat(entries, ', ') .. ')'
105+
end
78106

79107
return {
80108
function_call = typst_function_call,
109+
sortedPairs = sortedPairs,
81110
as_typst_content = as_typst_content,
111+
as_typst_dictionary = as_typst_dictionary,
82112
css = require("modules/typst_css")
83113
}
84114
end

src/resources/filters/modules/typst_css.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ local typst_named_colors = {
175175
green = '#2ecc40',
176176
lime = '#01ff70',
177177
}
178+
178179
-- css can have fraction or percent
179180
-- typst can have int or percent
180181
-- what goes for opacity also goes for alpha

src/resources/filters/quarto-post/typst-brand-yaml.lua

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,16 +243,63 @@ function render_typst_brand_yaml()
243243
logoOptions.path = foundLogo.dark.path
244244
logoOptions.alt = foundLogo.dark.alt
245245
end
246-
-- todo: path relative to brand.yaml
247-
logoOptions.padding = _quarto.modules.typst.css.translate_length(logoOptions.padding or '0.5in')
246+
247+
local pads = {}
248+
for k, v in _quarto.modules.typst.sortedPairs(logoOptions) do
249+
if k == 'padding' then
250+
quarto.log.output('foo', k)
251+
local widths = {}
252+
_quarto.modules.typst.css.parse_multiple(v, 5, function(s, start)
253+
local width, newstart = _quarto.modules.typst.css.consume_width(s, start)
254+
table.insert(widths, width)
255+
return newstart
256+
end)
257+
local sides = _quarto.modules.typst.css.expand_side_shorthand(
258+
widths,
259+
'widths in padding list: ' .. v)
260+
pads.top = sides.top
261+
pads.right = sides.right
262+
pads.bottom = sides.bottom
263+
pads.left = sides.left
264+
elseif k:find '^padding-' then
265+
quarto.log.output('foo', k)
266+
local _, ndash = k:gsub('-', '')
267+
if ndash == 1 then
268+
local side = k:match('^padding--(%a+)')
269+
local padding_sides = {'left', 'top', 'right', 'bottom'}
270+
if tcontains(padding_sides, side) then
271+
pads[side] = _quarto.modules.typst.css.translate_length(v)
272+
else
273+
quarto.log.warning('invalid padding key ' .. k)
274+
end
275+
else
276+
quarto.log.warning('invalid padding key ' .. k)
277+
end
278+
end
279+
end
280+
local inset = nil
281+
if next(pads) then
282+
if pads.top == pads.right and
283+
pads.right == pads.bottom and
284+
pads.bottom == pads.left
285+
then
286+
inset = pads.top
287+
elseif pads.top == pads.bottom and pads.left == pads.right then
288+
inset = _quarto.modules.typst.as_typst_dictionary({x = pads.left, y = pads.top})
289+
else
290+
inset = _quarto.modules.typst.as_typst_dictionary(pads)
291+
end
292+
else
293+
inset = '0.5in'
294+
end
248295
logoOptions.width = _quarto.modules.typst.css.translate_length(logoOptions.width or '2in')
249296
logoOptions.location = logoOptions.location and
250297
location_to_typst_align(logoOptions.location) or 'left+top'
251298
quarto.log.debug('logo options', logoOptions)
252299
local altProp = logoOptions.alt and (', alt: "' .. logoOptions.alt .. '"') or ''
253300
local dblbackslash = string.gsub(logoOptions.path, '\\', '\\\\') -- double backslash?
254301
quarto.doc.include_text('in-header',
255-
'#set page(background: align(' .. logoOptions.location .. ', box(inset: ' .. logoOptions.padding .. ', image("' .. dblbackslash .. '", width: ' .. logoOptions.width .. altProp .. '))))')
302+
'#set page(background: align(' .. logoOptions.location .. ', box(inset: ' .. inset .. ', image("' .. dblbackslash .. '", width: ' .. logoOptions.width .. altProp .. '))))')
256303
end
257304
end
258305
end,

src/resources/filters/quarto-post/typst-css-property-processing.lua

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,6 @@ function render_typst_css_property_processing()
3232
end
3333
end
3434

35-
local function sortedPairs(t, f)
36-
local a = {}
37-
for n in pairs(t) do table.insert(a, n) end
38-
table.sort(a, f)
39-
local i = 0 -- iterator variable
40-
local iter = function() -- iterator function
41-
i = i + 1
42-
if a[i] == nil then return nil
43-
else return a[i], t[a[i]]
44-
end
45-
end
46-
return iter
47-
end
48-
4935
local function dequote(s)
5036
return s:gsub('^["\']', ''):gsub('["\']$', '')
5137
end
@@ -84,20 +70,6 @@ function render_typst_css_property_processing()
8470
return nil
8571
end
8672

87-
local function to_typst_dict(tab)
88-
local entries = {}
89-
for k, v in sortedPairs(tab) do
90-
if type(v) == 'table' then
91-
v = to_typst_dict(v)
92-
end
93-
if k and v then
94-
table.insert(entries, k .. ': ' .. v)
95-
end
96-
end
97-
if #entries == 0 then return nil end
98-
return '(' .. table.concat(entries, ', ') .. ')'
99-
end
100-
10173
local border_sides = {'left', 'top', 'right', 'bottom'}
10274
local border_properties = {'width', 'style', 'color'}
10375
local function all_equal(seq)
@@ -228,7 +200,7 @@ function render_typst_css_property_processing()
228200
-- inset seems either buggy or hard to get right, see
229201
-- https://github.com/quarto-dev/quarto-cli/pull/9387#issuecomment-2076015962
230202
-- if next(paddings) ~= nil then
231-
-- cell.attributes['typst:inset'] = to_typst_dict(paddings)
203+
-- cell.attributes['typst:inset'] = _quarto.modules.typst.as_typst_dictionary(paddings)
232204
-- end
233205

234206
-- since e.g. the left side of one cell can override the right side of another
@@ -259,9 +231,9 @@ function render_typst_css_property_processing()
259231
quarto.log.debug('paints', table.unpack(paints))
260232
if all_equal(thicknesses) and all_equal(dashes) and all_equal(paints) then
261233
assert(borders.left)
262-
cell.attributes['typst:stroke'] = to_typst_dict(borders.left)
234+
cell.attributes['typst:stroke'] = _quarto.modules.typst.as_typst_dictionary(borders.left)
263235
else
264-
cell.attributes['typst:stroke'] = to_typst_dict(borders)
236+
cell.attributes['typst:stroke'] = _quarto.modules.typst.as_typst_dictionary(borders)
265237
end
266238
end
267239
end
@@ -291,7 +263,7 @@ function render_typst_css_property_processing()
291263
hlprops.fill = 'rgb(0,0,0,0)'
292264
end
293265
return pandoc.Inlines({
294-
pandoc.RawInline('typst', '#highlight' .. to_typst_dict(hlprops) .. '['),
266+
pandoc.RawInline('typst', '#highlight' .. _quarto.modules.typst.as_typst_dictionary(hlprops) .. '['),
295267
span,
296268
pandoc.RawInline('typst', ']')
297269
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
logo:
2+
images:
3+
large-light: quarto.png
4+
large: large-light
5+
defaults:
6+
quarto:
7+
format:
8+
typst:
9+
logo:
10+
src: large
11+
location: center-top
12+
width: 300px
13+
padding: 1in
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: logo background
3+
format:
4+
typst:
5+
keep-typ: true
6+
logo:
7+
padding: 1in 2in
8+
_quarto:
9+
tests:
10+
typst:
11+
ensureTypstFileRegexMatches:
12+
-
13+
- '#set page\(background: align\(center\+top, box\(inset: \(x: 2in, y: 1\in\), image\("quarto.png", width: 225pt\)\)\)\)'
14+
- []
15+
---
16+
17+
{{< lipsum 4 >}}
18+
11.5 KB
Loading
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
logo:
2+
images:
3+
large-light: quarto.png
4+
large: large-light
5+
defaults:
6+
quarto:
7+
format:
8+
typst:
9+
logo:
10+
src: large
11+
location: center-top
12+
width: 300px
13+
padding: 1in
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: logo background
3+
format:
4+
typst:
5+
keep-typ: true
6+
logo:
7+
padding-left: 4pt
8+
padding: 9pt 2pt 3pt 17pt
9+
padding-top: 1pt
10+
_quarto:
11+
tests:
12+
typst:
13+
ensureTypstFileRegexMatches:
14+
-
15+
- '#set page\(background: align\(center\+top, box\(inset: \(bottom: 3pt, left: 4pt, right: 2pt, top: 1pt\), image\("quarto.png", width: 225pt\)\)\)\)'
16+
- []
17+
---
18+
19+
{{< lipsum 4 >}}
20+
11.5 KB
Loading

0 commit comments

Comments
 (0)