Muse writer: output tables as grid tables if they have multi-line cells

This commit is contained in:
Alexander Krotov 2018-11-15 12:14:08 +03:00
parent 367e8cac18
commit f393f4ba10

View file

@ -49,6 +49,7 @@ import Control.Monad.State.Strict
import Data.Char (isAlphaNum, isAsciiLower, isAsciiUpper, isDigit, isSpace)
import Data.Default
import Data.List (intersperse, isInfixOf, transpose)
import Data.Monoid (Any (..))
import qualified Data.Set as Set
import Data.Text (Text)
import System.FilePath (takeExtension)
@ -61,6 +62,7 @@ import Text.Pandoc.Shared
import Text.Pandoc.Templates (renderTemplate')
import Text.Pandoc.Writers.Math
import Text.Pandoc.Writers.Shared
import Text.Pandoc.Walk
type Notes = [[Block]]
@ -276,7 +278,7 @@ blockToMuse (Header level (ident,_,_) inlines) = do
-- https://www.gnu.org/software/emacs-muse/manual/muse.html#Horizontal-Rules-and-Anchors
blockToMuse HorizontalRule = return $ blankline $$ "----" $$ blankline
blockToMuse (Table caption aligns widths headers rows) =
if all (== 0.0) widths && length widths > 1
if isSimple && numcols > 1
then simpleTable caption headers rows
else do
opts <- asks envOptions
@ -284,6 +286,16 @@ blockToMuse (Table caption aligns widths headers rows) =
where
blocksToDoc opts blocks =
local (\env -> env { envOptions = opts }) $ blockListToMuse blocks
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
blockToMuse (Div _ bs) = flatBlockListToMuse bs
blockToMuse Null = return empty