@@ -34,7 +34,7 @@ import Text.Pandoc.Definition
3434import Text.Pandoc.Shared
3535import Text.Pandoc.Parsing
3636import Text.ParserCombinators.Parsec
37−import Control.Monad ( when, unless, liftM )
37+import Control.Monad ( when, unless )
3838import Data.List ( findIndex, intercalate, transpose, sort )
3939import qualified Data.Map as M
4040import Text.Printf ( printf )
@@ -608,41 +608,20 @@ dashedLine ch = do
608608simpleDashedLines :: Char -> GenParser Char st [(Int,Int)]
609609simpleDashedLines ch = try $ many1 (dashedLine ch)
610610
611−gridPart :: Char -> GenParser Char st (Int, Int)
612−gridPart ch = do
613− dashes <- many1 (char ch)
614− char '+'
615− return (length dashes, length dashes + 1)
616−
617−gridDashedLines :: Char -> GenParser Char st [(Int,Int)]
618−gridDashedLines ch = try $ char '+' >> many1 (gridPart ch) >>~ blankline
619−
620611-- Parse a table row separator
621612simpleTableSep :: Char -> GenParser Char ParserState Char
622613simpleTableSep ch = try $ simpleDashedLines ch >> newline
623614
624−gridTableSep :: Char -> GenParser Char ParserState Char
625−gridTableSep ch = try $ gridDashedLines ch >> return '\n'
626−
627615-- Parse a table footer
628616simpleTableFooter :: GenParser Char ParserState [Char]
629617simpleTableFooter = try $ simpleTableSep '=' >> blanklines
630618
631−gridTableFooter :: GenParser Char ParserState [Char]
632−gridTableFooter = blanklines
633−
634619-- Parse a raw line and split it into chunks by indices.
635620simpleTableRawLine :: [Int] -> GenParser Char ParserState [String]
636621simpleTableRawLine indices = do
637622 line <- many1Till anyChar newline
638623 return (simpleTableSplitLine indices line)
639624
640−gridTableRawLine :: [Int] -> GenParser Char ParserState [String]
641−gridTableRawLine indices = do
642− char '|'
643− line <- many1Till anyChar newline
644− return (gridTableSplitLine indices $ removeTrailingSpace line)
645−
646625-- Parse a table row and return a list of blocks (columns).
647626simpleTableRow :: [Int] -> GenParser Char ParserState [[Block]]
648627simpleTableRow indices = do
@@ -652,38 +631,11 @@ simpleTableRow indices = do
652631 let cols = map unlines . transpose $ firstLine : colLines
653632 mapM (parseFromString (many plain)) cols
654633
655−gridTableRow :: [Int]
656− -> GenParser Char ParserState [[Block]]
657−gridTableRow indices = do
658− colLines <- many1 (gridTableRawLine indices)
659− let cols = map ((++ "\n") . unlines . removeOneLeadingSpace) $
660− transpose colLines
661− mapM (liftM compactifyCell . parseFromString (many block)) cols
662−
663−compactifyCell :: [Block] -> [Block]
664−compactifyCell bs = head $ compactify [bs]
665−
666634simpleTableSplitLine :: [Int] -> String -> [String]
667635simpleTableSplitLine indices line =
668636 map removeLeadingTrailingSpace
669637 $ tail $ splitByIndices (init indices) line
670638
671−gridTableSplitLine :: [Int] -> String -> [String]
672−gridTableSplitLine indices line =
673− map removeFinalBar $ tail $ splitByIndices (init indices) line
674−
675−removeFinalBar :: String -> String
676−removeFinalBar = reverse . dropWhile (=='|') . dropWhile (`elem` " \t") .
677− reverse
678−
679−removeOneLeadingSpace :: [String] -> [String]
680−removeOneLeadingSpace xs =
681− if all startsWithSpace xs
682− then map (drop 1) xs
683− else xs
684− where startsWithSpace "" = True
685− startsWithSpace (y:_) = y == ' '
686−
687639-- Calculate relative widths of table columns, based on indices
688640widthsFromIndices :: Int -- Number of columns on terminal
689641 -> [Int] -- Indices
@@ -725,30 +677,10 @@ simpleTableHeader headless = try $ do
725677 else simpleTableSplitLine indices rawContent
726678 return (rawHeads, aligns, indices)
727679
728−gridTableHeader :: Bool -- ^ Headerless table
729− -> GenParser Char ParserState ([String], [Alignment], [Int])
730−gridTableHeader headless = try $ do
731− optional blanklines
732− dashes <- gridDashedLines '-'
733− rawContent <- if headless
734− then return $ repeat ""
735− else many1
736− (notFollowedBy (gridTableSep '=') >> char '|' >> many1Till anyChar newline)
737− if headless
738− then return ()
739− else gridTableSep '=' >> return ()
740− let lines' = map snd dashes
741− let indices = scanl (+) 0 lines'
742− let aligns = replicate (length lines') AlignDefault -- RST does not have a notion of alignments
743− let rawHeads = if headless
744− then replicate (length dashes) ""
745− else map (intercalate " ") $ transpose
746− $ map (gridTableSplitLine indices) rawContent
747− return (rawHeads, aligns, indices)
748−
749680-- Parse a table using 'headerParser', 'lineParser', and 'footerParser'.
750681tableWith :: GenParser Char ParserState ([[Char]], [Alignment], [Int])
751− -> ([Int] -> GenParser Char ParserState [[Block]])
682+ -> ([Int]
683+ -> GenParser Char ParserState [[Block]])
752684 -> GenParser Char ParserState sep
753685 -> GenParser Char ParserState end
754686 -> GenParser Char ParserState Block
@@ -778,9 +710,10 @@ simpleTable headless = do
778710-- which may be grid, separated by blank lines, and
779711-- ending with a footer (dashed line followed by blank line).
780712gridTable :: Bool -- ^ Headerless table
781− -> GenParser Char ParserState Block
713+ -> GenParser Char ParserState Block
782714gridTable headless =
783− tableWith (gridTableHeader headless) gridTableRow (gridTableSep '-') gridTableFooter
715+ tableWith (gridTableHeader headless) (gridTableRow block) (gridTableSep '-') gridTableFooter
716+
784717
785718table :: GenParser Char ParserState Block
786719table = gridTable False <|> simpleTable False <|>