Skip to content

Commit 7c9d6df

Browse files
committed
Fix a bug in computing table alignments.
When there are colspans in the header, alignments weren't computed correctly. With this patch, if a header cell with colspans has an alignment, that alignment is given to all the columns spanned. Resolves jgm/pandoc#9085. Found with the help of Claude Opus 5.5.
1 parent 10b584f commit 7c9d6df

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

‎src/Text/GridTable/Trace.hs‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import Data.Array.MArray
2727
import Data.Array.ST
2828
import Data.Function (on)
2929
import Data.List (foldl')
30-
import Data.Maybe (fromMaybe)
30+
import Data.Maybe (fromMaybe, listToMaybe)
3131
import Data.Set (Set)
3232
import Data.Text (Text)
3333
import Text.DocLayout (charWidth, realLength)
@@ -412,13 +412,18 @@ tableFromTraceInfo traceInfo partSeps colSpecsFirstLine =
412412
rowindex = Map.fromList $ zip rowseps [1..]
413413
colindex = Map.fromList $ zip colseps [1..]
414414
colwidths = [ b - a - 1 | (b, a) <- zip (drop 1 colseps) colseps ]
415+
alignSpecs = case partSeps of
416+
partSep:_ -> partSepColSpec partSep
417+
[] -> fromMaybe [] colSpecsFirstLine
418+
-- A segment of the alignment line may span multiple columns;
419+
-- each column gets the alignment of the segment containing it.
420+
alignFor (left, right) = fromMaybe AlignDefault $ listToMaybe
421+
[ colAlign cs | cs <- alignSpecs
422+
, colStart cs - 1 <= left
423+
, right <= colEnd cs ]
415424
colSpecs = zip
416-
(map colAlign
417-
(case partSeps of
418-
partSep:_ -> partSepColSpec partSep
419-
[] -> fromMaybe [] colSpecsFirstLine)
420-
++ repeat AlignDefault)
421-
(map fromCharCol colwidths)
425+
(map alignFor (zip colseps (drop 1 colseps)))
426+
(map fromCharCol colwidths)
422427
lastCol = ColIndex (length colwidths)
423428
mlastLine = Set.lookupMax (gridRowSeps traceInfo)
424429
tableHead = case partSeps of

‎test/test-gridtables.hs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,42 @@ gridTableTests = testGroup "parseArrayTable"
528528
, [Cell "3" 1 1]
529529
]
530530
]
531+
, testGroup "markers spanning multiple columns"
532+
[ testCase "marker in first line" $
533+
let gt = T.unlines
534+
[ "+:------------:+---------------:+"
535+
, "| Heading 1 | Heading 2 |"
536+
, "+---+----+-----+----+------+----+"
537+
, "| A | B | C | D | E | F |"
538+
, "+---+----+-----+----+------+----+"
539+
]
540+
in arrayTableColSpecs <$> parse' gridTable gt @?=
541+
Right (listArray (1, 6)
542+
[ (AlignCenter, 3)
543+
, (AlignCenter, 4)
544+
, (AlignCenter, 5)
545+
, (AlignRight, 4)
546+
, (AlignRight, 6)
547+
, (AlignRight, 4)
548+
])
549+
550+
, testCase "marker in part separator" $
551+
let gt = T.unlines
552+
[ "+-------------+-------+"
553+
, "| head | c |"
554+
, "+:===========:+======:+"
555+
, "| ab | c |"
556+
, "+------+------+-------+"
557+
, "| a | b | c |"
558+
, "+------+------+-------+"
559+
]
560+
in arrayTableColSpecs <$> parse' gridTable gt @?=
561+
Right (listArray (1, 3)
562+
[ (AlignCenter, 6)
563+
, (AlignCenter, 6)
564+
, (AlignRight, 7)
565+
])
566+
]
531567
]
532568

533569
defaultAlign :: [Int] -> Array ColIndex (Alignment, Int)

0 commit comments

Comments
 (0)