git.iko.soy/typeable/pandoc
on master help sign in
Parsing: fixed `inlineMath` so it handles `\text{..}` containing `$`.

For example: `$x = \text{the $n$th root of $y$}`. Closes #1677.

commitd7169c715d9a78a9a95acdebf089c1ee08f55dd8 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 19 Oct 2014, 16:42 (12 years ago) parent328ff8e
3 files changed+26−1
src/Text/Pandoc/Parsing.hs+23−1 tests/markdown-reader-more.native+1 tests/markdown-reader-more.txt+2
src/Text/Pandoc/Parsing.hs+23−1
@@ -472,7 +472,12 @@ mathInlineWith op cl = try $ do
472472 string op
473473 notFollowedBy space
474474 words' <- many1Till (count 1 (noneOf " \t\n\\")
475− <|> (char '\\' >> anyChar >>= \c -> return ['\\',c])
475+ <|> (char '\\' >>
476+ -- This next clause is needed because \text{..} can
477+ -- contain $, \(\), etc.
478+ (try (string "text" >>
479+ (("\\text" ++) <$> inBalancedBraces 0 ""))
480+ <|> (\c -> ['\\',c]) <$> anyChar))
476481 <|> do (blankline <* notFollowedBy' blankline) <|>
477482 (oneOf " \t" <* skipMany (oneOf " \t"))
478483 notFollowedBy (char '$')
@@ -480,6 +485,23 @@ mathInlineWith op cl = try $ do
480485 ) (try $ string cl)
481486 notFollowedBy digit -- to prevent capture of $5
482487 return $ concat words'
488+ where
489+ inBalancedBraces :: Stream s m Char => Int -> String -> ParserT s st m String
490+ inBalancedBraces 0 "" = do
491+ c <- anyChar
492+ if c == '{'
493+ then inBalancedBraces 1 "{"
494+ else mzero
495+ inBalancedBraces 0 s = return $ reverse s
496+ inBalancedBraces numOpen ('\\':xs) = do
497+ c <- anyChar
498+ inBalancedBraces numOpen (c:'\\':xs)
499+ inBalancedBraces numOpen xs = do
500+ c <- anyChar
501+ case c of
502+ '}' -> inBalancedBraces (numOpen - 1) (c:xs)
503+ '{' -> inBalancedBraces (numOpen + 1) (c:xs)
504+ _ -> inBalancedBraces numOpen (c:xs)
483505
484506mathDisplayWith :: Stream s m Char => String -> String -> ParserT s st m String
485507mathDisplayWith op cl = try $ do
tests/markdown-reader-more.native+1
@@ -17,6 +17,7 @@
1717,Header 3 ("my-header",[],[]) [Str "my",Space,Str "header"]
1818,Header 2 ("in-math",[],[]) [Str "$",Space,Str "in",Space,Str "math"]
1919,Para [Math InlineMath "\\$2 + \\$3"]
20+,Para [Math InlineMath "x = \\text{the $n$th root of $y$}"]
2021,Para [Str "This",Space,Str "should",Space,Str "not",Space,Str "be",Space,Str "math:"]
2122,Para [Str "$PATH",Space,Str "90",Space,Str "$PATH"]
2223,Header 2 ("commented-out-list-item",[],[]) [Str "Commented-out",Space,Str "list",Space,Str "item"]
tests/markdown-reader-more.txt+2
@@ -60,6 +60,8 @@
6060
6161$\$2 + \$3$
6262
63+$x = \text{the $n$th root of $y$}$
64+
6365This should not be math:
6466
6567$PATH 90 $PATH