summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2018-01-01 16:42:19 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2018-01-01 16:42:19 -0800
commit5733183b3de4ab93a1341410eef4249f09648f3f (patch)
tree9aaea66e3f7667ea5567b3d504209f95623a7508
parentecc46e229fde934f163d1f646383d24bfe2039e1 (diff)
Markdown reader: rewrite inlinesInBalancedBrackets.
The rewrite is much more direct, avoiding parseFromString. And it performs significantly better; unfortunately, parsing time still increases exponentially. See #1735.
-rw-r--r--src/Text/Pandoc/Readers/Markdown.hs32
1 files changed, 13 insertions, 19 deletions
diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs
index f5e6512f8..4a09c2aad 100644
--- a/src/Text/Pandoc/Readers/Markdown.hs
+++ b/src/Text/Pandoc/Readers/Markdown.hs
@@ -148,25 +148,19 @@ 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 = do
- char '['
- pos <- getPosition
- (_, raw) <- withRaw $ charsInBalancedBrackets 1
- guard $ not $ null raw
- parseFromString' (setPosition pos >>
- trimInlinesF <$> inlines) (init raw)
-
-charsInBalancedBrackets :: PandocMonad m => Int -> MarkdownParser m ()
-charsInBalancedBrackets 0 = return ()
-charsInBalancedBrackets openBrackets =
- (char '[' >> charsInBalancedBrackets (openBrackets + 1))
- <|> (char ']' >> charsInBalancedBrackets (openBrackets - 1))
- <|> (( (() <$ code)
- <|> (() <$ escapedChar')
- <|> (newline >> notFollowedBy blankline)
- <|> skipMany1 (noneOf "[]`\n\\")
- <|> (() <$ count 1 (oneOf "`\\"))
- ) >> charsInBalancedBrackets openBrackets)
+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)
--
-- document structure