Skip to content

Commit

Permalink
RST writer: fix two issues with list tables.
Browse files Browse the repository at this point in the history
- Fix alignment of list items corresponding to cells.
- Don't enclose the list table in a `.. table::`; this leads to
  doubled captions.

Closes #10226.
Closes #10227.
Modified test output for #4564.
  • Loading branch information
jgm committed Sep 27, 2024
1 parent 5bd1dbf commit 49d3383
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 65 deletions.
20 changes: 10 additions & 10 deletions src/Text/Pandoc/Writers/RST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ blockToRST (Table _attrs blkCapt specs thead tbody tfoot) = do
| otherwise = renderGrid
tbl <- rendered
return $ blankline $$
(if null caption
(if null caption || isList
then tbl
else (".. table:: " <> caption') $$ blankline $$ nest 3 tbl) $$
blankline
Expand Down Expand Up @@ -547,18 +547,18 @@ tableToRSTList caption _ propWidths headers rows = do
toColumns :: Int -> Double -> Int
toColumns t p = round (p * fromIntegral t)
listTableContent :: PandocMonad m => [[[Block]]] -> RST m (Doc Text)
listTableContent = joinTable joinDocsM joinDocsM .
listTableContent = joinTable (joinDocsM '-') (joinDocsM '*') .
mapTable blockListToRST
-- joinDocsM adapts joinDocs in order to work in the `RST m` monad
joinDocsM :: PandocMonad m => [RST m (Doc Text)] -> RST m (Doc Text)
joinDocsM = fmap joinDocs . sequence
joinDocsM :: PandocMonad m
=> Char -> [RST m (Doc Text)] -> RST m (Doc Text)
joinDocsM c = fmap (joinDocs c) . sequence
-- joinDocs will be used to join cells and to join rows
joinDocs :: [Doc Text] -> Doc Text
joinDocs items = blankline $$
(chomp . vcat . map formatItem) items $$
blankline
formatItem :: Doc Text -> Doc Text
formatItem i = hang 3 "- " (i <> cr)
joinDocs :: Char -> [Doc Text] -> Doc Text
joinDocs c items = (chomp . vcat . map (formatItem c)) items $$
blankline
formatItem :: Char -> Doc Text -> Doc Text
formatItem c i = hang 2 (text [c, ' ']) (i <> cr)
-- apply a function to all table cells changing their type
mapTable :: (a -> b) -> [[a]] -> [[b]]
mapTable = map . map
Expand Down
103 changes: 48 additions & 55 deletions test/command/4564.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,58 @@
```
% pandoc -f native -t rst --list-tables
[BlockQuote
[Table ("",[],[]) (Caption Nothing
[])
[(AlignDefault,ColWidth 0.1527777777777778)
,(AlignDefault,ColWidth 0.1388888888888889)
,(AlignDefault,ColWidth 0.16666666666666666)
,(AlignDefault,ColWidth 0.375)]
(TableHead ("",[],[])
[Table ("",[],[]) (Caption Nothing
[Plain [Str "Here",Space,Str "is",Space,Str "a",Space,Str "caption."]])
[(AlignDefault,ColWidth 0.1527777777777778)
,(AlignDefault,ColWidth 0.1388888888888889)
,(AlignDefault,ColWidth 0.16666666666666666)
,(AlignDefault,ColWidth 0.375)]
(TableHead ("",[],[])
[Row ("",[],[])
[Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "Centered",SoftBreak,Str "Header"]]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "Left",SoftBreak,Str "Aligned"]]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "Right",SoftBreak,Str "Aligned"]]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "Default",Space,Str "aligned"]]]])
[(TableBody ("",[],[]) (RowHeadColumns 0)
[]
[Row ("",[],[])
[Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "Centered",SoftBreak,Str "Header"]]
[Plain [Str "First"]]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "Left",SoftBreak,Str "Aligned"]]
[Plain []]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "Right",SoftBreak,Str "Aligned"]]
[Plain [Str "12.0"]]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "Default",Space,Str "aligned"]]]])
[(TableBody ("",[],[]) (RowHeadColumns 0)
[]
[Row ("",[],[])
[Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "First"]]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "row"]]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "12.0"]]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "Example",Space,Str "of",Space,Str "a",Space,Str "row",Space,Str "that",SoftBreak,Str "spans",Space,Str "multiple",Space,Str "lines."]]]
,Row ("",[],[])
[Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "Second"]]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "row"]]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "5.0"]]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "Here\8217s",Space,Str "another",Space,Str "one.",Space,Str "Note",SoftBreak,Str "the",Space,Str "blank",Space,Str "line",Space,Str "between",SoftBreak,Str "rows."]]]])]
(TableFoot ("",[],[])
[])]]
[Plain [Str "Example",Space,Str "of",Space,Str "a",Space,Str "row",Space,Str "that",SoftBreak,Str "spans",Space,Str "multiple",Space,Str "lines."]]]
,Row ("",[],[])
[Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "Second"]]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "row"]]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "5.0"]]
,Cell ("",[],[]) AlignDefault (RowSpan 1) (ColSpan 1)
[Plain [Str "Here\8217s",Space,Str "another",Space,Str "one.",Space,Str "Note",SoftBreak,Str "the",Space,Str "blank",Space,Str "line",Space,Str "between",SoftBreak,Str "rows."]]]])]
(TableFoot ("",[],[])
[])]
^D
.. list-table::
:widths: 11 10 12 27
:header-rows: 1
-
- Centered Header
- Left Aligned
- Right Aligned
- Default aligned
-
- First
- row
- 12.0
- Example of a row that spans multiple lines.
-
.. list-table:: Here is a caption.
:widths: 11 10 12 27
:header-rows: 1
- Second
- row
- 5.0
- Here’s another one. Note the blank line between rows.
* - Centered Header
- Left Aligned
- Right Aligned
- Default aligned
* - First
-
- 12.0
- Example of a row that spans multiple lines.
* - Second
- row
- 5.0
- Here’s another one. Note the blank line between rows.
```

0 comments on commit 49d3383

Please sign in to comment.