summaryrefslogtreecommitdiff
path: root/src/Text/Pandoc
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2017-03-06 22:51:28 +0100
committerJohn MacFarlane <jgm@berkeley.edu>2017-03-06 22:51:28 +0100
commit74afd2974a1fe4e70a6c6581ee61ec5bc28df2f7 (patch)
treeb410c4b276076ec5a5637c4e956230f253c443c6 /src/Text/Pandoc
parent9e87114234acf2f52524a59225ca3aa41e22d794 (diff)
Markdown writer: better handling of tables with empty columns.
E.g. an HTML table with two cells in the first row and one in the second (but no row/colspan). We now calculate the number of columns based on the longest row (or the length of aligns or widths). Closes #3337.
Diffstat (limited to 'src/Text/Pandoc')
-rw-r--r--src/Text/Pandoc/Writers/Markdown.hs34
1 files changed, 22 insertions, 12 deletions
diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index ad3eb62df..96d46ae5b 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -519,6 +519,8 @@ blockToMarkdown' opts (BlockQuote blocks) = do
contents <- blockListToMarkdown opts blocks
return $ (prefixed leader contents) <> blankline
blockToMarkdown' opts t@(Table caption aligns widths headers rows) = do
+ let numcols = maximum (length aligns : length widths :
+ map length (headers:rows))
caption' <- inlineListToMarkdown opts caption
let caption'' = if null caption || not (isEnabled Ext_table_captions opts)
then blankline
@@ -530,30 +532,38 @@ blockToMarkdown' opts t@(Table caption aligns widths headers rows) = do
let isPlainBlock (Plain _) = True
isPlainBlock _ = False
let hasBlocks = not (all isPlainBlock $ concat . concat $ headers:rows)
- rawHeaders <- mapM (blockListToMarkdown opts) headers
- rawRows <- mapM (mapM (blockListToMarkdown opts)) rows
+ let padRow r = case numcols - length r of
+ x | x > 0 -> r ++ replicate x empty
+ | otherwise -> r
+ rawHeaders <- padRow <$> mapM (blockListToMarkdown opts) headers
+ rawRows <- mapM (fmap padRow . mapM (blockListToMarkdown opts)) rows
+ let aligns' = case numcols - length aligns of
+ x | x > 0 -> aligns ++ replicate x AlignDefault
+ | otherwise -> aligns
+ let widths' = case numcols - length widths of
+ x | x > 0 -> widths ++ replicate x 0.0
+ | otherwise -> widths
(nst,tbl) <-
case True of
_ | isSimple &&
isEnabled Ext_simple_tables opts -> fmap (nest 2,) $
- pandocTable opts (all null headers) aligns widths
+ pandocTable opts (all null headers) aligns' widths'
rawHeaders rawRows
| isSimple &&
isEnabled Ext_pipe_tables opts -> fmap (id,) $
- pipeTable (all null headers) aligns rawHeaders rawRows
+ pipeTable (all null headers) aligns' rawHeaders rawRows
| not hasBlocks &&
isEnabled Ext_multiline_tables opts -> fmap (nest 2,) $
- pandocTable opts (all null headers) aligns widths
+ pandocTable opts (all null headers) aligns' widths'
rawHeaders rawRows
| isEnabled Ext_grid_tables opts &&
- writerColumns opts >= 8 * length headers -> do
- let numcols = length headers
- let widths' = if all (==0) widths
- then replicate numcols
+ writerColumns opts >= 8 * numcols -> do
+ let widths'' = if all (==0) widths'
+ then replicate numcols
(1.0 / fromIntegral numcols)
- else widths
+ else widths'
let widthsInChars = map ((\x -> x - 3) . floor .
- (fromIntegral (writerColumns opts) *)) widths'
+ (fromIntegral (writerColumns opts) *)) widths''
rawHeaders' <- zipWithM
blockListToMarkdown
(map (\w -> opts{writerColumns =
@@ -569,7 +579,7 @@ blockToMarkdown' opts t@(Table caption aligns widths headers rows) = do
cs)
rows
fmap (id,) $
- gridTable (all null headers) aligns widthsInChars
+ gridTable (all null headers) aligns' widthsInChars
rawHeaders' rawRows'
| isEnabled Ext_raw_html opts -> fmap (id,) $
text <$>