Skip to content

Commit 8e9ca37

Browse files
authored
Change the table builder to permit looser intermediate table heads (#77)
The table builder (and the normalizeTableBody function) now permit cells in the intermediate head of a TableBody to extend past the RowHeadColumns. This allows for intermediate tables to have subheadings that extend across the entire table. Formerly the table builder would treat the intermediate head like the intermediate body, and clip or drop cells that extended past the row head.
1 parent dc56b9a commit 8e9ca37

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

src/Text/Pandoc/Builder.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,13 +579,16 @@ normalizeTableHead twidth (TableHead attr rows)
579579

580580
-- | Normalize the intermediate head and body section of a
581581
-- 'TableBody', as in 'normalizeTableHead', but additionally ensure
582-
-- that row head cells do not go beyond the row head.
582+
-- that row head cells do not go beyond the row head inside the
583+
-- intermediate body.
583584
normalizeTableBody :: Int -> TableBody -> TableBody
584585
normalizeTableBody twidth (TableBody attr rhc th tb)
585-
= TableBody attr rhc' (normBody th) (normBody tb)
586+
= TableBody attr
587+
rhc'
588+
(normalizeHeaderSection twidth th)
589+
(normalizeBodySection twidth rhc' tb)
586590
where
587591
rhc' = max 0 $ min (RowHeadColumns twidth) rhc
588-
normBody = normalizeBodySection twidth rhc'
589592

590593
-- | Normalize the 'TableFoot', as in 'normalizeTableHead'.
591594
normalizeTableFoot :: Int -> TableFoot -> TableFoot

src/Text/Pandoc/Definition.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,9 @@ data Row = Row Attr [Cell]
238238
data TableHead = TableHead Attr [Row]
239239
deriving (Eq, Ord, Show, Read, Typeable, Data, Generic)
240240

241-
-- | A body of a table, with an intermediate head and the specified
242-
-- number of row header columns.
241+
-- | A body of a table, with an intermediate head, intermediate body,
242+
-- and the specified number of row header columns in the intermediate
243+
-- body.
243244
data TableBody = TableBody Attr RowHeadColumns [Row] [Row]
244245
deriving (Eq, Ord, Show, Read, Typeable, Data, Generic)
245246

test/test-pandoc-types.hs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,18 @@ cellsSubsetPad xs _ = all isPadCell xs
468468
rowSubset :: Row -> Row -> Bool
469469
rowSubset (Row a1 x1) (Row a2 x2) = a1 == a2 && cellsSubsetPad x1 x2
470470

471+
-- The remarks in rowSubset apply.
472+
rowsSubset :: [Row] -> [Row] -> Bool
473+
rowsSubset (x:xs) (y:ys) = rowSubset x y && rowsSubset xs ys
474+
rowsSubset [] _ = True
475+
rowsSubset (_:_) [] = False
476+
471477
normIsSubset :: (Arbitrary a, Show a, Eq a)
472478
=> (Int -> a -> a)
473479
-> (a -> [Row])
474480
-> Property
475481
normIsSubset f proj = withWidth $
476482
\n a -> let a' = f n a in proj a' `rowsSubset` proj a
477-
where
478-
rowsSubset (x:xs) (y:ys) = rowSubset x y && rowsSubset xs ys
479-
rowsSubset [] _ = True
480-
rowsSubset (_:_) [] = False
481483

482484
p_tableNormHeadIsSubset :: Property
483485
p_tableNormHeadIsSubset = normIsSubset normalizeTableHead thproj
@@ -486,7 +488,9 @@ p_tableNormHeadIsSubset = normIsSubset normalizeTableHead thproj
486488

487489
-- Checking that each row is a subset of its unnormalized version is a
488490
-- little onerous in the TableBody (because of the row head/row body
489-
-- distinction), so we settle for testing it only for the first row.
491+
-- distinction), so we settle for testing it only for the first row of
492+
-- the intermediate body. The intermediate head is still checked
493+
-- fully.
490494
p_tableNormBodyIsSubset :: Property
491495
p_tableNormBodyIsSubset = withWidth $
492496
\n tb -> checkBody n (normalizeTableBody n tb) tb
@@ -512,7 +516,7 @@ p_tableNormBodyIsSubset = withWidth $
512516
checkRows _ _ [] [] = True
513517
checkRows _ _ _ _ = False
514518
checkBody n (TableBody _ (RowHeadColumns rhc) th' tb') (TableBody _ _ th tb)
515-
= checkRows n rhc th' th && checkRows n rhc tb' tb
519+
= rowsSubset th' th && checkRows n rhc tb' tb
516520

517521
p_tableNormFootIsSubset :: Property
518522
p_tableNormFootIsSubset = normIsSubset normalizeTableFoot tfproj
@@ -570,20 +574,18 @@ t_tableNormExample = testCase "table normalization example" assertion
570574
,[cl "c" 1 1]
571575
]
572576
initialTB = tb 1
573-
[[cl "e" 4 3,cl "f" 4 3]
577+
[[]
578+
,[cl "g" (-7) 0,cl "h" 4 1]]
579+
[[cl "e" 4 3 ,cl "f" 4 3]
574580
,[]
575581
,[emptyCell]
576582
]
577-
[[]
578-
,[cl "g" (-7) 0]]
579583
finalTB = tb 1
584+
[[emptyCell,emptyCell,emptyCell]
585+
,[cl "g" 1 1,cl "h" 1 1,emptyCell]]
580586
[[cl "e" 3 1,cl "f" 3 2]
581587
,[]
582-
,[]
583-
]
584-
[[emptyCell,emptyCell,emptyCell]
585-
,[cl "g" 1 1,emptyCell,emptyCell]
586-
]
588+
,[]]
587589
spec = replicate 3 (AlignDefault, ColWidthDefault)
588590
expected = singleton $ Table nullAttr
589591
emptyCaption

0 commit comments

Comments
 (0)