From 80ccc78a522845e5c35c6f65845a4315ba699924 Mon Sep 17 00:00:00 2001 From: TuongNM Date: Tue, 14 Oct 2025 19:07:02 +0200 Subject: RST writer: Don't use simple tables with RowSpans (#11214) --- src/Text/Pandoc/Writers/RST.hs | 17 +- test/command/rst-writer-gridtable-if-rowspans.md | 246 +++++++++++++++++++++++ 2 files changed, 258 insertions(+), 5 deletions(-) create mode 100644 test/command/rst-writer-gridtable-if-rowspans.md diff --git a/src/Text/Pandoc/Writers/RST.hs b/src/Text/Pandoc/Writers/RST.hs index cfae285ba..7a6bc29b9 100644 --- a/src/Text/Pandoc/Writers/RST.hs +++ b/src/Text/Pandoc/Writers/RST.hs @@ -328,7 +328,7 @@ blockToRST (CodeBlock (_,classes,kvs) str) = do blockToRST (BlockQuote blocks) = do contents <- blockListToRST blocks return $ nest 3 contents <> blankline -blockToRST (Table _attrs blkCapt specs thead tbody tfoot) = do +blockToRST (Table _attrs blkCapt specs thead@(TableHead _ theadRows) tbody tfoot@(TableFoot _ tfootRows)) = do let (caption, aligns, widths, headers, rows) = toLegacyTable blkCapt specs thead tbody tfoot caption' <- inlineListToRST caption @@ -341,7 +341,9 @@ blockToRST (Table _attrs blkCapt specs thead tbody tfoot) = do opts <- gets stOptions let specs' = map (\(_,width) -> (AlignDefault, width)) specs renderGrid = gridTable opts blocksToDoc specs' thead tbody tfoot - isSimple = all (== 0) widths && length widths > 1 + rowHasRowSpan (Row _ cells) = any cellHasRowSpan cells + cellHasRowSpan (Cell _ _ rowSpan _ _) = rowSpan > 1 + isSimple = all (== 0) widths && length widths > 1 && not (any rowHasRowSpan $ theadRows ++ tableBodiesToRows tbody ++ tfootRows) renderSimple = do tbl' <- simpleTable opts blocksToDoc thead tbody tfoot if offset tbl' > writerColumns opts @@ -926,7 +928,7 @@ simpleTable opts blocksToDoc (TableHead _ headers) tbody (TableFoot _ footers) = headerDocs <- if all isEmptyRow headers then return [] else fixEmpties <$> mapM rowToDoc headers - rowDocs <- fixEmpties <$> mapM rowToDoc ((concatMap tableBodyToRows tbody) ++ footers) + rowDocs <- fixEmpties <$> mapM rowToDoc ((tableBodiesToRows tbody) ++ footers) let numChars = maybe 0 maximum . NE.nonEmpty . map (offset . fst) let colWidths = map numChars $ transpose (headerDocs ++ rowDocs) let hline = nowrap $ hsep (map (\n -> literal (T.replicate n "=")) colWidths) @@ -953,8 +955,6 @@ simpleTable opts blocksToDoc (TableHead _ headers) tbody (TableFoot _ footers) = cellToDocs (Cell _ _ _ colSpan blocks) = applyColSpan colSpan <$> (blocksToDoc opts) blocks - tableBodyToRows (TableBody _ _ headerRows bodyRows) = headerRows ++ bodyRows - applyColSpan col@(ColSpan colSpan) doc | colSpan > 1 = -- Fill up columns for the col spans by adding empty docs without a ColSpan. @@ -991,3 +991,10 @@ simpleTable opts blocksToDoc (TableHead _ headers) tbody (TableFoot _ footers) = then colWidthsSum + colWidthsLength - 1 else colWidthsSum in literal $ T.replicate dashLength "-" + +-- | Concatenates the header and body Rows of a List of TableBody into a flat +-- List of Rows. +tableBodiesToRows :: [TableBody] -> [Row] +tableBodiesToRows = concatMap tableBodyToRows + where + tableBodyToRows (TableBody _ _ headerRows bodyRows) = headerRows ++ bodyRows diff --git a/test/command/rst-writer-gridtable-if-rowspans.md b/test/command/rst-writer-gridtable-if-rowspans.md new file mode 100644 index 000000000..e94cf31da --- /dev/null +++ b/test/command/rst-writer-gridtable-if-rowspans.md @@ -0,0 +1,246 @@ +``` +% pandoc -f native -t rst +[ Table + ( "" , [] , [] ) + (Caption Nothing []) + [ ( AlignDefault , ColWidthDefault ) + , ( AlignDefault , ColWidthDefault ) + , ( AlignDefault , ColWidthDefault ) + ] + (TableHead ( "" , [] , [] ) []) + [ TableBody + ( "" , [] , [] ) + (RowHeadColumns 0) + [] + [ Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "1" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 2) + (ColSpan 1) + [ Plain [ Str "2" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "3" ] ] + ] + , Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "1" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "3" ] ] + ] + , Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "1" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "2" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "3" ] ] + ] + ] + ] + (TableFoot ( "" , [] , [] ) []) +, Table + ( "" , [] , [] ) + (Caption Nothing []) + [ ( AlignDefault , ColWidthDefault ) + , ( AlignDefault , ColWidthDefault ) + , ( AlignDefault , ColWidthDefault ) + ] + (TableHead + ( "" , [] , [] ) + [ Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "1" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 2) + (ColSpan 1) + [ Plain [ Str "2" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "3" ] ] + ] + , Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "1" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "3" ] ] + ] + ]) + [ TableBody + ( "" , [] , [] ) + (RowHeadColumns 0) + [] + [ Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "1" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "2" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "3" ] ] + ] + ] + ] + (TableFoot ( "" , [] , [] ) []) +, Table + ( "" , [] , [] ) + (Caption Nothing []) + [ ( AlignDefault , ColWidthDefault ) + , ( AlignDefault , ColWidthDefault ) + , ( AlignDefault , ColWidthDefault ) + ] + (TableHead ( "" , [] , [] ) []) + [ TableBody + ( "" , [] , [] ) + (RowHeadColumns 0) + [] [] + ] + (TableFoot ( "" , [] , [] ) + [ Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "1" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 2) + (ColSpan 1) + [ Plain [ Str "2" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "3" ] ] + ] + , Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "1" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "3" ] ] + ] + , Row + ( "" , [] , [] ) + [ Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "1" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "2" ] ] + , Cell + ( "" , [] , [] ) + AlignDefault + (RowSpan 1) + (ColSpan 1) + [ Plain [ Str "3" ] ] + ] + ]) +] +^D ++---+---+---+ +| 1 | 2 | 3 | ++---+ +---+ +| 1 | | 3 | ++---+---+---+ +| 1 | 2 | 3 | ++---+---+---+ + ++---+---+---+ +| 1 | 2 | 3 | ++---+ +---+ +| 1 | | 3 | ++===+===+===+ +| 1 | 2 | 3 | ++---+---+---+ + ++===+===+===+ +| 1 | 2 | 3 | ++---+ +---+ +| 1 | | 3 | ++---+---+---+ +| 1 | 2 | 3 | ++===+===+===+ +``` -- cgit v1.2.3