Skip to content

Commit

Permalink
Fixed an issue with figure attributes disappearing
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentRDC committed Jan 23, 2023
1 parent b08e186 commit 4aeaa4d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 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.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).
Expand Down
5 changes: 3 additions & 2 deletions 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.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
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/Text/Pandoc/Filter/Plot/Embed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions tests/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
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,
testAttributesPreservedOnFigure,
testCleanOutputDirs,
testChecksFail
]
Expand Down

0 comments on commit 4aeaa4d

Please sign in to comment.