From f8ca36525d8a8c855dcfa33d6215c47533567d20 Mon Sep 17 00:00:00 2001
From: Alexander Krotov <ilabdsf@gmail.com>
Date: Tue, 9 Oct 2018 23:34:50 +0300
Subject: [PATCH] Muse: Make tables round-trip

---
 test/Tests/Readers/Muse.hs | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/test/Tests/Readers/Muse.hs b/test/Tests/Readers/Muse.hs
index bb61e848f..42dec0ffd 100644
--- a/test/Tests/Readers/Muse.hs
+++ b/test/Tests/Readers/Muse.hs
@@ -4,6 +4,7 @@ module Tests.Readers.Muse (tests) where
 
 import Prelude
 import Data.List (intersperse)
+import Data.Monoid (Any (..))
 import Data.Text (Text)
 import qualified Data.Text as T
 import Test.Tasty
@@ -14,7 +15,7 @@ import Text.Pandoc
 import Text.Pandoc.Arbitrary ()
 import Text.Pandoc.Builder
 import Text.Pandoc.Shared (underlineSpan)
-import Text.Pandoc.Walk (walk)
+import Text.Pandoc.Walk
 
 amuse :: Text -> Pandoc
 amuse = purely $ readMuse def { readerExtensions = extensionsFromList [Ext_amuse]}
@@ -33,7 +34,20 @@ spcSep = mconcat . intersperse space
 -- Tables don't round-trip yet
 --
 makeRoundTrip :: Block -> Block
-makeRoundTrip Table{} = Para [Str "table was here"]
+makeRoundTrip t@(Table _caption aligns widths headers rows) =
+  if isSimple && numcols > 1
+    then t
+    else Para [Str "table was here"]
+  where numcols = maximum (length aligns : length widths : map length (headers:rows))
+        hasSimpleCells = all isSimpleCell (concat (headers:rows))
+        isLineBreak LineBreak = Any True
+        isLineBreak _         = Any False
+        hasLineBreak = getAny . query isLineBreak
+        isSimple = hasSimpleCells && all (== 0) widths
+        isSimpleCell [Plain ils] = not (hasLineBreak ils)
+        isSimpleCell [Para ils ] = not (hasLineBreak ils)
+        isSimpleCell []          = True
+        isSimpleCell _           = False
 makeRoundTrip (OrderedList (start, LowerAlpha, _) items) = OrderedList (start, Decimal, Period) items
 makeRoundTrip (OrderedList (start, UpperAlpha, _) items) = OrderedList (start, Decimal, Period) items
 makeRoundTrip x = x