@@ -717,7 +717,7 @@ dashedLine ch = do
717717-- Parse a table header with dashed lines of '-' preceded by
718718-- one (or zero) line of text.
719719simpleTableHeader :: Bool -- ^ Headerless table
720− -> GenParser Char ParserState ([[Char]], [Alignment], [Int])
720+ -> GenParser Char ParserState ([[Block]], [Alignment], [Int])
721721simpleTableHeader headless = try $ do
722722 rawContent <- if headless
723723 then return ""
@@ -736,7 +736,9 @@ simpleTableHeader headless = try $ do
736736 let rawHeads' = if headless
737737 then replicate (length dashes) ""
738738 else rawHeads
739− return (rawHeads', aligns, indices)
739+ heads <- mapM (parseFromString (many plain)) $
740+ map removeLeadingTrailingSpace rawHeads'
741+ return (heads, aligns, indices)
740742
741743-- Parse a table footer - dashed lines followed by blank line.
742744tableFooter :: GenParser Char ParserState [Char]
@@ -765,34 +767,9 @@ multilineRow :: [Int]
765767 -> GenParser Char ParserState [[Block]]
766768multilineRow indices = do
767769 colLines <- many1 (rawTableLine indices)
768− optional blanklines
769770 let cols = map unlines $ transpose colLines
770771 mapM (parseFromString (many plain)) cols
771772
772−-- Calculate relative widths of table columns, based on indices
773−widthsFromIndices :: Int -- Number of columns on terminal
774− -> [Int] -- Indices
775− -> [Double] -- Fractional relative sizes of columns
776−widthsFromIndices _ [] = []
777−widthsFromIndices numColumns indices =
778− let lengths' = zipWith (-) indices (0:indices)
779− lengths = reverse $
780− case reverse lengths' of
781− [] -> []
782− [x] -> [x]
783− -- compensate for the fact that intercolumn
784− -- spaces are counted in widths of all columns
785− -- but the last...
786− (x:y:zs) -> if x < y && y - x <= 2
787− then y:y:zs
788− else x:y:zs
789− totLength = sum lengths
790− quotient = if totLength > numColumns
791− then fromIntegral totLength
792− else fromIntegral numColumns
793− fracs = map (\l -> (fromIntegral l) / quotient) lengths in
794− tail fracs
795−
796773-- Parses a table caption: inlines beginning with 'Table:'
797774-- and followed by blank lines.
798775tableCaption :: GenParser Char ParserState [Inline]
@@ -803,27 +780,14 @@ tableCaption = try $ do
803780 blanklines
804781 return $ normalizeSpaces result
805782
806−-- Parse a table using 'headerParser', 'lineParser', and 'footerParser'.
807−tableWith :: GenParser Char ParserState ([[Char]], [Alignment], [Int])
808− -> ([Int] -> GenParser Char ParserState [[Block]])
809− -> GenParser Char ParserState end
810− -> GenParser Char ParserState Block
811−tableWith headerParser lineParser footerParser = try $ do
812− (rawHeads, aligns, indices) <- headerParser
813− lines' <- many1Till (lineParser indices) footerParser
814− caption <- option [] tableCaption
815− heads <- mapM (parseFromString (many plain)) rawHeads
816− state <- getState
817− let numColumns = stateColumns state
818− let widths = widthsFromIndices numColumns indices
819− return $ Table caption aligns widths heads lines'
820−
821783-- Parse a simple table with '---' header and one line per row.
822784simpleTable :: Bool -- ^ Headerless table
823785 -> GenParser Char ParserState Block
824786simpleTable headless = do
825787 Table c a _w h l <- tableWith (simpleTableHeader headless) tableLine
826− (if headless then tableFooter else tableFooter <|> blanklines)
788+ (return ())
789+ (if headless then tableFooter else tableFooter <|> blanklines)
790+ tableCaption
827791 -- Simple tables get 0s for relative column widths (i.e., use default)
828792 return $ Table c a (replicate (length a) 0) h l
829793
@@ -834,10 +798,10 @@ simpleTable headless = do
834798multilineTable :: Bool -- ^ Headerless table
835799 -> GenParser Char ParserState Block
836800multilineTable headless =
837− tableWith (multilineTableHeader headless) multilineRow tableFooter
801+ tableWith (multilineTableHeader headless) multilineRow blanklines tableFooter tableCaption
838802
839803multilineTableHeader :: Bool -- ^ Headerless table
840− -> GenParser Char ParserState ([String], [Alignment], [Int])
804+ -> GenParser Char ParserState ([[Block]], [Alignment], [Int])
841805multilineTableHeader headless = try $ do
842806 if headless
843807 then return '\n'
@@ -861,7 +825,9 @@ multilineTableHeader headless = try $ do
861825 let rawHeads = if headless
862826 then replicate (length dashes) ""
863827 else map (intercalate " ") rawHeadsList
864− return ((map removeLeadingTrailingSpace rawHeads), aligns, indices)
828+ heads <- mapM (parseFromString (many plain)) $
829+ map removeLeadingTrailingSpace rawHeads
830+ return (heads, aligns, indices)
865831
866832-- Returns an alignment type for a table, based on a list of strings
867833-- (the rows of the column header) and a number (the length of the
@@ -881,9 +847,15 @@ alignType strLst len =
881847 (True, True) -> AlignCenter
882848 (False, False) -> AlignDefault
883849
850+gridTable :: Bool -- ^ Headerless table
851+ -> GenParser Char ParserState Block
852+gridTable = gridTableWith block tableCaption
853+
854+-- TODO - add grid tables here...add tests for markdown grid tables...document markdown grid tables.
884855table :: GenParser Char ParserState Block
885856table = multilineTable False <|> simpleTable True <|>
886− simpleTable False <|> multilineTable True <?> "table"
857+ simpleTable False <|> multilineTable True <|>
858+ gridTable False <|> gridTable True <?> "table"
887859
888860--
889861-- inline