git.iko.soy/typeable/pandoc
on master help sign in
Org reader: fix handling of empty table cells, rows
2d82560Albert Krewinkel
src/Text/Pandoc/Readers/Org.hs+17−13
@@ -35,6 +35,7 @@ import Text.Pandoc.Builder ( Inlines, Blocks, HasMeta(..),
35 trimInlines )
35 trimInlines )
36import Text.Pandoc.Definition
36import Text.Pandoc.Definition
37import Text.Pandoc.Compat.Monoid ((<>))
37import Text.Pandoc.Compat.Monoid ((<>))
38+import Text.Pandoc.Error
38import Text.Pandoc.Options
39import Text.Pandoc.Options
39import qualified Text.Pandoc.Parsing as P
40import qualified Text.Pandoc.Parsing as P
40import Text.Pandoc.Parsing hiding ( F, unF, askF, asksF, runF
41import Text.Pandoc.Parsing hiding ( F, unF, askF, asksF, runF
@@ -57,8 +58,6 @@ import qualified Data.Set as Set
57import Data.Maybe (fromMaybe, isJust)
58import Data.Maybe (fromMaybe, isJust)
58import Network.HTTP (urlEncode)
59import Network.HTTP (urlEncode)
59
60
60−import Text.Pandoc.Error
61−
62-- | Parse org-mode string and return a Pandoc document.
61-- | Parse org-mode string and return a Pandoc document.
63readOrg :: ReaderOptions -- ^ Reader options
62readOrg :: ReaderOptions -- ^ Reader options
64 -> String -- ^ String to parse (assuming @'\n'@ line endings)
63 -> String -- ^ String to parse (assuming @'\n'@ line endings)
@@ -807,18 +806,19 @@ tableRows = try $ many (tableAlignRow <|> tableHline <|> tableContentRow)
807
806
808tableContentRow :: OrgParser OrgTableRow
807tableContentRow :: OrgParser OrgTableRow
809tableContentRow = try $
808tableContentRow = try $
810− OrgContentRow . sequence <$> (tableStart *> manyTill tableContentCell newline)
809+ OrgContentRow . sequence <$> (tableStart *> many1Till tableContentCell newline)
811
810
812tableContentCell :: OrgParser (F Blocks)
811tableContentCell :: OrgParser (F Blocks)
813tableContentCell = try $
812tableContentCell = try $
814− fmap B.plain . trimInlinesF . mconcat <$> many1Till inline endOfCell
813+ fmap B.plain . trimInlinesF . mconcat <$> manyTill inline endOfCell
815−
816−endOfCell :: OrgParser Char
817−endOfCell = try $ char '|' <|> lookAhead newline
818
814
819tableAlignRow :: OrgParser OrgTableRow
815tableAlignRow :: OrgParser OrgTableRow
820−tableAlignRow = try $
816+tableAlignRow = try $ do
817+ tableStart
821− OrgAlignRow <$> (tableStart *> manyTill tableAlignCell newline)
818+ cells <- many1Till tableAlignCell newline
819+ -- Empty rows are regular (i.e. content) rows, not alignment rows.
820+ guard $ any (/= AlignDefault) cells
821+ return $ OrgAlignRow cells
822
822
823tableAlignCell :: OrgParser Alignment
823tableAlignCell :: OrgParser Alignment
824tableAlignCell =
824tableAlignCell =
@@ -833,15 +833,19 @@ tableAlignCell =
833 where emptyCell = try $ skipSpaces *> endOfCell
833 where emptyCell = try $ skipSpaces *> endOfCell
834
834
835tableAlignFromChar :: OrgParser Alignment
835tableAlignFromChar :: OrgParser Alignment
836+tableAlignFromChar = try $
836−tableAlignFromChar = try $ choice [ char 'l' *> return AlignLeft
837+ choice [ char 'l' *> return AlignLeft
837− , char 'c' *> return AlignCenter
838+ , char 'c' *> return AlignCenter
838− , char 'r' *> return AlignRight
839+ , char 'r' *> return AlignRight
839− ]
840+ ]
840
841
841tableHline :: OrgParser OrgTableRow
842tableHline :: OrgParser OrgTableRow
842tableHline = try $
843tableHline = try $
843 OrgHlineRow <$ (tableStart *> char '-' *> anyLine)
844 OrgHlineRow <$ (tableStart *> char '-' *> anyLine)
844
845
846+endOfCell :: OrgParser Char
847+endOfCell = try $ char '|' <|> lookAhead newline
848+
845rowsToTable :: [OrgTableRow]
849rowsToTable :: [OrgTableRow]
846 -> F OrgTable
850 -> F OrgTable
847rowsToTable = foldM rowToContent emptyTable
851rowsToTable = foldM rowToContent emptyTable
tests/Tests/Readers/Org.hs+12−1
@@ -941,7 +941,7 @@ tests =
941
941
942 , "Empty table" =:
942 , "Empty table" =:
943 "||" =?>
943 "||" =?>
944− simpleTable' 1 mempty mempty
944+ simpleTable' 1 mempty [[mempty]]
945
945
946 , "Glider Table" =:
946 , "Glider Table" =:
947 unlines [ "| 1 | 0 | 0 |"
947 unlines [ "| 1 | 0 | 0 |"
@@ -996,6 +996,17 @@ tests =
996 , [ plain "dynamic", plain "Lisp" ]
996 , [ plain "dynamic", plain "Lisp" ]
997 ]
997 ]
998
998
999+ , "Table with empty cells" =:
1000+ "|||c|" =?>
1001+ simpleTable' 3 mempty [[mempty, mempty, plain "c"]]
1002+
1003+ , "Table with empty rows" =:
1004+ unlines [ "| first |"
1005+ , "| |"
1006+ , "| third |"
1007+ ] =?>
1008+ simpleTable' 1 mempty [[plain "first"], [mempty], [plain "third"]]
1009+
999 , "Table with alignment row" =:
1010 , "Table with alignment row" =:
1000 unlines [ "| Numbers | Text | More |"
1011 unlines [ "| Numbers | Text | More |"
1001 , "| <c> | <r> | |"
1012 , "| <c> | <r> | |"