From 49df8131a5069f24c60714aee617510978053cbc Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 25 Apr 2024 14:26:17 -0700 Subject: [PATCH] Markdown writer: don't try to align columns in pipe tables... ...with lines greater than COLUMNS. The alignment just reduces readibility when the lines soft wrap. --- src/Text/Pandoc/Writers/Markdown/Table.hs | 15 ++++++++++----- test/command/3734.md | 14 +++++++------- test/command/7847.md | 14 +++++++------- test/command/9346.md | 2 +- test/tables.markua | 16 ++++++++-------- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/Text/Pandoc/Writers/Markdown/Table.hs b/src/Text/Pandoc/Writers/Markdown/Table.hs index caef0a0ea..dbffc7da3 100644 --- a/src/Text/Pandoc/Writers/Markdown/Table.hs +++ b/src/Text/Pandoc/Writers/Markdown/Table.hs @@ -36,15 +36,18 @@ pipeTable :: PandocMonad m -> MD m (Doc Text) pipeTable opts headless aligns widths rawHeaders rawRows = do let sp = literal " " - let blockFor AlignLeft x y = lblock (x + 2) (sp <> y) <> lblock 0 empty - blockFor AlignCenter x y = cblock (x + 2) (sp <> y <> sp) <> lblock 0 empty - blockFor AlignRight x y = rblock (x + 2) (y <> sp) <> lblock 0 empty - blockFor _ x y = lblock (x + 2) (sp <> y) <> lblock 0 empty let contentWidths = map (max 3 . maybe 3 maximum . nonEmpty . map offset) $ transpose (rawHeaders : rawRows) let colwidth = writerColumns opts let numcols = length contentWidths let maxwidth = sum contentWidths + -- if cell contents are > COLUMNS, adding padding looks bad + let pad = maxwidth <= writerColumns opts + let blockFor _ _ y | not pad = sp <> y <> sp <> lblock 0 empty + blockFor AlignLeft x y = lblock (x + 2) (sp <> y) <> lblock 0 empty + blockFor AlignCenter x y = cblock (x + 2) (sp <> y <> sp) <> lblock 0 empty + blockFor AlignRight x y = rblock (x + 2) (y <> sp) <> lblock 0 empty + blockFor AlignDefault x y = lblock (x + 2) (sp <> y) <> lblock 0 empty variant <- asks envVariant let pipeWidths = if variant == Markdown && not (all (== 0) widths) && @@ -54,7 +57,9 @@ pipeTable opts headless aligns widths rawHeaders rawRows = do floor . (* fromIntegral (colwidth - (numcols +1)))) widths - else contentWidths + else if pad + then contentWidths + else map (const 2) widths let torow cs = nowrap $ literal "|" <> hcat (intersperse (literal "|") $ zipWith3 blockFor aligns contentWidths (map chomp cs)) diff --git a/test/command/3734.md b/test/command/3734.md index 6bdf1a14b..832712c5f 100644 --- a/test/command/3734.md +++ b/test/command/3734.md @@ -4,9 +4,9 @@ |--------------|-------|--------------------------------------------------------------------------| | aaaaaaaaaaaa | | cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc | ^D -| aaaaaaaaaaaa | bbbbb | ccccccccccc | +| aaaaaaaaaaaa | bbbbb | ccccccccccc | |------------|-------|------------------------------------------------------| -| aaaaaaaaaaaa | | cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc | +| aaaaaaaaaaaa | | cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc | ``` ``` @@ -15,9 +15,9 @@ |--------------|-------|--------------------------------------------------------------------------| | aaaaaaaaaaaa | | cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc | ^D -| aaaaaaaaaaaa | bbbbb | ccccccccccc | +| aaaaaaaaaaaa | bbbbb | ccccccccccc | |------------|-------|------------------------------------------------------| -| aaaaaaaaaaaa | | cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc | +| aaaaaaaaaaaa | | cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc | ``` ``` @@ -26,7 +26,7 @@ |--------------|-------|--------------------------------------------------------------------------| | aaaaaaaaaaaa | | cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc | ^D -| aaaaaaaaaaaa | bbbbb | ccccccccccc | -|--------------|-------|--------------------------------------------------------------------------| -| aaaaaaaaaaaa | | cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc | +| aaaaaaaaaaaa | bbbbb | ccccccccccc | +|----|----|----| +| aaaaaaaaaaaa | | cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc | ``` diff --git a/test/command/7847.md b/test/command/7847.md index e714f3e00..06fa5cc46 100644 --- a/test/command/7847.md +++ b/test/command/7847.md @@ -23,9 +23,9 @@ sit amet, rhoncus porta, malesuada elementum, nisi. ^D -| aaa | bbb | ccc | +| aaa | bbb | ccc | |---------------|------------------------------------------|---------------| -| Consequat nisi sit amet nibh. Nunc mi tortor, tristique sit amet, rhoncus porta, malesuada elementum, nisi. | | ccc | +| Consequat nisi sit amet nibh. Nunc mi tortor, tristique sit amet, rhoncus porta, malesuada elementum, nisi. | | ccc | ``` @@ -45,9 +45,9 @@ sit amet, rhoncus porta, malesuada elementum, nisi. ^D -| aaa | bbb | ccc | -|-------------------------------------------------------------------------------------------------------------|-----|-----| -| Consequat nisi sit amet nibh. Nunc mi tortor, tristique sit amet, rhoncus porta, malesuada elementum, nisi. | | ccc | +| aaa | bbb | ccc | +|----|----|----| +| Consequat nisi sit amet nibh. Nunc mi tortor, tristique sit amet, rhoncus porta, malesuada elementum, nisi. | | ccc | ``` @@ -76,8 +76,8 @@ sit amet, rhoncus porta, malesuada elementum, nisi. ^D -| aaa | bbb | ccc | -|-------------------------------------------------------------------------------------------------------------|-----|-----| +| aaa | bbb | ccc | +|----|----|----| | Consequat nisi sit amet nibh. Nunc mi tortor, tristique sit amet, rhoncus porta, malesuada elementum, nisi. | bbb | ccc | ``` diff --git a/test/command/9346.md b/test/command/9346.md index b09970914..fd6243834 100644 --- a/test/command/9346.md +++ b/test/command/9346.md @@ -3,6 +3,6 @@ |V1 |V2 |V3 |V4 |V5 |V6 |V7 |V8 |V9 |V10 |V11 |V12 |V13 |V14 |V15 |V16 |V17 |V18 |V19 |V20 |V21 |V22 |V23 |V24 |V25 |V26 |V27 |V28 |V29 |V30 |V31 |V32 |V33 |V34 |V35 |V36 |V37 |V38 |V39 |V40 |V41 |V42 |V43 |V44 |V45 |V46 |V47 |V48 |V49 |V50 |V51 |V52 |V53 |V54 |V55 |V56 |V57 |V58 |V59 |V60 |V61 |V62 |V63 |V64 |V65 |V66 |V67 |V68 |V69 |V70 |V71 |V72 | |:--|:--|:--|:--|:--|:--|:--|:--|:--|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---|:---| ^D -| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 | V11 | V12 | V13 | V14 | V15 | V16 | V17 | V18 | V19 | V20 | V21 | V22 | V23 | V24 | V25 | V26 | V27 | V28 | V29 | V30 | V31 | V32 | V33 | V34 | V35 | V36 | V37 | V38 | V39 | V40 | V41 | V42 | V43 | V44 | V45 | V46 | V47 | V48 | V49 | V50 | V51 | V52 | V53 | V54 | V55 | V56 | V57 | V58 | V59 | V60 | V61 | V62 | V63 | V64 | V65 | V66 | V67 | V68 | V69 | V70 | V71 | V72 | +| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 | V11 | V12 | V13 | V14 | V15 | V16 | V17 | V18 | V19 | V20 | V21 | V22 | V23 | V24 | V25 | V26 | V27 | V28 | V29 | V30 | V31 | V32 | V33 | V34 | V35 | V36 | V37 | V38 | V39 | V40 | V41 | V42 | V43 | V44 | V45 | V46 | V47 | V48 | V49 | V50 | V51 | V52 | V53 | V54 | V55 | V56 | V57 | V58 | V59 | V60 | V61 | V62 | V63 | V64 | V65 | V66 | V67 | V68 | V69 | V70 | V71 | V72 | |:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-|:-| ``` diff --git a/test/tables.markua b/test/tables.markua index b82264fd7..252a9bf28 100644 --- a/test/tables.markua +++ b/test/tables.markua @@ -28,19 +28,19 @@ Demonstration of simple table syntax. Multiline table with caption: -| Centered Header | Left Aligned | Right Aligned | Default aligned | -|:---------------:|:-------------|--------------:|:------------------------------------------------------| -| First | row | 12.0 | Example of a row that spans multiple lines. | -| Second | row | 5.0 | Here’s another one. Note the blank line between rows. | +| Centered Header | Left Aligned | Right Aligned | Default aligned | +|:--:|:---|---:|:---| +| First | row | 12.0 | Example of a row that spans multiple lines. | +| Second | row | 5.0 | Here’s another one. Note the blank line between rows. | Here’s the caption. It may span multiple lines. Multiline table without caption: -| Centered Header | Left Aligned | Right Aligned | Default aligned | -|:---------------:|:-------------|--------------:|:------------------------------------------------------| -| First | row | 12.0 | Example of a row that spans multiple lines. | -| Second | row | 5.0 | Here’s another one. Note the blank line between rows. | +| Centered Header | Left Aligned | Right Aligned | Default aligned | +|:--:|:---|---:|:---| +| First | row | 12.0 | Example of a row that spans multiple lines. | +| Second | row | 5.0 | Here’s another one. Note the blank line between rows. | Table without column headers: