aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2019-11-15 23:09:53 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2019-11-15 23:09:53 -0800
commit1f69162ffd5f16367d2de0b62c9a80f4d3b9bc59 (patch)
tree40177e3c06735d88b1d1b8799454cb78b117b1b0
parent6c1692ea2271446caba6e95d43aeee6db805e3ce (diff)
downloadpandoc-1f69162ffd5f16367d2de0b62c9a80f4d3b9bc59.tar.gz
RST writer: Improve spacing for tables with no width information.
If a simple table would be too wide, we use a grid table. The code for generating grid tables has been adjusted to give more intelligent column widths when widths aren't given. (This also affects the markdown writer.) Closes #5899.
-rw-r--r--src/Text/Pandoc/Writers/RST.hs8
-rw-r--r--src/Text/Pandoc/Writers/Shared.hs24
-rw-r--r--test/command/5128.md26
-rw-r--r--test/command/5899.md51
4 files changed, 94 insertions, 15 deletions
diff --git a/src/Text/Pandoc/Writers/RST.hs b/src/Text/Pandoc/Writers/RST.hs
index 4a3b1b066..dc732b0e6 100644
--- a/src/Text/Pandoc/Writers/RST.hs
+++ b/src/Text/Pandoc/Writers/RST.hs
@@ -296,7 +296,13 @@ blockToRST (Table caption aligns widths headers rows) = do
opts <- gets stOptions
let isSimple = all (== 0) widths
tbl <- if isSimple
- then simpleTable opts blocksToDoc headers rows
+ then do
+ tbl' <- simpleTable opts blocksToDoc headers rows
+ if offset tbl' > writerColumns opts
+ then gridTable opts blocksToDoc (all null headers)
+ (map (const AlignDefault) aligns) widths
+ headers rows
+ else return tbl'
else gridTable opts blocksToDoc (all null headers)
(map (const AlignDefault) aligns) widths
headers rows
diff --git a/src/Text/Pandoc/Writers/Shared.hs b/src/Text/Pandoc/Writers/Shared.hs
index 39035832f..0161db506 100644
--- a/src/Text/Pandoc/Writers/Shared.hs
+++ b/src/Text/Pandoc/Writers/Shared.hs
@@ -229,8 +229,7 @@ gridTable opts blocksToDoc headless aligns widths headers rows = do
-- handleGivenWidths wraps the given blocks in order for them to fit
-- in cells with given widths. the returned content can be
-- concatenated with borders and frames
- let handleGivenWidths widths' = do
- let widthsInChars' = officialWidthsInChars widths'
+ let handleGivenWidthsInChars widthsInChars' = do
-- replace page width (in columns) in the options with a
-- given width if smaller (adjusting by two)
let useWidth w = opts{writerColumns = min (w - 2) (writerColumns opts)}
@@ -241,6 +240,8 @@ gridTable opts blocksToDoc headless aligns widths headers rows = do
(\cs -> zipWithM blocksToDoc columnOptions cs)
rows
return (widthsInChars', rawHeaders', rawRows')
+ let handleGivenWidths widths' = handleGivenWidthsInChars
+ (officialWidthsInChars widths')
-- handleFullWidths tries to wrap cells to the page width or even
-- more in cases where `--wrap=none`. thus the content here is left
-- as wide as possible
@@ -262,9 +263,22 @@ gridTable opts blocksToDoc headless aligns widths headers rows = do
let handleZeroWidths widths' = do
(widthsInChars', rawHeaders', rawRows') <- handleFullWidths widths'
if foldl' (+) 0 widthsInChars' > writerColumns opts
- then -- use even widths
- handleGivenWidths
- (replicate numcols (1.0 / fromIntegral numcols) :: [Double])
+ then do -- use even widths except for thin columns
+ let evenCols = max 5
+ (((writerColumns opts - 1) `div` numcols) - 3)
+ let (numToExpand, colsToExpand) =
+ foldr (\w (n, tot) -> if w < evenCols
+ then (n, tot + (evenCols - w))
+ else (n + 1, tot))
+ (0,0) widthsInChars'
+ let expandAllowance = colsToExpand `div` numToExpand
+ let newWidthsInChars = map (\w -> if w < evenCols
+ then w
+ else min
+ (evenCols + expandAllowance)
+ w)
+ widthsInChars'
+ handleGivenWidthsInChars newWidthsInChars
else return (widthsInChars', rawHeaders', rawRows')
-- render the contents of header and row cells differently depending
-- on command line options, widths given in this specific table, and
diff --git a/test/command/5128.md b/test/command/5128.md
index 2ab051c04..35866547e 100644
--- a/test/command/5128.md
+++ b/test/command/5128.md
@@ -1,5 +1,5 @@
```
-pandoc -f org -t rst
+pandoc -f org -t rst --columns=78
| Option | Meaning |
|--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| =<= | Left alignment, additional characters are added to the right (default for string). |
@@ -7,12 +7,20 @@ pandoc -f org -t rst
| =^= | Centered , the same amount of characters is added to the left and the right. |
| === | Padding. If a numeric value is printed with a sign, then additional characters are added after the sign. Otherwise it behaves like "=>=". This option is only available for numbers (default for numbers). |
^D
-====== ============================================================================================================================================================================================================
-Option Meaning
-====== ============================================================================================================================================================================================================
-``<`` Left alignment, additional characters are added to the right (default for string).
-``>`` Right alignment, additional characters are added to the left.
-``^`` Centered , the same amount of characters is added to the left and the right.
-``=`` Padding. If a numeric value is printed with a sign, then additional characters are added after the sign. Otherwise it behaves like "``>``". This option is only available for numbers (default for numbers).
-====== ============================================================================================================================================================================================================
++--------+------------------------------------------------------------------+
+| Option | Meaning |
++========+==================================================================+
+| ``<`` | Left alignment, additional characters are added to the right |
+| | (default for string). |
++--------+------------------------------------------------------------------+
+| ``>`` | Right alignment, additional characters are added to the left. |
++--------+------------------------------------------------------------------+
+| ``^`` | Centered , the same amount of characters is added to the left |
+| | and the right. |
++--------+------------------------------------------------------------------+
+| ``=`` | Padding. If a numeric value is printed with a sign, then |
+| | additional characters are added after the sign. Otherwise it |
+| | behaves like "``>``". This option is only available for numbers |
+| | (default for numbers). |
++--------+------------------------------------------------------------------+
```
diff --git a/test/command/5899.md b/test/command/5899.md
new file mode 100644
index 000000000..28e020d20
--- /dev/null
+++ b/test/command/5899.md
@@ -0,0 +1,51 @@
+```
+% pandoc -f html -t rst
+<html>
+ <body>
+ <ul>
+ <li>A list of stuff with a table inside
+ <table>
+ <thead>
+ <tr>
+ <th>First</th><th>Second</th><th>Third</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>First</td>
+ <td>
+ The big long table cell.
+ The big long table cell.
+ The big long table cell.
+ The big long table cell.
+ The big long table cell.
+ The big long table cell.
+ The big long table cell.
+ The big long table cell.
+ The big long table cell.
+ The big long table cell.
+ </td>
+ <td>Third</td>
+ </tr>
+ </tbody>
+ </table>
+ </li>
+ <li>Another list item</li>
+ </ul>
+ </body>
+</html>
+^D
+- A list of stuff with a table inside
+
+ +-------+----------------------------------------------------+-------+
+ | First | Second | Third |
+ +=======+====================================================+=======+
+ | First | The big long table cell. The big long table cell. | Third |
+ | | The big long table cell. The big long table cell. | |
+ | | The big long table cell. The big long table cell. | |
+ | | The big long table cell. The big long table cell. | |
+ | | The big long table cell. The big long table cell. | |
+ +-------+----------------------------------------------------+-------+
+
+- Another list item
+ ```