Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/resources/filters/modules/brand/brand.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
-- brand.lua
-- Copyright (C) 2020-2024 Posit Software, PBC

local function get_color(name)
local function get_color_css(name)
local brand = param("brand")
brand = brand and brand.processedData -- from src/core/brand/brand.ts
if not brand then return nil end
local cssColor = brand.color[name]
return cssColor
end

local function get_color(name)
local cssColor = get_color_css(name)
if not cssColor then return nil end
if _quarto.format.isTypstOutput() then
return _quarto.format.typst.css.output_color(_quarto.format.typst.css.parse_color(cssColor))
Expand All @@ -32,10 +37,8 @@ local function get_typography(fontName)
if not brand then return nil end
local typography = brand.typography and brand.typography[fontName]
if not typography then return nil end
if type(typography) == 'string' then
typography = { family = typography }
end
local typsted = {}
if type(typography) == 'string' then typography = {family = typography} end
for k, v in pairs(typography) do
if k == 'color' or k == 'background-color' then
typsted[k] = get_color(v) or _quarto.format.typst.css.output_color(_quarto.format.typst.css.parse_color(v))
Expand Down Expand Up @@ -63,6 +66,7 @@ local function get_logo(name)
end

return {
get_color_css = get_color_css,
get_color = get_color,
get_background_color = get_background_color,
get_typography = get_typography,
Expand Down
24 changes: 24 additions & 0 deletions src/resources/filters/modules/typst_css.lua
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,16 @@ local function parse_color(color, warnings)
}
elseif color:find '^rgb%(' or color:find '^rgba%(' then
return parse_rgb(color)
elseif color:find '^var%(%-%-brand%-' then
local colorName = color:match '^var%(%-%-brand%-([%a--]*)%)'
if not colorName then
output_warning(warnings, 'invalid brand color reference ' .. v)
return null
end
return colorName and {
type = 'brand',
value = colorName
}
elseif css_named_colors[color] then
return {
type = 'named',
Expand Down Expand Up @@ -335,6 +345,13 @@ local function output_color(color, opacity, warnings)
return nil
end
color = parse_color(typst_named_colors[color.value] or css_named_colors[color.value])
elseif color.type == 'brand' then
local cssColor = _quarto.modules.brand.get_color_css(color.value)
if not cssColor then
output_warning(warnings, 'unknown brand color ' .. color.value)
return nil
end
color = _quarto.format.typst.css.parse_color(cssColor)
end
local mult = 1
if opacity.unit == 'int' then
Expand Down Expand Up @@ -366,6 +383,13 @@ local function output_color(color, opacity, warnings)
else
return nil
end
elseif color.type == 'brand' then
local cssColor = _quarto.modules.brand.get_color_css(color.value)
if not cssColor then
output_warning(warnings, 'unknown brand color ' .. color.value)
return nil
end
return 'brand-color.' .. color.value
end
end
quarto.log.debug('output_color output', color)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,12 @@ function render_typst_css_property_processing()
local k, v = to_kv(clause)
if k == 'font-family' then
div.attributes['typst:text:font'] = translate_string_list(v)
end
if k == 'font-size' then
elseif k == 'font-size' then
div.attributes['typst:text:size'] = _quarto.format.typst.css.translate_length(v, _warnings)
elseif k == 'background-color' then
div.attributes['typst:fill'] = _quarto.format.typst.css.output_color(_quarto.format.typst.css.parse_color(v, _warnings), nil, _warnings)
elseif k == 'color' then
div.attributes['typst:text:fill'] = _quarto.format.typst.css.output_color(_quarto.format.typst.css.parse_color(v, _warnings), nil, _warnings)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: Translate brand named color references from CSS to Typst
format:
typst:
keep-typ: true
brand:
color:
palette:
dark-grey: "#222"
blue: "#82aeef"
_quarto:
tests:
typst:
ensureTypstFileRegexMatches:
-
- '#block\(fill: brand-color\.dark-grey\)\[\s*#set text\(fill: brand-color\.blue\);'
- []
---


```{=typst}
// stopgap to make this look ok
#set block(inset: 6pt)
```

:::{style="background-color: var(--brand-dark-grey); color: var(--brand-blue)"}
This div is blue on dark grey.
:::

{{< lipsum 2 >}}
Loading