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

commit74afd2974a1fe4e70a6c6581ee61ec5bc28df2f7 authorJohn MacFarlane <jgm@berkeley.edu> dateMon 6 Mar 2017, 22:51 (10 years ago) parent9e87114
2 files changed+38−12
src/Text/Pandoc/Writers/Markdown.hs+22−12 test/command/3337.md+16
src/Text/Pandoc/Writers/Markdown.hs+22−12
@@ -519,6 +519,8 @@ blockToMarkdown' opts (BlockQuote blocks) = do
519519 contents <- blockListToMarkdown opts blocks
520520 return $ (prefixed leader contents) <> blankline
521521blockToMarkdown' opts t@(Table caption aligns widths headers rows) = do
522+ let numcols = maximum (length aligns : length widths :
523+ map length (headers:rows))
522524 caption' <- inlineListToMarkdown opts caption
523525 let caption'' = if null caption || not (isEnabled Ext_table_captions opts)
524526 then blankline
@@ -530,30 +532,38 @@ blockToMarkdown' opts t@(Table caption aligns widths headers rows) = do
530532 let isPlainBlock (Plain _) = True
531533 isPlainBlock _ = False
532534 let hasBlocks = not (all isPlainBlock $ concat . concat $ headers:rows)
533− rawHeaders <- mapM (blockListToMarkdown opts) headers
534− rawRows <- mapM (mapM (blockListToMarkdown opts)) rows
535+ let padRow r = case numcols - length r of
536+ x | x > 0 -> r ++ replicate x empty
537+ | otherwise -> r
538+ rawHeaders <- padRow <$> mapM (blockListToMarkdown opts) headers
539+ rawRows <- mapM (fmap padRow . mapM (blockListToMarkdown opts)) rows
540+ let aligns' = case numcols - length aligns of
541+ x | x > 0 -> aligns ++ replicate x AlignDefault
542+ | otherwise -> aligns
543+ let widths' = case numcols - length widths of
544+ x | x > 0 -> widths ++ replicate x 0.0
545+ | otherwise -> widths
535546 (nst,tbl) <-
536547 case True of
537548 _ | isSimple &&
538549 isEnabled Ext_simple_tables opts -> fmap (nest 2,) $
539− pandocTable opts (all null headers) aligns widths
550+ pandocTable opts (all null headers) aligns' widths'
540551 rawHeaders rawRows
541552 | isSimple &&
542553 isEnabled Ext_pipe_tables opts -> fmap (id,) $
543− pipeTable (all null headers) aligns rawHeaders rawRows
554+ pipeTable (all null headers) aligns' rawHeaders rawRows
544555 | not hasBlocks &&
545556 isEnabled Ext_multiline_tables opts -> fmap (nest 2,) $
546− pandocTable opts (all null headers) aligns widths
557+ pandocTable opts (all null headers) aligns' widths'
547558 rawHeaders rawRows
548559 | isEnabled Ext_grid_tables opts &&
549− writerColumns opts >= 8 * length headers -> do
550− let numcols = length headers
551− let widths' = if all (==0) widths
552− then replicate numcols
560+ writerColumns opts >= 8 * numcols -> do
561+ let widths'' = if all (==0) widths'
562+ then replicate numcols
553563 (1.0 / fromIntegral numcols)
554− else widths
564+ else widths'
555565 let widthsInChars = map ((\x -> x - 3) . floor .
556− (fromIntegral (writerColumns opts) *)) widths'
566+ (fromIntegral (writerColumns opts) *)) widths''
557567 rawHeaders' <- zipWithM
558568 blockListToMarkdown
559569 (map (\w -> opts{writerColumns =
@@ -569,7 +579,7 @@ blockToMarkdown' opts t@(Table caption aligns widths headers rows) = do
569579 cs)
570580 rows
571581 fmap (id,) $
572− gridTable (all null headers) aligns widthsInChars
582+ gridTable (all null headers) aligns' widthsInChars
573583 rawHeaders' rawRows'
574584 | isEnabled Ext_raw_html opts -> fmap (id,) $
575585 text <$>
test/command/3337.mdnew file+16
@@ -0,0 +1,16 @@
1+```
2+% pandoc -f html -t markdown
3+<table>
4+<tr><td>a</td></tr>
5+<tr><td>1</td><td>2</td></tr>
6+</table>
7+^D
8+ --- ---
9+ a
10+ 1 2
11+ --- ---
12+
13+
14+
15+```
16+