From 506866ef7368ed1cae9236bfd8323fde64eeb154 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Sun, 13 Aug 2017 09:52:37 -0700
Subject: [PATCH] Markdown writer: Use pipe tables if `raw_html` disabled...

and `pipe_tables` enabled, even if the table has relative
width information.

Closes #3734.
---
 src/Text/Pandoc/Writers/Markdown.hs | 12 +++++++--
 test/command/3734.md                | 39 +++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 2 deletions(-)
 create mode 100644 test/command/3734.md

diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index 837c177f1..95977ce17 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -553,8 +553,13 @@ blockToMarkdown' opts t@(Table caption aligns widths headers rows) =  do
                      else blankline $$ (": " <> caption') $$ blankline
   let isLineBreak LineBreak = Any True
       isLineBreak _         = Any False
-  let isSimple = all (==0) widths &&
-                 not ( getAny (query isLineBreak (headers:rows)) )
+  let hasLineBreak = getAny . query isLineBreak
+  let isSimpleCell [Plain ils] = not (hasLineBreak ils)
+      isSimpleCell [Para ils ] = not (hasLineBreak ils)
+      isSimpleCell []          = True
+      isSimpleCell _           = False
+  let hasSimpleCells = all isSimpleCell (concat (headers:rows))
+  let isSimple = hasSimpleCells && all (==0) widths
   let isPlainBlock (Plain _) = True
       isPlainBlock _         = False
   let hasBlocks = not (all isPlainBlock $ concat . concat $ headers:rows)
@@ -589,6 +594,9 @@ blockToMarkdown' opts t@(Table caption aligns widths headers rows) =  do
             | isEnabled Ext_raw_html opts -> fmap (id,) $
                    (text . T.unpack) <$>
                    (writeHtml5String def $ Pandoc nullMeta [t])
+            | hasSimpleCells &&
+              isEnabled Ext_pipe_tables opts -> fmap (id,) $
+                   pipeTable (all null headers) aligns' rawHeaders rawRows
             | otherwise -> return $ (id, text "[TABLE]")
   return $ nst $ tbl $$ caption'' $$ blankline
 blockToMarkdown' opts (BulletList items) = do
diff --git a/test/command/3734.md b/test/command/3734.md
new file mode 100644
index 000000000..1adce6761
--- /dev/null
+++ b/test/command/3734.md
@@ -0,0 +1,39 @@
+```
+% pandoc -t markdown_strict+pipe_tables
+| aaaaaaaaaaaa | bbbbb | ccccccccccc                                                              |
+|--------------|-------|--------------------------------------------------------------------------|
+| aaaaaaaaaaaa |       | cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc |
+^D
+<table>
+<colgroup>
+<col style="width: 15%" />
+<col style="width: 8%" />
+<col style="width: 76%" />
+</colgroup>
+<thead>
+<tr class="header">
+<th>aaaaaaaaaaaa</th>
+<th>bbbbb</th>
+<th>ccccccccccc</th>
+</tr>
+</thead>
+<tbody>
+<tr class="odd">
+<td>aaaaaaaaaaaa</td>
+<td></td>
+<td>cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc</td>
+</tr>
+</tbody>
+</table>
+```
+
+```
+% pandoc -t markdown_strict+pipe_tables-raw_html
+| aaaaaaaaaaaa | bbbbb | ccccccccccc                                                              |
+|--------------|-------|--------------------------------------------------------------------------|
+| aaaaaaaaaaaa |       | cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc |
+^D
+| aaaaaaaaaaaa | bbbbb | ccccccccccc                                                              |
+|--------------|-------|--------------------------------------------------------------------------|
+| aaaaaaaaaaaa |       | cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc |
+```