Skip to content

Commit f84b735

Browse files
tarlebargent0cjjdespres
committed
[API change] Add Figure block constructor
The new Figure block represents a figure with attributes, caption, and arbitrary block content. Co-authored-by: Aner Lucero <[email protected]> Co-authored-by: Christian Despres <[email protected]>
1 parent a34f5c2 commit f84b735

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
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: 13 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

@@ -569,9 +577,13 @@ simpleCaption = caption Nothing
569577
emptyCaption :: Caption
570578
emptyCaption = simpleCaption mempty
571579

580+
-- | Creates a simple figure from attributes, a figure caption, an image
581+
-- path and image title. The attributes are used as the image
582+
-- attributes.
572583
simpleFigureWith :: Attr -> Inlines -> Text -> Text -> Blocks
573584
simpleFigureWith attr figureCaption url title =
574-
para $ imageWith attr url ("fig:" <> title) figureCaption
585+
figure (simpleCaption (plain figureCaption)) . plain $
586+
imageWith attr url title mempty
575587

576588
simpleFigure :: Inlines -> Text -> Text -> Blocks
577589
simpleFigure = simpleFigureWith nullAttr

src/Text/Pandoc/Definition.hs

Lines changed: 3 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,8 @@ 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 content (list of blocks)
305+
| Figure Attr Caption [Block]
304306
-- | Generic block container with attributes
305307
| Div Attr [Block]
306308
-- | Nothing

src/Text/Pandoc/Walk.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,10 @@ walkBlockM f (Table attr capt as hs bs fs)
492492
bs' <- walkM f bs
493493
fs' <- walkM f fs
494494
return $ Table attr capt' as hs' bs' fs'
495+
walkBlockM f (Figure attr capt blks)
496+
= do capt' <- walkM f capt
497+
blks' <- walkM f blks
498+
return $ Figure attr capt' blks'
495499

496500
-- | Perform a query on elements nested below a @'Block'@ element by
497501
-- querying all directly nested lists of @Inline@s or @Block@s.
@@ -515,6 +519,9 @@ queryBlock f (Table _ capt _ hs bs fs)
515519
query f hs <>
516520
query f bs <>
517521
query f fs
522+
queryBlock f (Figure _ capt blks)
523+
= query f capt <>
524+
query f blks
518525
queryBlock f (Div _ bs) = query f bs
519526
queryBlock _ Null = mempty
520527

test/test-pandoc-types.hs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,14 @@ t_table = ( Table
451451
tCell' i = Cell ("id", ["kls"], [("k1", "v1"), ("k2", "v2")]) AlignDefault 1 1 [Plain i]
452452
tRow = Row ("id", ["kls"], [("k1", "v1"), ("k2", "v2")])
453453

454+
t_figure :: (Block, ByteString)
455+
t_figure = (Figure
456+
("id", ["kls"], [("k1", "v1"), ("k2", "v2")])
457+
(Caption (Just [Str "hello"]) [Para [Str "cap content"]])
458+
[Para [Str "fig content"]]
459+
,[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"}]}]]}|]
460+
)
461+
454462
t_div :: (Block, ByteString)
455463
t_div = ( Div ("id", ["kls"], [("k1", "v1"), ("k2", "v2")]) [Para [Str "Hello"]]
456464
, [s|{"t":"Div","c":[["id",["kls"],[["k1","v1"],["k2","v2"]]],[{"t":"Para","c":[{"t":"Str","c":"Hello"}]}]]}|]
@@ -658,15 +666,17 @@ t_tableNormExample = testCase "table normalization example" assertion
658666
generated = table emptyCaption spec (th initialHeads) [initialTB] (tf initialHeads)
659667

660668
p_figureRepresentation :: Property
661-
p_figureRepresentation = forAll (arbitrary :: Gen [Inline]) (\figureCaption ->
669+
p_figureRepresentation = forAll (arbitrary :: Gen [Inline]) $ \figureCaption ->
662670
simpleFigureWith
663-
("", [], [])
671+
("test", [], [])
664672
(Builder.fromList figureCaption)
665673
"url"
666674
"title" ==
667675
Builder.fromList
668-
[Para [Image ("", [], []) figureCaption ("url", "fig:title") ]]
669-
)
676+
[Figure
677+
nullAttr
678+
(Caption Nothing [Plain figureCaption | not (null figureCaption)])
679+
[Plain [Image ("test", [], []) mempty ("url", "title") ]]]
670680

671681
tests :: [Test]
672682
tests =
@@ -745,6 +755,7 @@ tests =
745755
, testEncodeDecode "DefinitionList" t_definitionlist
746756
, testEncodeDecode "Header" t_header
747757
, testEncodeDecode "Table" t_table
758+
, testEncodeDecode "Figure" t_figure
748759
, testEncodeDecode "Div" t_div
749760
, testEncodeDecode "Null" t_null
750761
]

0 commit comments

Comments
 (0)