diff options
| author | John MacFarlane <[email protected]> | 2025-05-16 18:09:40 -0700 |
|---|---|---|
| committer | John MacFarlane <[email protected]> | 2025-05-16 18:10:06 -0700 |
| commit | ca321e1b7559040f7c73ddfec9b9b036658e1a14 (patch) | |
| tree | 14a6f0b81c57f668c59f6b03a1b05312bc1a970e | |
| parent | 24cbdbdda59f9c3a61b73839c1baaaf80d0b9943 (diff) | |
Fix problems with gridTable and add tests.
Closes #10848.
| -rw-r--r-- | pandoc.cabal | 1 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/Shared.hs | 59 | ||||
| -rw-r--r-- | test/Tests/Old.hs | 2 | ||||
| -rw-r--r-- | test/command/10848.md | 27 | ||||
| -rw-r--r-- | test/tables/nordics.markdown | 18 | ||||
| -rw-r--r-- | test/tables/planets.markdown | 31 | ||||
| -rw-r--r-- | test/tables/students.markdown | 23 |
7 files changed, 128 insertions, 33 deletions
diff --git a/pandoc.cabal b/pandoc.cabal index f87e83843..3d589b1a8 100644 --- a/pandoc.cabal +++ b/pandoc.cabal @@ -345,6 +345,7 @@ extra-source-files: test/tables/*.native test/tables/*.mediawiki test/tables/*.jats_archiving + test/tables/*.markdown test/testsuite.txt test/ansi-test.txt test/writer.latex diff --git a/src/Text/Pandoc/Writers/Shared.hs b/src/Text/Pandoc/Writers/Shared.hs index a67c491f1..4c7baad1c 100644 --- a/src/Text/Pandoc/Writers/Shared.hs +++ b/src/Text/Pandoc/Writers/Shared.hs @@ -51,7 +51,7 @@ module Text.Pandoc.Writers.Shared ( , delimited ) where -import Safe (lastMay, maximumMay, atDef) +import Safe (lastMay, maximumMay) import qualified Data.ByteString.Lazy as BL import Control.Monad (MonadPlus, mzero) import Data.Either (isRight) @@ -297,8 +297,7 @@ gridTable opts blocksToDoc colspecs' thead' tbodies' tfoot' = do let Ann.Table _ _ colspecs thead tbodies tfoot = Ann.toTable mempty (Caption Nothing mempty) colspecs' thead' tbodies' tfoot' - let widths = map (toCharWidth opts . getColWidth) colspecs - let renderRows = fmap (map (addDummies widths)) . mapM (gridRow opts blocksToDoc) + let renderRows = fmap addDummies . mapM (gridRow opts blocksToDoc) let getHeadCells (Ann.HeaderRow _ _ cells) = cells let getHeadRows (Ann.TableHead _ rs) = map getHeadCells rs headCells <- renderRows (getHeadRows thead) @@ -340,7 +339,7 @@ resetWidths (w:ws) (c:cs) = case cellColSpan c of 1 -> c{ cellWidth = w } : resetWidths ws cs n | n < 1 -> c : resetWidths ws cs - | otherwise -> c{ cellWidth = sum (take (n - 1) ws) + (3 * (n - 1)) + 1 } + | otherwise -> c{ cellWidth = w + sum (take (n - 1) ws) + (3 * (n-1)) } : resetWidths (drop (n - 1) ws) cs redoWidths :: WriterOptions -> [ColSpec] -> [[RenderedCell Text]] -> [[RenderedCell Text]] @@ -364,31 +363,49 @@ redoWidths opts colspecs rows = map (resetWidths newwidths) rows else evenwidth') fullwidths False -> actualwidths -makeDummy :: [Int] -> Int -> Int -> RenderedCell Text -makeDummy widths n len = - let width = atDef 0 widths n - in RenderedCell{ cellColNum = n, - cellColSpan = len, - cellAlign = AlignDefault, - cellRowSpan = 0, -- indicates dummy - cellWidth = width, - cellContents = mempty, - cellBottomBorder = NoLine, - cellTopBorder = NoLine } - -addDummies :: [Int] -> [RenderedCell Text] -> [RenderedCell Text] -addDummies widths cells = +makeDummy :: RenderedCell Text -> RenderedCell Text +makeDummy c = + RenderedCell{ cellColNum = cellColNum c, + cellColSpan = cellColSpan c, + cellAlign = AlignDefault, + cellRowSpan = cellRowSpan c - 1, + cellWidth = cellWidth c, + cellContents = mempty, + cellBottomBorder = NoLine, + cellTopBorder = NoLine } + +addDummies :: [[RenderedCell Text]] -> [[RenderedCell Text]] +addDummies = reverse . foldl' go [] + where + go [] cs = [cs] + go (prevRow:rs) cs = addDummiesToRow prevRow cs : prevRow : rs + addDummiesToRow [] cs = cs + addDummiesToRow ds [] = map makeDummy ds + addDummiesToRow (d:ds) (c:cs) = + if cellColNum d < cellColNum c + then makeDummy d : addDummiesToRow ds (c:cs) + else c : addDummiesToRow + (dropWhile (\x -> + cellColNum x < cellColNum c + cellColSpan c) (d:ds)) + cs + +{- reverse $ (case numcols - i' of 0 -> id - n -> (makeDummy widths i' n:)) $ cs' + -- TODO this is wrong; it assumes that the row span + -- above covers ALL the relevant columns. We need + -- to pay attention to the details of the row above + -- so we get the borders right. + -- TODO the 2 parameter is a placeholder + n -> (makeDummy widths i' n 2 :)) cs' where (i',cs') = foldl' addDummy (0,[]) cells numcols = length widths addDummy (i,cs) c = case cellColNum c - i of 0 -> (cellColNum c + cellColSpan c, c:cs) - len -> (cellColNum c + cellColSpan c, c : makeDummy widths i len : cs) - + len -> (cellColNum c + cellColSpan c, c : makeDummy widths i len 2 : cs) +-} setTopBorder :: LineStyle -> [[RenderedCell Text]] -> [[RenderedCell Text]] setTopBorder _ [] = [] diff --git a/test/Tests/Old.hs b/test/Tests/Old.hs index ad642b1a5..d74fa3d36 100644 --- a/test/Tests/Old.hs +++ b/test/Tests/Old.hs @@ -26,7 +26,7 @@ tests :: FilePath -> [TestTree] tests pandocPath = [ testGroup "markdown" [ testGroup "writer" - $ writerTests' "markdown" ++ lhsWriterTests' "markdown" + $ writerTests' "markdown" ++ lhsWriterTests' "markdown" ++ extWriterTests' "markdown" , testGroup "reader" [ test' "basic" ["-r", "markdown", "-w", "native", "-s"] "testsuite.txt" "testsuite.native" diff --git a/test/command/10848.md b/test/command/10848.md index e572aad8e..5ac700103 100644 --- a/test/command/10848.md +++ b/test/command/10848.md @@ -31,24 +31,29 @@ <table> <tr> <td colspan="2">A</td> -<td rowspan="2">J</td> -<td colspan="2">F</td> +<td colspan="2">J</td> +<td rowspan="3">F</td> </tr> <tr> -<td rowspan="2">C</td> +<td rowspan="3">C</td> <td>B</td> <td rowspan="2" colspan="2">H</td> </tr> <tr> -<td colspan="2">D</td> +<td>D</td> +</tr> +<tr> +<td colspan="4">K</td> </tr> </table> ^D -+-------+---+-------+ -| A | J | F | -+---+---+ +-------+ -| C | B | | H | -| +---+---+ | -| | D | | -+---+-------+-------+ ++-------+-------+---+ +| A | J | F | ++---+---+-------+ | +| C | B | H | | +| +---+ | | +| | D | | | +| +---+-------+---+ +| | K | ++---+---------------+ ``` diff --git a/test/tables/nordics.markdown b/test/tables/nordics.markdown new file mode 100644 index 000000000..f193b1239 --- /dev/null +++ b/test/tables/nordics.markdown @@ -0,0 +1,18 @@ + ------------------------------------------------------------------------------- + Name Capital Population\ Area\ + (in 2018) (in km^2^) + ----------------------- ----------------------- --------------- --------------- + Denmark Copenhagen 5,809,502 43,094 + + Finland Helsinki 5,537,364 338,145 + + Iceland Reykjavik 343,518 103,000 + + Norway Oslo 5,372,191 323,802 + + Sweden Stockholm 10,313,447 450,295 + + Total 27,376,022 1,258,336 + ------------------------------------------------------------------------------- + + : States belonging to the *Nordics.* {#nordics} diff --git a/test/tables/planets.markdown b/test/tables/planets.markdown new file mode 100644 index 000000000..31125ce64 --- /dev/null +++ b/test/tables/planets.markdown @@ -0,0 +1,31 @@ ++------------------+---------+------------+----------+-----------+----------+---------+-----------+-------------+--------+--------------+ +| | Name | Mass | Diameter | Density | Gravity | Length | Distance | Mean | Number | Notes | +| | | (10\^24kg) | (km) | (kg/m\^3) | (m/s\^2) | of day | from Sun | temperature | of | | +| | | | | | | (hours) | (10\^6km) | (C) | moons | | ++==================+=========+============+==========+===========+==========+=========+===========+=============+========+==============+ +| Terrestrial | Mercury | 0.330 | 4,879 | 5427 | 3.7 | 4222.6 | 57.9 | 167 | 0 | Closest to | +| planets | | | | | | | | | | the Sun | +| +---------+------------+----------+-----------+----------+---------+-----------+-------------+--------+--------------+ +| | Venus | 4.87 | 12,104 | 5243 | 8.9 | 2802.0 | 108.2 | 464 | 0 | | +| +---------+------------+----------+-----------+----------+---------+-----------+-------------+--------+--------------+ +| | Earth | 5.97 | 12,756 | 5514 | 9.8 | 24.0 | 149.6 | 15 | 1 | Our world | +| +---------+------------+----------+-----------+----------+---------+-----------+-------------+--------+--------------+ +| | Mars | 0.642 | 6,792 | 3933 | 3.7 | 24.7 | 227.9 | -65 | 2 | The red | +| | | | | | | | | | | planet | ++---------+--------+---------+------------+----------+-----------+----------+---------+-----------+-------------+--------+--------------+ +| Jovian | Gas | Jupiter | 1898 | 142,984 | 1326 | 23.1 | 9.9 | 778.6 | -110 | 67 | The largest | +| planets | giants | | | | | | | | | | planet | +| | +---------+------------+----------+-----------+----------+---------+-----------+-------------+--------+--------------+ +| | | Saturn | 568 | 120,536 | 687 | 9.0 | 10.7 | 1433.5 | -140 | 62 | | +| +--------+---------+------------+----------+-----------+----------+---------+-----------+-------------+--------+--------------+ +| | Ice | Uranus | 86.8 | 51,118 | 1271 | 8.7 | 17.2 | 2872.5 | -195 | 27 | | +| | giants | | | | | | | | | | | +| | +---------+------------+----------+-----------+----------+---------+-----------+-------------+--------+--------------+ +| | | Neptune | 102 | 49,528 | 1638 | 11.0 | 16.1 | 4495.1 | -200 | 14 | | ++---------+--------+---------+------------+----------+-----------+----------+---------+-----------+-------------+--------+--------------+ +| Dwarf planets | Pluto | 0.0146 | 2,370 | 2095 | 0.7 | 153.3 | 5906.4 | -225 | 5 | Declassified | +| | | | | | | | | | | as a planet | +| | | | | | | | | | | in 2006. | ++------------------+---------+------------+----------+-----------+----------+---------+-----------+-------------+--------+--------------+ + +: Data about the planets of our solar system. diff --git a/test/tables/students.markdown b/test/tables/students.markdown new file mode 100644 index 000000000..931c4fd8a --- /dev/null +++ b/test/tables/students.markdown @@ -0,0 +1,23 @@ ++-----------------------------------------+-----------------------------------------+ +| Student ID | Name | ++=========================================+=========================================+ +| Computer Science | ++-----------------------------------------+-----------------------------------------+ +| 3741255 | Jones, Martha | ++-----------------------------------------+-----------------------------------------+ +| 4077830 | Pierce, Benjamin | ++-----------------------------------------+-----------------------------------------+ +| 5151701 | Kirk, James | ++-----------------------------------------+-----------------------------------------+ +| Russian Literature | ++-----------------------------------------+-----------------------------------------+ +| 3971244 | Nim, Victor | ++-----------------------------------------+-----------------------------------------+ +| Astrophysics | ++-----------------------------------------+-----------------------------------------+ +| 4100332 | Petrov, Alexandra | ++-----------------------------------------+-----------------------------------------+ +| 4100332 | Toyota, Hiroko | ++-----------------------------------------+-----------------------------------------+ + +: List of Students {#students} |
