From f24a05dda9dac8e52dbbae99cb837a9eb9dde29e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20P=2E=20Ren=C3=A9=20de=20Cotret?= Date: Thu, 31 Mar 2022 07:52:55 -0400 Subject: [PATCH] (#37) remove figure captions when caption is empty --- CHANGELOG.md | 4 ++++ pandoc-plot.cabal | 2 +- src/Text/Pandoc/Filter/Plot/Embed.hs | 11 +++++++---- tests/Common.hs | 24 ++++++++++++++++++++++++ tests/Main.hs | 1 + 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c75db77..b46af06a 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.5.1 + +* Figures with no captions (and no link to the source script), will now be shown as an image, without figure numbering (#37). + ## Release 1.5.0 * Added support for [Sage](https://www.sagemath.org/) (#44). diff --git a/pandoc-plot.cabal b/pandoc-plot.cabal index b2ccfb68..28b7959d 100644 --- a/pandoc-plot.cabal +++ b/pandoc-plot.cabal @@ -1,6 +1,6 @@ cabal-version: 2.2 name: pandoc-plot -version: 1.5.0 +version: 1.5.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 diff --git a/src/Text/Pandoc/Filter/Plot/Embed.hs b/src/Text/Pandoc/Filter/Plot/Embed.hs index 9fbcd5f6..330ba5c7 100644 --- a/src/Text/Pandoc/Filter/Plot/Embed.hs +++ b/src/Text/Pandoc/Filter/Plot/Embed.hs @@ -79,12 +79,15 @@ figure :: FilePath -> Inlines -> PlotM Block --- To render images as figures with captions, the target title --- must be "fig:" --- Janky? yes figure as fp caption' = return . head . toList . para $ - imageWith as (pack fp) "fig:" caption' + imageWith as (pack fp) title caption' + where + -- To render images as figures with captions, the target title + -- must be "fig:" + -- Janky? yes + -- In case there is no caption, make this an image instead of a figure + title = if caption' /= mempty then "fig:" else mempty -- 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 16616a86..8e05e649 100644 --- a/tests/Common.hs +++ b/tests/Common.hs @@ -317,6 +317,30 @@ testMarkdownFormattingCaption2 tk = extractImageCaption (Image _ c _) = c extractImageCaption _ = mempty +------------------------------------------------------------------------------- +-- Test that Markdown bold formatting in captions is correctly rendered +testFigureWithoutCaption :: Toolkit -> TestTree +testFigureWithoutCaption tk = + testCase "appropriately build an image if no caption" $ do + let postfix = unpack . cls $ tk + tempDir <- ( "test-image-if-no-caption-" <> postfix) <$> getTemporaryDirectory + ensureDirectoryExistsAndEmpty tempDir + + -- Note that this test is fragile, in the sense that the expected result must be carefully + -- constructed + let cb = + addDirectory tempDir $ codeBlock tk (trivialContent tk) + fmt = B.Format "markdown" + result <- runPlotM Nothing (defaultTestConfig {captionFormat = fmt}) $ make cb + assertEqual "" (Just mempty) (extractTitle result) + where + extractTitle (B.Para blocks) = extractImageCaption . head $ blocks + extractTitle _ = Nothing + + extractImageCaption (Image _ _ (_, title)) = Just title + extractImageCaption _ = Nothing + + ------------------------------------------------------------------------------- -- Test that cleanOutpuDirs correctly cleans the output directory specified in a block. testCleanOutputDirs :: Toolkit -> TestTree diff --git a/tests/Main.hs b/tests/Main.hs index e558cca2..bf969e40 100644 --- a/tests/Main.hs +++ b/tests/Main.hs @@ -58,6 +58,7 @@ toolkitSuite tk = testOverrideConfiguration, testMarkdownFormattingCaption1, testMarkdownFormattingCaption2, + testFigureWithoutCaption, testCleanOutputDirs, testChecksFail ]