diff options
author | John MacFarlane <jgm@berkeley.edu> | 2021-11-02 23:25:27 -0700 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2021-11-02 23:25:27 -0700 |
commit | fa83246d7de8527bbf59dfac9636a42ede185194 (patch) | |
tree | c9c3bc9d317ce413ecd13d8d2203e47bf42191eb | |
parent | 3e466b9346d68ea711d8d3063ac24e7d24512a07 (diff) | |
download | pandoc-fa83246d7de8527bbf59dfac9636a42ede185194.tar.gz |
Markdown reader: Improve inlinesInBalancedBrackets.
This is just a small improvement in terms of performance,
but it's simpler and more direct code.
Also, we avoid parsing interparagraph spaces in balanced
brackets, as the original did.
-rw-r--r-- | src/Text/Pandoc/Readers/Markdown.hs | 32 |
1 files changed, 12 insertions, 20 deletions
diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs index a73c0cba3..e24c38d33 100644 --- a/src/Text/Pandoc/Readers/Markdown.hs +++ b/src/Text/Pandoc/Readers/Markdown.hs @@ -186,26 +186,18 @@ litChar = escapedChar' -- including inlines between balanced pairs of square brackets. inlinesInBalancedBrackets :: PandocMonad m => MarkdownParser m (F Inlines) inlinesInBalancedBrackets = - try $ char '[' >> withRaw (go 1) >>= - parseFromString inlines . stripBracket . snd - where stripBracket t = case T.unsnoc t of - Just (t', ']') -> t' - _ -> t - go :: PandocMonad m => Int -> MarkdownParser m () - go 0 = return () - go openBrackets = - (() <$ (escapedChar <|> - code <|> - math <|> - rawHtmlInline <|> - rawLaTeXInline') >> go openBrackets) - <|> - (do char ']' - Control.Monad.when (openBrackets > 1) $ go (openBrackets - 1)) - <|> - (char '[' >> go (openBrackets + 1)) - <|> - (anyChar >> go openBrackets) + mconcat <$> try (char '[' >> go (1 :: Int)) + where + go n = + (:) <$> (note <|> cite <|> bracketedSpan <|> link) <*> go n + <|> + (char '[' *> ((:) <$> pure (pure (B.str "[")) <*> go (n + 1))) + <|> + (char ']' *> (if n > 1 + then (:) <$> pure (pure (B.str "]")) <*> go (n - 1) + else pure [])) + <|> + (:) <$> inline <*> go n -- -- document structure |