Skip to content

Commit

Permalink
Markdown reader: Improved pipe table parsing.
Browse files Browse the repository at this point in the history
Fixes jgm#2765.
Added test case.
  • Loading branch information
jgm committed Mar 9, 2016
1 parent 54a6861 commit 2b55b76
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
30 changes: 15 additions & 15 deletions src/Text/Pandoc/Readers/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,25 +1376,25 @@ sepPipe = try $ do
pipeTableRow :: MarkdownParser (F [Blocks])
pipeTableRow = try $ do
scanForPipe
raw <- anyLine
parseFromString pipeTableRow' raw

pipeTableRow' :: MarkdownParser (F [Blocks])
pipeTableRow' = do
skipMany spaceChar
openPipe <- (True <$ char '|') <|> return False
let cell = mconcat <$> (many (notFollowedBy (char '|') >> inline))
cells <- cell `sepEndBy1` (char '|')
-- split into cells
let chunk = void (code <|> rawHtmlInline <|> escapedChar <|> rawLaTeXInline')
<|> void (noneOf "|\n\r")
let cellContents = ((trim . snd) <$> withRaw (many chunk)) >>=
parseFromString pipeTableCell
cells <- cellContents `sepEndBy1` (char '|')
-- surrounding pipes needed for a one-column table:
guard $ not (length cells == 1 && not openPipe)
spaces >> eof
return $ do
cells' <- sequence cells
return $ map
(\ils ->
case trimInlines ils of
ils' | B.isNull ils' -> mempty
| otherwise -> B.plain $ ils') cells'
blankline
return $ sequence cells

pipeTableCell :: MarkdownParser (F Blocks)
pipeTableCell = do
result <- many inline
if null result
then return mempty
else return $ B.plain . mconcat <$> sequence result

pipeTableHeaderPart :: Parser [Char] st (Alignment, Int)
pipeTableHeaderPart = try $ do
Expand Down
16 changes: 15 additions & 1 deletion tests/pipe-tables.native
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,18 @@
,Para [Str "Pipe",Space,Str "table",Space,Str "with",Space,Str "no",Space,Str "body:"]
,Table [] [AlignDefault] [0.0]
[[Plain [Str "Header"]]]
[]]
[]
,Para [Str "Pipe",Space,Str "table",Space,Str "with",Space,Str "tricky",Space,Str "cell",Space,Str "contents",Space,Str "(see",Space,Str "#2765):"]
,Table [] [AlignLeft,AlignRight,AlignRight] [0.0,0.0,0.0]
[[]
,[Plain [Str "IP_gene8-_1st"]]
,[Plain [Str "IP_gene8+_1st"]]]
[[[Plain [Str "IP_gene8-_1st"]]
,[Plain [Str "1.0000000"]]
,[Plain [Str "0.4357325"]]]
,[[Plain [Str "IP_gene8+_1st"]]
,[Plain [Str "0.4357325"]]
,[Plain [Str "1.0000000"]]]
,[[Plain [Str "foo",Code ("",[],[]) "bar|baz"]]
,[Plain [Str "and|escaped"]]
,[Plain [Str "3.0000000"]]]]]
8 changes: 8 additions & 0 deletions tests/pipe-tables.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ Pipe table with no body:
| Header |
| ------ |

Pipe table with tricky cell contents (see #2765):

| | IP_gene8-_1st| IP_gene8+_1st|
|:--------------|-------------:|-------------:|
|IP_gene8-_1st | 1.0000000| 0.4357325|
|IP_gene8+_1st | 0.4357325| 1.0000000|
|foo`bar|baz` | and\|escaped | 3.0000000|

0 comments on commit 2b55b76

Please sign in to comment.