@@ -33,7 +33,7 @@ module Text.Pandoc.Readers.RST (
33import Text.Pandoc.Definition
33import Text.Pandoc.Definition
34import Text.Pandoc.Shared
34import Text.Pandoc.Shared
35import Text.ParserCombinators.Parsec
35import Text.ParserCombinators.Parsec
36−import Control.Monad ( when, unless )
36+import Control.Monad ( when, unless, liftM )
37import Data.List ( findIndex, delete, intercalate, transpose )
37import Data.List ( findIndex, delete, intercalate, transpose )
39-- | Parse reStructuredText string and return Pandoc document.
39-- | Parse reStructuredText string and return Pandoc document.
@@ -598,29 +598,30 @@ regularKey = try $ do
598-- Grid tables TODO:
598-- Grid tables TODO:
599-- - column spans
599-- - column spans
601−dashedLine :: Char -> Char
602− -> GenParser Char st (Int, Int)
601+dashedLine :: Char -> GenParser Char st (Int, Int)
603−dashedLine ch sch = do
602+dashedLine ch = do
604 dashes <- many1 (char ch)
603 dashes <- many1 (char ch)
605− sp <- many (char sch)
604+ sp <- many (char ' ')
606 return (length dashes, length $ dashes ++ sp)
605 return (length dashes, length $ dashes ++ sp)
608simpleDashedLines :: Char -> GenParser Char st [(Int,Int)]
607simpleDashedLines :: Char -> GenParser Char st [(Int,Int)]
609−simpleDashedLines ch = try $ many1 (dashedLine ch sch)
608+simpleDashedLines ch = try $ many1 (dashedLine ch)
611− sch = ' '
610+gridPart :: Char -> GenParser Char st (Int, Int)
612+ dashes <- many1 (char ch)
614+ return (length dashes, length dashes + 1)
613gridDashedLines :: Char -> GenParser Char st [(Int,Int)]
616gridDashedLines :: Char -> GenParser Char st [(Int,Int)]
614−gridDashedLines ch = try $ char sch >> many1 (dashedLine ch sch)
617+gridDashedLines ch = try $ char '+' >> many1 (gridPart ch) >>~ blankline
618-- Parse a table row separator
619-- Parse a table row separator
619simpleTableSep :: Char -> GenParser Char ParserState Char
620simpleTableSep :: Char -> GenParser Char ParserState Char
620simpleTableSep ch = try $ simpleDashedLines ch >> newline
621simpleTableSep ch = try $ simpleDashedLines ch >> newline
622gridTableSep :: Char -> GenParser Char ParserState Char
623gridTableSep :: Char -> GenParser Char ParserState Char
623−gridTableSep ch = try $ gridDashedLines ch >> newline
624+gridTableSep ch = try $ gridDashedLines ch >> return '\n'
625-- Parse a table footer
626-- Parse a table footer
626simpleTableFooter :: GenParser Char ParserState [Char]
627simpleTableFooter :: GenParser Char ParserState [Char]
@@ -639,7 +640,7 @@ gridTableRawLine :: [Int] -> GenParser Char ParserState [String]
639gridTableRawLine indices = do
640gridTableRawLine indices = do
641 line <- many1Till anyChar newline
642 line <- many1Till anyChar newline
642− return (gridTableSplitLine indices line)
643+ return (gridTableSplitLine indices $ removeTrailingSpace line)
644-- Parse a table row and return a list of blocks (columns).
645-- Parse a table row and return a list of blocks (columns).
645simpleTableRow :: [Int] -> GenParser Char ParserState [[Block]]
646simpleTableRow :: [Int] -> GenParser Char ParserState [[Block]]
@@ -654,8 +655,12 @@ gridTableRow :: [Int]
654 -> GenParser Char ParserState [[Block]]
655 -> GenParser Char ParserState [[Block]]
655gridTableRow indices = do
656gridTableRow indices = do
656 colLines <- many1 (gridTableRawLine indices)
657 colLines <- many1 (gridTableRawLine indices)
658+ let cols = map ((++ "\n") . unlines . removeOneLeadingSpace) $
657− let cols = map unlines $ transpose colLines
659+ transpose colLines
658− mapM (parseFromString (many plain)) cols
660+ mapM (liftM compactifyCell . parseFromString (many block)) cols
662+compactifyCell :: [Block] -> [Block]
663+compactifyCell bs = head $ compactify [bs]
660simpleTableSplitLine :: [Int] -> String -> [String]
665simpleTableSplitLine :: [Int] -> String -> [String]
661simpleTableSplitLine indices line =
666simpleTableSplitLine indices line =
@@ -664,9 +669,19 @@ simpleTableSplitLine indices line =
665gridTableSplitLine :: [Int] -> String -> [String]
670gridTableSplitLine :: [Int] -> String -> [String]
666gridTableSplitLine indices line =
671gridTableSplitLine indices line =
667− map removeLeadingTrailingSpace
668− $ map (takeWhile (/= '|')) -- strip trailing '|' off each column
669− $ tail $ splitByIndices (init indices) line
672+ map removeFinalBar $ tail $ splitByIndices (init indices) line
674+removeFinalBar :: String -> String
675+removeFinalBar = reverse . dropWhile (=='|') . dropWhile (`elem` " \t") .
678+removeOneLeadingSpace :: [String] -> [String]
679+removeOneLeadingSpace xs =
680+ if all startsWithSpace xs
681+ then map (drop 1) xs
683+ where startsWithSpace "" = True
684+ startsWithSpace (y:_) = y == ' '
671-- Calculate relative widths of table columns, based on indices
686-- Calculate relative widths of table columns, based on indices
672widthsFromIndices :: Int -- Number of columns on terminal
687widthsFromIndices :: Int -- Number of columns on terminal
@@ -710,11 +725,10 @@ simpleTableHeader headless = try $ do
710 return (rawHeads, aligns, indices)
725 return (rawHeads, aligns, indices)
712gridTableHeader :: Bool -- ^ Headerless table
727gridTableHeader :: Bool -- ^ Headerless table
713− -> GenParser Char ParserState ([String], [Alignment], [Int])
728+ -> GenParser Char ParserState ([String], [Alignment], [Int])
714gridTableHeader headless = try $ do
729gridTableHeader headless = try $ do
715 optional blanklines
730 optional blanklines
716 dashes <- gridDashedLines '-'
731 dashes <- gridDashedLines '-'
718 rawContent <- if headless
732 rawContent <- if headless
719 then return $ repeat ""
733 then return $ repeat ""
720 else many1
734 else many1