Skip to content

Commit f24a05d

Browse files
committed
(#37) remove figure captions when caption is empty
1 parent ab50ac6 commit f24a05d

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
pandoc-plot uses [Semantic Versioning](http://semver.org/spec/v2.0.0.html)
44

5+
## Release 1.5.1
6+
7+
* Figures with no captions (and no link to the source script), will now be shown as an image, without figure numbering (#37).
8+
59
## Release 1.5.0
610

711
* Added support for [Sage](https://www.sagemath.org/) (#44).

pandoc-plot.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 2.2
22
name: pandoc-plot
3-
version: 1.5.0
3+
version: 1.5.1
44
synopsis: A Pandoc filter to include figures generated from code blocks using your plotting toolkit of choice.
55
description: A Pandoc filter to include figures generated from code blocks.
66
Keep the document and code in the same location. Output is

src/Text/Pandoc/Filter/Plot/Embed.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,15 @@ figure ::
7979
FilePath ->
8080
Inlines ->
8181
PlotM Block
82-
-- To render images as figures with captions, the target title
83-
-- must be "fig:"
84-
-- Janky? yes
8582
figure as fp caption' =
8683
return . head . toList . para $
87-
imageWith as (pack fp) "fig:" caption'
84+
imageWith as (pack fp) title caption'
85+
where
86+
-- To render images as figures with captions, the target title
87+
-- must be "fig:"
88+
-- Janky? yes
89+
-- In case there is no caption, make this an image instead of a figure
90+
title = if caption' /= mempty then "fig:" else mempty
8891

8992
-- TODO: also add the case where SVG plots can be
9093
-- embedded in HTML output

tests/Common.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,30 @@ testMarkdownFormattingCaption2 tk =
317317
extractImageCaption (Image _ c _) = c
318318
extractImageCaption _ = mempty
319319

320+
-------------------------------------------------------------------------------
321+
-- Test that Markdown bold formatting in captions is correctly rendered
322+
testFigureWithoutCaption :: Toolkit -> TestTree
323+
testFigureWithoutCaption tk =
324+
testCase "appropriately build an image if no caption" $ do
325+
let postfix = unpack . cls $ tk
326+
tempDir <- (</> "test-image-if-no-caption-" <> postfix) <$> getTemporaryDirectory
327+
ensureDirectoryExistsAndEmpty tempDir
328+
329+
-- Note that this test is fragile, in the sense that the expected result must be carefully
330+
-- constructed
331+
let cb =
332+
addDirectory tempDir $ codeBlock tk (trivialContent tk)
333+
fmt = B.Format "markdown"
334+
result <- runPlotM Nothing (defaultTestConfig {captionFormat = fmt}) $ make cb
335+
assertEqual "" (Just mempty) (extractTitle result)
336+
where
337+
extractTitle (B.Para blocks) = extractImageCaption . head $ blocks
338+
extractTitle _ = Nothing
339+
340+
extractImageCaption (Image _ _ (_, title)) = Just title
341+
extractImageCaption _ = Nothing
342+
343+
320344
-------------------------------------------------------------------------------
321345
-- Test that cleanOutpuDirs correctly cleans the output directory specified in a block.
322346
testCleanOutputDirs :: Toolkit -> TestTree

tests/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ toolkitSuite tk =
5858
testOverrideConfiguration,
5959
testMarkdownFormattingCaption1,
6060
testMarkdownFormattingCaption2,
61+
testFigureWithoutCaption,
6162
testCleanOutputDirs,
6263
testChecksFail
6364
]

0 commit comments

Comments
 (0)