@@ -16,7 +16,9 @@ module Text.Pandoc.Filter.Plot.Embed
1616 )
1717where
1818
19+ import Control.Exception (Exception , throwIO )
1920import Data.Default (def )
21+ import Data.Maybe (listToMaybe )
2022import Data.Text (Text , pack )
2123import qualified Data.Text.IO as T
2224import Text.HTML.TagSoup
@@ -70,7 +72,7 @@ toFigure fmt spec = do
7072 Right c -> do
7173 debug $ " Parsed caption: " <> (pack $ show c)
7274 pure $ fromList c
73-
75+
7476 let srcLink = link scp mempty (str sourceLabel)
7577 attrs' = blockAttrs spec
7678 captionLinks = mconcat [" (" , srcLink, " )" ]
@@ -88,11 +90,15 @@ figure ::
8890 Inlines ->
8991 PlotM Block
9092figure as fp caption' =
91- return . head . toList $
92- if null caption'
93+ maybe
94+ (liftIO $ throwIO PandocBuilderException ) -- We cannot build a single `Block` unfortunately, so we must be ready to throw an exception.
95+ pure
96+ . listToMaybe
97+ . toList
98+ $ if null caption'
9399 -- If there is no caption, a LaTeX figure may look strange. See #37
94100 then plain $ imageWith as (pack fp) mempty caption'
95- else
101+ else
96102 -- We want the attributes both on the Figure element and the contained Image element
97103 -- so that pandoc-plot plays nice with pandoc-crossref and other filters
98104 figureWith as (simpleCaption (plain caption')) $
@@ -199,6 +205,14 @@ extractPlot t =
199205-- | Get content inside a tag, e.g. /inside "body"/ returns all tags
200206-- between /<body>/ and /</body>/
201207inside :: Text -> [Tag Text ] -> [Tag Text ]
202- inside t = init . tail . tgs
208+ inside t = init . drop 1 . tgs
203209 where
204210 tgs = takeWhile (~/= TagClose t) . dropWhile (~/= TagOpen t [] )
211+
212+
213+ -- | Exception thrown when a Pandoc builder (e.g. `Blocks`)
214+ -- has an unexpected structure.
215+ data PandocBuilderException = PandocBuilderException
216+ deriving (Show )
217+
218+ instance Exception PandocBuilderException
0 commit comments