Muse reader: add grid tables support

This commit is contained in:
Alexander Krotov 2018-11-09 14:19:17 +03:00
parent 195b3af8b6
commit c61b67410a
2 changed files with 74 additions and 2 deletions

View file

@ -32,8 +32,6 @@ Conversion of Muse text to 'Pandoc' document.
-}
{-
TODO:
- Org tables
- table.el tables
- <cite> tag
-}
module Text.Pandoc.Readers.Muse (readMuse) where
@ -335,6 +333,7 @@ blockElements = (mempty <$ blankline)
<|> playTag
<|> verseTag
<|> lineBlock
<|> museGridTable
<|> table
<|> commentTag
@ -681,6 +680,36 @@ elementsToTable :: [MuseTableElement] -> MuseTable
elementsToTable = foldr museAppendElement emptyTable
where emptyTable = MuseTable mempty mempty mempty mempty
museGridPart :: PandocMonad m => MuseParser m Int
museGridPart = try $ length <$> many1 (char '-') <* char '+'
museGridTableHeader :: PandocMonad m => MuseParser m [Int]
museGridTableHeader = try $ char '+' *> many1 museGridPart <* manyTill spaceChar eol
museGridTableRow :: PandocMonad m
=> Int
-> [Int]
-> MuseParser m (F [Blocks])
museGridTableRow indent indices = try $ do
lns <- many1 $ try (indentWith indent *> museGridTableRawLine indices)
let cols = map unlines $ transpose lns
indentWith indent *> museGridTableHeader
sequence <$> mapM (parseFromString parseBlocks) cols
museGridTableRawLine :: PandocMonad m
=> [Int]
-> MuseParser m [String]
museGridTableRawLine indices =
char '|' *> forM indices (\n -> count n anyChar <* char '|') <* manyTill spaceChar eol
museGridTable :: PandocMonad m => MuseParser m (F Blocks)
museGridTable = try $ do
indent <- getIndent
indices <- museGridTableHeader
fmap rowsToTable . sequence <$> many1 (museGridTableRow indent indices)
where rowsToTable rows = B.table mempty attrs [] rows
where attrs = const (AlignDefault, 0.0) <$> transpose rows
-- | Parse a table.
table :: PandocMonad m => MuseParser m (F Blocks)
table = try $ fmap (museToPandocTable . elementsToTable) <$> tableElements

View file

@ -976,6 +976,49 @@ tests =
[[plain "1", plain "2", plain "3"],
[plain "4", mempty, plain "6"],
[plain "7", plain "8", plain "9"]]
, "Grid table" =:
T.unlines
[ "+-----+-----+"
, "| foo | bar |"
, "+-----+-----+"
] =?>
table mempty [(AlignDefault, 0.0), (AlignDefault, 0.0)]
[]
[[para "foo", para "bar"]]
, "Grid table inside list" =:
T.unlines
[ " - +-----+-----+"
, " | foo | bar |"
, " +-----+-----+"
] =?>
bulletList [table mempty [(AlignDefault, 0.0), (AlignDefault, 0.0)]
[]
[[para "foo", para "bar"]]]
, "Grid table with two rows" =:
T.unlines
[ "+-----+-----+"
, "| foo | bar |"
, "+-----+-----+"
, "| bat | baz |"
, "+-----+-----+"
] =?>
table mempty [(AlignDefault, 0.0), (AlignDefault, 0.0)]
[]
[[para "foo", para "bar"]
,[para "bat", para "baz"]]
, "Grid table inside grid table" =:
T.unlines
[ "+-----+"
, "|+---+|"
, "||foo||"
, "|+---+|"
, "+-----+"
] =?>
table mempty [(AlignDefault, 0.0)]
[]
[[table mempty [(AlignDefault, 0.0)]
[]
[[para "foo"]]]]
]
, testGroup "Lists"
[ "Bullet list" =: