summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2023-07-25 23:24:43 -0600
committerJohn MacFarlane <jgm@berkeley.edu>2023-07-25 23:24:43 -0600
commit704a07d9639e65d9efd4b9d49b3685b0e4206601 (patch)
tree70824f7b558e0b2ca3d911ab79f34cd800d40ad1
parent77e01242da8ab64af52a75459d42dc230282647a (diff)
SelfContained: Improve inline SVGs.
- Ensure that width and height attributes don't get specified twice is both the img tag and the svg include them. - Omit unnecessary attributes xmlns, xmlns:xlink, and version on svg element. - Use 20 character rather than 40 character hashes for generated IDs. Closes #8965.
-rw-r--r--src/Text/Pandoc/SelfContained.hs29
-rw-r--r--test/command/8948.md4
2 files changed, 23 insertions, 10 deletions
diff --git a/src/Text/Pandoc/SelfContained.hs b/src/Text/Pandoc/SelfContained.hs
index bfbfc6f6f..428a18f0f 100644
--- a/src/Text/Pandoc/SelfContained.hs
+++ b/src/Text/Pandoc/SelfContained.hs
@@ -23,7 +23,7 @@ import Data.ByteString.Base64 (encodeBase64)
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy as L
import qualified Data.Text as T
-import Data.Char (isAlphaNum, isAscii)
+import Data.Char (isAlphaNum, isAscii, isDigit)
import Data.Digest.Pure.SHA (sha1, showDigest)
import Network.URI (escapeURIString)
import System.FilePath (takeDirectory, takeExtension, (</>))
@@ -189,9 +189,9 @@ convertTags (t@(TagOpen tagname as):ts)
Fetched ("image/svg+xml", bs) -> do
-- we filter CR in the hash to ensure that Windows
-- and non-Windows tests agree:
- let hash = T.pack (showDigest
- (sha1 (L.fromStrict
- (B.filter (/='\r') bs))))
+ let hash = T.pack $ take 20 $ showDigest $
+ sha1 $ L.fromStrict
+ $ B.filter (/='\r') bs
return $ Left (hash, getSvgTags (toText bs))
Fetched (mt,bs) -> return $ Right (x, makeDataURI (mt,bs))
CouldNotFetch _ -> return $ Right (x, y)
@@ -214,13 +214,26 @@ combineSvgAttrs svgAttrs imgAttrs =
(Nothing, Just h, Just w) -> -- calculate viewBox
combinedAttrs ++ [("viewBox", T.unwords ["0", "0", tshow w, tshow h])]
(Just (llx,lly,urx,ury), Nothing, Nothing) -> -- calculate width, height
- combinedAttrs ++
- [ ("width", tshow (floor urx - floor llx :: Int))
- , ("height", tshow (floor ury - floor lly :: Int)) ]
+ combinedAttrs ++
+ [ ("width", tshow (floor urx - floor llx :: Int)) |
+ isNothing (lookup "width" combinedAttrs) ] ++
+ [ ("height", tshow (floor ury - floor lly :: Int)) |
+ isNothing (lookup "height" combinedAttrs) ]
_ -> combinedAttrs
where
combinedAttrs = imgAttrs ++
- [(k,v) | (k,v) <- svgAttrs, isNothing (lookup k imgAttrs)]
+ [(k,v') | (k,v) <- svgAttrs
+ , v' <- fixAttr k v
+ , isNothing (lookup k imgAttrs)
+ , k `notElem` ["xmlns", "xmlns:xlink", "version"]]
+ fixAttr k v =
+ if k == "width" || k == "height"
+ then if T.all isDigit v
+ then [v]
+ else case T.stripSuffix "px" v of
+ Just v' | T.all isDigit v' -> [v']
+ _ -> []
+ else [v]
parseViewBox t =
case map (safeRead . addZero) $ T.words t of
[Just llx, Just lly, Just urx, Just ury] -> Just (llx, lly, urx, ury)
diff --git a/test/command/8948.md b/test/command/8948.md
index 3d779bdbf..233f9e585 100644
--- a/test/command/8948.md
+++ b/test/command/8948.md
@@ -3,7 +3,7 @@
![minimal](command/minimal.svg)
![minimal](command/minimal.svg)
^D
-<p><svg alt="minimal"><use href="#svg_7868854ffb8f30209cd098bb6207b390b001249e" /></svg> <svg id="svg_7868854ffb8f30209cd098bb6207b390b001249e" alt="minimal" viewBox="-.333 -.333 480 150" style="background-color:#ffffff00" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" width="481" height="151">
+<p><svg alt="minimal"><use href="#svg_7868854ffb8f30209cd0" /></svg> <svg id="svg_7868854ffb8f30209cd0" alt="minimal" viewBox="-.333 -.333 480 150" style="background-color:#ffffff00" xml:space="preserve" width="481" height="151">
<path d="M 0 35.5 L 6.5 22.5 L 16 37 L 23 24 L 34.8 43.7 L 42.5 30 L 50.3 47 L 59.7 27.7 L 69 47 L 85 17.7 L 98.3 39 L 113 9.7 L 127.7 42.3 L 136.3 23.7 L 147 44.3 L 158.3 20.3 L 170.3 40.3 L 177.7 25.7 L 189.7 43 L 199.7 21 L 207.7 35 L 219 11 L 233 37 L 240.3 23.7 L 251 43 L 263 18.3 L 272.7 33.3 L 283 10 L 295 32.3 L 301.3 23 L 311.7 37 L 323.7 7.7 L 339.3 39 L 346.3 25.7 L 356.3 42.3 L 369.7 15 L 376.3 25.7 L 384 9 L 393 28.3 L 400.3 19 L 411.7 38.3 L 421 21 L 434.3 43 L 445 25 L 453 36.3 L 464.3 18.3 L 476.2 40.3 L 480 33.5 L 480 215 L 0 215 L 0 35.5 Z" fill="#175720"></path>
</svg></p>
```
@@ -13,7 +13,7 @@
![minimal](command/minimal.svg)
![minimal](command/minimal.svg){#foo}
^D
-<p><svg alt="minimal"><use href="#foo" /></svg> <svg id="foo" alt="minimal" viewBox="-.333 -.333 480 150" style="background-color:#ffffff00" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" width="481" height="151">
+<p><svg alt="minimal"><use href="#foo" /></svg> <svg id="foo" alt="minimal" viewBox="-.333 -.333 480 150" style="background-color:#ffffff00" xml:space="preserve" width="481" height="151">
<path d="M 0 35.5 L 6.5 22.5 L 16 37 L 23 24 L 34.8 43.7 L 42.5 30 L 50.3 47 L 59.7 27.7 L 69 47 L 85 17.7 L 98.3 39 L 113 9.7 L 127.7 42.3 L 136.3 23.7 L 147 44.3 L 158.3 20.3 L 170.3 40.3 L 177.7 25.7 L 189.7 43 L 199.7 21 L 207.7 35 L 219 11 L 233 37 L 240.3 23.7 L 251 43 L 263 18.3 L 272.7 33.3 L 283 10 L 295 32.3 L 301.3 23 L 311.7 37 L 323.7 7.7 L 339.3 39 L 346.3 25.7 L 356.3 42.3 L 369.7 15 L 376.3 25.7 L 384 9 L 393 28.3 L 400.3 19 L 411.7 38.3 L 421 21 L 434.3 43 L 445 25 L 453 36.3 L 464.3 18.3 L 476.2 40.3 L 480 33.5 L 480 215 L 0 215 L 0 35.5 Z" fill="#175720"></path>
</svg></p>
```