Skip to content

Commit

Permalink
(#37) remove figure captions when caption is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentRDC committed Mar 31, 2022
1 parent ab50ac6 commit f24a05d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion pandoc-plot.cabal
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 7 additions & 4 deletions src/Text/Pandoc/Filter/Plot/Embed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions tests/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ toolkitSuite tk =
testOverrideConfiguration,
testMarkdownFormattingCaption1,
testMarkdownFormattingCaption2,
testFigureWithoutCaption,
testCleanOutputDirs,
testChecksFail
]
Expand Down

0 comments on commit f24a05d

Please sign in to comment.