Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simpleFigure builder [GSoC 2021] #90

Closed
wants to merge 8 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove CaptionPos
argent0 committed Jul 1, 2021
commit 74874de3264dee960e33697f365608d5229b5123
15 changes: 5 additions & 10 deletions src/Text/Pandoc/Arbitrary.hs
Original file line number Diff line number Diff line change
@@ -85,7 +85,7 @@ instance Arbitrary Blocks where
flattenBlock (Table _ _ hd bd ft) = flattenTableHead hd <>
concatMap flattenTableBody bd <>
flattenTableFoot ft
flattenBlock (Figure _ _ capt blks) = flattenCaption capt <> blks
flattenBlock (Figure _ capt blks) = flattenCaption capt <> blks
flattenBlock (Div _ blks) = blks
flattenBlock Null = []

@@ -203,10 +203,10 @@ instance Arbitrary Block where
[Table attr specs thead' tbody tfoot | thead' <- shrink thead] ++
[Table attr specs thead tbody' tfoot | tbody' <- shrink tbody] ++
[Table attr specs thead tbody tfoot' | tfoot' <- shrink tfoot]
shrink (Figure attr cp capt blks) =
[Figure attr cp capt blks' | blks' <- shrinkBlockList blks] ++
[Figure attr cp capt' blks | capt' <- shrink capt] ++
[Figure attr' cp capt blks | attr' <- shrinkAttr attr]
shrink (Figure attr capt blks) =
[Figure attr capt blks' | blks' <- shrinkBlockList blks] ++
[Figure attr capt' blks | capt' <- shrink capt] ++
[Figure attr' capt blks | attr' <- shrinkAttr attr]
shrink (Div attr blks) = (Div attr <$> shrinkBlockList blks)
++ (flip Div blks <$> shrinkAttr attr)
shrink Null = []
@@ -249,7 +249,6 @@ arbBlock n = frequency $ [ (10, Plain <$> arbInlines (n-1))
<*> vectorOf bs (arbTableBody (n-1))
<*> arbTableFoot (n-1))
, (2, Figure <$> arbAttr
<*> arbitrary
<*> arbitrary
<*> listOf1 (arbBlock (n-1)))
]
@@ -347,10 +346,6 @@ instance Arbitrary Caption where
[Caption attr mshort body' | body' <- shrinkBlockList body] ++
[Caption attr' mshort body | attr' <- shrinkAttr attr]

instance Arbitrary CaptionPos where
arbitrary
= arbitraryBoundedEnum

instance Arbitrary MathType where
arbitrary
= do x <- choose (0 :: Int, 1)
6 changes: 3 additions & 3 deletions src/Text/Pandoc/Builder.hs
Original file line number Diff line number Diff line change
@@ -560,11 +560,11 @@ simpleTable headers rows =
tb = TableBody nullAttr 0 [] $ map toRow rows
tf = TableFoot nullAttr []

figure :: CaptionPos -> Caption -> Blocks -> Blocks
figure :: Caption -> Blocks -> Blocks
figure = figureWith nullAttr

figureWith :: Attr -> CaptionPos -> Caption -> Blocks -> Blocks
figureWith attr capt cp = singleton . Figure attr capt cp . toList
figureWith :: Attr -> Caption -> Blocks -> Blocks
figureWith attr capt = singleton . Figure attr capt . toList

caption :: Maybe ShortCaption -> Blocks -> Caption
caption = captionWith nullAttr
9 changes: 1 addition & 8 deletions src/Text/Pandoc/Definition.hs
Original file line number Diff line number Diff line change
@@ -67,7 +67,6 @@ module Text.Pandoc.Definition ( Pandoc(..)
, nullAttr
, Caption(..)
, ShortCaption
, CaptionPos(..)
, RowHeadColumns(..)
, Alignment(..)
, ColWidth(..)
@@ -271,10 +270,6 @@ newtype RowSpan = RowSpan Int
newtype ColSpan = ColSpan Int
deriving (Eq, Ord, Show, Read, Typeable, Data, Generic, Num, Enum, ToJSON, FromJSON)

-- | The position of a caption relative to the content of a figure.
data CaptionPos = CaptionBefore | CaptionAfter
deriving (Eq, Ord, Show, Read, Typeable, Data, Generic, Enum, Bounded)

-- | Block element.
data Block
-- | Plain text, not a paragraph
@@ -307,7 +302,7 @@ data Block
| Table Attr [ColSpec] TableHead [TableBody] TableFoot
-- | Figure, with attributes, caption and caption position, width
-- (optional), and content (list of blocks)
| Figure Attr CaptionPos Caption [Block]
| Figure Attr Caption [Block]
-- | Generic block container with attributes
| Div Attr [Block]
-- | Nothing
@@ -410,7 +405,6 @@ $(let jsonOpts = defaultOptions
, ''ColWidth
, ''Row
, ''Caption
, ''CaptionPos
, ''TableHead
, ''TableBody
, ''TableFoot
@@ -477,7 +471,6 @@ instance NFData ListNumberDelim
instance NFData ListNumberStyle
instance NFData ColWidth
instance NFData RowHeadColumns
instance NFData CaptionPos
instance NFData Block
instance NFData Pandoc

6 changes: 3 additions & 3 deletions src/Text/Pandoc/Walk.hs
Original file line number Diff line number Diff line change
@@ -471,10 +471,10 @@ walkBlockM f (Table attr as hs bs fs)
bs' <- walkM f bs
fs' <- walkM f fs
return $ Table attr as hs' bs' fs'
walkBlockM f (Figure attr cp capt blks)
walkBlockM f (Figure attr capt blks)
= do capt' <- walkM f capt
blks' <- walkM f blks
return $ Figure attr cp capt' blks'
return $ Figure attr capt' blks'

-- | Perform a query on elements nested below a @'Block'@ element by
-- querying all directly nested lists of @Inline@s or @Block@s.
@@ -497,7 +497,7 @@ queryBlock f (Table _ _ hs bs fs)
= query f hs <>
query f bs <>
query f fs
queryBlock f (Figure _ _ capt blks)
queryBlock f (Figure _ capt blks)
= query f capt <>
query f blks
queryBlock f (Div _ bs) = query f bs
3 changes: 1 addition & 2 deletions test/test-pandoc-types.hs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import Text.Pandoc.Definition
import Text.Pandoc.Walk
import Text.Pandoc.Builder (singleton, plain, text, simpleTable, table, emptyCell,
normalizeTableHead, normalizeTableBody, normalizeTableFoot,
emptyCaption, simpleFigureWith)
simpleFigureWith)
import qualified Text.Pandoc.Builder as Builder
import Data.Generics
import Data.List (tails)
@@ -430,7 +430,6 @@ t_table = ( Table
t_figure :: (Block, ByteString)
t_figure = (Figure
("id", ["kls"], [("k1", "v1"), ("k2", "v2")])
CaptionBefore
(Caption nullAttr (Just [Str "hello"]) [Para [Str "cap content"]])
[Para [Str "fig content"]]
,[s|{"t":"Figure","c":[["id",["kls"],[["k1","v1"],["k2","v2"]]],{"t":"CaptionBefore"},[["",[],[]],[{"t":"Str","c":"hello"}],[{"t":"Para","c":[{"t":"Str","c":"cap content"}]}]],[{"t":"Para","c":[{"t":"Str","c":"fig content"}]}]]}|]