{-# 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.Readers.LaTeX.Types
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 qualified Data.Map as M
import Data.Text (Text)
import qualified Data.Text as T

macroDef :: (PandocMonad m, Monoid a) => (Text -> a) -> LP m a
macroDef :: (Text -> a) -> LP m a
macroDef Text -> a
constructor = do
    (()
_, [Tok]
s) <- LP m () -> LP m ((), [Tok])
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
withRaw (LP m ()
commandDef LP m () -> LP m () -> LP m ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m ()
environmentDef)
    (Text -> a
constructor ([Tok] -> Text
untokenize [Tok]
s) a -> LP m () -> LP m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$
      Extension -> LP m ()
forall s (m :: * -> *) a st.
(Stream s m a, HasReaderOptions st) =>
Extension -> ParserT s st m ()
guardDisabled Extension
Ext_latex_macros)
     LP m a -> LP m a -> LP m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> a -> LP m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
forall a. Monoid a => a
mempty
  where commandDef :: LP m ()
commandDef = do
          [(Text, Macro)]
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 (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> 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 (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 (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m [(Text, Macro)]
forall (m :: * -> *). PandocMonad m => LP m [(Text, Macro)]
newif
          Extension -> LP m ()
forall s (m :: * -> *) a st.
(Stream s m a, HasReaderOptions st) =>
Extension -> ParserT s st m ()
guardDisabled Extension
Ext_latex_macros LP m () -> LP m () -> LP m ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
           ((Text, Macro) -> LP m ()) -> [(Text, Macro)] -> LP m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\(Text
name, Macro
macro') ->
                   (LaTeXState -> LaTeXState) -> LP m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState (\LaTeXState
s -> LaTeXState
s{ sMacros :: Map Text Macro
sMacros = Text -> Macro -> Map Text Macro -> Map Text Macro
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
name Macro
macro'
                                          (LaTeXState -> Map Text Macro
sMacros LaTeXState
s) })) [(Text, Macro)]
nameMacroPairs
        environmentDef :: LP m ()
environmentDef = do
          Maybe (Text, Macro, Macro)
mbenv <- LP m (Maybe (Text, Macro, Macro))
forall (m :: * -> *).
PandocMonad m =>
LP m (Maybe (Text, Macro, Macro))
newenvironment
          case Maybe (Text, Macro, Macro)
mbenv of
            Maybe (Text, Macro, Macro)
Nothing -> () -> LP m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            Just (Text
name, Macro
macro1, Macro
macro2) ->
              Extension -> LP m ()
forall s (m :: * -> *) a st.
(Stream s m a, HasReaderOptions st) =>
Extension -> ParserT s st m ()
guardDisabled Extension
Ext_latex_macros LP m () -> LP m () -> LP m ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                do (LaTeXState -> LaTeXState) -> LP m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> LP m ())
-> (LaTeXState -> LaTeXState) -> LP m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
s -> LaTeXState
s{ sMacros :: Map Text Macro
sMacros =
                    Text -> Macro -> Map Text Macro -> Map Text Macro
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
name Macro
macro1 (LaTeXState -> Map Text Macro
sMacros LaTeXState
s) }
                   (LaTeXState -> LaTeXState) -> LP m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> LP m ())
-> (LaTeXState -> LaTeXState) -> LP m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
s -> LaTeXState
s{ sMacros :: Map Text Macro
sMacros =
                    Text -> Macro -> Map Text Macro -> Map Text Macro
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert (Text
"end" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name) Macro
macro2 (LaTeXState -> Map Text Macro
sMacros LaTeXState
s) }
        -- @\newenvironment{envname}[n-args][default]{begin}{end}@
        -- is equivalent to
        -- @\newcommand{\envname}[n-args][default]{begin}@
        -- @\newcommand{\endenvname}@

letmacro :: PandocMonad m => LP m [(Text, Macro)]
letmacro :: LP m [(Text, Macro)]
letmacro = do
  Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"let"
  (Text
name, [Tok]
contents) <- LP m (Text, [Tok]) -> LP m (Text, [Tok])
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
withVerbatimMode (LP m (Text, [Tok]) -> LP m (Text, [Tok]))
-> LP m (Text, [Tok]) -> LP m (Text, [Tok])
forall a b. (a -> b) -> a -> b
$ do
    Tok SourcePos
_ (CtrlSeq Text
name) Text
_ <- LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq
    LP m Tok -> ParsecT [Tok] LaTeXState m (Maybe Tok)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (LP m Tok -> ParsecT [Tok] LaTeXState m (Maybe Tok))
-> LP m Tok -> ParsecT [Tok] LaTeXState m (Maybe Tok)
forall a b. (a -> b) -> a -> b
$ Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'='
    LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
    -- we first parse in verbatim mode, and then expand macros,
    -- because we don't want \let\foo\bar to turn into
    -- \let\foo hello if we have previously \def\bar{hello}
    [Tok]
contents <- LP m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracedOrToken
    (Text, [Tok]) -> LP m (Text, [Tok])
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
name, [Tok]
contents)
  [Tok]
contents' <- Int -> [Tok] -> LP m [Tok]
forall (m :: * -> *). PandocMonad m => Int -> [Tok] -> LP m [Tok]
doMacros' Int
0 [Tok]
contents
  [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. Monad m => a -> m a
return [(Text
name, ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro ExpansionPoint
ExpandWhenDefined [] Maybe [Tok]
forall a. Maybe a
Nothing [Tok]
contents')]

defmacro :: PandocMonad m => LP m [(Text, Macro)]
defmacro :: LP m [(Text, Macro)]
defmacro = do
  -- we use withVerbatimMode, because macros are to be expanded
  -- at point of use, not point of definition
  Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"def"
  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 SourcePos
_ (CtrlSeq Text
name) Text
_ <- LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq
    [ArgSpec]
argspecs <- ParsecT [Tok] LaTeXState m ArgSpec
-> ParsecT [Tok] LaTeXState m [ArgSpec]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (ParsecT [Tok] LaTeXState m ArgSpec
forall (m :: * -> *). PandocMonad m => LP m ArgSpec
argspecArg ParsecT [Tok] LaTeXState m ArgSpec
-> ParsecT [Tok] LaTeXState m ArgSpec
-> ParsecT [Tok] LaTeXState m ArgSpec
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT [Tok] LaTeXState m ArgSpec
forall (m :: * -> *). PandocMonad m => LP m ArgSpec
argspecPattern)
    [Tok]
contents <- LP m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracedOrToken
    [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. Monad m => a -> m a
return [(Text
name, ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro ExpansionPoint
ExpandWhenUsed [ArgSpec]
argspecs Maybe [Tok]
forall a. Maybe a
Nothing [Tok]
contents)]

-- \newif\iffoo' defines:
-- \iffoo to be \iffalse
-- \footrue to be a command that defines \iffoo to be \iftrue
-- \foofalse to be a command that defines \iffoo to be \iffalse
newif :: PandocMonad m => LP m [(Text, Macro)]
newif :: LP m [(Text, Macro)]
newif = do
  Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"newif"
  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 SourcePos
pos (CtrlSeq Text
name) Text
_ <- LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq
    -- \def\iffoo\iffalse
    -- \def\footrue{\def\iffoo\iftrue}
    -- \def\foofalse{\def\iffoo\iffalse}
    let base :: Text
base = Int -> Text -> Text
T.drop Int
2 Text
name
    [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. Monad m => a -> m a
return [ (Text
name, ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro ExpansionPoint
ExpandWhenUsed [] Maybe [Tok]
forall a. Maybe a
Nothing
                    [SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"iffalse") Text
"\\iffalse"])
           , (Text
base Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"true",
                   ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro ExpansionPoint
ExpandWhenUsed [] Maybe [Tok]
forall a. Maybe a
Nothing
                   [ SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"def") Text
"\\def"
                   , SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
name) (Text
"\\" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name)
                   , SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"iftrue") Text
"\\iftrue"
                   ])
           , (Text
base Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"false",
                   ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro ExpansionPoint
ExpandWhenUsed [] Maybe [Tok]
forall a. Maybe a
Nothing
                   [ SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"def") Text
"\\def"
                   , SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
name) (Text
"\\" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name)
                   , SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
"iffalse") Text
"\\iffalse"
                   ])
           ]

argspecArg :: PandocMonad m => LP m ArgSpec
argspecArg :: LP m ArgSpec
argspecArg = do
  Tok SourcePos
_ (Arg Int
i) Text
_ <- (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isArgTok
  ArgSpec -> LP m ArgSpec
forall (m :: * -> *) a. Monad m => a -> m a
return (ArgSpec -> LP m ArgSpec) -> ArgSpec -> LP m ArgSpec
forall a b. (a -> b) -> a -> b
$ Int -> ArgSpec
ArgNum Int
i

argspecPattern :: PandocMonad m => LP m ArgSpec
argspecPattern :: LP m ArgSpec
argspecPattern =
  [Tok] -> ArgSpec
Pattern ([Tok] -> ArgSpec)
-> ParsecT [Tok] LaTeXState m [Tok] -> LP m ArgSpec
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m Tok -> ParsecT [Tok] LaTeXState m [Tok]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ((Tok -> Bool) -> ParsecT [Tok] LaTeXState 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 :: LP m [(Text, Macro)]
newcommand = do
  Tok SourcePos
pos (CtrlSeq Text
mtype) Text
_ <- 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 (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 (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 (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 (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"
  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 SourcePos
_ (CtrlSeq Text
name) Text
txt <- do
      LP m Tok -> ParsecT [Tok] LaTeXState m (Maybe Tok)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'*')
      LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq LP m Tok -> LP m Tok -> LP m Tok
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
        (Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{' LP m Tok
-> ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces ParsecT [Tok] LaTeXState m () -> LP m Tok -> LP m Tok
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq LP m Tok -> ParsecT [Tok] LaTeXState m () -> LP m Tok
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m Tok -> LP m Tok -> LP m Tok
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'}')
    ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
    Int
numargs <- Int
-> ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Int
0 (ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int)
-> ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int
forall a b. (a -> b) -> a -> b
$ ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT [Tok] LaTeXState m Int
forall (m :: * -> *). PandocMonad m => LP m Int
bracketedNum
    let argspecs :: [ArgSpec]
argspecs = (Int -> ArgSpec) -> [Int] -> [ArgSpec]
forall a b. (a -> b) -> [a] -> [b]
map Int -> ArgSpec
ArgNum [Int
1..Int
numargs]
    ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
    Maybe [Tok]
optarg <- Maybe [Tok]
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Maybe [Tok]
forall a. Maybe a
Nothing (ParsecT [Tok] LaTeXState m (Maybe [Tok])
 -> ParsecT [Tok] LaTeXState m (Maybe [Tok]))
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
forall a b. (a -> b) -> a -> b
$ [Tok] -> Maybe [Tok]
forall a. a -> Maybe a
Just ([Tok] -> Maybe [Tok])
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m [Tok]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracketedToks
    ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
    [Tok]
contents' <- ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracedOrToken
    let contents :: [Tok]
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'
    Map Text Macro
macros <- LaTeXState -> Map Text Macro
sMacros (LaTeXState -> Map Text Macro)
-> ParsecT [Tok] LaTeXState m LaTeXState
-> ParsecT [Tok] LaTeXState m (Map Text Macro)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
    case Text -> Map Text Macro -> Maybe Macro
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Text
name Map Text Macro
macros of
        Just Macro
macro
          | Text
mtype Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"newcommand" -> do
              LogMessage -> ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report (LogMessage -> ParsecT [Tok] LaTeXState m ())
-> LogMessage -> ParsecT [Tok] LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ Text -> SourcePos -> LogMessage
MacroAlreadyDefined Text
txt SourcePos
pos
              [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. Monad m => a -> m a
return [(Text
name, Macro
macro)]
          | Text
mtype Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"providecommand" -> [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. Monad m => a -> m a
return [(Text
name, Macro
macro)]
        Maybe Macro
_ -> [(Text, Macro)] -> LP m [(Text, Macro)]
forall (m :: * -> *) a. Monad m => a -> m a
return [(Text
name, ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro ExpansionPoint
ExpandWhenUsed [ArgSpec]
argspecs Maybe [Tok]
optarg [Tok]
contents)]

newenvironment :: PandocMonad m => LP m (Maybe (Text, Macro, Macro))
newenvironment :: LP m (Maybe (Text, Macro, Macro))
newenvironment = do
  SourcePos
pos <- ParsecT [Tok] LaTeXState m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
  Tok SourcePos
_ (CtrlSeq Text
mtype) Text
_ <- Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"newenvironment" LP m Tok -> LP m Tok -> LP m Tok
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
"renewenvironment" LP m Tok -> LP m Tok -> LP m Tok
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
"provideenvironment"
  LP m (Maybe (Text, Macro, Macro))
-> LP m (Maybe (Text, Macro, Macro))
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
withVerbatimMode (LP m (Maybe (Text, Macro, Macro))
 -> LP m (Maybe (Text, Macro, Macro)))
-> LP m (Maybe (Text, Macro, Macro))
-> LP m (Maybe (Text, Macro, Macro))
forall a b. (a -> b) -> a -> b
$ do
    LP m Tok -> ParsecT [Tok] LaTeXState m (Maybe Tok)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (LP m Tok -> ParsecT [Tok] LaTeXState m (Maybe Tok))
-> LP m Tok -> ParsecT [Tok] LaTeXState m (Maybe Tok)
forall a b. (a -> b) -> a -> b
$ Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'*'
    LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
    Text
name <- [Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
    LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
    Int
numargs <- Int
-> ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Int
0 (ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int)
-> ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int
forall a b. (a -> b) -> a -> b
$ ParsecT [Tok] LaTeXState m Int -> ParsecT [Tok] LaTeXState m Int
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT [Tok] LaTeXState m Int
forall (m :: * -> *). PandocMonad m => LP m Int
bracketedNum
    LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
    Maybe [Tok]
optarg <- Maybe [Tok]
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Maybe [Tok]
forall a. Maybe a
Nothing (ParsecT [Tok] LaTeXState m (Maybe [Tok])
 -> ParsecT [Tok] LaTeXState m (Maybe [Tok]))
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
forall a b. (a -> b) -> a -> b
$ [Tok] -> Maybe [Tok]
forall a. a -> Maybe a
Just ([Tok] -> Maybe [Tok])
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m (Maybe [Tok])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m [Tok]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracketedToks
    let argspecs :: [ArgSpec]
argspecs = (Int -> ArgSpec) -> [Int] -> [ArgSpec]
forall a b. (a -> b) -> [a] -> [b]
map (\Int
i -> Int -> ArgSpec
ArgNum Int
i) [Int
1..Int
numargs]
    [Tok]
startcontents <- LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m ()
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracedOrToken
    [Tok]
endcontents <- LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m ()
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracedOrToken
    Map Text Macro
macros <- LaTeXState -> Map Text Macro
sMacros (LaTeXState -> Map Text Macro)
-> ParsecT [Tok] LaTeXState m LaTeXState
-> ParsecT [Tok] LaTeXState m (Map Text Macro)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
    case Text -> Map Text Macro -> Maybe Macro
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Text
name Map Text Macro
macros of
         Just Macro
_
           | Text
mtype Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"newenvironment" -> 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) -> LP m (Maybe (Text, Macro, Macro))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Text, Macro, Macro)
forall a. Maybe a
Nothing
           | Text
mtype Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"provideenvironment" ->
               Maybe (Text, Macro, Macro) -> LP m (Maybe (Text, Macro, Macro))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Text, Macro, Macro)
forall a. Maybe a
Nothing
         Maybe Macro
_ -> Maybe (Text, Macro, Macro) -> LP m (Maybe (Text, Macro, Macro))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Text, Macro, Macro) -> LP m (Maybe (Text, Macro, Macro)))
-> Maybe (Text, Macro, Macro) -> LP m (Maybe (Text, Macro, Macro))
forall a b. (a -> b) -> a -> b
$ (Text, Macro, Macro) -> Maybe (Text, Macro, Macro)
forall a. a -> Maybe a
Just (Text
name,
                      ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro ExpansionPoint
ExpandWhenUsed [ArgSpec]
argspecs Maybe [Tok]
optarg [Tok]
startcontents,
                      ExpansionPoint -> [ArgSpec] -> Maybe [Tok] -> [Tok] -> Macro
Macro ExpansionPoint
ExpandWhenUsed [] Maybe [Tok]
forall a. Maybe a
Nothing [Tok]
endcontents)

bracketedNum :: PandocMonad m => LP m Int
bracketedNum :: LP m Int
bracketedNum = do
  Text
ds <- [Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracketedToks
  case Text -> Maybe Int
forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
ds of
       Just Int
i -> Int -> LP m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i
       Maybe Int
_      -> Int -> LP m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
0