@@ -47,8 +47,7 @@ import Text.Pandoc.Logging
47import Text.Pandoc.Options
47import Text.Pandoc.Options
48import Text.Pandoc.Parsing hiding (blankline, many, mathDisplay, mathInline,
48import Text.Pandoc.Parsing hiding (blankline, many, mathDisplay, mathInline,
49 optional, space, spaces, withRaw, (<|>))
49 optional, space, spaces, withRaw, (<|>))
50−import Text.Pandoc.Readers.LaTeX.Types (ExpansionPoint (..), Macro (..),
50+import Text.Pandoc.Readers.LaTeX.Types (Tok (..), TokType (..))
51− ArgSpec (..), Tok (..), TokType (..))
52import Text.Pandoc.Readers.LaTeX.Parsing
51import Text.Pandoc.Readers.LaTeX.Parsing
53import Text.Pandoc.Readers.LaTeX.Accent (accentCommands)
52import Text.Pandoc.Readers.LaTeX.Accent (accentCommands)
54import Text.Pandoc.Readers.LaTeX.Citation (citationCommands, cites)
53import Text.Pandoc.Readers.LaTeX.Citation (citationCommands, cites)
@@ -58,6 +57,7 @@ import Text.Pandoc.Readers.LaTeX.Math (dollarsMath, inlineEnvironments,
58 newtheorem, theoremstyle, proof,
57 newtheorem, theoremstyle, proof,
59 theoremEnvironment)
58 theoremEnvironment)
60import Text.Pandoc.Readers.LaTeX.Table (tableEnvironments)
59import Text.Pandoc.Readers.LaTeX.Table (tableEnvironments)
60+import Text.Pandoc.Readers.LaTeX.Macro (macroDef)
61import Text.Pandoc.Readers.LaTeX.Lang (polyglossiaLangToBCP47,
61import Text.Pandoc.Readers.LaTeX.Lang (polyglossiaLangToBCP47,
62 babelLangToBCP47, setDefaultLanguage)
62 babelLangToBCP47, setDefaultLanguage)
63import Text.Pandoc.Readers.LaTeX.SIunitx (siunitxCommands)
63import Text.Pandoc.Readers.LaTeX.SIunitx (siunitxCommands)
@@ -1027,143 +1027,6 @@ authors = try $ do
1028 addMeta "author" (map trimInlines auths)
1028 addMeta "author" (map trimInlines auths)
1030−macroDef :: (PandocMonad m, Monoid a) => (Text -> a) -> LP m a
1031−macroDef constructor = do
1032− (_, s) <- withRaw (commandDef <|> environmentDef)
1033− (constructor (untokenize s) <$
1034− guardDisabled Ext_latex_macros)
1036− where commandDef = do
1037− (name, macro') <- newcommand <|> letmacro <|> defmacro
1038− guardDisabled Ext_latex_macros <|>
1039− updateState (\s -> s{ sMacros = M.insert name macro' (sMacros s) })
1040− environmentDef = do
1041− mbenv <- newenvironment
1043− Nothing -> return ()
1044− Just (name, macro1, macro2) ->
1045− guardDisabled Ext_latex_macros <|>
1046− do updateState $ \s -> s{ sMacros =
1047− M.insert name macro1 (sMacros s) }
1048− updateState $ \s -> s{ sMacros =
1049− M.insert ("end" <> name) macro2 (sMacros s) }
1050− -- @\newenvironment{envname}[n-args][default]{begin}{end}@
1051− -- is equivalent to
1052− -- @\newcommand{\envname}[n-args][default]{begin}@
1053− -- @\newcommand{\endenvname}@
1055−letmacro :: PandocMonad m => LP m (Text, Macro)
1058− (name, contents) <- withVerbatimMode $ do
1059− Tok _ (CtrlSeq name) _ <- anyControlSeq
1060− optional $ symbol '='
1062− -- we first parse in verbatim mode, and then expand macros,
1063− -- because we don't want \let\foo\bar to turn into
1064− -- \let\foo hello if we have previously \def\bar{hello}
1065− contents <- bracedOrToken
1066− return (name, contents)
1067− contents' <- doMacros' 0 contents
1068− return (name, Macro ExpandWhenDefined [] Nothing contents')
1070−defmacro :: PandocMonad m => LP m (Text, Macro)
1072− -- we use withVerbatimMode, because macros are to be expanded
1073− -- at point of use, not point of definition
1074− withVerbatimMode $ do
1076− Tok _ (CtrlSeq name) _ <- anyControlSeq
1077− argspecs <- many (argspecArg <|> argspecPattern)
1078− contents <- bracedOrToken
1079− return (name, Macro ExpandWhenUsed argspecs Nothing contents)
1081−argspecArg :: PandocMonad m => LP m ArgSpec
1083− Tok _ (Arg i) _ <- satisfyTok isArgTok
1086−argspecPattern :: PandocMonad m => LP m ArgSpec
1088− Pattern <$> many1 (satisfyTok (\(Tok _ toktype' txt) ->
1089− (toktype' == Symbol || toktype' == Word) &&
1090− (txt /= "{" && txt /= "\\" && txt /= "}")))
1092−newcommand :: PandocMonad m => LP m (Text, Macro)
1095− Tok _ (CtrlSeq mtype) _ <- controlSeq "newcommand" <|>
1096− controlSeq "renewcommand" <|>
1097− controlSeq "providecommand" <|>
1098− controlSeq "DeclareMathOperator" <|>
1099− controlSeq "DeclareRobustCommand"
1100− withVerbatimMode $ do
1101− Tok _ (CtrlSeq name) txt <- do
1102− optional (symbol '*')
1104− (symbol '{' *> spaces *> anyControlSeq <* spaces <* symbol '}')
1106− numargs <- option 0 $ try bracketedNum
1107− let argspecs = map ArgNum [1..numargs]
1109− optarg <- option Nothing $ Just <$> try bracketedToks
1111− contents' <- bracedOrToken
1114− "DeclareMathOperator" ->
1115− Tok pos (CtrlSeq "mathop") "\\mathop"
1116− : Tok pos Symbol "{"
1117− : Tok pos (CtrlSeq "mathrm") "\\mathrm"
1118− : Tok pos Symbol "{"
1120− [ Tok pos Symbol "}", Tok pos Symbol "}" ])
1122− macros <- sMacros <$> getState
1123− case M.lookup name macros of
1125− | mtype == "newcommand" -> do
1126− report $ MacroAlreadyDefined txt pos
1127− return (name, macro)
1128− | mtype == "providecommand" -> return (name, macro)
1129− _ -> return (name, Macro ExpandWhenUsed argspecs optarg contents)
1131−newenvironment :: PandocMonad m => LP m (Maybe (Text, Macro, Macro))
1134− Tok _ (CtrlSeq mtype) _ <- controlSeq "newenvironment" <|>
1135− controlSeq "renewenvironment" <|>
1136− controlSeq "provideenvironment"
1137− withVerbatimMode $ do
1138− optional $ symbol '*'
1140− name <- untokenize <$> braced
1142− numargs <- option 0 $ try bracketedNum
1144− optarg <- option Nothing $ Just <$> try bracketedToks
1145− let argspecs = map (\i -> ArgNum i) [1..numargs]
1146− startcontents <- spaces >> bracedOrToken
1147− endcontents <- spaces >> bracedOrToken
1148− macros <- sMacros <$> getState
1149− case M.lookup name macros of
1151− | mtype == "newenvironment" -> do
1152− report $ MacroAlreadyDefined name pos
1154− | mtype == "provideenvironment" ->
1156− _ -> return $ Just (name,
1157− Macro ExpandWhenUsed argspecs optarg startcontents,
1158− Macro ExpandWhenUsed [] Nothing endcontents)
1160−bracketedNum :: PandocMonad m => LP m Int
1162− ds <- untokenize <$> bracketedToks
1163− case safeRead ds of
1167looseItem :: PandocMonad m => LP m Blocks
1030looseItem :: PandocMonad m => LP m Blocks
1168looseItem = do
1031looseItem = do
1169 inListItem <- sInListItem <$> getState
1032 inListItem <- sInListItem <$> getState