Markdown reader: Fixed bug with headerless grid tables.

The 1.10 code assumed that each table header cell contains
exactly one block. That failed for headerless tables (0) and also
for tables with multiple blocks in a header cell.

The code is fixed and tests provided. Thanks to Andrew Lee for
pointing out the bug.
This commit is contained in:
John MacFarlane 2013-01-21 09:50:39 -08:00
parent c4b93bc3e7
commit af84953781
3 changed files with 93 additions and 3 deletions

View file

@ -1110,7 +1110,7 @@ gridTableHeader headless = try $ do
then replicate (length dashes) ""
else map (intercalate " ") $ transpose
$ map (gridTableSplitLine indices) rawContent
heads <- fmap sequence $ mapM (parseFromString block) $
heads <- fmap sequence $ mapM (parseFromString parseBlocks) $
map trim rawHeads
return (heads, aligns, indices)
@ -1127,7 +1127,7 @@ gridTableRow indices = do
colLines <- many1 (gridTableRawLine indices)
let cols = map ((++ "\n") . unlines . removeOneLeadingSpace) $
transpose colLines
fmap compactify' <$> fmap sequence (mapM (parseFromString block) cols)
fmap compactify' <$> fmap sequence (mapM (parseFromString parseBlocks) cols)
removeOneLeadingSpace :: [String] -> [String]
removeOneLeadingSpace xs =

View file

@ -67,4 +67,55 @@
,Header 2 ("foobar",["baz"],[("key","val")]) [Str "Explicit",Space,Str "header",Space,Str "attributes"]
,Header 2 ("line-blocks",[],[]) [Str "Line",Space,Str "blocks"]
,Para [Str "But",Space,Str "can",Space,Str "a",Space,Str "bee",Space,Str "be",Space,Str "said",Space,Str "to",Space,Str "be",LineBreak,Str "\160\160\160\160or",Space,Str "not",Space,Str "to",Space,Str "be",Space,Str "an",Space,Str "entire",Space,Str "bee,",LineBreak,Str "\160\160\160\160\160\160\160\160when",Space,Str "half",Space,Str "the",Space,Str "bee",Space,Str "is",Space,Str "not",Space,Str "a",Space,Str "bee,",LineBreak,Str "\160\160\160\160\160\160\160\160\160\160\160\160due",Space,Str "to",Space,Str "some",Space,Str "ancient",Space,Str "injury?"]
,Para [Str "Continuation",Space,Str "line",LineBreak,Str "\160\160and",Space,Str "another"]]
,Para [Str "Continuation",Space,Str "line",LineBreak,Str "\160\160and",Space,Str "another"]
,Header 2 ("grid-tables",[],[]) [Str "Grid",Space,Str "Tables"]
,Table [] [AlignDefault,AlignDefault,AlignDefault] [0.2638888888888889,0.16666666666666666,0.18055555555555555]
[[Plain [Str "col",Space,Str "1"]]
,[Plain [Str "col",Space,Str "2"]]
,[Plain [Str "col",Space,Str "3"]]]
[[[Para [Str "r1",Space,Str "a",Space,Str "r1",Space,Str "bis"]]
,[Para [Str "b",Space,Str "b",Space,Str "2"]]
,[Para [Str "c",Space,Str "c",Space,Str "2"]]]
,[[Para [Str "r2",Space,Str "d"]]
,[Para [Str "e"]]
,[Para [Str "f"]]]]
,Para [Str "Headless"]
,Table [] [AlignDefault,AlignDefault,AlignDefault] [0.2638888888888889,0.16666666666666666,0.18055555555555555]
[[]
,[]
,[]]
[[[Para [Str "r1",Space,Str "a",Space,Str "r1",Space,Str "bis"]]
,[Para [Str "b",Space,Str "b",Space,Str "2"]]
,[Para [Str "c",Space,Str "c",Space,Str "2"]]]
,[[Para [Str "r2",Space,Str "d"]]
,[Para [Str "e"]]
,[Para [Str "f"]]]]
,Para [Str "Spaces",Space,Str "at",Space,Str "ends",Space,Str "of",Space,Str "lines"]
,Table [] [AlignDefault,AlignDefault,AlignDefault] [0.2638888888888889,0.16666666666666666,0.18055555555555555]
[[]
,[]
,[]]
[[[Para [Str "r1",Space,Str "a",Space,Str "r1",Space,Str "bis"]]
,[Para [Str "b",Space,Str "b",Space,Str "2"]]
,[Para [Str "c",Space,Str "c",Space,Str "2"]]]
,[[Para [Str "r2",Space,Str "d"]]
,[Para [Str "e"]]
,[Para [Str "f"]]]]
,Para [Str "Multiple",Space,Str "blocks",Space,Str "in",Space,Str "a",Space,Str "cell"]
,Table [] [AlignDefault,AlignDefault,AlignDefault] [0.2638888888888889,0.16666666666666666,0.18055555555555555]
[[]
,[]
,[]]
[[[Header 1 ("col-1",[],[]) [Str "col",Space,Str "1"]
,Para [Str "col",Space,Str "1"]]
,[Header 1 ("col-2",[],[]) [Str "col",Space,Str "2"]
,Para [Str "col",Space,Str "2"]]
,[Header 1 ("col-3",[],[]) [Str "col",Space,Str "3"]
,Para [Str "col",Space,Str "3"]]]
,[[Para [Str "r1",Space,Str "a"]
,Para [Str "r1",Space,Str "bis"]]
,[BulletList
[[Plain [Str "b"]]
,[Plain [Str "b",Space,Str "2"]]
,[Plain [Str "b",Space,Str "2"]]]]
,[Para [Str "c",Space,Str "c",Space,Str "2",Space,Str "c",Space,Str "2"]]]]]

View file

@ -162,3 +162,42 @@ But this is not a link to [My other header], since the reference is defined.
| and
another
## Grid Tables
+------------------+-----------+------------+
| col 1 | col 2 | col 3 |
+==================+===========+============+
| r1 a | b | c |
| r1 bis | b 2 | c 2 |
+------------------+-----------+------------+
| r2 d | e | f |
+------------------+-----------+------------+
Headless
+------------------+-----------+------------+
| r1 a | b | c |
| r1 bis | b 2 | c 2 |
+------------------+-----------+------------+
| r2 d | e | f |
+------------------+-----------+------------+
Spaces at ends of lines
+------------------+-----------+------------+
| r1 a | b | c |
| r1 bis | b 2 | c 2 |
+------------------+-----------+------------+
| r2 d | e | f |
+------------------+-----------+------------+
Multiple blocks in a cell
+------------------+-----------+------------+
| # col 1 | # col 2 | # col 3 |
| col 1 | col 2 | col 3 |
+------------------+-----------+------------+
| r1 a | - b | c |
| | - b 2 | c 2 |
| r1 bis | - b 2 | c 2 |
+------------------+-----------+------------+