Parsing: fixed inlineMath so it handles \text{..} containing $.

For example: `$x = \text{the $n$th root of $y$}`.  Closes #1677.
This commit is contained in:
John MacFarlane 2014-10-19 16:42:56 -07:00
parent 328ff8e71f
commit d7169c715d
3 changed files with 26 additions and 1 deletions

View file

@ -472,7 +472,12 @@ mathInlineWith op cl = try $ do
string op
notFollowedBy space
words' <- many1Till (count 1 (noneOf " \t\n\\")
<|> (char '\\' >> anyChar >>= \c -> return ['\\',c])
<|> (char '\\' >>
-- This next clause is needed because \text{..} can
-- contain $, \(\), etc.
(try (string "text" >>
(("\\text" ++) <$> inBalancedBraces 0 ""))
<|> (\c -> ['\\',c]) <$> anyChar))
<|> do (blankline <* notFollowedBy' blankline) <|>
(oneOf " \t" <* skipMany (oneOf " \t"))
notFollowedBy (char '$')
@ -480,6 +485,23 @@ mathInlineWith op cl = try $ do
) (try $ string cl)
notFollowedBy digit -- to prevent capture of $5
return $ concat words'
where
inBalancedBraces :: Stream s m Char => Int -> String -> ParserT s st m String
inBalancedBraces 0 "" = do
c <- anyChar
if c == '{'
then inBalancedBraces 1 "{"
else mzero
inBalancedBraces 0 s = return $ reverse s
inBalancedBraces numOpen ('\\':xs) = do
c <- anyChar
inBalancedBraces numOpen (c:'\\':xs)
inBalancedBraces numOpen xs = do
c <- anyChar
case c of
'}' -> inBalancedBraces (numOpen - 1) (c:xs)
'{' -> inBalancedBraces (numOpen + 1) (c:xs)
_ -> inBalancedBraces numOpen (c:xs)
mathDisplayWith :: Stream s m Char => String -> String -> ParserT s st m String
mathDisplayWith op cl = try $ do

View file

@ -17,6 +17,7 @@
,Header 3 ("my-header",[],[]) [Str "my",Space,Str "header"]
,Header 2 ("in-math",[],[]) [Str "$",Space,Str "in",Space,Str "math"]
,Para [Math InlineMath "\\$2 + \\$3"]
,Para [Math InlineMath "x = \\text{the $n$th root of $y$}"]
,Para [Str "This",Space,Str "should",Space,Str "not",Space,Str "be",Space,Str "math:"]
,Para [Str "$PATH",Space,Str "90",Space,Str "$PATH"]
,Header 2 ("commented-out-list-item",[],[]) [Str "Commented-out",Space,Str "list",Space,Str "item"]

View file

@ -60,6 +60,8 @@
$\$2 + \$3$
$x = \text{the $n$th root of $y$}$
This should not be math:
$PATH 90 $PATH