Skip to content

Commit da02c68

Browse files
reflect dark brand in lua metadata
both light and dark brand available to Lua filters lua brand functions now take `light` or `dark` as first parameter this is a "pure" refactor, no new features
1 parent deea869 commit da02c68

File tree

12 files changed

+67
-53
lines changed

12 files changed

+67
-53
lines changed

src/command/render/render-contexts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ async function resolveFormats(
614614

615615
// resolve brand in project and forward it to format
616616
const brand = await project.resolveBrand(target.source);
617-
mergedFormats[format].render.brand = brand?.light;
617+
mergedFormats[format].render.brand = brand;
618618

619619
// apply defaults from brand yaml under the metadata of the current format
620620
const brandFormatDefaults: Metadata =

src/config/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ export const kSelfContainedMath = "self-contained-math";
110110
export const kBiblioConfig = "biblio-config";
111111
export const kBodyClasses = "body-classes";
112112
export const kBrand = "brand";
113+
export const kLight = "light";
114+
export const kDark = "dark";
113115

114116
export const kLatexAutoMk = "latex-auto-mk";
115117
export const kLatexAutoInstall = "latex-auto-install";

src/config/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import {
5858
kCrossrefThmTitle,
5959
kCss,
6060
kCssPropertyProcessing,
61+
kDark,
6162
kDfPrint,
6263
kDisplayName,
6364
kDownloadUrl,
@@ -123,6 +124,7 @@ import {
123124
kLatexTlmgrOpts,
124125
kLaunchBinderTitle,
125126
kLaunchDevContainerTitle,
127+
kLight,
126128
kLinkExternalFilter,
127129
kLinkExternalIcon,
128130
kLinkExternalNewwindow,
@@ -453,6 +455,11 @@ export interface Format {
453455
extensions?: Record<string, unknown>;
454456
}
455457

458+
export interface LightDarkBrand {
459+
[kLight]?: Brand;
460+
[kDark]?: Brand
461+
}
462+
456463
export interface FormatRender {
457464
[kKeepTex]?: boolean;
458465
[kKeepTyp]?: boolean;
@@ -514,7 +521,7 @@ export interface FormatRender {
514521
[kValidateYaml]?: boolean;
515522
[kCanonicalUrl]?: boolean | string;
516523
[kBodyClasses]?: string;
517-
[kBrand]?: Brand;
524+
[kBrand]?: LightDarkBrand;
518525
}
519526

520527
export interface FormatExecute {

src/format/reveal/format-reveal.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ export function revealjsFormat() {
375375
}
376376

377377
const determineRevealLogo = (format: Format): string | undefined => {
378-
const brandData = format.render.brand?.processedData;
378+
const brandData = format.render.brand?.light?.processedData;
379379
if (brandData?.logo) {
380380
const keys: ("medium" | "small" | "large")[] = ["medium", "small", "large"];
381381
// add slide logo if we have one
@@ -404,7 +404,7 @@ function revealMarkdownAfterBody(format: Format) {
404404
revealLogo = revealLogo.path;
405405
}
406406
if (["small", "medium", "large"].includes(revealLogo)) {
407-
const brandData = format.render.brand?.processedData;
407+
const brandData = format.render.brand?.light?.processedData;
408408
const logoInfo = brandData?.logo
409409
?.[revealLogo as ("medium" | "small" | "large")];
410410
if (typeof logoInfo === "string") {

src/resources/filters/customnodes/callout.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ function _callout_main()
252252
icon = attrs.fa_icon_typst
253253
end
254254
local brand = param("brand")
255+
local brandMode = 'light'
256+
brand = brand and brand[brandMode]
255257
if brand then
256258
local color = brand.processedData and brand.processedData.color
257259
if color and callout_theme_color_map[callout.type] and
Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,37 @@
11
-- brand.lua
22
-- Copyright (C) 2020-2024 Posit Software, PBC
33

4-
local function get_color_css(name)
4+
local function get_color_css(brandMode, name)
5+
assert(brandMode == 'light' or brandMode == 'dark')
56
local brand = param("brand")
6-
brand = brand and brand.processedData -- from src/core/brand/brand.ts
7+
brand = brand and brand[brandMode] and brand[brandMode].processedData
78
if not brand then return nil end
89
local cssColor = brand.color[name]
910
return cssColor
1011
end
1112

12-
local function get_color(name)
13-
local cssColor = get_color_css(name)
13+
local function get_color(brandMode, name)
14+
assert(brandMode == 'light' or brandMode == 'dark')
15+
local cssColor = get_color_css(brandMode, name)
1416
if not cssColor then return nil end
1517
if _quarto.format.isTypstOutput() then
1618
return _quarto.format.typst.css.output_color(_quarto.format.typst.css.parse_color(cssColor))
1719
end
1820
return cssColor
1921
end
2022

21-
local function get_background_color(name, opacity)
23+
local function get_typography(brandMode, fontName)
24+
assert(brandMode == 'light' or brandMode == 'dark')
2225
local brand = param("brand")
23-
brand = brand and brand.processedData -- from src/core/brand/brand.ts
24-
if not brand then return nil end
25-
local cssColor = brand.color[name]
26-
if not cssColor then return nil end
27-
if _quarto.format.isTypstOutput() then
28-
return _quarto.format.typst.css.output_color(_quarto.modules.typst.css.parse_color(cssColor), {unit = 'fraction', value = opacity})
29-
end
30-
-- todo: implement for html if useful
31-
return cssColor
32-
end
33-
34-
local function get_typography(fontName)
35-
local brand = param("brand")
36-
brand = brand and brand.processedData -- from src/core/brand/brand.ts
26+
brand = brand and brand[brandMode] and brand[brandMode].processedData
3727
if not brand then return nil end
3828
local typography = brand.typography and brand.typography[fontName]
3929
if not typography then return nil end
4030
local typsted = {}
4131
if type(typography) == 'string' then typography = {family = typography} end
4232
for k, v in pairs(typography) do
4333
if k == 'color' or k == 'background-color' then
44-
typsted[k] = get_color(v) or _quarto.format.typst.css.output_color(_quarto.format.typst.css.parse_color(v))
34+
typsted[k] = get_color(brandMode, v) or _quarto.format.typst.css.output_color(_quarto.format.typst.css.parse_color(v))
4535
elseif k == 'size' then
4636
local length = _quarto.format.typst.css.parse_length(v)
4737
if length and fontName == 'base' and length.unit == 'rem' then
@@ -58,17 +48,17 @@ local function get_typography(fontName)
5848
return typsted
5949
end
6050

61-
local function get_logo(name)
51+
local function get_logo(brandMode, name)
52+
assert(brandMode == 'light' or brandMode == 'dark')
6253
local brand = param("brand")
63-
brand = brand and brand.processedData -- from src/core/brand/brand.ts
54+
brand = brand and brand[brandMode] and brand[brandMode].processedData
6455
if not brand then return nil end
6556
return brand.logo and (brand.logo[name] or brand.logo.images[name])
6657
end
6758

6859
return {
6960
get_color_css = get_color_css,
7061
get_color = get_color,
71-
get_background_color = get_background_color,
7262
get_typography = get_typography,
7363
get_logo = get_logo,
7464
}

src/resources/filters/modules/typst_css.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ local typst_named_colors = {
176176
lime = '#01ff70',
177177
}
178178

179+
local brandMode = 'light' --- ugh
180+
179181
-- css can have fraction or percent
180182
-- typst can have int or percent
181183
-- what goes for opacity also goes for alpha
@@ -346,7 +348,7 @@ local function output_color(color, opacity, warnings)
346348
end
347349
color = parse_color(typst_named_colors[color.value] or css_named_colors[color.value])
348350
elseif color.type == 'brand' then
349-
local cssColor = _quarto.modules.brand.get_color_css(color.value)
351+
local cssColor = _quarto.modules.brand.get_color_css(brandMode, color.value)
350352
if not cssColor then
351353
output_warning(warnings, 'unknown brand color ' .. color.value)
352354
return nil
@@ -384,7 +386,7 @@ local function output_color(color, opacity, warnings)
384386
return nil
385387
end
386388
elseif color.type == 'brand' then
387-
local cssColor = _quarto.modules.brand.get_color_css(color.value)
389+
local cssColor = _quarto.modules.brand.get_color_css(brandMode, color.value)
388390
if not cssColor then
389391
output_warning(warnings, 'unknown brand color ' .. color.value)
390392
return nil
@@ -488,7 +490,7 @@ local css_lengths = {
488490
mm = passthrough,
489491
em = passthrough,
490492
rem = function(val, _, _, warnings)
491-
local base = _quarto.modules.brand.get_typography('base')
493+
local base = _quarto.modules.brand.get_typography(brandMode, 'base')
492494
if base and base.size then
493495
local base_size = parse_length(base.size)
494496
if not base_size then

src/resources/filters/quarto-post/dashboard.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -746,10 +746,10 @@ function render_dashboard()
746746
local which
747747
if pandoc.utils.type(logo) == 'Inlines' then
748748
which = logo[1].text
749-
local brandLogo = _quarto.modules.brand.get_logo(logo[1].text)
749+
local brandLogo = _quarto.modules.brand.get_logo('light', logo[1].text)
750750
resolved = brandLogo and brandLogo.light
751751
elseif type(logo) == 'table' then
752-
local brandLogo = _quarto.modules.brand.get_logo(logo.path[1].text)
752+
local brandLogo = _quarto.modules.brand.get_logo('light', logo.path[1].text)
753753
if brandLogo then
754754
resolved = brandLogo.light
755755
if logo.alt then
@@ -763,9 +763,9 @@ function render_dashboard()
763763
end
764764
end
765765
else
766-
logo = _quarto.modules.brand.get_logo('small')
767-
or _quarto.modules.brand.get_logo('medium')
768-
or _quarto.modules.brand.get_logo('large')
766+
logo = _quarto.modules.brand.get_logo('light', 'small')
767+
or _quarto.modules.brand.get_logo('light', 'medium')
768+
or _quarto.modules.brand.get_logo('light', 'large')
769769
resolved = logo and logo.light
770770
end
771771
if resolved then

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ function render_typst_brand_yaml()
5959
return {
6060
Pandoc = function(pandoc0)
6161
local brand = param('brand')
62-
local raw_block_shown = false
62+
local brandMode = 'light'
63+
brand = brand and brand[brandMode]
6364
if brand and brand.processedData then
6465
-- color
6566
if brand.processedData.color and next(brand.processedData.color) then
6667
local brandColor = brand.processedData.color
6768
local colors = {}
6869
for name, _ in pairs(brandColor) do
69-
colors[name] = _quarto.modules.brand.get_color(name)
70+
colors[name] = _quarto.modules.brand.get_color(brandMode, name)
7071
end
7172
local decl = '#let brand-color = ' .. to_typst_dict_indent(colors)
7273
quarto.doc.include_text('in-header', decl)
@@ -93,7 +94,7 @@ function render_typst_brand_yaml()
9394
return key .. ': ' .. value .. ', '
9495
end
9596
-- typography
96-
local base = _quarto.modules.brand.get_typography('base')
97+
local base = _quarto.modules.brand.get_typography(brandMode, 'base')
9798
if base and next(base) then
9899
quarto.doc.include_text('in-header', table.concat({
99100
'#set text(',
@@ -112,7 +113,7 @@ function render_typst_brand_yaml()
112113
end
113114
end
114115

115-
local headings = _quarto.modules.brand.get_typography('headings')
116+
local headings = _quarto.modules.brand.get_typography(brandMode, 'headings')
116117
if headings and next(headings) then
117118
quarto.doc.include_text('in-header', table.concat({
118119
'#show heading: set text(',
@@ -133,7 +134,7 @@ function render_typst_brand_yaml()
133134
end
134135
end
135136

136-
local monospaceInline = _quarto.modules.brand.get_typography('monospace-inline')
137+
local monospaceInline = _quarto.modules.brand.get_typography(brandMode, 'monospace-inline')
137138
if monospaceInline and next(monospaceInline) then
138139
quarto.doc.include_text('in-header', table.concat({
139140
'#show raw.where(block: false): set text(',
@@ -152,7 +153,7 @@ function render_typst_brand_yaml()
152153
}))
153154
end
154155

155-
local monospaceBlock = _quarto.modules.brand.get_typography('monospace-block')
156+
local monospaceBlock = _quarto.modules.brand.get_typography(brandMode, 'monospace-block')
156157
if monospaceBlock and next(monospaceBlock) then
157158
quarto.doc.include_text('in-header', table.concat({
158159
'#show raw.where(block: true): set text(',
@@ -181,8 +182,8 @@ function render_typst_brand_yaml()
181182
end
182183
end
183184

184-
local link = _quarto.modules.brand.get_typography('link')
185-
local primaryColor = _quarto.modules.brand.get_color('primary')
185+
local link = _quarto.modules.brand.get_typography(brandMode, 'link')
186+
local primaryColor = _quarto.modules.brand.get_color(brandMode, 'primary')
186187
if link and next(link) or primaryColor then
187188
link = link or {}
188189
quarto.doc.include_text('in-header', table.concat({
@@ -209,21 +210,21 @@ function render_typst_brand_yaml()
209210
local foundLogo = null
210211
if logo then
211212
if type(logo) == 'string' then
212-
foundLogo = _quarto.modules.brand.get_logo(logo) or {light={path=logo}}
213+
foundLogo = _quarto.modules.brand.get_logo(brandMode, logo) or {light={path=logo}}
213214
elseif type(logo) == 'table' then
214215
for k, v in pairs(logo) do
215216
logoOptions[k] = v
216217
end
217218
if logo.path then
218-
foundLogo = _quarto.modules.brand.get_logo(logo.path) or {light={path=logo}}
219+
foundLogo = _quarto.modules.brand.get_logo(brandMode, logo.path) or {light={path=logo}}
219220
end
220221
end
221222
end
222223
if not foundLogo and brand.processedData.logo then
223224
local tries = {'large', 'small', 'medium'} -- low to high priority
224-
foundLogo = _quarto.modules.brand.get_logo('medium')
225-
or _quarto.modules.brand.get_logo('small')
226-
or _quarto.modules.brand.get_logo('large')
225+
foundLogo = _quarto.modules.brand.get_logo(brandMode, 'medium')
226+
or _quarto.modules.brand.get_logo(brandMode, 'small')
227+
or _quarto.modules.brand.get_logo(brandMode, 'large')
227228
end
228229
if foundLogo then
229230
if foundLogo.light then
@@ -299,21 +300,22 @@ function render_typst_brand_yaml()
299300
end
300301
end,
301302
Meta = function(meta)
303+
local brandMode = 'light'
302304
-- it can contain the path but we want to store an object here
303305
if not meta.brand or pandoc.utils.type(meta.brand) == 'Inlines' then
304306
meta.brand = {}
305307
end
306308
meta.brand.typography = meta.brand.typography or {}
307-
local base = _quarto.modules.brand.get_typography('base')
309+
local base = _quarto.modules.brand.get_typography(brandMode, 'base')
308310
if base and next(base) then
309311
meta.brand.typography.base = {
310312
family = base.family,
311313
size = base.size,
312314
}
313315
end
314316

315-
local headings = _quarto.modules.brand.get_typography('headings')
316-
local foregroundColor = _quarto.modules.brand.get_color('foreground')
317+
local headings = _quarto.modules.brand.get_typography(brandMode, 'headings')
318+
local foregroundColor = _quarto.modules.brand.get_color(brandMode, 'foreground')
317319
if headings and next(headings) or base and next(base) or foregroundColor then
318320
base = base or {}
319321
headings = headings or {}

src/resources/filters/quarto-pre/shortcodes-handlers.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,12 @@ function initShortcodeHandlers()
134134
end
135135

136136
if brandCommand == "color" then
137+
local brandMode = 'light'
138+
if #args > 2 then
139+
brandMode = read_arg(args, 3) or brandMode
140+
end
137141
local color_name = read_arg(args, 2)
138-
local color_value = brand.get_color(color_name)
142+
local color_value = brand.get_color(brandMode, color_name)
139143
if color_value == nil then
140144
return warn_bad_brand_command()
141145
else

0 commit comments

Comments
 (0)