Skip to content

Commit 4b57e0a

Browse files
committed
[API change] Add Figure block constructor
The new Figure block represents a figure with attributes, caption, and arbitrary block content.
1 parent 57e7904 commit 4b57e0a

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

src/Text/Pandoc/Arbitrary.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ instance Arbitrary Blocks where
8686
flattenTableHead hd <>
8787
concatMap flattenTableBody bd <>
8888
flattenTableFoot ft
89+
flattenBlock (Figure _ capt blks) = flattenCaption capt <> blks
8990
flattenBlock (Div _ blks) = blks
9091
flattenBlock Null = []
9192

@@ -204,6 +205,10 @@ instance Arbitrary Block where
204205
[Table attr capt specs thead tbody' tfoot | tbody' <- shrink tbody] ++
205206
[Table attr capt specs thead tbody tfoot' | tfoot' <- shrink tfoot] ++
206207
[Table attr capt' specs thead tbody tfoot | capt' <- shrink capt]
208+
shrink (Figure attr capt blks) =
209+
[Figure attr capt blks' | blks' <- shrinkBlockList blks] ++
210+
[Figure attr capt' blks | capt' <- shrink capt] ++
211+
[Figure attr' capt blks | attr' <- shrinkAttr attr]
207212
shrink (Div attr blks) = (Div attr <$> shrinkBlockList blks)
208213
++ (flip Div blks <$> shrinkAttr attr)
209214
shrink Null = []
@@ -246,6 +251,9 @@ arbBlock n = frequency $ [ (10, Plain <$> arbInlines (n-1))
246251
<*> arbTableHead (n-1)
247252
<*> vectorOf bs (arbTableBody (n-1))
248253
<*> arbTableFoot (n-1))
254+
, (2, Figure <$> arbAttr
255+
<*> arbitrary
256+
<*> listOf1 (arbBlock (n-1)))
249257
]
250258

251259
arbRow :: Int -> Gen Row

src/Text/Pandoc/Builder.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ module Text.Pandoc.Builder ( module Text.Pandoc.Definition
168168
, table
169169
, simpleTable
170170
, tableWith
171+
, figure
172+
, figureWith
171173
, caption
172174
, simpleCaption
173175
, emptyCaption
@@ -560,6 +562,12 @@ simpleTable headers rows =
560562
tb = TableBody nullAttr 0 [] $ map toRow rows
561563
tf = TableFoot nullAttr []
562564

565+
figure :: Caption -> Blocks -> Blocks
566+
figure = figureWith nullAttr
567+
568+
figureWith :: Attr -> Caption -> Blocks -> Blocks
569+
figureWith attr capt = singleton . Figure attr capt . toList
570+
563571
caption :: Maybe ShortCaption -> Blocks -> Caption
564572
caption x = Caption x . toList
565573

@@ -571,7 +579,8 @@ emptyCaption = simpleCaption mempty
571579

572580
simpleFigureWith :: Attr -> Inlines -> Text -> Text -> Blocks
573581
simpleFigureWith attr figureCaption url title =
574-
para $ imageWith attr url ("fig:" <> title) figureCaption
582+
figure (simpleCaption $ plain figureCaption) . plain $
583+
imageWith attr url ("fig:" <> title) mempty
575584

576585
simpleFigure :: Inlines -> Text -> Text -> Blocks
577586
simpleFigure = simpleFigureWith nullAttr

src/Text/Pandoc/Definition.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ data TableFoot = TableFoot Attr [Row]
254254
-- | A short caption, for use in, for instance, lists of figures.
255255
type ShortCaption = [Inline]
256256

257-
-- | The caption of a table, with an optional short caption.
257+
-- | The caption of a table or figure, with optional short caption.
258258
data Caption = Caption (Maybe ShortCaption) [Block]
259259
deriving (Eq, Ord, Show, Read, Typeable, Data, Generic)
260260

@@ -301,6 +301,9 @@ data Block
301301
-- column alignments and widths (required), table head, table
302302
-- bodies, and table foot
303303
| Table Attr Caption [ColSpec] TableHead [TableBody] TableFoot
304+
-- | Figure, with attributes, caption and caption position, width
305+
-- (optional), and content (list of blocks)
306+
| Figure Attr Caption [Block]
304307
-- | Generic block container with attributes
305308
| Div Attr [Block]
306309
-- | Nothing

src/Text/Pandoc/Walk.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,10 @@ walkBlockM f (Table attr capt as hs bs fs)
472472
bs' <- walkM f bs
473473
fs' <- walkM f fs
474474
return $ Table attr capt' as hs' bs' fs'
475+
walkBlockM f (Figure attr capt blks)
476+
= do capt' <- walkM f capt
477+
blks' <- walkM f blks
478+
return $ Figure attr capt' blks'
475479

476480
-- | Perform a query on elements nested below a @'Block'@ element by
477481
-- querying all directly nested lists of @Inline@s or @Block@s.
@@ -495,6 +499,9 @@ queryBlock f (Table _ capt _ hs bs fs)
495499
query f hs <>
496500
query f bs <>
497501
query f fs
502+
queryBlock f (Figure _ capt blks)
503+
= query f capt <>
504+
query f blks
498505
queryBlock f (Div _ bs) = query f bs
499506
queryBlock _ Null = mempty
500507

test/test-pandoc-types.hs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,14 @@ t_table = ( Table
437437
tCell' i = Cell ("id", ["kls"], [("k1", "v1"), ("k2", "v2")]) AlignDefault 1 1 [Plain i]
438438
tRow = Row ("id", ["kls"], [("k1", "v1"), ("k2", "v2")])
439439

440+
t_figure :: (Block, ByteString)
441+
t_figure = (Figure
442+
("id", ["kls"], [("k1", "v1"), ("k2", "v2")])
443+
(Caption (Just [Str "hello"]) [Para [Str "cap content"]])
444+
[Para [Str "fig content"]]
445+
,[s|{"t":"Figure","c":[["id",["kls"],[["k1","v1"],["k2","v2"]]],[[{"t":"Str","c":"hello"}],[{"t":"Para","c":[{"t":"Str","c":"cap content"}]}]],[{"t":"Para","c":[{"t":"Str","c":"fig content"}]}]]}|]
446+
)
447+
440448
t_div :: (Block, ByteString)
441449
t_div = ( Div ("id", ["kls"], [("k1", "v1"), ("k2", "v2")]) [Para [Str "Hello"]]
442450
, [s|{"t":"Div","c":[["id",["kls"],[["k1","v1"],["k2","v2"]]],[{"t":"Para","c":[{"t":"Str","c":"Hello"}]}]]}|]
@@ -651,7 +659,10 @@ p_figureRepresentation = forAll (arbitrary :: Gen [Inline]) (\figureCaption ->
651659
"url"
652660
"title" ==
653661
Builder.fromList
654-
[Para [Image ("", [], []) figureCaption ("url", "fig:title") ]]
662+
[Figure
663+
nullAttr
664+
(Caption Nothing [Plain figureCaption | not (null figureCaption)])
665+
[Plain [Image ("", [], []) [] ("url", "fig:title") ]]]
655666
)
656667

657668
tests :: [Test]
@@ -727,6 +738,7 @@ tests =
727738
, testEncodeDecode "DefinitionList" t_definitionlist
728739
, testEncodeDecode "Header" t_header
729740
, testEncodeDecode "Table" t_table
741+
, testEncodeDecode "Figure" t_figure
730742
, testEncodeDecode "Div" t_div
731743
, testEncodeDecode "Null" t_null
732744
]

0 commit comments

Comments
 (0)