Skip to content

Commit e2e62e3

Browse files
argent0jgm
authored andcommitted
Add the SimpleFigure bidirectional pattern synonym.
1 parent 15d08bf commit e2e62e3

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

src/Text/Pandoc/Builder.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, DeriveDataTypeable,
22
GeneralizedNewtypeDeriving, CPP, StandaloneDeriving, DeriveGeneric,
33
DeriveTraversable, OverloadedStrings, PatternGuards #-}
4+
45
{-
56
Copyright (C) 2010-2019 John MacFarlane
67
@@ -170,6 +171,8 @@ module Text.Pandoc.Builder ( module Text.Pandoc.Definition
170171
, caption
171172
, simpleCaption
172173
, emptyCaption
174+
, simpleFigureWith
175+
, simpleFigure
173176
, divWith
174177
-- * Table processing
175178
, normalizeTableHead
@@ -566,6 +569,13 @@ simpleCaption = caption Nothing
566569
emptyCaption :: Caption
567570
emptyCaption = simpleCaption mempty
568571

572+
simpleFigureWith :: Attr -> Inlines -> Text -> Text -> Blocks
573+
simpleFigureWith attr figureCaption url title =
574+
para $ imageWith attr url ("fig:" <> title) figureCaption
575+
576+
simpleFigure :: Inlines -> Text -> Text -> Blocks
577+
simpleFigure = simpleFigureWith nullAttr
578+
569579
divWith :: Attr -> Blocks -> Blocks
570580
divWith attr = singleton . Div attr . toList
571581

src/Text/Pandoc/Definition.hs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{-# LANGUAGE OverloadedStrings, DeriveDataTypeable, DeriveGeneric,
22
FlexibleContexts, GeneralizedNewtypeDeriving, PatternGuards, CPP,
3-
TemplateHaskell #-}
3+
TemplateHaskell , PatternSynonyms, ViewPatterns #-}
44

55
{-
66
Copyright (c) 2006-2019, John MacFarlane
@@ -57,6 +57,7 @@ module Text.Pandoc.Definition ( Pandoc(..)
5757
, docAuthors
5858
, docDate
5959
, Block(..)
60+
, pattern SimpleFigure
6061
, Inline(..)
6162
, ListAttributes
6263
, ListNumberStyle(..)
@@ -99,6 +100,7 @@ import Control.DeepSeq
99100
import Paths_pandoc_types (version)
100101
import Data.Version (Version, versionBranch)
101102
import Data.Semigroup (Semigroup(..))
103+
import Control.Arrow (second)
102104

103105
data Pandoc = Pandoc Meta [Block]
104106
deriving (Eq, Ord, Read, Show, Typeable, Data, Generic)
@@ -311,6 +313,34 @@ data QuoteType = SingleQuote | DoubleQuote deriving (Show, Eq, Ord, Read, Typeab
311313
-- | Link target (URL, title).
312314
type Target = (Text, Text)
313315

316+
isFigureTarget :: Target -> Maybe Target
317+
isFigureTarget tgt
318+
| (src, Just tit) <- second (T.stripPrefix "fig:") tgt = Just (src, tit)
319+
| otherwise = Nothing
320+
321+
-- | Bidirectional patter synonym
322+
--
323+
-- It can pass as a Block constructor
324+
--
325+
-- >>> SimpleFigure nullAttr [] (T.pack "", T.pack "title")
326+
-- Para [Image ("",[],[]) [] ("","fig:title")]
327+
--
328+
--
329+
-- It can be used to pattern match
330+
-- >>> let img = Para [Image undefined undefined (undefined, T.pack "title")]
331+
-- >>> case img of { SimpleFigure _ _ _ -> True; _ -> False }
332+
-- False
333+
-- >>> let fig = Para [Image undefined undefined (undefined, T.pack "fig:title")]
334+
-- >>> case fig of { SimpleFigure _ _ tit -> snd tit; _ -> T.pack "" }
335+
-- "title"
336+
pattern SimpleFigure :: Attr -> [Inline] -> Target -> Block
337+
pattern SimpleFigure attr figureCaption tgt <-
338+
Para [Image attr figureCaption
339+
(isFigureTarget -> Just tgt)] where
340+
SimpleFigure attr figureCaption tgt =
341+
Para [Image attr figureCaption (second ("fig:" <>) tgt)]
342+
343+
314344
-- | Type of math element (display or inline).
315345
data MathType = DisplayMath | InlineMath deriving (Show, Eq, Ord, Read, Typeable, Data, Generic)
316346

test/test-pandoc-types.hs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import Text.Pandoc.Definition
55
import Text.Pandoc.Walk
66
import Text.Pandoc.Builder (singleton, plain, text, simpleTable, table, emptyCell,
77
normalizeTableHead, normalizeTableBody, normalizeTableFoot,
8-
emptyCaption)
8+
emptyCaption, simpleFigureWith)
9+
import qualified Text.Pandoc.Builder as Builder
910
import Data.Generics
1011
import Data.List (tails)
1112
import Test.HUnit (Assertion, assertEqual, assertFailure)
1213
import Data.Aeson (FromJSON, ToJSON, encode, decode)
1314
import Test.Framework
1415
import Test.Framework.Providers.QuickCheck2 (testProperty)
1516
import Test.Framework.Providers.HUnit (testCase)
16-
import Test.QuickCheck (forAll, choose, Property, Arbitrary, Testable)
17+
import Test.QuickCheck (forAll, choose, Property, Arbitrary, Testable, arbitrary, Gen)
1718
import qualified Data.Map as M
1819
import Data.Text (Text)
1920
import qualified Data.Text as T
@@ -642,6 +643,17 @@ t_tableNormExample = testCase "table normalization example" assertion
642643
(tf finalHeads)
643644
generated = table emptyCaption spec (th initialHeads) [initialTB] (tf initialHeads)
644645

646+
p_figureRepresentation :: Property
647+
p_figureRepresentation = forAll (arbitrary :: Gen [Inline]) (\figureCaption ->
648+
simpleFigureWith
649+
("", [], [])
650+
(Builder.fromList figureCaption)
651+
"url"
652+
"title" ==
653+
Builder.fromList
654+
[Para [Image ("", [], []) figureCaption ("url", "fig:title") ]]
655+
)
656+
645657
tests :: [Test]
646658
tests =
647659
[ testGroup "Walk"
@@ -744,6 +756,8 @@ tests =
744756
]
745757
, t_tableSan
746758
, t_tableNormExample
759+
, testGroup "Figure"
760+
[ testProperty "p_figureRepresentation figure representation" p_figureRepresentation ]
747761
]
748762

749763

0 commit comments

Comments
 (0)