@@ -33,7 +33,7 @@ module Text.Pandoc.Readers.RST (
3333import Text.Pandoc.Definition
3434import Text.Pandoc.Shared
3535import Text.ParserCombinators.Parsec
36−import Control.Monad ( when, unless )
36+import Control.Monad ( when, unless, liftM )
3737import Data.List ( findIndex, delete, intercalate, transpose )
3838
3939-- | Parse reStructuredText string and return Pandoc document.
@@ -598,29 +598,30 @@ regularKey = try $ do
598598-- Grid tables TODO:
599599-- - column spans
600600
601−dashedLine :: Char -> Char
602− -> GenParser Char st (Int, Int)
603−dashedLine ch sch = do
601+dashedLine :: Char -> GenParser Char st (Int, Int)
602+dashedLine ch = do
604603 dashes <- many1 (char ch)
605− sp <- many (char sch)
604+ sp <- many (char ' ')
606605 return (length dashes, length $ dashes ++ sp)
607606
608607simpleDashedLines :: Char -> GenParser Char st [(Int,Int)]
609−simpleDashedLines ch = try $ many1 (dashedLine ch sch)
610− where
611− sch = ' '
608+simpleDashedLines ch = try $ many1 (dashedLine ch)
609+
610+gridPart :: Char -> GenParser Char st (Int, Int)
611+gridPart ch = do
612+ dashes <- many1 (char ch)
613+ char '+'
614+ return (length dashes, length dashes + 1)
612615
613616gridDashedLines :: Char -> GenParser Char st [(Int,Int)]
614−gridDashedLines ch = try $ char sch >> many1 (dashedLine ch sch)
615− where
616− sch = '+'
617+gridDashedLines ch = try $ char '+' >> many1 (gridPart ch) >>~ blankline
617618
618619-- Parse a table row separator
619620simpleTableSep :: Char -> GenParser Char ParserState Char
620621simpleTableSep ch = try $ simpleDashedLines ch >> newline
621622
622623gridTableSep :: Char -> GenParser Char ParserState Char
623−gridTableSep ch = try $ gridDashedLines ch >> newline
624+gridTableSep ch = try $ gridDashedLines ch >> return '\n'
624625
625626-- Parse a table footer
626627simpleTableFooter :: GenParser Char ParserState [Char]
@@ -639,7 +640,7 @@ gridTableRawLine :: [Int] -> GenParser Char ParserState [String]
639640gridTableRawLine indices = do
640641 char '|'
641642 line <- many1Till anyChar newline
642− return (gridTableSplitLine indices line)
643+ return (gridTableSplitLine indices $ removeTrailingSpace line)
643644
644645-- Parse a table row and return a list of blocks (columns).
645646simpleTableRow :: [Int] -> GenParser Char ParserState [[Block]]
@@ -654,8 +655,12 @@ gridTableRow :: [Int]
654655 -> GenParser Char ParserState [[Block]]
655656gridTableRow indices = do
656657 colLines <- many1 (gridTableRawLine indices)
657− let cols = map unlines $ transpose colLines
658− mapM (parseFromString (many plain)) cols
658+ let cols = map ((++ "\n") . unlines . removeOneLeadingSpace) $
659+ transpose colLines
660+ mapM (liftM compactifyCell . parseFromString (many block)) cols
661+
662+compactifyCell :: [Block] -> [Block]
663+compactifyCell bs = head $ compactify [bs]
659664
660665simpleTableSplitLine :: [Int] -> String -> [String]
661666simpleTableSplitLine indices line =
@@ -664,9 +669,19 @@ simpleTableSplitLine indices line =
664669
665670gridTableSplitLine :: [Int] -> String -> [String]
666671gridTableSplitLine 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
673+
674+removeFinalBar :: String -> String
675+removeFinalBar = reverse . dropWhile (=='|') . dropWhile (`elem` " \t") .
676+ reverse
677+
678+removeOneLeadingSpace :: [String] -> [String]
679+removeOneLeadingSpace xs =
680+ if all startsWithSpace xs
681+ then map (drop 1) xs
682+ else xs
683+ where startsWithSpace "" = True
684+ startsWithSpace (y:_) = y == ' '
670685
671686-- Calculate relative widths of table columns, based on indices
672687widthsFromIndices :: Int -- Number of columns on terminal
@@ -710,11 +725,10 @@ simpleTableHeader headless = try $ do
710725 return (rawHeads, aligns, indices)
711726
712727gridTableHeader :: Bool -- ^ Headerless table
713− -> GenParser Char ParserState ([String], [Alignment], [Int])
728+ -> GenParser Char ParserState ([String], [Alignment], [Int])
714729gridTableHeader headless = try $ do
715730 optional blanklines
716731 dashes <- gridDashedLines '-'
717− newline
718732 rawContent <- if headless
719733 then return $ repeat ""
720734 else many1