-
Notifications
You must be signed in to change notification settings - Fork 362
Description
From #11616 (reply in thread)_
We do not currently have any general facilities for accessing brand metadata in templates.
Given that templates are the same level of abstraction as brands, it would make sense to expose brand metadata directly to templates.
But currently you can only access the entire brand metadata from Lua or via the brand shortcode, neither of which help you with templates.
The background logo with positioning that we currently support in Typst
https://quarto.org/docs/authoring/brand.html#document-logo-customization
is implemented in this Lua filter:
quarto-cli/src/resources/filters/quarto-post/typst-brand-yaml.lua
Lines 217 to 305 in bf03850
local logo = param('logo') | |
local logoOptions = {} | |
local foundLogo = null | |
if logo then | |
if type(logo) == 'string' then | |
foundLogo = _quarto.modules.brand.get_logo(brandMode, logo) or {path=logo} | |
elseif type(logo) == 'table' then | |
for k, v in pairs(logo) do | |
logoOptions[k] = v | |
end | |
if logo.path then | |
foundLogo = _quarto.modules.brand.get_logo(brandMode, logo.path) or {path=logo} | |
end | |
end | |
end | |
if not foundLogo and brand.processedData.logo then | |
local tries = {'large', 'small', 'medium'} -- low to high priority | |
foundLogo = _quarto.modules.brand.get_logo(brandMode, 'medium') | |
or _quarto.modules.brand.get_logo(brandMode, 'small') | |
or _quarto.modules.brand.get_logo(brandMode, 'large') | |
end | |
if foundLogo then | |
logoOptions.path = foundLogo.path | |
logoOptions.alt = foundLogo.alt | |
local pads = {} | |
for k, v in _quarto.utils.table.sortedPairs(logoOptions) do | |
if k == 'padding' then | |
local widths = {} | |
_quarto.modules.typst.css.parse_multiple(v, 5, function(s, start) | |
local width, newstart = _quarto.modules.typst.css.consume_width(s, start) | |
table.insert(widths, width) | |
return newstart | |
end) | |
local sides = _quarto.modules.typst.css.expand_side_shorthand( | |
widths, | |
'widths in padding list: ' .. v) | |
pads.top = sides.top | |
pads.right = sides.right | |
pads.bottom = sides.bottom | |
pads.left = sides.left | |
elseif k:find '^padding-' then | |
local _, ndash = k:gsub('-', '') | |
if ndash == 1 then | |
local side = k:match('^padding--(%a+)') | |
local padding_sides = {'left', 'top', 'right', 'bottom'} | |
if tcontains(padding_sides, side) then | |
pads[side] = _quarto.modules.typst.css.translate_length(v) | |
else | |
quarto.log.warning('invalid padding key ' .. k) | |
end | |
else | |
quarto.log.warning('invalid padding key ' .. k) | |
end | |
end | |
end | |
local inset = nil | |
if next(pads) then | |
if pads.top == pads.right and | |
pads.right == pads.bottom and | |
pads.bottom == pads.left | |
then | |
inset = pads.top | |
elseif pads.top == pads.bottom and pads.left == pads.right then | |
inset = _quarto.modules.typst.as_typst_dictionary({x = pads.left, y = pads.top}) | |
else | |
inset = _quarto.modules.typst.as_typst_dictionary(pads) | |
end | |
else | |
inset = '0.75in' | |
end | |
logoOptions.width = _quarto.modules.typst.css.translate_length(logoOptions.width or '1.5in') | |
logoOptions.location = logoOptions.location and | |
location_to_typst_align(logoOptions.location) or 'left+top' | |
quarto.log.debug('logo options', logoOptions) | |
local altProp = logoOptions.alt and (', alt: "' .. logoOptions.alt .. '"') or '' | |
local imageFilename = logoOptions.path | |
if _quarto.modules.mediabag.should_mediabag(imageFilename) then | |
imageFilename = _quarto.modules.mediabag.resolved_url_cache[logoOptions.path] or _quarto.modules.mediabag.fetch_and_store_image(logoOptions.path) | |
imageFilename = _quarto.modules.mediabag.write_mediabag_entry(imageFilename) or imageFilename | |
else | |
-- backslashes need to be doubled for Windows | |
imageFilename = string.gsub(imageFilename, '\\', '\\\\') | |
end | |
quarto.doc.include_text('in-header', | |
'#set page(background: align(' .. logoOptions.location .. ', box(inset: ' .. inset .. ', image("' .. imageFilename .. '", width: ' .. logoOptions.width .. altProp .. '))))') | |
end | |
end | |
end, |
We have discussed separately that the logo should be implemented as a partial instead of this Lua filter directly generating the
#set page(background: ...)
Exposing brand metadata to templates would be a similar effort, but more general, because it would expose all of the brand logos to the template, not just the chosen one.
It could be done either through definition of more constants in Typst as is done for colors, or through more metadata exposed to the template as the Typst brand.typography
metadata discussed here.