From d9584d73f94501787026c57b77d217e51f21505d Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Sun, 14 Jan 2018 12:23:16 -0800
Subject: Markdown reader: Improved inlinesInBalancedBrackets.

The change both improves performance and fixes a
regression whereby normal citations inside inline notes
were not parsed correctly.

Closes jgm/pandoc-citeproc#315.
---
 src/Text/Pandoc/Parsing.hs          |  1 +
 src/Text/Pandoc/Readers/Markdown.hs | 34 +++++++++++++++++++++-------------
 test/Tests/Readers/Markdown.hs      |  2 +-
 test/command/cite-in-inline-note.md |  6 ++++++
 4 files changed, 29 insertions(+), 14 deletions(-)
 create mode 100644 test/command/cite-in-inline-note.md

diff --git a/src/Text/Pandoc/Parsing.hs b/src/Text/Pandoc/Parsing.hs
index 9573d7875..f1b823965 100644
--- a/src/Text/Pandoc/Parsing.hs
+++ b/src/Text/Pandoc/Parsing.hs
@@ -180,6 +180,7 @@ module Text.Pandoc.Parsing ( takeWhileP,
                              sourceLine,
                              setSourceColumn,
                              setSourceLine,
+                             incSourceColumn,
                              newPos,
                              Line,
                              Column
diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs
index aaefa5ba1..b76ff498b 100644
--- a/src/Text/Pandoc/Readers/Markdown.hs
+++ b/src/Text/Pandoc/Readers/Markdown.hs
@@ -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
diff --git a/test/Tests/Readers/Markdown.hs b/test/Tests/Readers/Markdown.hs
index c331eb3e9..1cd32b87d 100644
--- a/test/Tests/Readers/Markdown.hs
+++ b/test/Tests/Readers/Markdown.hs
@@ -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\\))"
diff --git a/test/command/cite-in-inline-note.md b/test/command/cite-in-inline-note.md
new file mode 100644
index 000000000..069484eed
--- /dev/null
+++ b/test/command/cite-in-inline-note.md
@@ -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]"]]]]]
+```
-- 
cgit v1.2.3