Skip to content

brand,logo,lua - add support for resolving logo images in brand short… #11140

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 1 commit into from
Oct 21, 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
16 changes: 14 additions & 2 deletions src/core/brand/brand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ type CanonicalLogoInfo = {
type ProcessedBrandData = {
color: Record<string, string>;
typography: BrandTypography;
logo: Record<string, CanonicalLogoInfo>;
logo: {
small?: CanonicalLogoInfo;
medium?: CanonicalLogoInfo;
large?: CanonicalLogoInfo;
images: Record<string, BrandLogoExplicitResource>;
};
};

export class Brand {
Expand Down Expand Up @@ -139,7 +144,7 @@ export class Brand {
};
}

const logo: Record<string, CanonicalLogoInfo> = {};
const logo: ProcessedBrandData["logo"] = { images: {} };
for (
const size of [
"small",
Expand All @@ -151,6 +156,13 @@ export class Brand {
if (v) {
logo[size] = v;
}
for (const [key, value] of Object.entries(data.logo?.images ?? {})) {
if (typeof value === "string") {
logo.images[key] = { path: value };
} else {
logo.images[key] = value;
}
}
}

return {
Expand Down
3 changes: 2 additions & 1 deletion src/format/reveal/format-reveal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,9 @@ export function revealjsFormat() {
const determineRevealLogo = (format: Format): string | undefined => {
const brandData = format.render.brand?.processedData;
if (brandData?.logo) {
const keys: ("medium" | "small" | "large")[] = ["medium", "small", "large"]
// add slide logo if we have one
for (const size of ["medium", "small", "large"]) {
for (const size of keys) {
const logoInfo = brandData.logo[size];
if (!logoInfo) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/resources/filters/modules/brand/brand.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ local function get_logo(name)
local brand = param("brand")
brand = brand and brand.processedData -- from src/core/brand/brand.ts
if not brand then return nil end
return brand.logo and brand.logo[name]
return brand.logo and (brand.logo[name] or brand.logo.images[name])
end

return {
Expand Down
72 changes: 62 additions & 10 deletions src/resources/filters/quarto-pre/shortcodes-handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,73 @@ function initShortcodeHandlers()
return { pandoc.RawInline('quarto-internal', quarto.json.encode(data)) }
end

local function handle_brand(args)
local function handle_brand(args, _kwargs, _meta, _raw_args, context)
local brand = require("modules/brand/brand")
local brandCommand = read_arg(args, 1)
if brandCommand ~= "color" then

local warn_bad_brand_command = function()
warn("Unknown brand command " .. brandCommand .. " specified in a brand shortcode.")
return { pandoc.Strong({pandoc.Str("?brand:" .. brandCommand)}) }
if context == "block" then
return pandoc.Blocks { pandoc.Strong({pandoc.Str("?brand:" .. table.concat(args, " "))}) }
elseif context == "inline" then
return pandoc.Inlines { pandoc.Strong({pandoc.Str("?brand:" .. table.concat(args, " "))}) }
elseif context == "text" then
return "?brand:" .. table.concat(args, " ")
else
warn("Unknown context for brand shortcode error: " .. context)
return { }
end
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)}) }

if brandCommand == "color" then
local color_name = read_arg(args, 2)
local color_value = brand.get_color(color_name)
if color_value == nil then
return warn_bad_brand_command()
else
return pandoc.Inlines { pandoc.Str(color_value) }
end
end

if brandCommand == "logo" then
local logo_name = read_arg(args, 2)
local logo_value = brand.get_logo(logo_name)
local entry = { path = nil }

if type(logo_value) ~= "table" then
warn("unexpected logo value entry: " .. type(logo_value))
return warn_bad_brand_command()
end

quarto.utils.dump(logo_value)

-- does this have light/dark variants?
-- TODO handle light-dark theme switching
if logo_value.light then
entry = logo_value.light
else
entry = logo_value
end

if type(entry.path) ~= "string" then
warn("unexpected type in logo light entry: " .. type(entry.path))
return warn_bad_brand_command()
end

-- TODO fix alt text handling
if context == "block" then
return pandoc.Blocks { pandoc.Image(pandoc.Inlines {}, entry.path) }
elseif context == "inline" then
return pandoc.Inlines { pandoc.Image(pandoc.Inlines {}, entry.path) }
elseif context == "text" then
return entry.path
else
warn("unexpected context for logo shortcode: " .. context)
return warn_bad_brand_command()
end
end

return warn_bad_brand_command()
end

-- built in handlers (these override any user handlers)
Expand Down
Loading