git.iko.soy/typeable/pandoc
on master help sign in
Markdown writer: Fixed grid tables embedded in grid tables.

Closes #2834.

commitea619bfcb4dd58f4ea0f3b02a61dc734180b79b2 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 1 Mar 2017, 17:41 (10 years ago) parent8b641f3
2 files changed+81−32
src/Text/Pandoc/Writers/Markdown.hs+49−32 test/command/2834.md+32
src/Text/Pandoc/Writers/Markdown.hs+49−32
@@ -44,6 +44,7 @@ import Data.List ( group, stripPrefix, find, intersperse, transpose, sortBy )
4444import Data.Char ( isSpace, isPunctuation, ord, chr )
4545import Data.Ord ( comparing )
4646import Text.Pandoc.Pretty
47+import Control.Monad (zipWithM)
4748import Control.Monad.Reader
4849import Control.Monad.State
4950import Control.Monad.Except (throwError)
@@ -514,10 +515,8 @@ blockToMarkdown' opts (BlockQuote blocks) = do
514515blockToMarkdown' opts t@(Table caption aligns widths headers rows) = do
515516 caption' <- inlineListToMarkdown opts caption
516517 let caption'' = if null caption || not (isEnabled Ext_table_captions opts)
517− then empty
518− else blankline <> ": " <> caption' <> blankline
519− rawHeaders <- mapM (blockListToMarkdown opts) headers
520− rawRows <- mapM (mapM (blockListToMarkdown opts)) rows
518+ then blankline
519+ else blankline $$ (": " <> caption') $$ blankline
521520 let isLineBreak LineBreak = Any True
522521 isLineBreak _ = Any False
523522 let isSimple = all (==0) widths &&
@@ -525,34 +524,52 @@ blockToMarkdown' opts t@(Table caption aligns widths headers rows) = do
525524 let isPlainBlock (Plain _) = True
526525 isPlainBlock _ = False
527526 let hasBlocks = not (all isPlainBlock $ concat . concat $ headers:rows)
528− (nst,tbl) <- case True of
529− _ | isSimple &&
530− isEnabled Ext_simple_tables opts -> fmap (nest 2,) $
531− pandocTable opts (all null headers) aligns widths
532− rawHeaders rawRows
533− | isSimple &&
534− isEnabled Ext_pipe_tables opts -> fmap (id,) $
535− pipeTable (all null headers) aligns rawHeaders rawRows
536− | not hasBlocks &&
537− isEnabled Ext_multiline_tables opts -> fmap (nest 2,) $
538− pandocTable opts (all null headers) aligns widths
539− rawHeaders rawRows
540− | isEnabled Ext_grid_tables opts -> do
541− let numcols = length headers
542− let widths' = if all (==0) widths
543− then replicate numcols
544− (1.0 / fromIntegral numcols)
545− else widths
546− let widthsInChars = map ((\x -> x - 3) . floor .
547− (fromIntegral (writerColumns opts) *)) widths'
548− fmap (id,) $
549− gridTable (all null headers) aligns widthsInChars
550− rawHeaders rawRows
551− | isEnabled Ext_raw_html opts -> fmap (id,) $
552− text <$>
553− (writeHtml5String def $ Pandoc nullMeta [t])
554− | otherwise -> return $ (id, text "[TABLE]")
555− return $ nst $ tbl $$ blankline $$ caption'' $$ blankline
527+ rawHeaders <- mapM (blockListToMarkdown opts) headers
528+ rawRows <- mapM (mapM (blockListToMarkdown opts)) rows
529+ (nst,tbl) <-
530+ case True of
531+ _ | isSimple &&
532+ isEnabled Ext_simple_tables opts -> fmap (nest 2,) $
533+ pandocTable opts (all null headers) aligns widths
534+ rawHeaders rawRows
535+ | isSimple &&
536+ isEnabled Ext_pipe_tables opts -> fmap (id,) $
537+ pipeTable (all null headers) aligns rawHeaders rawRows
538+ | not hasBlocks &&
539+ isEnabled Ext_multiline_tables opts -> fmap (nest 2,) $
540+ pandocTable opts (all null headers) aligns widths
541+ rawHeaders rawRows
542+ | isEnabled Ext_grid_tables opts &&
543+ writerColumns opts >= 8 * length headers -> do
544+ let numcols = length headers
545+ let widths' = if all (==0) widths
546+ then replicate numcols
547+ (1.0 / fromIntegral numcols)
548+ else widths
549+ let widthsInChars = map ((\x -> x - 3) . floor .
550+ (fromIntegral (writerColumns opts) *)) widths'
551+ rawHeaders' <- zipWithM
552+ blockListToMarkdown
553+ (map (\w -> opts{writerColumns =
554+ min (w - 2) (writerColumns opts)})
555+ widthsInChars)
556+ headers
557+ rawRows' <- mapM
558+ (\cs -> zipWithM
559+ blockListToMarkdown
560+ (map (\w -> opts{writerColumns =
561+ min (w - 2) (writerColumns opts)})
562+ widthsInChars)
563+ cs)
564+ rows
565+ fmap (id,) $
566+ gridTable (all null headers) aligns widthsInChars
567+ rawHeaders' rawRows'
568+ | isEnabled Ext_raw_html opts -> fmap (id,) $
569+ text <$>
570+ (writeHtml5String def $ Pandoc nullMeta [t])
571+ | otherwise -> return $ (id, text "[TABLE]")
572+ return $ nst $ tbl $$ caption'' $$ blankline
556573blockToMarkdown' opts (BulletList items) = do
557574 contents <- inList $ mapM (bulletListItemToMarkdown opts) items
558575 return $ cat contents <> blankline
test/command/2834.mdnew file+32
@@ -0,0 +1,32 @@
1+Nested grid tables.
2+```
3+% pandoc -f html -t markdown --columns=72
4+<table>
5+ <tr>
6+ <td>
7+ <table>
8+ <tr>
9+ <td>
10+ <table>
11+ <tr>
12+ <td>some text</td>
13+ </tr>
14+ </table>
15+ </td>
16+ </tr>
17+ </table>
18+ </td>
19+ </tr>
20+</table>
21+^D
22++-----------------------------------------------------------------------+
23+| +------------------------------------------------------------------+ |
24+| | ----------- | |
25+| | some text | |
26+| | ----------- | |
27+| +------------------------------------------------------------------+ |
28++-----------------------------------------------------------------------+
29+
30+
31+
32+```