Skip to content

Commit

Permalink
typst brand yaml logo: use mediabag
Browse files Browse the repository at this point in the history
refactor mediabag to allow working directly with paths instead of images

because this image is written straight to typst and we can't put a Pandoc
image inside include-in-headers

and even if we could, this goes through dependencies json
  • Loading branch information
gordonwoodhull committed Jan 8, 2025
1 parent a2046c0 commit 2b6ec72
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
29 changes: 24 additions & 5 deletions src/resources/filters/modules/mediabag.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,20 @@ local function fetch_and_store_image(src)
return filename
end

local function should_mediabag(src)
local relativePath = src:match("https?://[%w%$%-%_%.%+%!%*%'%(%)%:%%]+/(.+)") or
src:match("data:image/.+;base64,(.+)")
return relativePath or param("has-resource-path", false)
end

local function resolve_image_from_url(image)
if resolved_url_cache[image.src] then
image.src = resolved_url_cache[image.src]
return image
end
local relativePath = image.src:match("https?://[%w%$%-%_%.%+%!%*%'%(%)%:%%]+/(.+)") or
image.src:match("data:image/.+;base64,(.+)")
if not (relativePath or param("has-resource-path", false)) then
if not should_mediabag(image.src) then
return nil
end

local filename = fetch_and_store_image(image.src)
if filename == nil then
return nil
Expand All @@ -74,9 +77,25 @@ local function resolve_image_from_url(image)
return image
end

local function write_mediabag_entry(src)
local mt, contents = pandoc.mediabag.lookup(src)
if contents == nil then return nil end

local mediabagDir = param("mediabag-dir", nil)
local mediaFile = pandoc.path.join{mediabagDir, src}

local file = _quarto.file.write(mediaFile, contents)
if not file then
warn('failed to write mediabag entry: ' .. mediaFile)
end
return mediaFile
end

return {
resolved_url_cache = resolved_url_cache,
with_mediabag_contents = with_mediabag_contents,
fetch_and_store_image = fetch_and_store_image,
resolve_image_from_url = resolve_image_from_url
should_mediabag = should_mediabag,
resolve_image_from_url = resolve_image_from_url,
write_mediabag_entry = write_mediabag_entry
}
12 changes: 2 additions & 10 deletions src/resources/filters/quarto-finalize/mediabag.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@ function mediabag_filter()
Image = function(el)
if not _quarto.format.isWordProcessorOutput() and
not _quarto.format.isPowerPointOutput() then
local mt, contents = pandoc.mediabag.lookup(el.src)
if contents ~= nil then

local mediabagDir = param("mediabag-dir", nil)
local mediaFile = pandoc.path.join{mediabagDir, el.src}

local file = _quarto.file.write(mediaFile, contents)
if not file then
warn('failed to write mediabag entry: ' .. mediaFile)
end
local mediaFile = _quarto.modules.mediabag.write_mediabag_entry(el.src)
if mediaFile then
el.src = mediaFile
return el
end
Expand Down
13 changes: 10 additions & 3 deletions src/resources/filters/quarto-post/typst-brand-yaml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function render_typst_brand_yaml()
end

return {
Pandoc = function(pandoc)
Pandoc = function(pandoc0)
local brand = param('brand')
local raw_block_shown = false
if brand and brand.processedData then
Expand Down Expand Up @@ -285,9 +285,16 @@ function render_typst_brand_yaml()
location_to_typst_align(logoOptions.location) or 'left+top'
quarto.log.debug('logo options', logoOptions)
local altProp = logoOptions.alt and (', alt: "' .. logoOptions.alt .. '"') or ''
local dblbackslash = string.gsub(logoOptions.path, '\\', '\\\\') -- double backslash?
local imageFilename = logoOptions.path
if _quarto.modules.mediabag.should_mediabag(imageFilename) then
imageFilename = _quarto.modules.mediabag.resolved_url_cache[logoOptions.path] or _quarto.modules.mediabag.fetch_and_store_image(logoOptions.path)
imageFilename = _quarto.modules.mediabag.write_mediabag_entry(imageFilename) or imageFilename
else
-- backslashes need to be doubled for Windows
imageFilename = string.gsub(imageFilename, '\\', '\\\\')
end
quarto.doc.include_text('in-header',
'#set page(background: align(' .. logoOptions.location .. ', box(inset: ' .. inset .. ', image("' .. dblbackslash .. '", width: ' .. logoOptions.width .. altProp .. '))))')
'#set page(background: align(' .. logoOptions.location .. ', box(inset: ' .. inset .. ', image("' .. imageFilename .. '", width: ' .. logoOptions.width .. altProp .. '))))')
end
end
end,
Expand Down
17 changes: 17 additions & 0 deletions tests/docs/smoke-all/typst/brand-yaml/logo/online-logo.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: "Untitled"
format:
typst:
keep-typ: true
brand:
logo:
medium: https://quarto.org/quarto.png
_quarto:
tests:
typst:
ensureTypstFileRegexMatches:
-
- '#set page\(background: align\(left\+top, box\(inset: 0.75in, image\("online-logo_files(/|\\)mediabag(/|\\)quarto.png", width: 1.5in\)\)\)\)'
- []
---

0 comments on commit 2b6ec72

Please sign in to comment.