Skip to content

Commit 5c6e18c

Browse files
committed
Add support for fetching remove imags for PDF
1 parent 68a0b78 commit 5c6e18c

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/resources/filters/quarto-post/svg.lua renamed to src/resources/filters/quarto-post/pdf-images.lua

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ local function convert_svg(path)
1313
end
1414
end
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

src/resources/filters/quarto-post/quarto-post.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import("tikz.lua")
2323
import("meta.lua")
2424
import("delink.lua")
2525
import("book.lua")
26-
import("svg.lua")
26+
import("pdf-images.lua")
2727
import("../common/lunacolors.lua")
2828
import("../common/log.lua")
2929
import("../common/base64.lua")
@@ -48,7 +48,7 @@ return {
4848
tikz(),
4949
delink(),
5050
figCleanup(),
51-
svg()
51+
pdfImages()
5252
}),
5353
ojs(),
5454
quartoPostMetaInject(),

0 commit comments

Comments
 (0)