Skip to content

Commit

Permalink
Markdown reader: Improved inlinesInBalancedBrackets.
Browse files Browse the repository at this point in the history
The change both improves performance and fixes a
regression whereby normal citations inside inline notes
were not parsed correctly.

Closes jgm/pandoc-citeproc#315.
  • Loading branch information
jgm committed Jan 14, 2018
1 parent cd80b8d commit d9584d7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/Text/Pandoc/Parsing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ module Text.Pandoc.Parsing ( takeWhileP,
sourceLine,
setSourceColumn,
setSourceLine,
incSourceColumn,
newPos,
Line,
Column
Expand Down
34 changes: 21 additions & 13 deletions src/Text/Pandoc/Readers/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,27 @@ litChar = escapedChar'
-- | Parse a sequence of inline elements between square brackets,
-- including inlines between balanced pairs of square brackets.
inlinesInBalancedBrackets :: PandocMonad m => MarkdownParser m (F Inlines)
inlinesInBalancedBrackets = try $ char '[' >> go 1
where go :: PandocMonad m => Int -> MarkdownParser m (F Inlines)
go 0 = return mempty
go openBrackets =
(mappend <$> (bracketedSpan <|> link <|> image) <*>
go openBrackets)
<|> ((if openBrackets > 1
then (return (B.str "]") <>)
else id) <$>
(char ']' >> go (openBrackets - 1)))
<|> ((return (B.str "[") <>) <$>
(char '[' >> go (openBrackets + 1)))
<|> (mappend <$> inline <*> go openBrackets)
inlinesInBalancedBrackets =
try $ char '[' >> withRaw (go 1) >>=
parseFromString inlines . stripBracket . snd
where stripBracket [] = []
stripBracket xs = if last xs == ']' then init xs else xs
go :: PandocMonad m => Int -> MarkdownParser m ()
go 0 = return ()
go openBrackets = do
(() <$ (escapedChar <|>
code <|>
rawHtmlInline <|>
rawLaTeXInline') >> go openBrackets)
<|>
(do char ']'
if openBrackets > 1
then go (openBrackets - 1)
else return ())
<|>
(char '[' >> go (openBrackets + 1))
<|>
(anyChar >> go openBrackets)

--
-- document structure
Expand Down
2 changes: 1 addition & 1 deletion test/Tests/Readers/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ tests = [ testGroup "inline code"
":smile: and :+1:" =?> para (text "😄 and 👍")
]
, "unbalanced brackets" =:
"[[[[[[[[[[[hi" =?> para (text "[[[[[[[[[[[hi")
"[[[[[[[[[[[[hi" =?> para (text "[[[[[[[[[[[[hi")
, testGroup "backslash escapes"
[ "in URL" =:
"[hi](/there\\))"
Expand Down
6 changes: 6 additions & 0 deletions test/command/cite-in-inline-note.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```
% pandoc -t native
foo^[bar [@doe]]
^D
[Para [Str "foo",Note [Para [Str "bar",Space,Cite [Citation {citationId = "doe", citationPrefix = [], citationSuffix = [], citationMode = NormalCitation, citationNoteNum = 0, citationHash = 0}] [Str "[@doe]"]]]]]
```

0 comments on commit d9584d7

Please sign in to comment.