diff --git a/src/resources/filters/modules/mediabag.lua b/src/resources/filters/modules/mediabag.lua index 62f10bb2e8..658b613b15 100644 --- a/src/resources/filters/modules/mediabag.lua +++ b/src/resources/filters/modules/mediabag.lua @@ -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 @@ -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 } \ No newline at end of file diff --git a/src/resources/filters/quarto-finalize/mediabag.lua b/src/resources/filters/quarto-finalize/mediabag.lua index 3566f02895..6e7ab40e05 100644 --- a/src/resources/filters/quarto-finalize/mediabag.lua +++ b/src/resources/filters/quarto-finalize/mediabag.lua @@ -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 diff --git a/src/resources/filters/quarto-post/typst-brand-yaml.lua b/src/resources/filters/quarto-post/typst-brand-yaml.lua index 07e734b151..3a94820bab 100644 --- a/src/resources/filters/quarto-post/typst-brand-yaml.lua +++ b/src/resources/filters/quarto-post/typst-brand-yaml.lua @@ -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 @@ -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, diff --git a/tests/docs/smoke-all/typst/brand-yaml/logo/online-logo.qmd b/tests/docs/smoke-all/typst/brand-yaml/logo/online-logo.qmd new file mode 100644 index 0000000000..7e13f71403 --- /dev/null +++ b/tests/docs/smoke-all/typst/brand-yaml/logo/online-logo.qmd @@ -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\)\)\)\)' + - [] +--- +