From b0421a111fe40400b809b7ad64d5fef306e30b67 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Fri, 12 Apr 2024 11:30:04 -0700 Subject: [PATCH 1/5] merge 'class' attr when both img and svg specify it --- src/Text/Pandoc/SelfContained.hs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Text/Pandoc/SelfContained.hs b/src/Text/Pandoc/SelfContained.hs index c20492d9e14f..086498218eb4 100644 --- a/src/Text/Pandoc/SelfContained.hs +++ b/src/Text/Pandoc/SelfContained.hs @@ -258,10 +258,12 @@ combineSvgAttrs svgAttrs imgAttrs = dropPointZero t = case T.stripSuffix ".0" t of Nothing -> t Just t' -> t' - combinedAttrs = imgAttrs ++ + combinedAttrs = concat [ + imgAttrs, [(k, v) | (k, v) <- svgAttrs , isNothing (lookup k imgAttrs) - , k `notElem` ["xmlns", "xmlns:xlink", "version"]] + , k `notElem` ["xmlns", "xmlns:xlink", "version"]], + mergedClasses] parseViewBox t = case map (safeRead . addZero) $ T.words t of [Just llx, Just lly, Just urx, Just ury] -> Just (llx, lly, urx, ury) @@ -274,6 +276,12 @@ combineSvgAttrs svgAttrs imgAttrs = lookup "viewBox" svgAttrs >>= parseViewBox (mbHeight :: Maybe Int) = lookup "height" combinedAttrs >>= safeRead (mbWidth :: Maybe Int) = lookup "width" combinedAttrs >>= safeRead + -- https://github.com/jgm/pandoc/issues/9652 + imgClasses = lookup "class" imgAttrs + svgClasses = lookup "class" svgAttrs + mergedClasses = case (imgClasses, svgClasses) of + (Just c1, Just c2) -> [("class", c1 <> " " <> c2)] + _ -> [] cssURLs :: PandocMonad m => FilePath -> ByteString -> m ByteString From 8e209e2221caa5eaa3ae9ef73b9cc82a3570f67c Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Fri, 12 Apr 2024 11:37:24 -0700 Subject: [PATCH 2/5] command test for #9652 --- test/command/9652.md | 9 +++++++++ test/command/9652.svg | 3 +++ 2 files changed, 12 insertions(+) create mode 100644 test/command/9652.md create mode 100644 test/command/9652.svg diff --git a/test/command/9652.md b/test/command/9652.md new file mode 100644 index 000000000000..b061fd6c34f0 --- /dev/null +++ b/test/command/9652.md @@ -0,0 +1,9 @@ +```` +% pandoc -f markdown -t html --embed-resources +```{=html} + +``` +^D + + +```` diff --git a/test/command/9652.svg b/test/command/9652.svg new file mode 100644 index 000000000000..70063b873389 --- /dev/null +++ b/test/command/9652.svg @@ -0,0 +1,3 @@ + + + From 7590aa9bd9667a1009fd2efb1e6b51eb2cfb91ea Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Fri, 12 Apr 2024 13:09:40 -0700 Subject: [PATCH 3/5] fix duplicate 'class' --- src/Text/Pandoc/SelfContained.hs | 17 ++++++++--------- test/command/9652.md | 2 +- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Text/Pandoc/SelfContained.hs b/src/Text/Pandoc/SelfContained.hs index 086498218eb4..4cfdca28584b 100644 --- a/src/Text/Pandoc/SelfContained.hs +++ b/src/Text/Pandoc/SelfContained.hs @@ -258,12 +258,10 @@ combineSvgAttrs svgAttrs imgAttrs = dropPointZero t = case T.stripSuffix ".0" t of Nothing -> t Just t' -> t' - combinedAttrs = concat [ - imgAttrs, + combinedAttrs = replaceMaybe mergedClasses $ imgAttrs ++ [(k, v) | (k, v) <- svgAttrs , isNothing (lookup k imgAttrs) - , k `notElem` ["xmlns", "xmlns:xlink", "version"]], - mergedClasses] + , k `notElem` ["xmlns", "xmlns:xlink", "version"]] parseViewBox t = case map (safeRead . addZero) $ T.words t of [Just llx, Just lly, Just urx, Just ury] -> Just (llx, lly, urx, ury) @@ -277,11 +275,12 @@ combineSvgAttrs svgAttrs imgAttrs = (mbHeight :: Maybe Int) = lookup "height" combinedAttrs >>= safeRead (mbWidth :: Maybe Int) = lookup "width" combinedAttrs >>= safeRead -- https://github.com/jgm/pandoc/issues/9652 - imgClasses = lookup "class" imgAttrs - svgClasses = lookup "class" svgAttrs - mergedClasses = case (imgClasses, svgClasses) of - (Just c1, Just c2) -> [("class", c1 <> " " <> c2)] - _ -> [] + mergedClasses = case (lookup "class" imgAttrs, lookup "class" svgAttrs) of + (Just c1, Just c2) -> Just ("class", c1 <> " " <> c2) + _ -> Nothing + replaceMaybe :: Eq a => Maybe (a, b) -> [(a, b)] -> [(a, b)] + replaceMaybe Nothing = id + replaceMaybe (Just x) = map (\pair@(a, _) -> if a == fst x then x else pair) cssURLs :: PandocMonad m => FilePath -> ByteString -> m ByteString diff --git a/test/command/9652.md b/test/command/9652.md index b061fd6c34f0..e3f586d7fa84 100644 --- a/test/command/9652.md +++ b/test/command/9652.md @@ -4,6 +4,6 @@ ``` ^D - + ```` From 2fd0564bafebf6a53a105f5333281cb8ed8b0aab Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Mon, 15 Apr 2024 08:53:48 -0700 Subject: [PATCH 4/5] use a second comprehension for imgAttrs --- src/Text/Pandoc/SelfContained.hs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Text/Pandoc/SelfContained.hs b/src/Text/Pandoc/SelfContained.hs index 4cfdca28584b..73a42f3b85ed 100644 --- a/src/Text/Pandoc/SelfContained.hs +++ b/src/Text/Pandoc/SelfContained.hs @@ -258,10 +258,11 @@ combineSvgAttrs svgAttrs imgAttrs = dropPointZero t = case T.stripSuffix ".0" t of Nothing -> t Just t' -> t' - combinedAttrs = replaceMaybe mergedClasses $ imgAttrs ++ + combinedAttrs = imgAttrs ++ [(k, v) | (k, v) <- svgAttrs , isNothing (lookup k imgAttrs) - , k `notElem` ["xmlns", "xmlns:xlink", "version"]] + , k `notElem` ["xmlns", "xmlns:xlink", "version", "class"]] ++ + mergedClasses parseViewBox t = case map (safeRead . addZero) $ T.words t of [Just llx, Just lly, Just urx, Just ury] -> Just (llx, lly, urx, ury) @@ -276,11 +277,8 @@ combineSvgAttrs svgAttrs imgAttrs = (mbWidth :: Maybe Int) = lookup "width" combinedAttrs >>= safeRead -- https://github.com/jgm/pandoc/issues/9652 mergedClasses = case (lookup "class" imgAttrs, lookup "class" svgAttrs) of - (Just c1, Just c2) -> Just ("class", c1 <> " " <> c2) - _ -> Nothing - replaceMaybe :: Eq a => Maybe (a, b) -> [(a, b)] -> [(a, b)] - replaceMaybe Nothing = id - replaceMaybe (Just x) = map (\pair@(a, _) -> if a == fst x then x else pair) + (Just c1, Just c2) -> [("class", c1 <> " " <> c2)] + _ -> [] cssURLs :: PandocMonad m => FilePath -> ByteString -> m ByteString From 2aaead54b2d32d3211368488948ac68449c5cd16 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Mon, 15 Apr 2024 08:54:21 -0700 Subject: [PATCH 5/5] use a second comprehension for imgAttrs --- src/Text/Pandoc/SelfContained.hs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Text/Pandoc/SelfContained.hs b/src/Text/Pandoc/SelfContained.hs index 73a42f3b85ed..4aa32ca0069b 100644 --- a/src/Text/Pandoc/SelfContained.hs +++ b/src/Text/Pandoc/SelfContained.hs @@ -258,7 +258,9 @@ combineSvgAttrs svgAttrs imgAttrs = dropPointZero t = case T.stripSuffix ".0" t of Nothing -> t Just t' -> t' - combinedAttrs = imgAttrs ++ + combinedAttrs = + [(k, v) | (k, v) <- imgAttrs + , k /= "class"] ++ [(k, v) | (k, v) <- svgAttrs , isNothing (lookup k imgAttrs) , k `notElem` ["xmlns", "xmlns:xlink", "version", "class"]] ++ @@ -275,7 +277,6 @@ combineSvgAttrs svgAttrs imgAttrs = lookup "viewBox" svgAttrs >>= parseViewBox (mbHeight :: Maybe Int) = lookup "height" combinedAttrs >>= safeRead (mbWidth :: Maybe Int) = lookup "width" combinedAttrs >>= safeRead - -- https://github.com/jgm/pandoc/issues/9652 mergedClasses = case (lookup "class" imgAttrs, lookup "class" svgAttrs) of (Just c1, Just c2) -> [("class", c1 <> " " <> c2)] _ -> []