From 4aeaa4dbe14c23adc0bcafe08f3ec570c514e774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Ren=C3=A9=20de=20Cotret?= Date: Mon, 23 Jan 2023 18:39:31 -0500 Subject: [PATCH] Fixed an issue with figure attributes disappearing --- CHANGELOG.md | 4 ++++ pandoc-plot.cabal | 5 +++-- src/Text/Pandoc/Filter/Plot/Embed.hs | 9 +++++++-- tests/Common.hs | 29 ++++++++++++++++++++++++++++ tests/Main.hs | 1 + 5 files changed, 44 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36bef48..6aa2f62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ pandoc-plot uses [Semantic Versioning](http://semver.org/spec/v2.0.0.html) +## Release 1.6.1 + +* Fixed an issue where figure attributes were lost, which prevent other filters (e.g. pandoc-crossref) from working in conjunction with pandoc-plot. + ## Release 1.6.0 * Support for pandoc 3. Support for older pandoc version has also been dropped (pandoc 2.19 and earlier). diff --git a/pandoc-plot.cabal b/pandoc-plot.cabal index d67e3c0..3c88cd8 100644 --- a/pandoc-plot.cabal +++ b/pandoc-plot.cabal @@ -1,6 +1,6 @@ cabal-version: 2.2 name: pandoc-plot -version: 1.6.0 +version: 1.6.1 synopsis: A Pandoc filter to include figures generated from code blocks using your plotting toolkit of choice. description: A Pandoc filter to include figures generated from code blocks. Keep the document and code in the same location. Output is @@ -18,7 +18,8 @@ tested-with: GHC == 8.10.4, GHC == 9.0.1, GHC == 9.0.1, GHC == 9.2.1, - GHC == 9.2.2 + GHC == 9.2.2, + GHC == 9.4.4 extra-source-files: CHANGELOG.md LICENSE diff --git a/src/Text/Pandoc/Filter/Plot/Embed.hs b/src/Text/Pandoc/Filter/Plot/Embed.hs index 9b24ead..11f3616 100644 --- a/src/Text/Pandoc/Filter/Plot/Embed.hs +++ b/src/Text/Pandoc/Filter/Plot/Embed.hs @@ -33,9 +33,12 @@ import Text.HTML.TagSoup import Text.Pandoc.Builder as Builder ( Inlines, fromList, - simpleFigureWith, + figureWith, + imageWith, + plain, link, str, + simpleCaption, toList, ) import Text.Pandoc.Class (runPure) @@ -79,7 +82,9 @@ figure :: PlotM Block figure as fp caption' = return . head . toList $ - simpleFigureWith as caption' (pack fp) mempty + -- We want the attributes both on the Figure element and the contained Image element + -- so that pandoc-plot plays nice with pandoc-crossref and other filters + figureWith as (simpleCaption (plain caption')) $ plain $ imageWith mempty (pack fp) mempty caption' -- TODO: also add the case where SVG plots can be -- embedded in HTML output diff --git a/tests/Common.hs b/tests/Common.hs index e5d1414..441bd99 100644 --- a/tests/Common.hs +++ b/tests/Common.hs @@ -341,6 +341,32 @@ testChecksFail tk = assertBool "" (expectedCheck result) assertChecksFail _ = assertEqual "Test skipped" True True +------------------------------------------------------------------------------- +-- Test that Markdown bold formatting in captions is correctly rendered +testAttributesPreservedOnFigure :: Toolkit -> TestTree +testAttributesPreservedOnFigure tk = + testCase "preserves code block attributes and sets them on the Figure element" $ do + let postfix = unpack . cls $ tk + tempDir <- ( "test-preserved-attrs-" <> postfix) <$> getTemporaryDirectory + ensureDirectoryExistsAndEmpty tempDir + + -- Note that this test is fragile, in the sense that the expected result must be carefully + -- constructed + let expectedAttrs = ("hello", [cls tk], [("key1", "val1"), ("key2", "val2")]) + cb = setAttrs expectedAttrs $ + addDirectory tempDir $ + addCaption "[title](https://google.com)" $ + codeBlock tk (trivialContent tk) + fmt = B.Format "markdown" + Figure (id', _, keyvals) _ _ <- runPlotM Nothing (defaultTestConfig { captionFormat = fmt + , defaultDirectory = tempDir + }) $ make cb + let (expectedId, _, expectedKeyVals) = expectedAttrs + assertEqual "identifier" expectedId id' + assertEqual "key-value pairs" expectedKeyVals keyvals + where + extractCaption (B.Figure _ (Caption _ caption) _) = caption + codeBlock :: Toolkit -> Script -> Block codeBlock tk script = CodeBlock (mempty, [cls tk], mempty) script @@ -388,6 +414,9 @@ addWithSource :: Bool -> Block -> Block addWithSource yn (CodeBlock (id', cls, attrs) script) = CodeBlock (id', cls, attrs ++ [(tshow WithSourceK, pack . show $ yn)]) script +setAttrs :: Attr -> Block -> Block +setAttrs attrs (CodeBlock _ script) = CodeBlock attrs script + -- | Assert that a file exists assertFileExists :: HasCallStack => FilePath -> Assertion assertFileExists filepath = do diff --git a/tests/Main.hs b/tests/Main.hs index ee0027b..4b66177 100644 --- a/tests/Main.hs +++ b/tests/Main.hs @@ -58,6 +58,7 @@ toolkitSuite tk = testOverrideConfiguration, testMarkdownFormattingCaption1, testMarkdownFormattingCaption2, + testAttributesPreservedOnFigure, testCleanOutputDirs, testChecksFail ]