git.iko.soy/typeable/pandoc
on master help sign in
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.

commit1f69162ffd5f16367d2de0b62c9a80f4d3b9bc59 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 15 Nov 2019, 23:09 (7 years ago) parent6c1692e
4 files changed+94−15
src/Text/Pandoc/Writers/RST.hs+7−1 src/Text/Pandoc/Writers/Shared.hs+19−5 test/command/5128.md+17−9 test/command/5899.md+51
src/Text/Pandoc/Writers/RST.hs+7−1
@@ -296,7 +296,13 @@ blockToRST (Table caption aligns widths headers rows) = do
296296 opts <- gets stOptions
297297 let isSimple = all (== 0) widths
298298 tbl <- if isSimple
299− then simpleTable opts blocksToDoc headers rows
299+ then do
300+ tbl' <- simpleTable opts blocksToDoc headers rows
301+ if offset tbl' > writerColumns opts
302+ then gridTable opts blocksToDoc (all null headers)
303+ (map (const AlignDefault) aligns) widths
304+ headers rows
305+ else return tbl'
300306 else gridTable opts blocksToDoc (all null headers)
301307 (map (const AlignDefault) aligns) widths
302308 headers rows
src/Text/Pandoc/Writers/Shared.hs+19−5
@@ -229,8 +229,7 @@ gridTable opts blocksToDoc headless aligns widths headers rows = do
229229 -- handleGivenWidths wraps the given blocks in order for them to fit
230230 -- in cells with given widths. the returned content can be
231231 -- concatenated with borders and frames
232− let handleGivenWidths widths' = do
233− let widthsInChars' = officialWidthsInChars widths'
232+ let handleGivenWidthsInChars widthsInChars' = do
234233 -- replace page width (in columns) in the options with a
235234 -- given width if smaller (adjusting by two)
236235 let useWidth w = opts{writerColumns = min (w - 2) (writerColumns opts)}
@@ -241,6 +240,8 @@ gridTable opts blocksToDoc headless aligns widths headers rows = do
241240 (\cs -> zipWithM blocksToDoc columnOptions cs)
242241 rows
243242 return (widthsInChars', rawHeaders', rawRows')
243+ let handleGivenWidths widths' = handleGivenWidthsInChars
244+ (officialWidthsInChars widths')
244245 -- handleFullWidths tries to wrap cells to the page width or even
245246 -- more in cases where `--wrap=none`. thus the content here is left
246247 -- as wide as possible
@@ -262,9 +263,22 @@ gridTable opts blocksToDoc headless aligns widths headers rows = do
262263 let handleZeroWidths widths' = do
263264 (widthsInChars', rawHeaders', rawRows') <- handleFullWidths widths'
264265 if foldl' (+) 0 widthsInChars' > writerColumns opts
265− then -- use even widths
266− handleGivenWidths
267− (replicate numcols (1.0 / fromIntegral numcols) :: [Double])
266+ then do -- use even widths except for thin columns
267+ let evenCols = max 5
268+ (((writerColumns opts - 1) `div` numcols) - 3)
269+ let (numToExpand, colsToExpand) =
270+ foldr (\w (n, tot) -> if w < evenCols
271+ then (n, tot + (evenCols - w))
272+ else (n + 1, tot))
273+ (0,0) widthsInChars'
274+ let expandAllowance = colsToExpand `div` numToExpand
275+ let newWidthsInChars = map (\w -> if w < evenCols
276+ then w
277+ else min
278+ (evenCols + expandAllowance)
279+ w)
280+ widthsInChars'
281+ handleGivenWidthsInChars newWidthsInChars
268282 else return (widthsInChars', rawHeaders', rawRows')
269283 -- render the contents of header and row cells differently depending
270284 -- on command line options, widths given in this specific table, and
test/command/5128.md+17−9
@@ -1,5 +1,5 @@
11```
2−pandoc -f org -t rst
2+pandoc -f org -t rst --columns=78
33| Option | Meaning |
44|--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
55| =<= | Left alignment, additional characters are added to the right (default for string). |
@@ -7,12 +7,20 @@ pandoc -f org -t rst
77| =^= | Centered , the same amount of characters is added to the left and the right. |
88| === | 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). |
99^D
10−====== ============================================================================================================================================================================================================
11−Option Meaning
12−====== ============================================================================================================================================================================================================
13−``<`` Left alignment, additional characters are added to the right (default for string).
14−``>`` Right alignment, additional characters are added to the left.
15−``^`` Centered , the same amount of characters is added to the left and the right.
16−``=`` 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).
17−====== ============================================================================================================================================================================================================
10++--------+------------------------------------------------------------------+
11+| Option | Meaning |
12++========+==================================================================+
13+| ``<`` | Left alignment, additional characters are added to the right |
14+| | (default for string). |
15++--------+------------------------------------------------------------------+
16+| ``>`` | Right alignment, additional characters are added to the left. |
17++--------+------------------------------------------------------------------+
18+| ``^`` | Centered , the same amount of characters is added to the left |
19+| | and the right. |
20++--------+------------------------------------------------------------------+
21+| ``=`` | Padding. If a numeric value is printed with a sign, then |
22+| | additional characters are added after the sign. Otherwise it |
23+| | behaves like "``>``". This option is only available for numbers |
24+| | (default for numbers). |
25++--------+------------------------------------------------------------------+
1826```
test/command/5899.mdnew file+51
@@ -0,0 +1,51 @@
1+```
2+% pandoc -f html -t rst
3+<html>
4+ <body>
5+ <ul>
6+ <li>A list of stuff with a table inside
7+ <table>
8+ <thead>
9+ <tr>
10+ <th>First</th><th>Second</th><th>Third</th>
11+ </tr>
12+ </thead>
13+ <tbody>
14+ <tr>
15+ <td>First</td>
16+ <td>
17+ The big long table cell.
18+ The big long table cell.
19+ The big long table cell.
20+ The big long table cell.
21+ The big long table cell.
22+ The big long table cell.
23+ The big long table cell.
24+ The big long table cell.
25+ The big long table cell.
26+ The big long table cell.
27+ </td>
28+ <td>Third</td>
29+ </tr>
30+ </tbody>
31+ </table>
32+ </li>
33+ <li>Another list item</li>
34+ </ul>
35+ </body>
36+</html>
37+^D
38+- A list of stuff with a table inside
39+
40+ +-------+----------------------------------------------------+-------+
41+ | First | Second | Third |
42+ +=======+====================================================+=======+
43+ | First | The big long table cell. The big long table cell. | Third |
44+ | | The big long table cell. The big long table cell. | |
45+ | | The big long table cell. The big long table cell. | |
46+ | | The big long table cell. The big long table cell. | |
47+ | | The big long table cell. The big long table cell. | |
48+ +-------+----------------------------------------------------+-------+
49+
50+- Another list item
51+ ```