Skip to content

_brand.yml - Lua API, shortcodes #10362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 23, 2024
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
27 changes: 27 additions & 0 deletions src/core/brand/brand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,38 @@ export const defaultColorNames: BrandNamedThemeColor[] = [
// "monospace",
// ];

type ProcessedBrandData = {
color: Record<string, string>;
// typography: Record<string, ???>;
// logo: Record<string, string>; // paths
};

export class Brand {
data: BrandJson;
processedData: ProcessedBrandData;

constructor(readonly brand: BrandJson) {
this.data = brand;
this.processedData = this.processData(brand);
}

processData(data: BrandJson): ProcessedBrandData {
const color: Record<string, string> = {};
for (const colorName of Object.keys(data.color?.with ?? {})) {
color[colorName] = this.getColor(colorName);
}
for (const colorName of Object.keys(data.color ?? {})) {
if (colorName === "with") {
continue;
}
color[colorName] = this.getColor(colorName);
}

return {
color,
// typography,
// logo,
};
}

// semantics of name resolution for colors, logo and fonts are:
Expand Down
11 changes: 11 additions & 0 deletions src/resources/filters/modules/brand/brand.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- brand.lua
-- Copyright (C) 2020-2024 Posit Software, PBC

local function get_color(name)
local brand = param("brand").processedData -- from src/core/brand/brand.ts
return brand.color[name]
end

return {
get_color = get_color
}
45 changes: 31 additions & 14 deletions src/resources/filters/quarto-pre/shortcodes-handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ end

local handlers = {}

local function read_arg(args, n)
local arg = args[n or 1]
local varName
if arg == nil then
return nil
end
if type(arg) ~= "string" then
varName = inlinesToString(arg)
else
varName = arg
end
return varName
end

function initShortcodeHandlers()

-- user provided handlers
Expand Down Expand Up @@ -90,32 +104,35 @@ function initShortcodeHandlers()
end)
end

local function handle_brand(args)
local brand = require("modules/brand/brand")
local brandCommand = read_arg(args, 1)
if brandCommand ~= "color" then
warn("Unknown brand command " .. brandCommand .. " specified in a brand shortcode.")
return { pandoc.Strong({pandoc.Str("?brand:" .. brandCommand)}) }
end
local brandName = read_arg(args, 2)
local brandValue = brand.get_color(brandName)
if brandValue ~= nil then
return { pandoc.Str(brandValue) }
else
warn("Unknown brand " .. brandName .. " specified in a brand shortcode.")
return { pandoc.Strong({pandoc.Str("?brand:" .. brandName)}) }
end
end

-- built in handlers (these override any user handlers)
handlers['meta'] = { handle = handleMeta }
handlers['var'] = { handle = handleVars }
handlers['env'] = { handle = handleEnv }
handlers['pagebreak'] = { handle = handlePagebreak }
handlers['brand'] = { handle = handle_brand }
end

function handlerForShortcode(shortCode)
return handlers[shortCode.name]
end

local function read_arg(args, n)
local arg = args[n or 1]
local varName
if arg == nil then
return nil
end
if type(arg) ~= "string" then
varName = inlinesToString(arg)
else
varName = arg
end
return varName
end

-- Implements reading values from envrionment variables
function handleEnv(args)
if #args > 0 then
Expand Down
1 change: 1 addition & 0 deletions tests/docs/smoke-all/brand/test-01/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.quarto/
4 changes: 4 additions & 0 deletions tests/docs/smoke-all/brand/test-01/_brand.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
color:
with:
red: "#ff0000"
primary: red
15 changes: 15 additions & 0 deletions tests/docs/smoke-all/brand/test-01/_quarto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
project:
type: website

website:
title: "test-01"
navbar:
left:
- href: index.qmd
text: Home
- about.qmd

format:
html:
css: styles.css
toc: true
8 changes: 8 additions & 0 deletions tests/docs/smoke-all/brand/test-01/dashboard.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: "test-01"
format: dashboard
---

This is a Quarto website.

To learn more about Quarto websites visit <https://quarto.org/docs/websites>.
17 changes: 17 additions & 0 deletions tests/docs/smoke-all/brand/test-01/index.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: "test-01"
filters:
- test_brand.lua
_quarto:
tests:
html:
ensureFileRegexMatches:
- ['<span style="color: #ff0000">a span</span>']
- []
---

This is a Quarto website.

[a span]{style='color: {{< brand color primary >}}'}

To learn more about Quarto websites visit <https://quarto.org/docs/websites>.
9 changes: 9 additions & 0 deletions tests/docs/smoke-all/brand/test-01/revealjs.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
format: revealjs
---

## Slides

This is a Quarto presentation.

To learn more about Quarto websites visit <https://quarto.org/docs/websites>.
1 change: 1 addition & 0 deletions tests/docs/smoke-all/brand/test-01/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* css styles */
6 changes: 6 additions & 0 deletions tests/docs/smoke-all/brand/test-01/test_brand.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
local brand = require("modules/brand/brand")

function Pandoc(doc)
assert(brand.get_color("primary") == "#ff0000")
return doc
end
Loading