Skip to content

Commit 05a4e26

Browse files
authored
Merge pull request #10362 from quarto-dev/feature/brand-yaml
`_brand.yml` - Lua API, shortcodes
2 parents f4b887b + 56e9968 commit 05a4e26

File tree

11 files changed

+130
-14
lines changed

11 files changed

+130
-14
lines changed

src/core/brand/brand.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,38 @@ export const defaultColorNames: BrandNamedThemeColor[] = [
3838
// "monospace",
3939
// ];
4040

41+
type ProcessedBrandData = {
42+
color: Record<string, string>;
43+
// typography: Record<string, ???>;
44+
// logo: Record<string, string>; // paths
45+
};
46+
4147
export class Brand {
4248
data: BrandJson;
49+
processedData: ProcessedBrandData;
4350

4451
constructor(readonly brand: BrandJson) {
4552
this.data = brand;
53+
this.processedData = this.processData(brand);
54+
}
55+
56+
processData(data: BrandJson): ProcessedBrandData {
57+
const color: Record<string, string> = {};
58+
for (const colorName of Object.keys(data.color?.with ?? {})) {
59+
color[colorName] = this.getColor(colorName);
60+
}
61+
for (const colorName of Object.keys(data.color ?? {})) {
62+
if (colorName === "with") {
63+
continue;
64+
}
65+
color[colorName] = this.getColor(colorName);
66+
}
67+
68+
return {
69+
color,
70+
// typography,
71+
// logo,
72+
};
4673
}
4774

4875
// semantics of name resolution for colors, logo and fonts are:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- brand.lua
2+
-- Copyright (C) 2020-2024 Posit Software, PBC
3+
4+
local function get_color(name)
5+
local brand = param("brand").processedData -- from src/core/brand/brand.ts
6+
return brand.color[name]
7+
end
8+
9+
return {
10+
get_color = get_color
11+
}

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

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ end
5959

6060
local handlers = {}
6161

62+
local function read_arg(args, n)
63+
local arg = args[n or 1]
64+
local varName
65+
if arg == nil then
66+
return nil
67+
end
68+
if type(arg) ~= "string" then
69+
varName = inlinesToString(arg)
70+
else
71+
varName = arg
72+
end
73+
return varName
74+
end
75+
6276
function initShortcodeHandlers()
6377

6478
-- user provided handlers
@@ -90,32 +104,35 @@ function initShortcodeHandlers()
90104
end)
91105
end
92106

107+
local function handle_brand(args)
108+
local brand = require("modules/brand/brand")
109+
local brandCommand = read_arg(args, 1)
110+
if brandCommand ~= "color" then
111+
warn("Unknown brand command " .. brandCommand .. " specified in a brand shortcode.")
112+
return { pandoc.Strong({pandoc.Str("?brand:" .. brandCommand)}) }
113+
end
114+
local brandName = read_arg(args, 2)
115+
local brandValue = brand.get_color(brandName)
116+
if brandValue ~= nil then
117+
return { pandoc.Str(brandValue) }
118+
else
119+
warn("Unknown brand " .. brandName .. " specified in a brand shortcode.")
120+
return { pandoc.Strong({pandoc.Str("?brand:" .. brandName)}) }
121+
end
122+
end
93123

94124
-- built in handlers (these override any user handlers)
95125
handlers['meta'] = { handle = handleMeta }
96126
handlers['var'] = { handle = handleVars }
97127
handlers['env'] = { handle = handleEnv }
98128
handlers['pagebreak'] = { handle = handlePagebreak }
129+
handlers['brand'] = { handle = handle_brand }
99130
end
100131

101132
function handlerForShortcode(shortCode)
102133
return handlers[shortCode.name]
103134
end
104135

105-
local function read_arg(args, n)
106-
local arg = args[n or 1]
107-
local varName
108-
if arg == nil then
109-
return nil
110-
end
111-
if type(arg) ~= "string" then
112-
varName = inlinesToString(arg)
113-
else
114-
varName = arg
115-
end
116-
return varName
117-
end
118-
119136
-- Implements reading values from envrionment variables
120137
function handleEnv(args)
121138
if #args > 0 then
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.quarto/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
color:
2+
with:
3+
red: "#ff0000"
4+
primary: red
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
project:
2+
type: website
3+
4+
website:
5+
title: "test-01"
6+
navbar:
7+
left:
8+
- href: index.qmd
9+
text: Home
10+
- about.qmd
11+
12+
format:
13+
html:
14+
css: styles.css
15+
toc: true
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: "test-01"
3+
format: dashboard
4+
---
5+
6+
This is a Quarto website.
7+
8+
To learn more about Quarto websites visit <https://quarto.org/docs/websites>.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: "test-01"
3+
filters:
4+
- test_brand.lua
5+
_quarto:
6+
tests:
7+
html:
8+
ensureFileRegexMatches:
9+
- ['<span style="color: #ff0000">a span</span>']
10+
- []
11+
---
12+
13+
This is a Quarto website.
14+
15+
[a span]{style='color: {{< brand color primary >}}'}
16+
17+
To learn more about Quarto websites visit <https://quarto.org/docs/websites>.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
format: revealjs
3+
---
4+
5+
## Slides
6+
7+
This is a Quarto presentation.
8+
9+
To learn more about Quarto websites visit <https://quarto.org/docs/websites>.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* css styles */

0 commit comments

Comments
 (0)