Skip to content

Commit 75fbfd0

Browse files
typst: add page partial
move page stuff out of article() function move logo stuff out of typst-brand-yaml.lua
1 parent 9cfe15f commit 75fbfd0

File tree

6 files changed

+97
-100
lines changed

6 files changed

+97
-100
lines changed

src/format/typst/format-typst.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export function typstFormat(): Format {
9292
partials: [
9393
"definitions.typ",
9494
"typst-template.typ",
95+
"page.typ",
9596
"typst-show.typ",
9697
"notes.typ",
9798
"biblio.typ",

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

Lines changed: 86 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -239,102 +239,103 @@ function render_typst_brand_yaml()
239239
', content)'
240240
}))
241241
end
242-
243-
-- logo
244-
local logo = param('logo')
245-
local logoOptions = {}
246-
local foundLogo = null
247-
if logo then
248-
if type(logo) == 'string' then
249-
foundLogo = _quarto.modules.brand.get_logo(brandMode, logo) or {path=logo}
250-
elseif type(logo) == 'table' then
251-
for k, v in pairs(logo) do
252-
logoOptions[k] = v
253-
end
254-
if logo.path then
255-
foundLogo = _quarto.modules.brand.get_logo(brandMode, logo.path) or {path=logo}
256-
end
242+
end
243+
end,
244+
Meta = function(meta)
245+
local brand = param('brand')
246+
local brandMode = param('brand-mode') or 'light'
247+
brand = brand and brand[brandMode]
248+
-- it can contain the path but we want to store an object here
249+
if not meta.brand or pandoc.utils.type(meta.brand) == 'Inlines' then
250+
meta.brand = {}
251+
end
252+
-- logo
253+
local logo = param('logo')
254+
local logoOptions = {}
255+
local foundLogo = null
256+
if logo then
257+
if type(logo) == 'string' then
258+
foundLogo = _quarto.modules.brand.get_logo(brandMode, logo) or {path=logo}
259+
elseif type(logo) == 'table' then
260+
for k, v in pairs(logo) do
261+
logoOptions[k] = v
262+
end
263+
if logo.path then
264+
foundLogo = _quarto.modules.brand.get_logo(brandMode, logo.path) or {path=logo}
257265
end
258266
end
259-
if not foundLogo and brand.processedData.logo then
260-
local tries = {'large', 'small', 'medium'} -- low to high priority
261-
foundLogo = _quarto.modules.brand.get_logo(brandMode, 'medium')
262-
or _quarto.modules.brand.get_logo(brandMode, 'small')
263-
or _quarto.modules.brand.get_logo(brandMode, 'large')
264-
end
265-
if foundLogo then
266-
logoOptions.path = foundLogo.path
267-
logoOptions.alt = foundLogo.alt
267+
end
268+
if not foundLogo and brand and brand.processedData and brand.processedData.logo then
269+
foundLogo = _quarto.modules.brand.get_logo(brandMode, 'medium')
270+
or _quarto.modules.brand.get_logo(brandMode, 'small')
271+
or _quarto.modules.brand.get_logo(brandMode, 'large')
272+
end
273+
if foundLogo then
274+
logoOptions.path = foundLogo.path
275+
logoOptions.alt = foundLogo.alt
268276

269-
local pads = {}
270-
for k, v in _quarto.utils.table.sortedPairs(logoOptions) do
271-
if k == 'padding' then
272-
local widths = {}
273-
_quarto.modules.typst.css.parse_multiple(v, 5, function(s, start)
274-
local width, newstart = _quarto.modules.typst.css.consume_width(s, start)
275-
table.insert(widths, width)
276-
return newstart
277-
end)
278-
local sides = _quarto.modules.typst.css.expand_side_shorthand(
279-
widths,
280-
'widths in padding list: ' .. v)
281-
pads.top = sides.top
282-
pads.right = sides.right
283-
pads.bottom = sides.bottom
284-
pads.left = sides.left
285-
elseif k:find '^padding-' then
286-
local _, ndash = k:gsub('-', '')
287-
if ndash == 1 then
288-
local side = k:match('^padding--(%a+)')
289-
local padding_sides = {'left', 'top', 'right', 'bottom'}
290-
if tcontains(padding_sides, side) then
291-
pads[side] = _quarto.modules.typst.css.translate_length(v)
292-
else
293-
quarto.log.warning('invalid padding key ' .. k)
294-
end
277+
local pads = {}
278+
for k, v in _quarto.utils.table.sortedPairs(logoOptions) do
279+
if k == 'padding' then
280+
local widths = {}
281+
_quarto.modules.typst.css.parse_multiple(v, 5, function(s, start)
282+
local width, newstart = _quarto.modules.typst.css.consume_width(s, start)
283+
table.insert(widths, width)
284+
return newstart
285+
end)
286+
local sides = _quarto.modules.typst.css.expand_side_shorthand(
287+
widths,
288+
'widths in padding list: ' .. v)
289+
pads.top = sides.top
290+
pads.right = sides.right
291+
pads.bottom = sides.bottom
292+
pads.left = sides.left
293+
elseif k:find '^padding-' then
294+
local _, ndash = k:gsub('-', '')
295+
if ndash == 1 then
296+
local side = k:match('^padding--(%a+)')
297+
local padding_sides = {'left', 'top', 'right', 'bottom'}
298+
if tcontains(padding_sides, side) then
299+
pads[side] = _quarto.modules.typst.css.translate_length(v)
295300
else
296301
quarto.log.warning('invalid padding key ' .. k)
297302
end
298-
end
299-
end
300-
local inset = nil
301-
if next(pads) then
302-
if pads.top == pads.right and
303-
pads.right == pads.bottom and
304-
pads.bottom == pads.left
305-
then
306-
inset = pads.top
307-
elseif pads.top == pads.bottom and pads.left == pads.right then
308-
inset = _quarto.modules.typst.as_typst_dictionary({x = pads.left, y = pads.top})
309303
else
310-
inset = _quarto.modules.typst.as_typst_dictionary(pads)
304+
quarto.log.warning('invalid padding key ' .. k)
311305
end
312-
else
313-
inset = '0.75in'
314306
end
315-
logoOptions.width = _quarto.modules.typst.css.translate_length(logoOptions.width or '1.5in')
316-
logoOptions.location = logoOptions.location and
317-
location_to_typst_align(logoOptions.location) or 'left+top'
318-
quarto.log.debug('logo options', logoOptions)
319-
local altProp = logoOptions.alt and (', alt: "' .. logoOptions.alt .. '"') or ''
320-
local imageFilename = logoOptions.path
321-
if _quarto.modules.mediabag.should_mediabag(imageFilename) then
322-
imageFilename = _quarto.modules.mediabag.resolved_url_cache[logoOptions.path] or _quarto.modules.mediabag.fetch_and_store_image(logoOptions.path)
323-
imageFilename = _quarto.modules.mediabag.write_mediabag_entry(imageFilename) or imageFilename
307+
end
308+
local inset = nil
309+
if next(pads) then
310+
if pads.top == pads.right and
311+
pads.right == pads.bottom and
312+
pads.bottom == pads.left
313+
then
314+
inset = pads.top
315+
elseif pads.top == pads.bottom and pads.left == pads.right then
316+
inset = _quarto.modules.typst.as_typst_dictionary({x = pads.left, y = pads.top})
324317
else
325-
-- backslashes need to be doubled for Windows
326-
imageFilename = string.gsub(imageFilename, '\\', '\\\\')
318+
inset = _quarto.modules.typst.as_typst_dictionary(pads)
327319
end
328-
quarto.doc.include_text('in-header',
329-
'#set page(background: align(' .. logoOptions.location .. ', box(inset: ' .. inset .. ', image("' .. imageFilename .. '", width: ' .. logoOptions.width .. altProp .. '))))')
330-
end
331-
end
332-
end,
333-
Meta = function(meta)
334-
local brandMode = param('brand-mode') or 'light'
335-
-- it can contain the path but we want to store an object here
336-
if not meta.brand or pandoc.utils.type(meta.brand) == 'Inlines' then
337-
meta.brand = {}
320+
else
321+
inset = '0.75in'
322+
end
323+
logoOptions.width = _quarto.modules.typst.css.translate_length(logoOptions.width or '1.5in')
324+
logoOptions.inset = inset
325+
logoOptions.location = logoOptions.location and
326+
location_to_typst_align(logoOptions.location) or 'left+top'
327+
quarto.log.debug('logo options', logoOptions)
328+
local imageFilename = logoOptions.path
329+
if _quarto.modules.mediabag.should_mediabag(imageFilename) then
330+
imageFilename = _quarto.modules.mediabag.resolved_url_cache[logoOptions.path] or _quarto.modules.mediabag.fetch_and_store_image(logoOptions.path)
331+
imageFilename = _quarto.modules.mediabag.write_mediabag_entry(imageFilename) or imageFilename
332+
imageFilename = imageFilename and imageFilename:gsub('\\_', '_')
333+
else
334+
-- backslashes need to be doubled for Windows
335+
imageFilename = string.gsub(imageFilename, '\\', '\\\\')
336+
end
337+
logoOptions.path = pandoc.RawInline('typst', imageFilename)
338+
meta.logo = logoOptions
338339
end
339340
meta.brand.typography = meta.brand.typography or {}
340341
local base = _quarto.modules.brand.get_typography(brandMode, 'base')
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#set page(
2+
paper: $if(papersize)$"$papersize$"$else$"us-letter"$endif$,
3+
margin: $if(margin)$($for(margin/pairs)$$margin.key$: $margin.value$,$endfor$)$else$(x: 1.25in, y: 1.25in)$endif$,
4+
numbering: $if(page-numbering)$"$page-numbering$"$else$none$endif$,
5+
)
6+
$if(logo)$
7+
#set page(background: align($logo.location$, box(inset: $logo.inset$, image("$logo.path$", width: $logo.width$$if(logo.alt)$, alt: "$logo.alt$"$endif$))))
8+
$endif$

src/resources/formats/typst/pandoc/quarto/template.typ

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ $for(header-includes)$
66
$header-includes$
77
$endfor$
88

9+
$page.typ()$
10+
911
$typst-show.typ()$
1012

1113
$for(include-before)$

src/resources/formats/typst/pandoc/quarto/typst-show.typ

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ $if(abstract)$
2929
abstract: [$abstract$],
3030
abstract-title: "$labels.abstract$",
3131
$endif$
32-
$if(margin)$
33-
margin: ($for(margin/pairs)$$margin.key$: $margin.value$,$endfor$),
34-
$endif$
35-
$if(papersize)$
36-
paper: "$papersize$",
37-
$endif$
3832
$if(mainfont)$
3933
font: ("$mainfont$",),
4034
$elseif(brand.typography.base.family)$
@@ -65,7 +59,6 @@ $endif$
6559
$if(section-numbering)$
6660
sectionnumbering: "$section-numbering$",
6761
$endif$
68-
pagenumbering: $if(page-numbering)$"$page-numbering$"$else$none$endif$,
6962
$if(toc)$
7063
toc: $toc$,
7164
$endif$

src/resources/formats/typst/pandoc/quarto/typst-template.typ

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
abstract: none,
99
abstract-title: none,
1010
cols: 1,
11-
margin: (x: 1.25in, y: 1.25in),
12-
paper: "us-letter",
1311
lang: "en",
1412
region: "US",
1513
font: "libertinus serif",
@@ -22,18 +20,12 @@
2220
heading-color: black,
2321
heading-line-height: 0.65em,
2422
sectionnumbering: none,
25-
pagenumbering: "1",
2623
toc: false,
2724
toc_title: none,
2825
toc_depth: none,
2926
toc_indent: 1.5em,
3027
doc,
3128
) = {
32-
set page(
33-
paper: paper,
34-
margin: margin,
35-
numbering: pagenumbering,
36-
)
3729
set par(justify: true)
3830
set text(lang: lang,
3931
region: region,

0 commit comments

Comments
 (0)