aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2023-11-25 11:35:10 -0500
committerJohn MacFarlane <[email protected]>2023-11-25 11:35:10 -0500
commitfc3d202e3f758cc11e1853720e6bfa9860f0dc16 (patch)
tree01406cb07f9409c10c6f3ad5811f144031622a58 /src
parentf7f2a016d937c45f6dc4ab2124e066b5d8d38226 (diff)
SelfContained: improve treatment of embedded SVGs.
- Ensure unique ids for elements by prefixing SVG id. - Ensure SVG `id` attribute except when `use` element is used. - Remove `width`, `height` attributes from svg element when `use` element is used. Instead, add `width` and `height` 100% to the `use` element. This seems to get the sizing right. Closes #9206. Ref: #8948.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/SelfContained.hs25
1 files changed, 21 insertions, 4 deletions
diff --git a/src/Text/Pandoc/SelfContained.hs b/src/Text/Pandoc/SelfContained.hs
index e39d62961..dccf1b999 100644
--- a/src/Text/Pandoc/SelfContained.hs
+++ b/src/Text/Pandoc/SelfContained.hs
@@ -159,9 +159,14 @@ convertTags (t@(TagOpen tagname as):ts)
svgmap <- gets svgMap
case M.lookup hash svgmap of
Just (svgid, svgattrs) -> do
- let attrs' = combineSvgAttrs svgattrs attrs
+ let attrs' = [(k,v) | (k,v) <- combineSvgAttrs svgattrs attrs
+ , k /= "id"
+ , k /= "width"
+ , k /= "height"]
return $ TagOpen "svg" attrs' :
- TagOpen "use" [("href", "#" <> svgid)] :
+ TagOpen "use" [("href", "#" <> svgid),
+ ("width", "100%"),
+ ("height", "100%")] :
TagClose "use" :
TagClose "svg" :
rest'
@@ -172,10 +177,22 @@ convertTags (t@(TagOpen tagname as):ts)
let svgid = case lookup "id" attrs' of
Just id' -> id'
Nothing -> "svg_" <> hash
- let attrs'' = [(k,v) | (k,v) <- attrs', k /= "id"]
+ let attrs'' = ("id", svgid) :
+ [(k,v) | (k,v) <- attrs', k /= "id"]
modify $ \st ->
st{ svgMap = M.insert hash (svgid, attrs'') (svgMap st) }
- return $ TagOpen "svg" attrs'' : tags' ++ rest'
+ let addIdPrefix ("id", x) = ("id", svgid <> "_" <> x)
+ addIdPrefix (k, x)
+ | k == "xlink:href" || k == "href" =
+ case T.uncons x of
+ Just ('#', x') -> (k, "#" <> svgid <> "_" <> x')
+ _ -> (k, x)
+ addIdPrefix kv = kv
+ let ensureUniqueId (TagOpen tname ats) =
+ TagOpen tname (map addIdPrefix ats)
+ ensureUniqueId x = x
+ return $ TagOpen "svg" attrs'' :
+ map ensureUniqueId tags' ++ rest'
_ -> return $ TagOpen tagname attrs : rest
where processAttribute (x,y) =
if isSourceAttribute tagname (x,y)