Skip to content

Commit 4aeaa4d

Browse files
committed
Fixed an issue with figure attributes disappearing
1 parent b08e186 commit 4aeaa4d

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
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.6.1
6+
7+
* Fixed an issue where figure attributes were lost, which prevent other filters (e.g. pandoc-crossref) from working in conjunction with pandoc-plot.
8+
59
## Release 1.6.0
610

711
* Support for pandoc 3. Support for older pandoc version has also been dropped (pandoc 2.19 and earlier).

pandoc-plot.cabal

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 2.2
22
name: pandoc-plot
3-
version: 1.6.0
3+
version: 1.6.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
@@ -18,7 +18,8 @@ tested-with: GHC == 8.10.4,
1818
GHC == 9.0.1,
1919
GHC == 9.0.1,
2020
GHC == 9.2.1,
21-
GHC == 9.2.2
21+
GHC == 9.2.2,
22+
GHC == 9.4.4
2223
extra-source-files:
2324
CHANGELOG.md
2425
LICENSE

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ import Text.HTML.TagSoup
3333
import Text.Pandoc.Builder as Builder
3434
( Inlines,
3535
fromList,
36-
simpleFigureWith,
36+
figureWith,
37+
imageWith,
38+
plain,
3739
link,
3840
str,
41+
simpleCaption,
3942
toList,
4043
)
4144
import Text.Pandoc.Class (runPure)
@@ -79,7 +82,9 @@ figure ::
7982
PlotM Block
8083
figure as fp caption' =
8184
return . head . toList $
82-
simpleFigureWith as caption' (pack fp) mempty
85+
-- We want the attributes both on the Figure element and the contained Image element
86+
-- so that pandoc-plot plays nice with pandoc-crossref and other filters
87+
figureWith as (simpleCaption (plain caption')) $ plain $ imageWith mempty (pack fp) mempty caption'
8388

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

tests/Common.hs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,32 @@ testChecksFail tk =
341341
assertBool "" (expectedCheck result)
342342
assertChecksFail _ = assertEqual "Test skipped" True True
343343

344+
-------------------------------------------------------------------------------
345+
-- Test that Markdown bold formatting in captions is correctly rendered
346+
testAttributesPreservedOnFigure :: Toolkit -> TestTree
347+
testAttributesPreservedOnFigure tk =
348+
testCase "preserves code block attributes and sets them on the Figure element" $ do
349+
let postfix = unpack . cls $ tk
350+
tempDir <- (</> "test-preserved-attrs-" <> postfix) <$> getTemporaryDirectory
351+
ensureDirectoryExistsAndEmpty tempDir
352+
353+
-- Note that this test is fragile, in the sense that the expected result must be carefully
354+
-- constructed
355+
let expectedAttrs = ("hello", [cls tk], [("key1", "val1"), ("key2", "val2")])
356+
cb = setAttrs expectedAttrs $
357+
addDirectory tempDir $
358+
addCaption "[title](https://google.com)" $
359+
codeBlock tk (trivialContent tk)
360+
fmt = B.Format "markdown"
361+
Figure (id', _, keyvals) _ _ <- runPlotM Nothing (defaultTestConfig { captionFormat = fmt
362+
, defaultDirectory = tempDir
363+
}) $ make cb
364+
let (expectedId, _, expectedKeyVals) = expectedAttrs
365+
assertEqual "identifier" expectedId id'
366+
assertEqual "key-value pairs" expectedKeyVals keyvals
367+
where
368+
extractCaption (B.Figure _ (Caption _ caption) _) = caption
369+
344370
codeBlock :: Toolkit -> Script -> Block
345371
codeBlock tk script = CodeBlock (mempty, [cls tk], mempty) script
346372

@@ -388,6 +414,9 @@ addWithSource :: Bool -> Block -> Block
388414
addWithSource yn (CodeBlock (id', cls, attrs) script) =
389415
CodeBlock (id', cls, attrs ++ [(tshow WithSourceK, pack . show $ yn)]) script
390416

417+
setAttrs :: Attr -> Block -> Block
418+
setAttrs attrs (CodeBlock _ script) = CodeBlock attrs script
419+
391420
-- | Assert that a file exists
392421
assertFileExists :: HasCallStack => FilePath -> Assertion
393422
assertFileExists filepath = do

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+
testAttributesPreservedOnFigure,
6162
testCleanOutputDirs,
6263
testChecksFail
6364
]

0 commit comments

Comments
 (0)