@@ -48,6 +48,8 @@ module Text.Pandoc.Parsing ( (>>~),
4848 romanNumeral,
4949 emailAddress,
5050 uri,
51+ mathInline,
52+ mathDisplay,
5153 withHorizDisplacement,
5254 withRaw,
5355 escaped,
@@ -455,6 +457,39 @@ uri = try $ do
455457 let uri' = scheme ++ ":" ++ fromEntities str'
456458 return (uri', escapeURI uri')
457459
460+mathInlineWith :: String -> String -> Parser [Char] st String
461+mathInlineWith op cl = try $ do
462+ string op
463+ notFollowedBy space
464+ words' <- many1Till (count 1 (noneOf "\n\\")
465+ <|> (char '\\' >> anyChar >>= \c -> return ['\\',c])
466+ <|> count 1 newline <* notFollowedBy' blankline
467+ *> return " ")
468+ (try $ string cl)
469+ notFollowedBy digit -- to prevent capture of $5
470+ return $ concat words'
471+
472+mathDisplayWith :: String -> String -> Parser [Char] st String
473+mathDisplayWith op cl = try $ do
474+ string op
475+ many1Till (noneOf "\n" <|> (newline >>~ notFollowedBy' blankline)) (try $ string cl)
476+
477+mathDisplay :: Parser [Char] ParserState String
478+mathDisplay =
479+ (guardEnabled Ext_tex_math_dollars >> mathDisplayWith "$$" "$$")
480+ <|> (guardEnabled Ext_tex_math_single_backslash >>
481+ mathDisplayWith "\\[" "\\]")
482+ <|> (guardEnabled Ext_tex_math_double_backslash >>
483+ mathDisplayWith "\\\\[" "\\\\]")
484+
485+mathInline :: Parser [Char] ParserState String
486+mathInline =
487+ (guardEnabled Ext_tex_math_dollars >> mathInlineWith "$" "$")
488+ <|> (guardEnabled Ext_tex_math_single_backslash >>
489+ mathInlineWith "\\(" "\\)")
490+ <|> (guardEnabled Ext_tex_math_double_backslash >>
491+ mathInlineWith "\\\\(" "\\\\)")
492+
458493-- | Applies a parser, returns tuple of its results and its horizontal
459494-- displacement (the difference between the source column at the end
460495-- and the source column at the beginning). Vertical displacement