git.iko.soy/typeable/pandoc
on master help sign in
Markdown writer: better handling of tables with empty columns.
74afd29John MacFarlane
src/Text/Pandoc/Writers/Markdown.hs+22−12
@@ -519,6 +519,8 @@ blockToMarkdown' opts (BlockQuote blocks) = do
519 contents <- blockListToMarkdown opts blocks
519 contents <- blockListToMarkdown opts blocks
520 return $ (prefixed leader contents) <> blankline
520 return $ (prefixed leader contents) <> blankline
521blockToMarkdown' opts t@(Table caption aligns widths headers rows) = do
521blockToMarkdown' opts t@(Table caption aligns widths headers rows) = do
522+ let numcols = maximum (length aligns : length widths :
523+ map length (headers:rows))
522 caption' <- inlineListToMarkdown opts caption
524 caption' <- inlineListToMarkdown opts caption
523 let caption'' = if null caption || not (isEnabled Ext_table_captions opts)
525 let caption'' = if null caption || not (isEnabled Ext_table_captions opts)
524 then blankline
526 then blankline
@@ -530,30 +532,38 @@ blockToMarkdown' opts t@(Table caption aligns widths headers rows) = do
530 let isPlainBlock (Plain _) = True
532 let isPlainBlock (Plain _) = True
531 isPlainBlock _ = False
533 isPlainBlock _ = False
532 let hasBlocks = not (all isPlainBlock $ concat . concat $ headers:rows)
534 let hasBlocks = not (all isPlainBlock $ concat . concat $ headers:rows)
535+ let padRow r = case numcols - length r of
536+ x | x > 0 -> r ++ replicate x empty
537+ | otherwise -> r
533− rawHeaders <- mapM (blockListToMarkdown opts) headers
538+ rawHeaders <- padRow <$> mapM (blockListToMarkdown opts) headers
534− rawRows <- mapM (mapM (blockListToMarkdown opts)) rows
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
535 (nst,tbl) <-
546 (nst,tbl) <-
536 case True of
547 case True of
537 _ | isSimple &&
548 _ | isSimple &&
538 isEnabled Ext_simple_tables opts -> fmap (nest 2,) $
549 isEnabled Ext_simple_tables opts -> fmap (nest 2,) $
539− pandocTable opts (all null headers) aligns widths
550+ pandocTable opts (all null headers) aligns' widths'
540 rawHeaders rawRows
551 rawHeaders rawRows
541 | isSimple &&
552 | isSimple &&
542 isEnabled Ext_pipe_tables opts -> fmap (id,) $
553 isEnabled Ext_pipe_tables opts -> fmap (id,) $
543− pipeTable (all null headers) aligns rawHeaders rawRows
554+ pipeTable (all null headers) aligns' rawHeaders rawRows
544 | not hasBlocks &&
555 | not hasBlocks &&
545 isEnabled Ext_multiline_tables opts -> fmap (nest 2,) $
556 isEnabled Ext_multiline_tables opts -> fmap (nest 2,) $
546− pandocTable opts (all null headers) aligns widths
557+ pandocTable opts (all null headers) aligns' widths'
547 rawHeaders rawRows
558 rawHeaders rawRows
548 | isEnabled Ext_grid_tables opts &&
559 | isEnabled Ext_grid_tables opts &&
549− writerColumns opts >= 8 * length headers -> do
560+ writerColumns opts >= 8 * numcols -> do
550− let numcols = length headers
551− let widths' = if all (==0) widths
561+ let widths'' = if all (==0) widths'
552− then replicate numcols
562+ then replicate numcols
553 (1.0 / fromIntegral numcols)
563 (1.0 / fromIntegral numcols)
554− else widths
564+ else widths'
555 let widthsInChars = map ((\x -> x - 3) . floor .
565 let widthsInChars = map ((\x -> x - 3) . floor .
556− (fromIntegral (writerColumns opts) *)) widths'
566+ (fromIntegral (writerColumns opts) *)) widths''
557 rawHeaders' <- zipWithM
567 rawHeaders' <- zipWithM
558 blockListToMarkdown
568 blockListToMarkdown
559 (map (\w -> opts{writerColumns =
569 (map (\w -> opts{writerColumns =
@@ -569,7 +579,7 @@ blockToMarkdown' opts t@(Table caption aligns widths headers rows) = do
569 cs)
579 cs)
570 rows
580 rows
571 fmap (id,) $
581 fmap (id,) $
572− gridTable (all null headers) aligns widthsInChars
582+ gridTable (all null headers) aligns' widthsInChars
573 rawHeaders' rawRows'
583 rawHeaders' rawRows'
574 | isEnabled Ext_raw_html opts -> fmap (id,) $
584 | isEnabled Ext_raw_html opts -> fmap (id,) $
575 text <$>
585 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+