git.iko.soy/typeable/pandoc
on master help sign in
Process LaTeX macros in markdown, and apply to TeX math.

Example: \newcommand{\plus}[2]{#1 + #2} $\plus{3}{4}$ yields: 3+4

commit6b722d1b45b59e8fa57b2fce4b74f2cb933f7b7e authorJohn MacFarlane <jgm@berkeley.edu> dateTue 26 Oct 2010, 09:03 (16 years ago) parent3b1d68b
4 files changed+39−8
pandoc.cabal+1−1 src/Text/Pandoc/Parsing.hs+7−2 src/Text/Pandoc/Readers/Markdown.hs+28−3 src/pandoc.hs+3−2
pandoc.cabal+1−1
@@ -157,7 +157,7 @@ Library
157157 process >= 1, directory >= 1,
158158 bytestring >= 0.9, zip-archive >= 0.1.1.4,
159159 utf8-string >= 0.3, old-time >= 1,
160− HTTP >= 4000.0.5, texmath >= 0.3, xml >= 1.3.5 && < 1.4,
160+ HTTP >= 4000.0.5, texmath >= 0.4, xml >= 1.3.5 && < 1.4,
161161 random, extensible-exceptions
162162 if impl(ghc >= 6.10)
163163 Build-depends: base >= 4 && < 5, syb
src/Text/Pandoc/Parsing.hs+7−2
@@ -79,6 +79,7 @@ import Network.URI ( parseURI, URI (..), isAllowedInURI )
7979import Control.Monad ( join, liftM )
8080import Text.Pandoc.Shared
8181import qualified Data.Map as M
82+import Text.TeXMath.Macros (Macro)
8283
8384-- | Like >>, but returns the operation on the left.
8485-- (Suggested by Tillmann Rendel on Haskell-cafe list.)
@@ -602,7 +603,9 @@ data ParserState = ParserState
602603 stateIndentedCodeClasses :: [String], -- ^ Classes to use for indented code blocks
603604 stateNextExample :: Int, -- ^ Number of next example
604605 stateExamples :: M.Map String Int, -- ^ Map from example labels to numbers
605− stateHasChapters :: Bool -- ^ True if \chapter encountered
606+ stateHasChapters :: Bool, -- ^ True if \chapter encountered
607+ stateApplyMacros :: Bool, -- ^ Apply LaTeX macros?
608+ stateMacros :: [Macro] -- ^ List of macros defined so far
606609 }
607610 deriving Show
608611
@@ -630,7 +633,9 @@ defaultParserState =
630633 stateIndentedCodeClasses = [],
631634 stateNextExample = 1,
632635 stateExamples = M.empty,
633− stateHasChapters = False }
636+ stateHasChapters = False,
637+ stateApplyMacros = True,
638+ stateMacros = []}
634639
635640data HeaderType
636641 = SingleHeader Char -- ^ Single line of characters underneath
src/Text/Pandoc/Readers/Markdown.hs+28−3
@@ -46,7 +46,8 @@ import Text.Pandoc.Readers.HTML ( rawHtmlBlock, anyHtmlBlockTag,
4646 htmlBlockElement, htmlComment, unsanitaryURI )
4747import Text.Pandoc.CharacterReferences ( decodeCharacterReferences )
4848import Text.ParserCombinators.Parsec
49−import Control.Monad (when, liftM, unless)
49+import Control.Monad (when, liftM, unless, guard)
50+import Text.TeXMath.Macros (applyMacros, Macro, pMacroDefinition)
5051
5152-- | Read markdown from an input string and return a Pandoc document.
5253readMarkdown :: ParserState -- ^ Parser state, including options for parser
@@ -284,6 +285,7 @@ block = do
284285 , plain
285286 , nullBlock ]
286287 else [ codeBlockDelimited
288+ , macro
287289 , header
288290 , table
289291 , codeBlockIndented
@@ -867,6 +869,29 @@ table = multilineTable False <|> simpleTable True <|>
867869 simpleTable False <|> multilineTable True <|>
868870 gridTable False <|> gridTable True <?> "table"
869871
872+--
873+-- Macros
874+--
875+
876+-- | Parse a \newcommand or \renewcommand macro definition.
877+macro :: GenParser Char ParserState Block
878+macro = getState >>= guard . stateApplyMacros >>
879+ pMacroDefinition >>= addMacro >> return Null
880+
881+-- | Add a macro to the list of macros in state.
882+addMacro :: Macro -> GenParser Char ParserState ()
883+addMacro m = do
884+ updateState $ \st -> st{ stateMacros = m : stateMacros st }
885+
886+-- | Apply current macros to string.
887+applyMacros' :: String -> GenParser Char ParserState String
888+applyMacros' target = do
889+ apply <- liftM stateApplyMacros getState
890+ if apply
891+ then do macros <- liftM stateMacros getState
892+ return $ applyMacros macros target
893+ else return target
894+
870895--
871896-- inline
872897--
@@ -969,8 +994,8 @@ mathChunk = do char '\\'
969994 <|> many1 (noneOf " \t\n\\$")
970995
971996math :: GenParser Char ParserState Inline
972−math = (mathDisplay >>= return . Math DisplayMath)
973− <|> (mathInline >>= return . Math InlineMath)
997+math = (mathDisplay >>= applyMacros' >>= return . Math DisplayMath)
998+ <|> (mathInline >>= applyMacros' >>= return . Math InlineMath)
974999
9751000mathDisplay :: GenParser Char ParserState String
9761001mathDisplay = try $ do
src/pandoc.hs+3−2
@@ -786,10 +786,11 @@ main = do
786786 stateCitations = map citeKey refs,
787787#endif
788788 stateSmart = smart || writerName' `elem`
789− ["latex", "context", "man"],
789+ ["latex", "context", "latex+lhs", "man"],
790790 stateColumns = columns,
791791 stateStrict = strict,
792− stateIndentedCodeClasses = codeBlockClasses }
792+ stateIndentedCodeClasses = codeBlockClasses,
793+ stateApplyMacros = writerName' `notElem` ["latex", "latex+lhs"] }
793794
794795 let writerOptions = WriterOptions { writerStandalone = standalone',
795796 writerTemplate = if null template