aboutsummaryrefslogtreecommitdiff
path: root/src/Text
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2017-08-07 16:06:19 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2017-08-07 16:06:19 -0700
commitc806ef1b150147ecaf5a4781e2ac1ce921559ca4 (patch)
treed86243da6349570c77681c3321e964da4318b5df /src/Text
parent9e6b9cdc5face62842cfee6a0eacefed82973239 (diff)
downloadpandoc-c806ef1b150147ecaf5a4781e2ac1ce921559ca4.tar.gz
LaTeX reader: Support simple `\def` macros.
Note that we still don't support macros with fancy parameter delimiters, like \def\foo#1..#2{...}
Diffstat (limited to 'src/Text')
-rw-r--r--src/Text/Pandoc/Readers/LaTeX.hs23
1 files changed, 21 insertions, 2 deletions
diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs
index 7004f2ba5..b9d4de935 100644
--- a/src/Text/Pandoc/Readers/LaTeX.hs
+++ b/src/Text/Pandoc/Readers/LaTeX.hs
@@ -1521,7 +1521,7 @@ macroDef :: PandocMonad m => LP m Blocks
macroDef = do
mempty <$ ((commandDef <|> environmentDef) <* doMacros 0)
where commandDef = do
- (name, macro') <- newcommand <|> letmacro
+ (name, macro') <- newcommand <|> letmacro <|> defmacro
guardDisabled Ext_latex_macros <|>
updateState (\s -> s{ sMacros = M.insert name macro' (sMacros s) })
environmentDef = do
@@ -1538,13 +1538,32 @@ macroDef = do
letmacro :: PandocMonad m => LP m (Text, Macro)
letmacro = do
- pos <- getPosition
controlSeq "let"
Tok _ (CtrlSeq name) _ <- anyControlSeq
optional $ symbol '='
+ spaces
contents <- braced <|> ((:[]) <$> anyControlSeq)
return (name, Macro ExpandWhenDefined 0 Nothing contents)
+defmacro :: PandocMonad m => LP m (Text, Macro)
+defmacro = try $ do
+ controlSeq "def"
+ Tok _ (CtrlSeq name) _ <- anyControlSeq
+ numargs <- option 0 $ argSeq 1
+ contents <- withVerbatimMode braced
+ return (name, Macro ExpandWhenUsed numargs Nothing contents)
+
+-- Note: we don't yet support fancy things like #1.#2
+argSeq :: PandocMonad m => Int -> LP m Int
+argSeq n = do
+ Tok _ (Arg i) _ <- satisfyTok isArgTok
+ guard $ i == n
+ argSeq (n+1) <|> return n
+
+isArgTok :: Tok -> Bool
+isArgTok (Tok _ (Arg _) _) = True
+isArgTok _ = False
+
newcommand :: PandocMonad m => LP m (Text, Macro)
newcommand = do
pos <- getPosition