{-# LANGUAGE OverloadedStrings #-}
module Text.Pandoc.Readers.LaTeX.Macro
( macroDef
)
where
import Text.Pandoc.Extensions (Extension(..))
import Text.Pandoc.Logging (LogMessage(MacroAlreadyDefined))
import Text.Pandoc.Readers.LaTeX.Parsing
import Text.Pandoc.TeX
import Text.Pandoc.Class
import Text.Pandoc.Shared (safeRead)
import Text.Pandoc.Parsing hiding (blankline, mathDisplay, mathInline,
optional, space, spaces, withRaw, (<|>))
import Control.Applicative ((<|>), optional)
import Control.Monad (guard)
import Data.Char (chr, isLetter, ord)
import qualified Data.Map as M
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.List.NonEmpty as NonEmpty
import Data.List.NonEmpty (NonEmpty(..))
macroDef :: (PandocMonad m, Monoid a) => (Text -> a) -> LP m a
macroDef :: forall (m :: * -> *) a.
(PandocMonad m, Monoid a) =>
(Text -> a) -> LP m a
macroDef Text -> a
constructor = do
Tok _ (CtrlSeq name) _ <- LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
peekTok
guard $ name `Set.member` macroDefCommands
(_, s) <- withRaw (commandDef <|> environmentDef)
(constructor (untokenize s) <$
guardDisabled Ext_latex_macros)
<|> return mempty
where commandDef :: ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ()
commandDef = do
nameMacroPairs <- LP m [(Text, Macro)]
forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
newcommand LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m [(Text, Macro)]
forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
newDocumentCommand LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
LP m [(Text, Macro)]
forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
newDocumentEnvironment LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m [(Text, Macro)]
forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
commandCopy LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m [(Text, Macro)]
forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
environmentCopy LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *).
PandocMonad m =>
LP m [(Text, Macro)] -> LP m [(Text, Macro)]
checkGlobal (LP m [(Text, Macro)]
forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
letmacro LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m [(Text, Macro)]
forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
edefmacro LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m [(Text, Macro)]
forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
defmacro LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m [(Text, Macro)]
forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
newif)
guardDisabled Ext_latex_macros <|>
mapM_ insertMacro nameMacroPairs
environmentDef :: ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ()
environmentDef = do
mbenv <- LP m (Maybe (Text, Macro, Macro))
forall (m :: * -> *).
PandocMonad m =>
LP m (Maybe (Text, Macro, Macro))
newenvironment
case mbenv of
Maybe (Text, Macro, Macro)
Nothing -> () -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ()
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just (Text
name, Macro
macro1, Macro
macro2) ->
Extension
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ()
forall s (m :: * -> *) a st.
(Stream s m a, HasReaderOptions st) =>
Extension -> ParsecT s st m ()
guardDisabled Extension
Ext_latex_macros ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ()
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ()
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ()
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
do (Text, Macro)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ()
forall (m :: * -> *). PandocMonad m => (Text, Macro) -> LP m ()
insertMacro (Text
name, Macro
macro1)
(Text, Macro)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ()
forall (m :: * -> *). PandocMonad m => (Text, Macro) -> LP m ()
insertMacro (Text
"end" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name, Macro
macro2)
macroDefCommands :: Set.Set Text
macroDefCommands :: Set Text
macroDefCommands = [Text] -> Set Text
forall a. Ord a => [a] -> Set a
Set.fromList
[ Text
"global"
, Text
"let", Text
"edef", Text
"xdef", Text
"def", Text
"gdef", Text
"newif"
, Text
"newcommand", Text
"renewcommand", Text
"providecommand"
, Text
"DeclareMathOperator", Text
"DeclareRobustCommand"
, Text
"NewDocumentCommand", Text
"RenewDocumentCommand"
, Text
"ProvideDocumentCommand", Text
"DeclareDocumentCommand"
, Text
"NewExpandableDocumentCommand", Text
"RenewExpandableDocumentCommand"
, Text
"ProvideExpandableDocumentCommand", Text
"DeclareExpandableDocumentCommand"
, Text
"NewDocumentEnvironment", Text
"RenewDocumentEnvironment"
, Text
"ProvideDocumentEnvironment", Text
"DeclareDocumentEnvironment"
, Text
"NewCommandCopy", Text
"RenewCommandCopy", Text
"DeclareCommandCopy"
, Text
"NewEnvironmentCopy", Text
"RenewEnvironmentCopy", Text
"DeclareEnvironmentCopy"
, Text
"newenvironment", Text
"renewenvironment", Text
"provideenvironment"
]
insertMacro :: PandocMonad m => (Text, Macro) -> LP m ()
insertMacro :: forall (m :: * -> *). PandocMonad m => (Text, Macro) -> LP m ()
insertMacro (Text
name, macro' :: Macro
macro'@(Macro MacroScope
GlobalScope ExpansionPoint
_ [ArgSpec]
_ Maybe [Tok]
_ [Tok]
_)) =
(LaTeXState -> LaTeXState)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ())
-> (LaTeXState -> LaTeXState)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
s ->
LaTeXState
s{ sMacros = NonEmpty.map (M.insert name macro') (sMacros s) }
insertMacro (Text
name, macro' :: Macro
macro'@(Macro MacroScope
GroupScope ExpansionPoint
_ [ArgSpec]
_ Maybe [Tok]
_ [Tok]
_)) =
(LaTeXState -> LaTeXState)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ())
-> (LaTeXState -> LaTeXState)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
s ->
LaTeXState
s{ sMacros = M.insert name macro' (NonEmpty.head (sMacros s)) :|
NonEmpty.tail (sMacros s) }
lookupMacro :: PandocMonad m => Text -> LP m Macro
lookupMacro :: forall (m :: * -> *). PandocMonad m => Text -> LP m Macro
lookupMacro Text
name = do
macros :| _ <- LaTeXState -> NonEmpty (Map Text Macro)
sMacros (LaTeXState -> NonEmpty (Map Text Macro))
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) LaTeXState
-> ParsecT
TokStream
LaTeXState
(ReaderT (LaTeXEnv m) m)
(NonEmpty (Map Text Macro))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
case M.lookup name macros of
Just Macro
m -> Macro
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Macro
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (m :: * -> *) a. Monad m => a -> m a
return Macro
m
Maybe Macro
Nothing -> String
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Macro
forall a.
String -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Macro not found"
letmacro :: PandocMonad m => LP m [(Text, Macro)]
letmacro :: forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
letmacro = do
Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"let"
LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
withVerbatimMode (LP m [(Text, Macro)] -> LP m [(Text, Macro)])
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a b. (a -> b) -> a -> b
$ do
Tok _ (CtrlSeq name) _ <- LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq
optional $ symbol '='
spaces
target <- anyControlSeq <|> singleChar
case target of
(Tok SourcePos
_ (CtrlSeq Text
name') Text
_) ->
(do m <- Text -> LP m Macro
forall (m :: * -> *). PandocMonad m => Text -> LP m Macro
lookupMacro Text
name'
pure [(name, m)])
LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [(Text, Macro)] -> LP m [(Text, Macro)]
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [(Text
name,
MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenDefined [] Maybe [Tok]
forall a. Maybe a
Nothing [Tok
target])]
Tok
_ -> [(Text, Macro)] -> LP m [(Text, Macro)]
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [(Text
name, MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenDefined [] Maybe [Tok]
forall a. Maybe a
Nothing [Tok
target])]
checkGlobal :: PandocMonad m => LP m [(Text, Macro)] -> LP m [(Text, Macro)]
checkGlobal :: forall (m :: * -> *).
PandocMonad m =>
LP m [(Text, Macro)] -> LP m [(Text, Macro)]
checkGlobal LP m [(Text, Macro)]
p =
(Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"global" LP m Tok -> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a b.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*>
(((Text, Macro) -> (Text, Macro))
-> [(Text, Macro)] -> [(Text, Macro)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Text
n, Macro MacroScope
_ ExpansionPoint
expand [ArgSpec]
arg Maybe [Tok]
optarg [Tok]
contents) ->
(Text
n, MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GlobalScope ExpansionPoint
expand [ArgSpec]
arg Maybe [Tok]
optarg [Tok]
contents)) ([(Text, Macro)] -> [(Text, Macro)])
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m [(Text, Macro)]
p))
LP m [(Text, Macro)]
-> LP m [(Text, Macro)] -> LP m [(Text, Macro)]
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m [(Text, Macro)]
p
edefmacro :: PandocMonad m => LP m [(Text, Macro)]
edefmacro :: forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
edefmacro = do
scope <- (MacroScope
GroupScope MacroScope
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Tok
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) MacroScope
forall a b.
a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"edef")
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) MacroScope
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) MacroScope
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) MacroScope
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (MacroScope
GlobalScope MacroScope
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Tok
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) MacroScope
forall a b.
a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"xdef")
(name, contents) <- withVerbatimMode $ do
Tok _ (CtrlSeq name) _ <- anyControlSeq
contents <- bracedOrToken
return (name, contents)
contents' <- parseFromToks (many anyTok) contents
return [(name, Macro scope ExpandWhenDefined [] Nothing contents')]
defmacro :: PandocMonad m => LP m [(Text, Macro)]
defmacro :: forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
defmacro = do
scope <- (MacroScope
GroupScope MacroScope
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Tok
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) MacroScope
forall a b.
a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"def")
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) MacroScope
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) MacroScope
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) MacroScope
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (MacroScope
GlobalScope MacroScope
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Tok
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) MacroScope
forall a b.
a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"gdef")
withVerbatimMode $ do
Tok _ (CtrlSeq name) _ <- anyControlSeq
argspecs <- many (argspecArg <|> argspecPattern)
contents <- bracedOrToken
return [(name, Macro scope ExpandWhenUsed argspecs Nothing contents)]
newif :: PandocMonad m => LP m [(Text, Macro)]
newif :: forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
newif = ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)])
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
forall a b. (a -> b) -> a -> b
$ do
Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"newif"
ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
withVerbatimMode (ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)])
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
forall a b. (a -> b) -> a -> b
$ do
Tok pos (CtrlSeq name) _ <- LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq
guard $ "if" `T.isPrefixOf` name
let base = Int -> Text -> Text
T.drop Int
2 Text
name
return [ (name, Macro GroupScope ExpandWhenUsed [] Nothing
[Tok pos (CtrlSeq "iffalse") "\\iffalse"])
, (base <> "true",
Macro GroupScope ExpandWhenUsed [] Nothing
[ Tok pos (CtrlSeq "def") "\\def"
, Tok pos (CtrlSeq name) ("\\" <> name)
, Tok pos Symbol "{"
, Tok pos (CtrlSeq "iftrue") "\\iftrue"
, Tok pos Symbol "}"
])
, (base <> "false",
Macro GroupScope ExpandWhenUsed [] Nothing
[ Tok pos (CtrlSeq "def") "\\def"
, Tok pos (CtrlSeq name) ("\\" <> name)
, Tok pos Symbol "{"
, Tok pos (CtrlSeq "iffalse") "\\iffalse"
, Tok pos Symbol "}"
])
]
argspecArg :: PandocMonad m => LP m ArgSpec
argspecArg :: forall (m :: * -> *). PandocMonad m => LP m ArgSpec
argspecArg = do
Tok _ (Arg i) _ <- (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isArgTok
return $ ArgNum i
argspecPattern :: PandocMonad m => LP m ArgSpec
argspecPattern :: forall (m :: * -> *). PandocMonad m => LP m ArgSpec
argspecPattern =
[Tok] -> ArgSpec
Pattern ([Tok] -> ArgSpec)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ArgSpec
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Tok
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many1 ((Tok -> Bool)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok (\(Tok SourcePos
_ TokType
toktype' Text
txt) ->
(TokType
toktype' TokType -> TokType -> Bool
forall a. Eq a => a -> a -> Bool
== TokType
Symbol Bool -> Bool -> Bool
|| TokType
toktype' TokType -> TokType -> Bool
forall a. Eq a => a -> a -> Bool
== TokType
Word) Bool -> Bool -> Bool
&&
(Text
txt Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
"{" Bool -> Bool -> Bool
&& Text
txt Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
"\\" Bool -> Bool -> Bool
&& Text
txt Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
"}")))
newcommand :: PandocMonad m => LP m [(Text, Macro)]
newcommand :: forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
newcommand = do
Tok pos (CtrlSeq mtype) _ <- Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"newcommand" LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"renewcommand" LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"providecommand" LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"DeclareMathOperator" LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"DeclareRobustCommand"
withVerbatimMode $ do
Tok _ (CtrlSeq name) txt <- do
optional (symbol '*')
anyControlSeq <|>
(symbol '{' *> spaces *> anyControlSeq <* spaces <* symbol '}')
spaces
numargs <- option 0 $ try bracketedNum
let argspecs = (Int -> ArgSpec) -> [Int] -> [ArgSpec]
forall a b. (a -> b) -> [a] -> [b]
map Int -> ArgSpec
ArgNum [Int
1..Int
numargs]
spaces
optarg <- option Nothing $ Just <$> try bracketedToks
spaces
contents' <- bracedOrToken
let contents =
case Text
mtype of
Text
"DeclareMathOperator" ->
SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"mathop") Text
"\\mathop"
Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"{"
Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"mathrm") Text
"\\mathrm"
Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"{"
Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: ([Tok]
contents' [Tok] -> [Tok] -> [Tok]
forall a. [a] -> [a] -> [a]
++
[ SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"}", SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"}" ])
Text
_ -> [Tok]
contents'
let macro = MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenUsed [ArgSpec]
argspecs Maybe [Tok]
optarg [Tok]
contents
(do lookupMacro name
case mtype of
Text
"providecommand" -> [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (m :: * -> *) a. Monad m => a -> m a
return []
Text
"renewcommand" -> [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (m :: * -> *) a. Monad m => a -> m a
return [(Text
name, Macro
macro)]
Text
_ -> [] [(Text, Macro)]
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ()
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
forall a b.
a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ LogMessage
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ()
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report (Text -> SourcePos -> LogMessage
MacroAlreadyDefined Text
txt SourcePos
pos))
<|> pure [(name, macro)]
newDocumentCommand :: PandocMonad m => LP m [(Text, Macro)]
newDocumentCommand :: forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
newDocumentCommand = ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)])
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
forall a b. (a -> b) -> a -> b
$ do
Tok pos (CtrlSeq mtype) _ <-
Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"NewDocumentCommand"
LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"RenewDocumentCommand"
LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"ProvideDocumentCommand"
LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"DeclareDocumentCommand"
LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"NewExpandableDocumentCommand"
LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"RenewExpandableDocumentCommand"
LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"ProvideExpandableDocumentCommand"
LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"DeclareExpandableDocumentCommand"
withVerbatimMode $ do
Tok _ (CtrlSeq name) txt <- do
spaces
anyControlSeq <|>
(symbol '{' *> spaces *> anyControlSeq <* spaces <* symbol '}')
spaces
(argspecs, _) <- xparseArgSpecs False
spaces
contents <- bracedOrToken
let macro = MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenUsed [ArgSpec]
argspecs Maybe [Tok]
forall a. Maybe a
Nothing [Tok]
contents
(do lookupMacro name
if "Provide" `T.isPrefixOf` mtype
then return []
else if "New" `T.isPrefixOf` mtype
then [] <$ report (MacroAlreadyDefined txt pos)
else return [(name, macro)])
<|> pure [(name, macro)]
newDocumentEnvironment :: PandocMonad m => LP m [(Text, Macro)]
newDocumentEnvironment :: forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
newDocumentEnvironment = ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)])
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
forall a b. (a -> b) -> a -> b
$ do
Tok pos (CtrlSeq mtype) _ <-
Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"NewDocumentEnvironment"
LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"RenewDocumentEnvironment"
LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"ProvideDocumentEnvironment"
LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"DeclareDocumentEnvironment"
withVerbatimMode $ do
spaces
name <- T.strip . untokenize <$> braced
spaces
(argspecs, usesBody) <- xparseArgSpecs True
startcontents <- spaces >> bracedOrToken
endcontents <- spaces >> bracedOrToken
let bg = SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"bgroup") Text
"\\bgroup "
let eg = SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"egroup") Text
"\\egroup "
let helperName = Text
"pandocxparseenvend" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
letterize Text
name
let helper = SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
helperName) (Text
"\\" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
helperName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" ")
let defHelper = SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"def") Text
"\\def"
Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: Tok
helper
Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"{"
Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: [Tok]
endcontents [Tok] -> [Tok] -> [Tok]
forall a. [a] -> [a] -> [a]
++ [SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"}"]
let result
| Bool
usesBody =
[ (Text
name,
MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenUsed
([ArgSpec]
argspecs [ArgSpec] -> [ArgSpec] -> [ArgSpec]
forall a. [a] -> [a] -> [a]
++ [[Tok] -> ArgSpec
Pattern (SourcePos -> Text -> [Tok]
tokenize SourcePos
pos (Text
"\\end{" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"}"))])
Maybe [Tok]
forall a. Maybe a
Nothing
(Tok
bg Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: [Tok]
defHelper [Tok] -> [Tok] -> [Tok]
forall a. [a] -> [a] -> [a]
++ [Tok]
startcontents [Tok] -> [Tok] -> [Tok]
forall a. [a] -> [a] -> [a]
++ [Tok
helper, Tok
eg])) ]
| Bool
otherwise =
[ (Text
name,
MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenUsed [ArgSpec]
argspecs Maybe [Tok]
forall a. Maybe a
Nothing
(Tok
bg Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: [Tok]
defHelper [Tok] -> [Tok] -> [Tok]
forall a. [a] -> [a] -> [a]
++ [Tok]
startcontents))
, (Text
"end" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name,
MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenUsed [] Maybe [Tok]
forall a. Maybe a
Nothing [Tok
helper, Tok
eg]) ]
(do lookupMacro name
if "Provide" `T.isPrefixOf` mtype
then return []
else if "New" `T.isPrefixOf` mtype
then [] <$ report (MacroAlreadyDefined name pos)
else return result)
<|> pure result
commandCopy :: PandocMonad m => LP m [(Text, Macro)]
commandCopy :: forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
commandCopy = ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)])
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
forall a b. (a -> b) -> a -> b
$ do
Tok pos (CtrlSeq mtype) _ <-
Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"NewCommandCopy"
LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"RenewCommandCopy"
LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"DeclareCommandCopy"
withVerbatimMode $ do
Tok _ (CtrlSeq name) txt <- do
spaces
anyControlSeq <|>
(symbol '{' *> spaces *> anyControlSeq <* spaces <* symbol '}')
spaces
target@(Tok _ (CtrlSeq targetName) _) <- anyControlSeq
result <- (do m <- lookupMacro targetName
pure [(name, m)])
<|> pure [(name, Macro GroupScope ExpandWhenDefined [] Nothing
[target])]
(do lookupMacro name
if "New" `T.isPrefixOf` mtype
then [] <$ report (MacroAlreadyDefined txt pos)
else return result)
<|> pure result
environmentCopy :: PandocMonad m => LP m [(Text, Macro)]
environmentCopy :: forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
environmentCopy = ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)])
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [(Text, Macro)]
forall a b. (a -> b) -> a -> b
$ do
Tok pos (CtrlSeq mtype) _ <-
Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"NewEnvironmentCopy"
LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"RenewEnvironmentCopy"
LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"DeclareEnvironmentCopy"
withVerbatimMode $ do
spaces
name <- untokenize <$> braced
spaces
target <- untokenize <$> braced
let copyOne a
to Text
from Text
fallback =
(do m <- Text -> LP m Macro
forall (m :: * -> *). PandocMonad m => Text -> LP m Macro
lookupMacro Text
from
pure (to, m))
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (a, Macro)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (a, Macro)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (a, Macro)
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (a, Macro)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (a, Macro)
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
to, MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenUsed [] Maybe [Tok]
forall a. Maybe a
Nothing
(SourcePos -> Text -> [Tok]
tokenize SourcePos
pos Text
fallback))
beginmacro <- copyOne name target ("\\begin{" <> target <> "}")
endmacro <- copyOne ("end" <> name) ("end" <> target)
("\\end{" <> target <> "}")
let result = [(Text, Macro)
beginmacro, (Text, Macro)
endmacro]
(do lookupMacro name
if "New" `T.isPrefixOf` mtype
then [] <$ report (MacroAlreadyDefined name pos)
else return result)
<|> pure result
letterize :: Text -> Text
letterize :: Text -> Text
letterize = (Char -> Text) -> Text -> Text
T.concatMap Char -> Text
go
where go :: Char -> Text
go Char
c | Char -> Bool
isLetter Char
c = Char -> Text
T.singleton Char
c
| Bool
otherwise = Text
"x" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (Char -> Char) -> Text -> Text
T.map Char -> Char
toLetter (String -> Text
T.pack (Int -> String
forall a. Show a => a -> String
show (Char -> Int
ord Char
c)))
toLetter :: Char -> Char
toLetter Char
d = Int -> Char
chr (Char -> Int
ord Char
d Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
49)
xparseArgSpecs :: PandocMonad m => Bool -> LP m ([ArgSpec], Bool)
xparseArgSpecs :: forall (m :: * -> *).
PandocMonad m =>
Bool -> LP m ([ArgSpec], Bool)
xparseArgSpecs Bool
allowBody = Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{' LP m Tok
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ([ArgSpec], Bool)
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ([ArgSpec], Bool)
forall a b.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ([ArgSpec], Bool)
forall {m :: * -> *}.
PandocMonad m =>
Int
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ([ArgSpec], Bool)
go Int
1
where
go :: Int
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ([ArgSpec], Bool)
go Int
n = do
LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
(([], Bool
False) ([ArgSpec], Bool)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Tok
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ([ArgSpec], Bool)
forall a b.
a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Char -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'}') ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ([ArgSpec], Bool)
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ([ArgSpec], Bool)
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) ([ArgSpec], Bool)
forall a.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
(do (spec, isBody) <- Bool -> Int -> LP m (ArgSpec, Bool)
forall (m :: * -> *).
PandocMonad m =>
Bool -> Int -> LP m (ArgSpec, Bool)
xparseArgSpec Bool
allowBody Int
n
if isBody
then ([spec], True) <$ (spaces <* symbol '}')
else do (rest, usesBody) <- go (n + numslots spec)
return (spec : rest, usesBody))
numslots :: ArgSpec -> Int
numslots (EmbellishArg [(Tok, Maybe [Tok])]
embs) = [(Tok, Maybe [Tok])] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Tok, Maybe [Tok])]
embs
numslots (ProcessedArg [[Tok]]
_ ArgSpec
spec) = ArgSpec -> Int
numslots ArgSpec
spec
numslots ArgSpec
_ = Int
1
xparseArgSpec :: PandocMonad m => Bool -> Int -> LP m (ArgSpec, Bool)
xparseArgSpec :: forall (m :: * -> *).
PandocMonad m =>
Bool -> Int -> LP m (ArgSpec, Bool)
xparseArgSpec Bool
allowBody Int
n = Bool
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
go Bool
True
where
go :: Bool
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
go Bool
noBang = do
Tok pos _ c <- LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
singleChar
let plain a
spec = (a, Bool) -> f (a, Bool)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
spec, Bool
False)
case c of
Text
"+" -> LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m ()
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall a b.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Bool
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
go Bool
noBang
Text
"!" -> LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m ()
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall a b.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Bool
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
go Bool
False
Text
"=" -> LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m ()
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
forall a b.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
-> LP m () -> LP m ()
forall a b.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m ()
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall a b.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Bool
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
go Bool
noBang
Text
">" -> do proc <- LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m ()
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
forall a b.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
(spec, isBody) <- spaces *> go noBang
if isBody
then pure (spec, isBody)
else case spec of
ProcessedArg [[Tok]]
procs ArgSpec
inner ->
(ArgSpec, Bool)
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([[Tok]] -> ArgSpec -> ArgSpec
ProcessedArg ([Tok]
proc [Tok] -> [[Tok]] -> [[Tok]]
forall a. a -> [a] -> [a]
: [[Tok]]
procs) ArgSpec
inner, Bool
False)
ArgSpec
_ -> (ArgSpec, Bool)
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([[Tok]] -> ArgSpec -> ArgSpec
ProcessedArg [[Tok]
proc] ArgSpec
spec, Bool
False)
Text
"m" -> ArgSpec
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall {f :: * -> *} {a}. Applicative f => a -> f (a, Bool)
plain (ArgSpec
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool))
-> ArgSpec
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall a b. (a -> b) -> a -> b
$ Int -> ArgSpec
ArgNum Int
n
Text
"o" -> ArgSpec
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall {f :: * -> *} {a}. Applicative f => a -> f (a, Bool)
plain (ArgSpec
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool))
-> ArgSpec
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall a b. (a -> b) -> a -> b
$ Bool -> Tok -> Tok -> Maybe [Tok] -> ArgSpec
DelimArg Bool
noBang (SourcePos -> Tok
lbTok SourcePos
pos) (SourcePos -> Tok
rbTok SourcePos
pos) Maybe [Tok]
forall a. Maybe a
Nothing
Text
"O" -> do dflt <- LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m ()
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
forall a b.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
plain $ DelimArg noBang (lbTok pos) (rbTok pos) (Just dflt)
Text
"s" -> ArgSpec
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall {f :: * -> *} {a}. Applicative f => a -> f (a, Bool)
plain (ArgSpec
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool))
-> ArgSpec
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall a b. (a -> b) -> a -> b
$ Bool -> Tok -> ArgSpec
BoolArg Bool
noBang (SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"*")
Text
"t" -> LP m Tok
specTok LP m Tok
-> (Tok
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool))
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall a b.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> (a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ArgSpec
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall {f :: * -> *} {a}. Applicative f => a -> f (a, Bool)
plain (ArgSpec
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool))
-> (Tok -> ArgSpec)
-> Tok
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Tok -> ArgSpec
BoolArg Bool
noBang
Text
"d" -> do o <- LP m Tok
specTok
c' <- specTok
plain $ DelimArg noBang o c' Nothing
Text
"D" -> do o <- LP m Tok
specTok
c' <- specTok
dflt <- spaces *> braced
plain $ DelimArg noBang o c' (Just dflt)
Text
"r" -> do o <- LP m Tok
specTok
c' <- specTok
plain $ DelimArg noBang o c' Nothing
Text
"R" -> do o <- LP m Tok
specTok
c' <- specTok
dflt <- spaces *> braced
plain $ DelimArg noBang o c' (Just dflt)
Text
"v" -> ArgSpec
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall {f :: * -> *} {a}. Applicative f => a -> f (a, Bool)
plain ArgSpec
VerbArg
Text
"e" -> do embtoks <- LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m ()
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
forall a b.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
plain $ EmbellishArg [(t, Nothing) | t <- embTokens embtoks]
Text
"E" -> do embtoks <- LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m ()
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
forall a b.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
spaces
defaults <- symbol '{' *> many (try (spaces *> braced))
<* spaces <* symbol '}'
plain $ EmbellishArg $
zip (embTokens embtoks) (map Just defaults ++ repeat Nothing)
Text
"b" | Bool
allowBody -> (ArgSpec, Bool)
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> Int -> ArgSpec
BodyArg Bool
noBang Int
n, Bool
True)
Text
"c" | Bool
allowBody -> (ArgSpec, Bool)
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> Int -> ArgSpec
VerbBodyArg Bool
noBang Int
n, Bool
True)
Text
_ -> String
-> ParsecT
TokStream LaTeXState (ReaderT (LaTeXEnv m) m) (ArgSpec, Bool)
forall a.
String -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unsupported xparse argument specifier"
lbTok :: SourcePos -> Tok
lbTok SourcePos
pos = SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"["
rbTok :: SourcePos -> Tok
rbTok SourcePos
pos = SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"]"
specTok :: LP m Tok
specTok = LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m () -> LP m Tok -> LP m Tok
forall a b.
ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
singleChar
embTokens :: [Tok] -> [Tok]
embTokens = (Tok -> Bool) -> [Tok] -> [Tok]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Tok -> Bool) -> Tok -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TokType] -> Tok -> Bool
tokTypeIn [TokType
Spaces, TokType
Newline, TokType
Comment])
newenvironment :: PandocMonad m => LP m (Maybe (Text, Macro, Macro))
newenvironment :: forall (m :: * -> *).
PandocMonad m =>
LP m (Maybe (Text, Macro, Macro))
newenvironment = do
pos <- ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
Tok _ (CtrlSeq mtype) _ <- controlSeq "newenvironment" <|>
controlSeq "renewenvironment" <|>
controlSeq "provideenvironment"
withVerbatimMode $ do
optional $ symbol '*'
spaces
name <- untokenize <$> braced
spaces
numargs <- option 0 $ try bracketedNum
spaces
optarg <- option Nothing $ Just <$> try bracketedToks
let argspecs = (Int -> ArgSpec) -> [Int] -> [ArgSpec]
forall a b. (a -> b) -> [a] -> [b]
map (\Int
i -> Int -> ArgSpec
ArgNum Int
i) [Int
1..Int
numargs]
startcontents <- spaces >> bracedOrToken
endcontents <- spaces >> bracedOrToken
let bg = SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"bgroup") Text
"\\bgroup "
let eg = SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"egroup") Text
"\\egroup "
let result = (Text
name,
MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenUsed [ArgSpec]
argspecs Maybe [Tok]
optarg
(Tok
bgTok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
:[Tok]
startcontents),
MacroScope
-> ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro MacroScope
GroupScope ExpansionPoint
ExpandWhenUsed [] Maybe [Tok]
forall a. Maybe a
Nothing
([Tok]
endcontents [Tok] -> [Tok] -> [Tok]
forall a. [a] -> [a] -> [a]
++ [Tok
eg]))
(do lookupMacro name
case mtype of
Text
"provideenvironment" -> Maybe (Text, Macro, Macro)
-> ParsecT
TokStream
LaTeXState
(ReaderT (LaTeXEnv m) m)
(Maybe (Text, Macro, Macro))
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Text, Macro, Macro)
forall a. Maybe a
Nothing
Text
"renewenvironment" -> Maybe (Text, Macro, Macro)
-> ParsecT
TokStream
LaTeXState
(ReaderT (LaTeXEnv m) m)
(Maybe (Text, Macro, Macro))
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Text, Macro, Macro) -> Maybe (Text, Macro, Macro)
forall a. a -> Maybe a
Just (Text, Macro, Macro)
result)
Text
_ -> do
LogMessage -> LP m ()
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report (LogMessage -> LP m ()) -> LogMessage -> LP m ()
forall a b. (a -> b) -> a -> b
$ Text -> SourcePos -> LogMessage
MacroAlreadyDefined Text
name SourcePos
pos
Maybe (Text, Macro, Macro)
-> ParsecT
TokStream
LaTeXState
(ReaderT (LaTeXEnv m) m)
(Maybe (Text, Macro, Macro))
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Text, Macro, Macro)
forall a. Maybe a
Nothing)
<|> return (Just result)
bracketedNum :: PandocMonad m => LP m Int
bracketedNum :: forall (m :: * -> *). PandocMonad m => LP m Int
bracketedNum = do
ds <- [Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
-> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracketedToks
case safeRead ds of
Just Int
i -> Int -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Int
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i
Maybe Int
_ -> Int -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) Int
forall a.
a -> ParsecT TokStream LaTeXState (ReaderT (LaTeXEnv m) m) a
forall (m :: * -> *) a. Monad m => a -> m a
return Int
0