git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: Apply macros to non-math; handle ensuremath.
commitb63a7f7c48ecacb7594c25460e13d1f924612224 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 5 Jan 2011, 16:55 (16 years ago) parent18e7a7a
1 file changed+23−9
src/Text/Pandoc/Readers/LaTeX.hs+23−9
src/Text/Pandoc/Readers/LaTeX.hs+23−9
@@ -415,6 +415,17 @@ ignore = try $ do
415415 spaces
416416 return Null
417417
418+demacro :: (String, String, [String]) -> GenParser Char ParserState Inline
419+demacro (n,st,args) = try $ do
420+ let raw = "\\" ++ n ++ st ++ concat args
421+ s' <- applyMacros' raw
422+ if raw == s'
423+ then return $ TeX raw
424+ else do
425+ inp <- getInput
426+ setInput $ s' ++ inp
427+ return $ Str ""
428+
418429unknownCommand :: GenParser Char ParserState Block
419430unknownCommand = try $ do
420431 spaces
@@ -423,10 +434,7 @@ unknownCommand = try $ do
423434 when (stateParserContext state == ListItemState) $
424435 notFollowedBy' (string "\\item")
425436 if stateParseRaw state
426− then do
427− (name, star, args) <- command
428− spaces
429− return $ Plain [TeX ("\\" ++ name ++ star ++ concat args)]
437+ then command >>= demacro >>= return . Plain . (:[])
430438 else do
431439 (name, _, args) <- command
432440 spaces
@@ -479,6 +487,7 @@ inline = choice [ str
479487 , nonbreakingSpace
480488 , cite
481489 , specialChar
490+ , ensureMath
482491 , rawLaTeXInline'
483492 , escapedChar
484493 , unescapedChar
@@ -767,6 +776,12 @@ math6 = try $ do
767776 res <- manyTill anyChar (end name)
768777 return $ filter (/= '&') res -- remove alignment codes
769778
779+ensureMath :: GenParser Char st Inline
780+ensureMath = try $ do
781+ (n, _, args) <- command
782+ guard $ n == "ensuremath" && not (null args)
783+ return $ Math InlineMath $ tail $ init $ head args
784+
770785--
771786-- links and images
772787--
@@ -915,12 +930,11 @@ rawLaTeXInline :: GenParser Char ParserState Inline
915930rawLaTeXInline = try $ do
916931 state <- getState
917932 if stateParseRaw state
918− then do
919− (name, star, args) <- command
920− return $ TeX ("\\" ++ name ++ star ++ concat args)
933+ then command >>= demacro
921934 else do
922− (name, _, args) <- command
923− unless (name `elem` commandsToIgnore) $ do
935+ (name,st,args) <- command
936+ x <- demacro (name,st,args)
937+ unless (x == Str "" || name `elem` commandsToIgnore) $ do
924938 inp <- getInput
925939 setInput $ intercalate " " args ++ inp
926940 return $ Str ""