@@ -68,7 +68,9 @@ module Text.Pandoc.Parsing ( (>>~),
6868 toKey,
6969 fromKey,
7070 lookupKeySrc,
71− smartPunctuation )
71+ smartPunctuation,
72+ macro,
73+ applyMacros' )
7274where
7375
7476import Text.Pandoc.Definition
@@ -82,7 +84,7 @@ import Network.URI ( parseURI, URI (..), isAllowedInURI )
8284import Control.Monad ( join, liftM, guard )
8385import Text.Pandoc.Shared
8486import qualified Data.Map as M
85−import Text.TeXMath.Macros (Macro)
87+import Text.TeXMath.Macros (applyMacros, Macro, pMacroDefinition)
8688
8789-- | Like >>, but returns the operation on the left.
8890-- (Suggested by Tillmann Rendel on Haskell-cafe list.)
@@ -781,3 +783,26 @@ emDash = do
781783 try (charOrRef "—") <|> (oneOfStrings ["---", "--"] >> return '—')
782784 return EmDash
783785
786+--
787+-- Macros
788+--
789+
790+-- | Parse a \newcommand or \renewcommand macro definition.
791+macro :: GenParser Char ParserState Block
792+macro = getState >>= guard . stateApplyMacros >>
793+ pMacroDefinition >>= addMacro >> blanklines >> return Null
794+
795+-- | Add a macro to the list of macros in state.
796+addMacro :: Macro -> GenParser Char ParserState ()
797+addMacro m = do
798+ updateState $ \st -> st{ stateMacros = m : stateMacros st }
799+
800+-- | Apply current macros to string.
801+applyMacros' :: String -> GenParser Char ParserState String
802+applyMacros' target = do
803+ apply <- liftM stateApplyMacros getState
804+ if apply
805+ then do macros <- liftM stateMacros getState
806+ return $ applyMacros macros target
807+ else return target
808+