git.iko.soy/typeable/pandoc
on master help sign in
HTML reader: Parse LaTeX math if appropriate options are set.
def05d3John MacFarlane
src/Text/Pandoc/Parsing.hs+35
@@ -48,6 +48,8 @@ module Text.Pandoc.Parsing ( (>>~),
48 romanNumeral,
48 romanNumeral,
49 emailAddress,
49 emailAddress,
50 uri,
50 uri,
51+ mathInline,
52+ mathDisplay,
51 withHorizDisplacement,
53 withHorizDisplacement,
52 withRaw,
54 withRaw,
53 escaped,
55 escaped,
@@ -455,6 +457,39 @@ uri = try $ do
455 let uri' = scheme ++ ":" ++ fromEntities str'
457 let uri' = scheme ++ ":" ++ fromEntities str'
456 return (uri', escapeURI uri')
458 return (uri', escapeURI uri')
457
459
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+
458-- | Applies a parser, returns tuple of its results and its horizontal
493-- | Applies a parser, returns tuple of its results and its horizontal
459-- displacement (the difference between the source column at the end
494-- displacement (the difference between the source column at the end
460-- and the source column at the beginning). Vertical displacement
495-- and the source column at the beginning). Vertical displacement
src/Text/Pandoc/Readers/HTML.hs+8−1
@@ -467,7 +467,13 @@ pBlank = try $ do
467
467
468pTagContents :: Parser [Char] ParserState Inline
468pTagContents :: Parser [Char] ParserState Inline
469pTagContents =
469pTagContents =
470+ Math InlineMath `fmap` mathInline
471+ <|> Math DisplayMath `fmap` mathDisplay
472+ <|> pStr
473+ <|> pSpace
470− pStr <|> pSpace <|> smartPunctuation pTagContents <|> pSymbol <|> pBad
474+ <|> smartPunctuation pTagContents
475+ <|> pSymbol
476+ <|> pBad
471
477
472pStr :: Parser [Char] ParserState Inline
478pStr :: Parser [Char] ParserState Inline
473pStr = do
479pStr = do
@@ -482,6 +488,7 @@ isSpecial '"' = True
482isSpecial '\'' = True
488isSpecial '\'' = True
483isSpecial '.' = True
489isSpecial '.' = True
484isSpecial '-' = True
490isSpecial '-' = True
491+isSpecial '$' = True
485isSpecial '\8216' = True
492isSpecial '\8216' = True
486isSpecial '\8217' = True
493isSpecial '\8217' = True
487isSpecial '\8220' = True
494isSpecial '\8220' = True
src/Text/Pandoc/Readers/LaTeX.hs+2−1
@@ -38,7 +38,8 @@ import Text.Pandoc.Definition
38import Text.Pandoc.Walk
38import Text.Pandoc.Walk
39import Text.Pandoc.Shared
39import Text.Pandoc.Shared
40import Text.Pandoc.Options
40import Text.Pandoc.Options
41−import Text.Pandoc.Parsing hiding ((<|>), many, optional, space)
41+import Text.Pandoc.Parsing hiding ((<|>), many, optional, space,
42+ mathDisplay, mathInline)
42import qualified Text.Pandoc.UTF8 as UTF8
43import qualified Text.Pandoc.UTF8 as UTF8
43import Data.Char ( chr, ord )
44import Data.Char ( chr, ord )
44import Control.Monad
45import Control.Monad
src/Text/Pandoc/Readers/Markdown.hs−33
@@ -1408,39 +1408,6 @@ math :: MarkdownParser (F Inlines)
1408math = (return . B.displayMath <$> (mathDisplay >>= applyMacros'))
1408math = (return . B.displayMath <$> (mathDisplay >>= applyMacros'))
1409 <|> (return . B.math <$> (mathInline >>= applyMacros'))
1409 <|> (return . B.math <$> (mathInline >>= applyMacros'))
1410
1410
1411−mathDisplay :: MarkdownParser String
1412−mathDisplay =
1413− (guardEnabled Ext_tex_math_dollars >> mathDisplayWith "$$" "$$")
1414− <|> (guardEnabled Ext_tex_math_single_backslash >>
1415− mathDisplayWith "\\[" "\\]")
1416− <|> (guardEnabled Ext_tex_math_double_backslash >>
1417− mathDisplayWith "\\\\[" "\\\\]")
1418−
1419−mathDisplayWith :: String -> String -> MarkdownParser String
1420−mathDisplayWith op cl = try $ do
1421− string op
1422− many1Till (noneOf "\n" <|> (newline >>~ notFollowedBy' blankline)) (try $ string cl)
1423−
1424−mathInline :: MarkdownParser String
1425−mathInline =
1426− (guardEnabled Ext_tex_math_dollars >> mathInlineWith "$" "$")
1427− <|> (guardEnabled Ext_tex_math_single_backslash >>
1428− mathInlineWith "\\(" "\\)")
1429− <|> (guardEnabled Ext_tex_math_double_backslash >>
1430− mathInlineWith "\\\\(" "\\\\)")
1431−
1432−mathInlineWith :: String -> String -> MarkdownParser String
1433−mathInlineWith op cl = try $ do
1434− string op
1435− notFollowedBy space
1436− words' <- many1Till (count 1 (noneOf "\n\\")
1437− <|> (char '\\' >> anyChar >>= \c -> return ['\\',c])
1438− <|> count 1 newline <* notFollowedBy' blankline
1439− *> return " ")
1440− (try $ string cl)
1441− notFollowedBy digit -- to prevent capture of $5
1442− return $ concat words'
1443−
1444-- Parses material enclosed in *s, **s, _s, or __s.
1411-- Parses material enclosed in *s, **s, _s, or __s.
1445-- Designed to avoid backtracking.
1412-- Designed to avoid backtracking.
1446enclosure :: Char
1413enclosure :: Char