@@ -13,12 +13,17 @@ local function convert_svg(path)
1313 end
1414end
1515
16- function svg ()
16+ -- A cache of image urls that we've resolved into the mediabag
17+ -- keyed by {url: mediabagpath}
18+ local resolvedUrls = {}
19+
20+ function pdfImages ()
1721 return {
1822 -- convert SVG images to PDF when rendering PDFS
1923 Image = function (image )
2024 if quarto .doc .is_format (" pdf" ) then
2125 if _quarto .file .exists (image .src ) then
26+ -- If the src is pointing to a local file that is an svg, process it
2227 local ext = select (2 , pandoc .path .split_extension (image .src ))
2328 if ext == ' .svg' then
2429 local converted = convert_svg (image .src )
@@ -28,7 +33,8 @@ function svg()
2833 end
2934 end
3035 else
31- -- take a look in the media bag
36+ -- See if the path points to an SVG in the media bag
37+ -- (been generated by a filter, for example)
3238 local mt , contents = pandoc .mediabag .lookup (image .src )
3339 if mt == ' image/svg+xml' then
3440 local result = pandoc .system .with_temporary_directory (' svg-convert' , function (tmpdir )
@@ -58,6 +64,32 @@ function svg()
5864 return nil
5965 end )
6066 return result
67+ elseif mt == nil then
68+ -- This file doesn't exist and isn't in the media bag
69+ -- see if it need to be fetched
70+ if resolvedUrls [image .src ] then
71+ image .src = resolvedUrls [image .src ]
72+ return image
73+ else
74+ local relativePath = image .src :match (' http[s]://[%w%.%:]+/(.+)' )
75+ if relativePath then
76+ local imgMt , imgContents = pandoc .mediabag .fetch (image .src )
77+ local filename = pandoc .path .filename (relativePath )
78+ if imgMt ~= nil then
79+ local existingMt = pandoc .mediabag .lookup (filename )
80+ local counter = 1
81+ while (existingMt ) do
82+ local stem , ext = pandoc .path .split_extension (filename )
83+ filename = stem .. counter .. ext
84+ existingMt = pandoc .mediabag .lookup (filename )
85+ end
86+ resolvedUrls [image .src ] = filename
87+ pandoc .mediabag .insert (filename , imgMt , imgContents )
88+ image .src = filename
89+ return image
90+ end
91+ end
92+ end
6193 end
6294 end
6395 end
0 commit comments