{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE BangPatterns          #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{- |
   Module      : Text.Pandoc.Readers.LaTeX.Parsing
   Copyright   : Copyright (C) 2006-2023 John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley.edu>
   Stability   : alpha
   Portability : portable

General parsing types and functions for LaTeX.
-}
module Text.Pandoc.Readers.LaTeX.Parsing
  ( DottedNum(..)
  , renderDottedNum
  , incrementDottedNum
  , TheoremSpec(..)
  , TheoremStyle(..)
  , LaTeXState(..)
  , defaultLaTeXState
  , LP
  , TokStream(..)
  , withVerbatimMode
  , rawLaTeXParser
  , applyMacros
  , tokenize
  , tokenizeSources
  , getInputTokens
  , untokenize
  , untoken
  , satisfyTok
  , peekTok
  , parseFromToks
  , disablingWithRaw
  , doMacros
  , doMacros'
  , setpos
  , anyControlSeq
  , anySymbol
  , isNewlineTok
  , isWordTok
  , isArgTok
  , infile
  , spaces
  , spaces1
  , tokTypeIn
  , controlSeq
  , symbol
  , symbolIn
  , sp
  , whitespace
  , newlineTok
  , comment
  , anyTok
  , singleChar
  , tokWith
  , specialChars
  , endline
  , blankline
  , primEscape
  , bgroup
  , egroup
  , grouped
  , braced
  , braced'
  , bracedUrl
  , bracedOrToken
  , bracketed
  , bracketedToks
  , parenWrapped
  , dimenarg
  , ignore
  , withRaw
  , keyvals
  , verbEnv
  , begin_
  , end_
  , getRawCommand
  , skipopts
  , rawopt
  , overlaySpecification
  , getNextNumber
  , label
  , setCaption
  , resetCaption
  , env
  , addMeta
  , removeLabel
  ) where

import Control.Applicative (many, (<|>))
import Control.Monad
import Control.Monad.Except (throwError)
import Control.Monad.Trans (lift)
import Data.Char (chr, isAlphaNum, isDigit, isLetter, ord)
import Data.Default
import Data.List (intercalate)
import qualified Data.IntMap as IntMap
import qualified Data.Map as M
import qualified Data.Set as Set
import Data.Text (Text)
import Data.Maybe (fromMaybe)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Text as T
import Text.Pandoc.Builder
import Text.Pandoc.Class.PandocMonad (PandocMonad, report)
import Text.Pandoc.Error
         (PandocError (PandocMacroLoop,PandocShouldNeverHappenError))
import Text.Pandoc.Logging
import Text.Pandoc.Options
import Text.Pandoc.Parsing hiding (blankline, many, mathDisplay, mathInline,
                            space, spaces, withRaw, (<|>))
import Text.Pandoc.TeX (ExpansionPoint (..), Macro (..),
                                        ArgSpec (..), Tok (..), TokType (..))
import Text.Pandoc.Shared
import Text.Pandoc.Walk

newtype DottedNum = DottedNum [Int]
  deriving (Int -> DottedNum -> ShowS
[DottedNum] -> ShowS
DottedNum -> [Char]
(Int -> DottedNum -> ShowS)
-> (DottedNum -> [Char])
-> ([DottedNum] -> ShowS)
-> Show DottedNum
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DottedNum -> ShowS
showsPrec :: Int -> DottedNum -> ShowS
$cshow :: DottedNum -> [Char]
show :: DottedNum -> [Char]
$cshowList :: [DottedNum] -> ShowS
showList :: [DottedNum] -> ShowS
Show, DottedNum -> DottedNum -> Bool
(DottedNum -> DottedNum -> Bool)
-> (DottedNum -> DottedNum -> Bool) -> Eq DottedNum
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DottedNum -> DottedNum -> Bool
== :: DottedNum -> DottedNum -> Bool
$c/= :: DottedNum -> DottedNum -> Bool
/= :: DottedNum -> DottedNum -> Bool
Eq)

renderDottedNum :: DottedNum -> T.Text
renderDottedNum :: DottedNum -> Text
renderDottedNum (DottedNum [Int]
xs) = [Char] -> Text
T.pack ([Char] -> Text) -> [Char] -> Text
forall a b. (a -> b) -> a -> b
$
  [Char] -> [[Char]] -> [Char]
forall a. [a] -> [[a]] -> [a]
intercalate [Char]
"." ((Int -> [Char]) -> [Int] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map Int -> [Char]
forall a. Show a => a -> [Char]
show [Int]
xs)

incrementDottedNum :: Int -> DottedNum -> DottedNum
incrementDottedNum :: Int -> DottedNum -> DottedNum
incrementDottedNum Int
level (DottedNum [Int]
ns) = [Int] -> DottedNum
DottedNum ([Int] -> DottedNum) -> [Int] -> DottedNum
forall a b. (a -> b) -> a -> b
$
  case [Int] -> [Int]
forall a. [a] -> [a]
reverse (Int -> [Int] -> [Int]
forall a. Int -> [a] -> [a]
take Int
level ([Int]
ns [Int] -> [Int] -> [Int]
forall a. [a] -> [a] -> [a]
++ Int -> [Int]
forall a. a -> [a]
repeat Int
0)) of
       (Int
x:[Int]
xs) -> [Int] -> [Int]
forall a. [a] -> [a]
reverse (Int
xInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1 Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Int]
xs)
       []     -> []  -- shouldn't happen

data TheoremStyle =
  PlainStyle | DefinitionStyle | RemarkStyle
  deriving (Int -> TheoremStyle -> ShowS
[TheoremStyle] -> ShowS
TheoremStyle -> [Char]
(Int -> TheoremStyle -> ShowS)
-> (TheoremStyle -> [Char])
-> ([TheoremStyle] -> ShowS)
-> Show TheoremStyle
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TheoremStyle -> ShowS
showsPrec :: Int -> TheoremStyle -> ShowS
$cshow :: TheoremStyle -> [Char]
show :: TheoremStyle -> [Char]
$cshowList :: [TheoremStyle] -> ShowS
showList :: [TheoremStyle] -> ShowS
Show, TheoremStyle -> TheoremStyle -> Bool
(TheoremStyle -> TheoremStyle -> Bool)
-> (TheoremStyle -> TheoremStyle -> Bool) -> Eq TheoremStyle
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TheoremStyle -> TheoremStyle -> Bool
== :: TheoremStyle -> TheoremStyle -> Bool
$c/= :: TheoremStyle -> TheoremStyle -> Bool
/= :: TheoremStyle -> TheoremStyle -> Bool
Eq)

data TheoremSpec =
  TheoremSpec
    { TheoremSpec -> Inlines
theoremName    :: Inlines
    , TheoremSpec -> TheoremStyle
theoremStyle   :: TheoremStyle
    , TheoremSpec -> Maybe Text
theoremSeries  :: Maybe Text
    , TheoremSpec -> Maybe Text
theoremSyncTo  :: Maybe Text
    , TheoremSpec -> Bool
theoremNumber  :: Bool
    , TheoremSpec -> DottedNum
theoremLastNum :: DottedNum }
    deriving (Int -> TheoremSpec -> ShowS
[TheoremSpec] -> ShowS
TheoremSpec -> [Char]
(Int -> TheoremSpec -> ShowS)
-> (TheoremSpec -> [Char])
-> ([TheoremSpec] -> ShowS)
-> Show TheoremSpec
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TheoremSpec -> ShowS
showsPrec :: Int -> TheoremSpec -> ShowS
$cshow :: TheoremSpec -> [Char]
show :: TheoremSpec -> [Char]
$cshowList :: [TheoremSpec] -> ShowS
showList :: [TheoremSpec] -> ShowS
Show, TheoremSpec -> TheoremSpec -> Bool
(TheoremSpec -> TheoremSpec -> Bool)
-> (TheoremSpec -> TheoremSpec -> Bool) -> Eq TheoremSpec
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TheoremSpec -> TheoremSpec -> Bool
== :: TheoremSpec -> TheoremSpec -> Bool
$c/= :: TheoremSpec -> TheoremSpec -> Bool
/= :: TheoremSpec -> TheoremSpec -> Bool
Eq)

data LaTeXState = LaTeXState{ LaTeXState -> ReaderOptions
sOptions       :: ReaderOptions
                            , LaTeXState -> Meta
sMeta          :: Meta
                            , LaTeXState -> QuoteContext
sQuoteContext  :: QuoteContext
                            , LaTeXState -> NonEmpty (Map Text Macro)
sMacros        :: NonEmpty (M.Map Text Macro)
                            , LaTeXState -> [Text]
sContainers    :: [Text]
                            , LaTeXState -> [LogMessage]
sLogMessages   :: [LogMessage]
                            , LaTeXState -> Set Text
sIdentifiers   :: Set.Set Text
                            , LaTeXState -> Bool
sVerbatimMode  :: Bool
                            , LaTeXState -> Maybe Caption
sCaption       :: Maybe Caption
                            , LaTeXState -> Bool
sInListItem    :: Bool
                            , LaTeXState -> Bool
sInTableCell   :: Bool
                            , LaTeXState -> DottedNum
sLastHeaderNum :: DottedNum
                            , LaTeXState -> DottedNum
sLastFigureNum :: DottedNum
                            , LaTeXState -> DottedNum
sLastTableNum  :: DottedNum
                            , LaTeXState -> Int
sLastNoteNum   :: Int
                            , LaTeXState -> Map Text TheoremSpec
sTheoremMap    :: M.Map Text TheoremSpec
                            , LaTeXState -> TheoremStyle
sLastTheoremStyle :: TheoremStyle
                            , LaTeXState -> Maybe Text
sLastLabel     :: Maybe Text
                            , LaTeXState -> Map Text [Inline]
sLabels        :: M.Map Text [Inline]
                            , LaTeXState -> Bool
sHasChapters   :: Bool
                            , LaTeXState -> Map Text Bool
sToggles       :: M.Map Text Bool
                            , LaTeXState -> Map Text Text
sFileContents  :: M.Map Text Text
                            , LaTeXState -> Bool
sEnableWithRaw :: Bool
                            , LaTeXState -> IntMap [Tok]
sRawTokens     :: IntMap.IntMap [Tok]
                            }
     deriving Int -> LaTeXState -> ShowS
[LaTeXState] -> ShowS
LaTeXState -> [Char]
(Int -> LaTeXState -> ShowS)
-> (LaTeXState -> [Char])
-> ([LaTeXState] -> ShowS)
-> Show LaTeXState
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LaTeXState -> ShowS
showsPrec :: Int -> LaTeXState -> ShowS
$cshow :: LaTeXState -> [Char]
show :: LaTeXState -> [Char]
$cshowList :: [LaTeXState] -> ShowS
showList :: [LaTeXState] -> ShowS
Show

defaultLaTeXState :: LaTeXState
defaultLaTeXState :: LaTeXState
defaultLaTeXState = LaTeXState{ sOptions :: ReaderOptions
sOptions       = ReaderOptions
forall a. Default a => a
def
                              , sMeta :: Meta
sMeta          = Meta
nullMeta
                              , sQuoteContext :: QuoteContext
sQuoteContext  = QuoteContext
NoQuote
                              , sMacros :: NonEmpty (Map Text Macro)
sMacros        = Map Text Macro
forall k a. Map k a
M.empty Map Text Macro -> [Map Text Macro] -> NonEmpty (Map Text Macro)
forall a. a -> [a] -> NonEmpty a
:| []
                              , sContainers :: [Text]
sContainers    = []
                              , sLogMessages :: [LogMessage]
sLogMessages   = []
                              , sIdentifiers :: Set Text
sIdentifiers   = Set Text
forall a. Set a
Set.empty
                              , sVerbatimMode :: Bool
sVerbatimMode  = Bool
False
                              , sCaption :: Maybe Caption
sCaption       = Maybe Caption
forall a. Maybe a
Nothing
                              , sInListItem :: Bool
sInListItem    = Bool
False
                              , sInTableCell :: Bool
sInTableCell   = Bool
False
                              , sLastHeaderNum :: DottedNum
sLastHeaderNum = [Int] -> DottedNum
DottedNum []
                              , sLastFigureNum :: DottedNum
sLastFigureNum = [Int] -> DottedNum
DottedNum []
                              , sLastTableNum :: DottedNum
sLastTableNum  = [Int] -> DottedNum
DottedNum []
                              , sLastNoteNum :: Int
sLastNoteNum   = Int
0
                              , sTheoremMap :: Map Text TheoremSpec
sTheoremMap    = Map Text TheoremSpec
forall k a. Map k a
M.empty
                              , sLastTheoremStyle :: TheoremStyle
sLastTheoremStyle = TheoremStyle
PlainStyle
                              , sLastLabel :: Maybe Text
sLastLabel     = Maybe Text
forall a. Maybe a
Nothing
                              , sLabels :: Map Text [Inline]
sLabels        = Map Text [Inline]
forall k a. Map k a
M.empty
                              , sHasChapters :: Bool
sHasChapters   = Bool
False
                              , sToggles :: Map Text Bool
sToggles       = Map Text Bool
forall k a. Map k a
M.empty
                              , sFileContents :: Map Text Text
sFileContents  = Map Text Text
forall k a. Map k a
M.empty
                              , sEnableWithRaw :: Bool
sEnableWithRaw = Bool
True
                              , sRawTokens :: IntMap [Tok]
sRawTokens     = IntMap [Tok]
forall a. IntMap a
IntMap.empty
                              }

instance PandocMonad m => HasQuoteContext LaTeXState m where
  getQuoteContext :: forall s t. Stream s m t => ParsecT s LaTeXState m QuoteContext
getQuoteContext = LaTeXState -> QuoteContext
sQuoteContext (LaTeXState -> QuoteContext)
-> ParsecT s LaTeXState m LaTeXState
-> ParsecT s LaTeXState m QuoteContext
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT s LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  withQuoteContext :: forall s a.
QuoteContext
-> ParsecT s LaTeXState m a -> ParsecT s LaTeXState m a
withQuoteContext QuoteContext
context ParsecT s LaTeXState m a
parser = do
    LaTeXState
oldState <- ParsecT s LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
    let oldQuoteContext :: QuoteContext
oldQuoteContext = LaTeXState -> QuoteContext
sQuoteContext LaTeXState
oldState
    LaTeXState -> ParsecT s LaTeXState m ()
forall (m :: * -> *) u s. Monad m => u -> ParsecT s u m ()
setState LaTeXState
oldState { sQuoteContext = context }
    a
result <- ParsecT s LaTeXState m a
parser
    LaTeXState
newState <- ParsecT s LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
    LaTeXState -> ParsecT s LaTeXState m ()
forall (m :: * -> *) u s. Monad m => u -> ParsecT s u m ()
setState LaTeXState
newState { sQuoteContext = oldQuoteContext }
    a -> ParsecT s LaTeXState m a
forall a. a -> ParsecT s LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
result

instance HasLogMessages LaTeXState where
  addLogMessage :: LogMessage -> LaTeXState -> LaTeXState
addLogMessage LogMessage
msg LaTeXState
st = LaTeXState
st{ sLogMessages = msg : sLogMessages st }
  getLogMessages :: LaTeXState -> [LogMessage]
getLogMessages LaTeXState
st = [LogMessage] -> [LogMessage]
forall a. [a] -> [a]
reverse ([LogMessage] -> [LogMessage]) -> [LogMessage] -> [LogMessage]
forall a b. (a -> b) -> a -> b
$ LaTeXState -> [LogMessage]
sLogMessages LaTeXState
st

instance HasIdentifierList LaTeXState where
  extractIdentifierList :: LaTeXState -> Set Text
extractIdentifierList     = LaTeXState -> Set Text
sIdentifiers
  updateIdentifierList :: (Set Text -> Set Text) -> LaTeXState -> LaTeXState
updateIdentifierList Set Text -> Set Text
f LaTeXState
st = LaTeXState
st{ sIdentifiers = f $ sIdentifiers st }

instance HasIncludeFiles LaTeXState where
  getIncludeFiles :: LaTeXState -> [Text]
getIncludeFiles = LaTeXState -> [Text]
sContainers
  addIncludeFile :: Text -> LaTeXState -> LaTeXState
addIncludeFile Text
f LaTeXState
s = LaTeXState
s{ sContainers = f : sContainers s }
  dropLatestIncludeFile :: LaTeXState -> LaTeXState
dropLatestIncludeFile LaTeXState
s = LaTeXState
s { sContainers = drop 1 $ sContainers s }

instance HasMacros LaTeXState where
  extractMacros :: LaTeXState -> Map Text Macro
extractMacros  LaTeXState
st  = NonEmpty (Map Text Macro) -> Map Text Macro
forall a. NonEmpty a -> a
NonEmpty.head (NonEmpty (Map Text Macro) -> Map Text Macro)
-> NonEmpty (Map Text Macro) -> Map Text Macro
forall a b. (a -> b) -> a -> b
$ LaTeXState -> NonEmpty (Map Text Macro)
sMacros LaTeXState
st
  updateMacros :: (Map Text Macro -> Map Text Macro) -> LaTeXState -> LaTeXState
updateMacros Map Text Macro -> Map Text Macro
f LaTeXState
st  = LaTeXState
st{ sMacros = f (NonEmpty.head (sMacros st))
                                     :| NonEmpty.tail (sMacros st) }

instance HasReaderOptions LaTeXState where
  extractReaderOptions :: LaTeXState -> ReaderOptions
extractReaderOptions = LaTeXState -> ReaderOptions
sOptions

instance HasMeta LaTeXState where
  setMeta :: forall b. ToMetaValue b => Text -> b -> LaTeXState -> LaTeXState
setMeta Text
field b
val LaTeXState
st =
    LaTeXState
st{ sMeta = setMeta field val $ sMeta st }
  deleteMeta :: Text -> LaTeXState -> LaTeXState
deleteMeta Text
field LaTeXState
st =
    LaTeXState
st{ sMeta = deleteMeta field $ sMeta st }

instance Default LaTeXState where
  def :: LaTeXState
def = LaTeXState
defaultLaTeXState

-- The Boolean is True if macros have already been expanded,
-- False if they need expanding.
data TokStream = TokStream !Bool [Tok]
  deriving (Int -> TokStream -> ShowS
[TokStream] -> ShowS
TokStream -> [Char]
(Int -> TokStream -> ShowS)
-> (TokStream -> [Char])
-> ([TokStream] -> ShowS)
-> Show TokStream
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TokStream -> ShowS
showsPrec :: Int -> TokStream -> ShowS
$cshow :: TokStream -> [Char]
show :: TokStream -> [Char]
$cshowList :: [TokStream] -> ShowS
showList :: [TokStream] -> ShowS
Show)

instance Semigroup TokStream where
  (TokStream Bool
exp1 [Tok]
ts1) <> :: TokStream -> TokStream -> TokStream
<> (TokStream Bool
exp2 [Tok]
ts2) =
    Bool -> [Tok] -> TokStream
TokStream (if [Tok] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Tok]
ts1 then Bool
exp2 else Bool
exp1) ([Tok]
ts1 [Tok] -> [Tok] -> [Tok]
forall a. Semigroup a => a -> a -> a
<> [Tok]
ts2)

instance Monoid TokStream where
  mempty :: TokStream
mempty = Bool -> [Tok] -> TokStream
TokStream Bool
False [Tok]
forall a. Monoid a => a
mempty
  mappend :: TokStream -> TokStream -> TokStream
mappend = TokStream -> TokStream -> TokStream
forall a. Semigroup a => a -> a -> a
(<>)

instance Monad m => Stream TokStream m Tok where
  uncons :: TokStream -> m (Maybe (Tok, TokStream))
uncons (TokStream Bool
_ []) = Maybe (Tok, TokStream) -> m (Maybe (Tok, TokStream))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Tok, TokStream)
forall a. Maybe a
Nothing
  uncons (TokStream Bool
_ (Tok
t:[Tok]
ts)) = Maybe (Tok, TokStream) -> m (Maybe (Tok, TokStream))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Tok, TokStream) -> m (Maybe (Tok, TokStream)))
-> Maybe (Tok, TokStream) -> m (Maybe (Tok, TokStream))
forall a b. (a -> b) -> a -> b
$ (Tok, TokStream) -> Maybe (Tok, TokStream)
forall a. a -> Maybe a
Just (Tok
t, Bool -> [Tok] -> TokStream
TokStream Bool
False [Tok]
ts)

type LP m = ParsecT TokStream LaTeXState m

withVerbatimMode :: PandocMonad m => LP m a -> LP m a
withVerbatimMode :: forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
withVerbatimMode LP m a
parser = do
  Bool
alreadyVerbatimMode <- LaTeXState -> Bool
sVerbatimMode (LaTeXState -> Bool)
-> ParsecT TokStream LaTeXState m LaTeXState
-> ParsecT TokStream LaTeXState m Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  if Bool
alreadyVerbatimMode
     then LP m a
parser
     else do
       (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ())
-> (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
st -> LaTeXState
st{ sVerbatimMode = True }
       a
result <- LP m a
parser
       (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ())
-> (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
st -> LaTeXState
st{ sVerbatimMode = False }
       a -> LP m a
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
result

rawLaTeXParser :: (PandocMonad m, HasMacros s, HasReaderOptions s, Show a)
               => [Tok] -> LP m a -> LP m a
               -> ParsecT Sources s m (a, Text)
rawLaTeXParser :: forall (m :: * -> *) s a.
(PandocMonad m, HasMacros s, HasReaderOptions s, Show a) =>
[Tok] -> LP m a -> LP m a -> ParsecT Sources s m (a, Text)
rawLaTeXParser [Tok]
toks LP m a
parser LP m a
valParser = do
  s
pstate <- ParsecT Sources s m s
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  let lstate :: LaTeXState
lstate = LaTeXState
forall a. Default a => a
def{ sOptions = extractReaderOptions pstate }
  let lstate' :: LaTeXState
lstate' = LaTeXState
lstate { sMacros = extractMacros pstate :| [] }
  let setStartPos :: ParsecT s u m ()
setStartPos = case [Tok]
toks of
                      Tok SourcePos
pos TokType
_ Text
_ : [Tok]
_ -> SourcePos -> ParsecT s u m ()
forall (m :: * -> *) s u. Monad m => SourcePos -> ParsecT s u m ()
setPosition SourcePos
pos
                      [Tok]
_ -> () -> ParsecT s u m ()
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  let preparser :: LP m a
preparser = ParsecT TokStream LaTeXState m ()
forall {s} {u}. ParsecT s u m ()
setStartPos ParsecT TokStream LaTeXState m () -> LP m a -> LP m a
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> LP m a
parser
  let rawparser :: ParsecT TokStream LaTeXState m ((a, [Tok]), LaTeXState)
rawparser = (,) ((a, [Tok]) -> LaTeXState -> ((a, [Tok]), LaTeXState))
-> ParsecT TokStream LaTeXState m (a, [Tok])
-> ParsecT
     TokStream LaTeXState m (LaTeXState -> ((a, [Tok]), LaTeXState))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m a -> ParsecT TokStream LaTeXState m (a, [Tok])
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
withRaw LP m a
valParser ParsecT
  TokStream LaTeXState m (LaTeXState -> ((a, [Tok]), LaTeXState))
-> ParsecT TokStream LaTeXState m LaTeXState
-> ParsecT TokStream LaTeXState m ((a, [Tok]), LaTeXState)
forall a b.
ParsecT TokStream LaTeXState m (a -> b)
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT TokStream LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  Either ParseError (SourcePos, [Tok])
res' <- m (Either ParseError (SourcePos, [Tok]))
-> ParsecT Sources s m (Either ParseError (SourcePos, [Tok]))
forall (m :: * -> *) a. Monad m => m a -> ParsecT Sources s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either ParseError (SourcePos, [Tok]))
 -> ParsecT Sources s m (Either ParseError (SourcePos, [Tok])))
-> m (Either ParseError (SourcePos, [Tok]))
-> ParsecT Sources s m (Either ParseError (SourcePos, [Tok]))
forall a b. (a -> b) -> a -> b
$ ParsecT TokStream LaTeXState m (SourcePos, [Tok])
-> LaTeXState
-> [Char]
-> TokStream
-> m (Either ParseError (SourcePos, [Tok]))
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> u -> [Char] -> s -> m (Either ParseError a)
runParserT (LP m SourcePos -> ParsecT TokStream LaTeXState m (SourcePos, [Tok])
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
withRaw (LP m a
preparser LP m a -> LP m SourcePos -> LP m SourcePos
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> LP m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition))
                            LaTeXState
lstate [Char]
"chunk" (TokStream -> m (Either ParseError (SourcePos, [Tok])))
-> TokStream -> m (Either ParseError (SourcePos, [Tok]))
forall a b. (a -> b) -> a -> b
$ Bool -> [Tok] -> TokStream
TokStream Bool
False [Tok]
toks
  case Either ParseError (SourcePos, [Tok])
res' of
       Left ParseError
_    -> ParsecT Sources s m (a, Text)
forall a. ParsecT Sources s m a
forall (m :: * -> *) a. MonadPlus m => m a
mzero
       Right (SourcePos
endpos, [Tok]
toks') -> do
         Either ParseError ((a, [Tok]), LaTeXState)
res <- m (Either ParseError ((a, [Tok]), LaTeXState))
-> ParsecT Sources s m (Either ParseError ((a, [Tok]), LaTeXState))
forall (m :: * -> *) a. Monad m => m a -> ParsecT Sources s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either ParseError ((a, [Tok]), LaTeXState))
 -> ParsecT
      Sources s m (Either ParseError ((a, [Tok]), LaTeXState)))
-> m (Either ParseError ((a, [Tok]), LaTeXState))
-> ParsecT Sources s m (Either ParseError ((a, [Tok]), LaTeXState))
forall a b. (a -> b) -> a -> b
$ ParsecT TokStream LaTeXState m ((a, [Tok]), LaTeXState)
-> LaTeXState
-> [Char]
-> TokStream
-> m (Either ParseError ((a, [Tok]), LaTeXState))
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> u -> [Char] -> s -> m (Either ParseError a)
runParserT ParsecT TokStream LaTeXState m ((a, [Tok]), LaTeXState)
rawparser LaTeXState
lstate' [Char]
"chunk"
                     (TokStream -> m (Either ParseError ((a, [Tok]), LaTeXState)))
-> TokStream -> m (Either ParseError ((a, [Tok]), LaTeXState))
forall a b. (a -> b) -> a -> b
$ Bool -> [Tok] -> TokStream
TokStream Bool
False [Tok]
toks'
         case Either ParseError ((a, [Tok]), LaTeXState)
res of
              Left ParseError
_    -> ParsecT Sources s m (a, Text)
forall a. ParsecT Sources s m a
forall (m :: * -> *) a. MonadPlus m => m a
mzero
              Right ((a
val, [Tok]
raw), LaTeXState
st) -> do
                (s -> s) -> ParsecT Sources s m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((Map Text Macro -> Map Text Macro) -> s -> s
forall st.
HasMacros st =>
(Map Text Macro -> Map Text Macro) -> st -> st
updateMacros ((NonEmpty (Map Text Macro) -> Map Text Macro
forall a. NonEmpty a -> a
NonEmpty.head (LaTeXState -> NonEmpty (Map Text Macro)
sMacros LaTeXState
st)) Map Text Macro -> Map Text Macro -> Map Text Macro
forall a. Semigroup a => a -> a -> a
<>))
                let skipTilPos :: SourcePos -> ParsecT s u m ()
skipTilPos SourcePos
stopPos = do
                      ParsecT s u m Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
anyChar
                      SourcePos
pos <- ParsecT s u m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
                      if SourcePos
pos SourcePos -> SourcePos -> Bool
forall a. Ord a => a -> a -> Bool
>= SourcePos
stopPos
                         then () -> ParsecT s u m ()
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                         else SourcePos -> ParsecT s u m ()
skipTilPos SourcePos
stopPos
                SourcePos -> ParsecT Sources s m ()
forall {m :: * -> *} {s} {u}.
(Stream s m Char, UpdateSourcePos s Char) =>
SourcePos -> ParsecT s u m ()
skipTilPos SourcePos
endpos
                let result :: Text
result = [Tok] -> Text
untokenize [Tok]
raw
                -- ensure we end with space if input did, see #4442
                let result' :: Text
result' =
                      case [Tok] -> [Tok]
forall a. [a] -> [a]
reverse [Tok]
toks' of
                        (Tok SourcePos
_ (CtrlSeq Text
_) Text
t : [Tok]
_)
                         | Text
" " Text -> Text -> Bool
`T.isSuffixOf` Text
t
                         , Bool -> Bool
not (Text
" " Text -> Text -> Bool
`T.isSuffixOf` Text
result)
                          -> Text
result Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" "
                        [Tok]
_ -> Text
result
                (a, Text) -> ParsecT Sources s m (a, Text)
forall a. a -> ParsecT Sources s m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
val, Text
result')

applyMacros :: (PandocMonad m, HasMacros s, HasReaderOptions s)
            => Text -> ParsecT Sources s m Text
applyMacros :: forall (m :: * -> *) s.
(PandocMonad m, HasMacros s, HasReaderOptions s) =>
Text -> ParsecT Sources s m Text
applyMacros Text
s = (Extension -> ParsecT Sources s m ()
forall s (m :: * -> *) a st.
(Stream s m a, HasReaderOptions st) =>
Extension -> ParsecT s st m ()
guardDisabled Extension
Ext_latex_macros ParsecT Sources s m ()
-> ParsecT Sources s m Text -> ParsecT Sources s m Text
forall a b.
ParsecT Sources s m a
-> ParsecT Sources s m b -> ParsecT Sources s m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> ParsecT Sources s m Text
forall a. a -> ParsecT Sources s m a
forall (m :: * -> *) a. Monad m => a -> m a
return Text
s) ParsecT Sources s m Text
-> ParsecT Sources s m Text -> ParsecT Sources s m Text
forall a.
ParsecT Sources s m a
-> ParsecT Sources s m a -> ParsecT Sources s m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
   do let retokenize :: ParsecT TokStream LaTeXState (ParsecT Sources s m) Text
retokenize = [Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT TokStream LaTeXState (ParsecT Sources s m) [Tok]
-> ParsecT TokStream LaTeXState (ParsecT Sources s m) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState (ParsecT Sources s m) Tok
-> ParsecT TokStream LaTeXState (ParsecT Sources s m) [Tok]
forall a.
ParsecT TokStream LaTeXState (ParsecT Sources s m) a
-> ParsecT TokStream LaTeXState (ParsecT Sources s m) [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many ParsecT TokStream LaTeXState (ParsecT Sources s m) Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok
      s
pstate <- ParsecT Sources s m s
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
      let lstate :: LaTeXState
lstate = LaTeXState
forall a. Default a => a
def{ sOptions = extractReaderOptions pstate
                      , sMacros  = extractMacros pstate :| [] }
      Either ParseError Text
res <- ParsecT TokStream LaTeXState (ParsecT Sources s m) Text
-> LaTeXState
-> [Char]
-> TokStream
-> ParsecT Sources s m (Either ParseError Text)
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> u -> [Char] -> s -> m (Either ParseError a)
runParserT ParsecT TokStream LaTeXState (ParsecT Sources s m) Text
retokenize LaTeXState
lstate [Char]
"math" (TokStream -> ParsecT Sources s m (Either ParseError Text))
-> TokStream -> ParsecT Sources s m (Either ParseError Text)
forall a b. (a -> b) -> a -> b
$
                 Bool -> [Tok] -> TokStream
TokStream Bool
False (SourcePos -> Text -> [Tok]
tokenize ([Char] -> SourcePos
initialPos [Char]
"math") Text
s)
      case Either ParseError Text
res of
           Left ParseError
e   -> [Char] -> ParsecT Sources s m Text
forall a. [Char] -> ParsecT Sources s m a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
Prelude.fail (ParseError -> [Char]
forall a. Show a => a -> [Char]
show ParseError
e)
           Right Text
s' -> Text -> ParsecT Sources s m Text
forall a. a -> ParsecT Sources s m a
forall (m :: * -> *) a. Monad m => a -> m a
return Text
s'

{-
When tokenize or untokenize change, test with this
QuickCheck property:

> tokUntokRoundtrip :: String -> Bool
> tokUntokRoundtrip s =
>   let t = T.pack s in untokenize (tokenize "random" t) == t
-}

tokenizeSources :: Sources -> [Tok]
tokenizeSources :: Sources -> [Tok]
tokenizeSources = ((SourcePos, Text) -> [Tok]) -> [(SourcePos, Text)] -> [Tok]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (SourcePos, Text) -> [Tok]
tokenizeSource ([(SourcePos, Text)] -> [Tok])
-> (Sources -> [(SourcePos, Text)]) -> Sources -> [Tok]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sources -> [(SourcePos, Text)]
unSources
 where
   tokenizeSource :: (SourcePos, Text) -> [Tok]
tokenizeSource (SourcePos
pos, Text
t) = SourcePos -> Text -> [Tok]
tokenize SourcePos
pos Text
t

-- Return tokens from input sources. Ensure that starting position is
-- correct.
getInputTokens :: PandocMonad m => ParsecT Sources s m [Tok]
getInputTokens :: forall (m :: * -> *) s. PandocMonad m => ParsecT Sources s m [Tok]
getInputTokens = do
  SourcePos
pos <- ParsecT Sources s m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
  Sources
ss <- ParsecT Sources s m Sources
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
  [Tok] -> ParsecT Sources s m [Tok]
forall a. a -> ParsecT Sources s m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Tok] -> ParsecT Sources s m [Tok])
-> [Tok] -> ParsecT Sources s m [Tok]
forall a b. (a -> b) -> a -> b
$
    case Sources
ss of
      Sources [] -> []
      Sources ((SourcePos
_,Text
t):[(SourcePos, Text)]
rest) -> Sources -> [Tok]
tokenizeSources (Sources -> [Tok]) -> Sources -> [Tok]
forall a b. (a -> b) -> a -> b
$ [(SourcePos, Text)] -> Sources
Sources ((SourcePos
pos,Text
t)(SourcePos, Text) -> [(SourcePos, Text)] -> [(SourcePos, Text)]
forall a. a -> [a] -> [a]
:[(SourcePos, Text)]
rest)

tokenize :: SourcePos -> Text -> [Tok]
tokenize :: SourcePos -> Text -> [Tok]
tokenize = SourcePos -> Text -> [Tok]
totoks
 where
  totoks :: SourcePos -> Text -> [Tok]
totoks SourcePos
pos Text
t =
    case Text -> Maybe (Char, Text)
T.uncons Text
t of
       Maybe (Char, Text)
Nothing        -> []
       Just (Char
c, Text
rest)
         | Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\n' ->
           SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Newline Text
"\n"
           Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
setSourceColumn (SourcePos -> Int -> SourcePos
incSourceLine SourcePos
pos Int
1) Int
1) Text
rest
         | Char -> Bool
isSpaceOrTab Char
c ->
           let (Text
sps, Text
rest') = (Char -> Bool) -> Text -> (Text, Text)
T.span Char -> Bool
isSpaceOrTab Text
t
           in  SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Spaces Text
sps
               Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos (Text -> Int
T.length Text
sps))
                 Text
rest'
         | Char -> Bool
isAlphaNum Char
c ->
           let (Text
ws, Text
rest') = (Char -> Bool) -> Text -> (Text, Text)
T.span Char -> Bool
isAlphaNum Text
t
           in  SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Word Text
ws
               Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos (Text -> Int
T.length Text
ws)) Text
rest'
         | Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'%' ->
           let (Text
cs, Text
rest') = (Char -> Bool) -> Text -> (Text, Text)
T.break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\n') Text
rest
           in  SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Comment (Text
"%" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
cs)
               Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos (Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Text -> Int
T.length Text
cs)) Text
rest'
         | Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\\' ->
           case Text -> Maybe (Char, Text)
T.uncons Text
rest of
                Maybe (Char, Text)
Nothing -> [SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
" ") Text
"\\"]
                Just (Char
d, Text
rest')
                  | Char -> Bool
isLetterOrAt Char
d ->
                      -- \makeatletter is common in macro defs;
                      -- ideally we should make tokenization sensitive
                      -- to \makeatletter and \makeatother, but this is
                      -- probably best for now
                      let (Text
ws, Text
rest'') = (Char -> Bool) -> Text -> (Text, Text)
T.span Char -> Bool
isLetterOrAt Text
rest
                          (Text
ss, Text
rest''') = (Char -> Bool) -> Text -> (Text, Text)
T.span Char -> Bool
isSpaceOrTab Text
rest''
                      in  SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
ws) (Text
"\\" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
ws Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
ss)
                          Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos
                               (Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Text -> Int
T.length Text
ws Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Text -> Int
T.length Text
ss)) Text
rest'''
                  | Char -> Bool
isSpaceOrTab Char
d Bool -> Bool -> Bool
|| Char
d Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\n' ->
                      let (Text
w1, Text
r1) = (Char -> Bool) -> Text -> (Text, Text)
T.span Char -> Bool
isSpaceOrTab Text
rest
                          (Text
w2, (Text
w3, Text
r3)) = case Text -> Maybe (Char, Text)
T.uncons Text
r1 of
                                          Just (Char
'\n', Text
r2)
                                                  -> ([Char] -> Text
T.pack [Char]
"\n",
                                                        (Char -> Bool) -> Text -> (Text, Text)
T.span Char -> Bool
isSpaceOrTab Text
r2)
                                          Maybe (Char, Text)
_ -> (Text
forall a. Monoid a => a
mempty, (Text
forall a. Monoid a => a
mempty, Text
r1))
                          ws :: Text
ws = Text
"\\" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
w1 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
w2 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
w3
                      in  case Text -> Maybe (Char, Text)
T.uncons Text
r3 of
                               Just (Char
'\n', Text
_) ->
                                 SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
" ") (Text
"\\" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
w1)
                                 Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos (Text -> Int
T.length Text
ws))
                                   Text
r1
                               Maybe (Char, Text)
_ ->
                                 SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq Text
" ") Text
ws
                                 Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos (Text -> Int
T.length Text
ws))
                                   Text
r3
                  | Bool
otherwise  ->
                      SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Text -> TokType
CtrlSeq (Char -> Text
T.singleton Char
d)) ([Char] -> Text
T.pack [Char
c,Char
d])
                      Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos Int
2) Text
rest'
         | Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'#' ->
           case Text -> Maybe (Char, Text)
T.uncons Text
rest of
             Just (Char
'#', Text
t3) ->
               let (Text
t1, Text
t2) = (Char -> Bool) -> Text -> (Text, Text)
T.span (\Char
d -> Char
d Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'0' Bool -> Bool -> Bool
&& Char
d Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'9') Text
t3
               in  case Text -> Maybe Int
forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
t1 of
                        Just Int
i ->
                           SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Int -> TokType
DeferredArg Int
i) (Text
"##" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t1)
                           Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Text -> Int
T.length Text
t1)) Text
t2
                        Maybe Int
Nothing -> SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"#"
                                  Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> TokType -> Text -> Tok
Tok (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos Int
1) TokType
Symbol Text
"#"
                                  Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos Int
1) Text
t3
             Maybe (Char, Text)
_ ->
               let (Text
t1, Text
t2) = (Char -> Bool) -> Text -> (Text, Text)
T.span (\Char
d -> Char
d Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'0' Bool -> Bool -> Bool
&& Char
d Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'9') Text
rest
               in  case Text -> Maybe Int
forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
t1 of
                        Just Int
i ->
                           SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos (Int -> TokType
Arg Int
i) (Text
"#" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t1)
                           Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos (Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Text -> Int
T.length Text
t1)) Text
t2
                        Maybe Int
Nothing -> SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"#"
                                  Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos Int
1) Text
rest
         | Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'^' ->
           case Text -> Maybe (Char, Text)
T.uncons Text
rest of
                Just (Char
'^', Text
rest') ->
                  case Text -> Maybe (Char, Text)
T.uncons Text
rest' of
                       Just (Char
d, Text
rest'')
                         | Char -> Bool
isLowerHex Char
d ->
                           case Text -> Maybe (Char, Text)
T.uncons Text
rest'' of
                                Just (Char
e, Text
rest''') | Char -> Bool
isLowerHex Char
e ->
                                  SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Esc2 ([Char] -> Text
T.pack [Char
'^',Char
'^',Char
d,Char
e])
                                  Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos Int
4) Text
rest'''
                                Maybe (Char, Text)
_ ->
                                  SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Esc1 ([Char] -> Text
T.pack [Char
'^',Char
'^',Char
d])
                                  Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos Int
3) Text
rest''
                         | Char
d Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
< Char
'\128' ->
                                  SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Esc1 ([Char] -> Text
T.pack [Char
'^',Char
'^',Char
d])
                                  Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos Int
3) Text
rest''
                       Maybe (Char, Text)
_ -> SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"^" Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
:
                            SourcePos -> TokType -> Text -> Tok
Tok (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos Int
1) TokType
Symbol Text
"^" Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
:
                            SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos Int
2) Text
rest'
                Maybe (Char, Text)
_ -> SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"^"
                     Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos Int
1) Text
rest
         | Bool
otherwise ->
           SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol (Char -> Text
T.singleton Char
c) Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> Text -> [Tok]
totoks (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos Int
1) Text
rest

isSpaceOrTab :: Char -> Bool
isSpaceOrTab :: Char -> Bool
isSpaceOrTab Char
' '  = Bool
True
isSpaceOrTab Char
'\t' = Bool
True
isSpaceOrTab Char
_    = Bool
False

isLetterOrAt :: Char -> Bool
isLetterOrAt :: Char -> Bool
isLetterOrAt Char
'@' = Bool
True
isLetterOrAt Char
c   = Char -> Bool
isLetter Char
c

isLowerHex :: Char -> Bool
isLowerHex :: Char -> Bool
isLowerHex Char
x = Char
x Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'0' Bool -> Bool -> Bool
&& Char
x Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'9' Bool -> Bool -> Bool
|| Char
x Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'a' Bool -> Bool -> Bool
&& Char
x Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'f'

untokenize :: [Tok] -> Text
untokenize :: [Tok] -> Text
untokenize = (Tok -> Text -> Text) -> Text -> [Tok] -> Text
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Tok -> Text -> Text
untokenAccum Text
forall a. Monoid a => a
mempty

untokenAccum :: Tok -> Text -> Text
untokenAccum :: Tok -> Text -> Text
untokenAccum (Tok SourcePos
_ (CtrlSeq Text
_) Text
t) Text
accum =
  -- insert space to prevent breaking a control sequence; see #5836
  case (Text -> Maybe (Text, Char)
T.unsnoc Text
t, Text -> Maybe (Char, Text)
T.uncons Text
accum) of
    (Just (Text
_,Char
c), Just (Char
d,Text
_))
      | Char -> Bool
isLetter Char
c
      , Char -> Bool
isLetter Char
d
      -> Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
accum
    (Maybe (Text, Char), Maybe (Char, Text))
_ -> Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
accum
untokenAccum (Tok SourcePos
_ TokType
_ Text
t) Text
accum = Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
accum

untoken :: Tok -> Text
untoken :: Tok -> Text
untoken Tok
t = Tok -> Text -> Text
untokenAccum Tok
t Text
forall a. Monoid a => a
mempty

parseFromToks :: PandocMonad m => LP m a -> [Tok] -> LP m a
parseFromToks :: forall (m :: * -> *) a. PandocMonad m => LP m a -> [Tok] -> LP m a
parseFromToks LP m a
parser [Tok]
toks = do
  TokStream
oldInput <- ParsecT TokStream LaTeXState m TokStream
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
  TokStream -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput (TokStream -> ParsecT TokStream LaTeXState m ())
-> TokStream -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ Bool -> [Tok] -> TokStream
TokStream Bool
False [Tok]
toks
  SourcePos
oldpos <- ParsecT TokStream LaTeXState m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
  case [Tok]
toks of
     Tok SourcePos
pos TokType
_ Text
_ : [Tok]
_ -> SourcePos -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) s u. Monad m => SourcePos -> ParsecT s u m ()
setPosition SourcePos
pos
     [Tok]
_ -> () -> ParsecT TokStream LaTeXState m ()
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  a
result <- LP m a -> LP m a
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
disablingWithRaw LP m a
parser
  TokStream -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput TokStream
oldInput
  SourcePos -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) s u. Monad m => SourcePos -> ParsecT s u m ()
setPosition SourcePos
oldpos
  a -> LP m a
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
result

disablingWithRaw :: PandocMonad m => LP m a -> LP m a
disablingWithRaw :: forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
disablingWithRaw LP m a
parser = do
  Bool
oldEnableWithRaw <- LaTeXState -> Bool
sEnableWithRaw (LaTeXState -> Bool)
-> ParsecT TokStream LaTeXState m LaTeXState
-> ParsecT TokStream LaTeXState m Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ())
-> (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
st -> LaTeXState
st{ sEnableWithRaw = False }
  a
result <- LP m a
parser
  (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ())
-> (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
st -> LaTeXState
st{ sEnableWithRaw = oldEnableWithRaw }
  a -> LP m a
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
result

satisfyTok :: PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok :: forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
f = do
    LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
doMacros -- apply macros on remaining input stream
    Tok
res <- (Tok -> [Char])
-> (SourcePos -> Tok -> TokStream -> SourcePos)
-> (Tok -> Maybe Tok)
-> LP m Tok
forall s (m :: * -> *) t a u.
Stream s m t =>
(t -> [Char])
-> (SourcePos -> t -> s -> SourcePos)
-> (t -> Maybe a)
-> ParsecT s u m a
tokenPrim (Text -> [Char]
T.unpack (Text -> [Char]) -> (Tok -> Text) -> Tok -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tok -> Text
untoken) SourcePos -> Tok -> TokStream -> SourcePos
updatePos Tok -> Maybe Tok
matcher
    (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
st ->
      if LaTeXState -> Bool
sEnableWithRaw LaTeXState
st
         then
           let !newraws :: IntMap [Tok]
newraws = ([Tok] -> [Tok]) -> IntMap [Tok] -> IntMap [Tok]
forall a b. (a -> b) -> IntMap a -> IntMap b
IntMap.map (Tok
resTok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
:) (IntMap [Tok] -> IntMap [Tok]) -> IntMap [Tok] -> IntMap [Tok]
forall a b. (a -> b) -> a -> b
$! LaTeXState -> IntMap [Tok]
sRawTokens LaTeXState
st
            in  LaTeXState
st{ sRawTokens = newraws }
         else LaTeXState
st
    Tok -> LP m Tok
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Tok -> LP m Tok) -> Tok -> LP m Tok
forall a b. (a -> b) -> a -> b
$! Tok
res
  where matcher :: Tok -> Maybe Tok
matcher Tok
t | Tok -> Bool
f Tok
t       = Tok -> Maybe Tok
forall a. a -> Maybe a
Just Tok
t
                  | Bool
otherwise = Maybe Tok
forall a. Maybe a
Nothing
        updatePos :: SourcePos -> Tok -> TokStream -> SourcePos
        updatePos :: SourcePos -> Tok -> TokStream -> SourcePos
updatePos SourcePos
_spos Tok
_ (TokStream Bool
_ (Tok SourcePos
pos TokType
_ Text
_ : [Tok]
_)) = SourcePos
pos
        updatePos SourcePos
spos (Tok SourcePos
_ TokType
_ Text
t) TokStream
_ = SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
spos (Text -> Int
T.length Text
t)

peekTok :: PandocMonad m => LP m Tok
peekTok :: forall (m :: * -> *). PandocMonad m => LP m Tok
peekTok = do
  LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
doMacros
  LP m Tok -> LP m Tok
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead ((Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok (Bool -> Tok -> Bool
forall a b. a -> b -> a
const Bool
True))

doMacros :: PandocMonad m => LP m ()
doMacros :: forall (m :: * -> *). PandocMonad m => LP m ()
doMacros = do
  TokStream Bool
macrosExpanded [Tok]
toks <- ParsecT TokStream LaTeXState m TokStream
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
  Bool -> LP m () -> LP m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
macrosExpanded (LP m () -> LP m ()) -> LP m () -> LP m ()
forall a b. (a -> b) -> a -> b
$ do
    LaTeXState
st <- ParsecT TokStream LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
    Bool -> LP m () -> LP m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (LaTeXState -> Bool
sVerbatimMode LaTeXState
st) (LP m () -> LP m ()) -> LP m () -> LP m ()
forall a b. (a -> b) -> a -> b
$
      Int -> [Tok] -> LP m [Tok]
forall (m :: * -> *). PandocMonad m => Int -> [Tok] -> LP m [Tok]
doMacros' Int
1 [Tok]
toks LP m [Tok] -> ([Tok] -> LP m ()) -> LP m ()
forall a b.
ParsecT TokStream LaTeXState m a
-> (a -> ParsecT TokStream LaTeXState m b)
-> ParsecT TokStream LaTeXState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= TokStream -> LP m ()
forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput (TokStream -> LP m ()) -> ([Tok] -> TokStream) -> [Tok] -> LP m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> [Tok] -> TokStream
TokStream Bool
True

doMacros' :: PandocMonad m => Int -> [Tok] -> LP m [Tok]
doMacros' :: forall (m :: * -> *). PandocMonad m => Int -> [Tok] -> LP m [Tok]
doMacros' Int
n [Tok]
inp =
  case [Tok]
inp of
     Tok SourcePos
spos (CtrlSeq Text
"begin") Text
_ : Tok SourcePos
_ TokType
Symbol Text
"{" :
      Tok SourcePos
_ TokType
Word Text
name : Tok SourcePos
_ TokType
Symbol Text
"}" : [Tok]
ts
        -> Int -> SourcePos -> Text -> [Tok] -> LP m [Tok]
forall {m :: * -> *}.
PandocMonad m =>
Int
-> SourcePos
-> Text
-> [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
handleMacros Int
n SourcePos
spos Text
name [Tok]
ts LP m [Tok] -> LP m [Tok] -> LP m [Tok]
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Tok] -> LP m [Tok]
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Tok]
inp
     Tok SourcePos
spos (CtrlSeq Text
"end") Text
_ : Tok SourcePos
_ TokType
Symbol Text
"{" :
      Tok SourcePos
_ TokType
Word Text
name : Tok SourcePos
_ TokType
Symbol Text
"}" : [Tok]
ts
        -> Int -> SourcePos -> Text -> [Tok] -> LP m [Tok]
forall {m :: * -> *}.
PandocMonad m =>
Int
-> SourcePos
-> Text
-> [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
handleMacros Int
n SourcePos
spos (Text
"end" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name) [Tok]
ts LP m [Tok] -> LP m [Tok] -> LP m [Tok]
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Tok] -> LP m [Tok]
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Tok]
inp
     Tok SourcePos
_ (CtrlSeq Text
"expandafter") Text
_ : Tok
t : [Tok]
ts
        -> Tok -> [Tok] -> [Tok]
combineTok Tok
t ([Tok] -> [Tok]) -> LP m [Tok] -> LP m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> [Tok] -> LP m [Tok]
forall (m :: * -> *). PandocMonad m => Int -> [Tok] -> LP m [Tok]
doMacros' Int
n [Tok]
ts
     Tok SourcePos
spos (CtrlSeq Text
name) Text
_ : [Tok]
ts
        -> Int -> SourcePos -> Text -> [Tok] -> LP m [Tok]
forall {m :: * -> *}.
PandocMonad m =>
Int
-> SourcePos
-> Text
-> [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
handleMacros Int
n SourcePos
spos Text
name [Tok]
ts LP m [Tok] -> LP m [Tok] -> LP m [Tok]
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Tok] -> LP m [Tok]
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Tok]
inp
     [Tok]
_ -> [Tok] -> LP m [Tok]
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Tok]
inp

  where
    combineTok :: Tok -> [Tok] -> [Tok]
combineTok (Tok SourcePos
spos (CtrlSeq Text
name) Text
x) (Tok SourcePos
_ TokType
Word Text
w : [Tok]
ts)
      | (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isLetterOrAt Text
w =
        SourcePos -> TokType -> Text -> Tok
Tok SourcePos
spos (Text -> TokType
CtrlSeq (Text
name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
w)) (Text
x1 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
w Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
x2) Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: [Tok]
ts
          where (Text
x1, Text
x2) = (Char -> Bool) -> Text -> (Text, Text)
T.break Char -> Bool
isSpaceOrTab Text
x
    combineTok Tok
t [Tok]
ts = Tok
tTok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
:[Tok]
ts

    matchTok :: Tok -> LP m Tok
matchTok (Tok SourcePos
_ TokType
toktype Text
txt) =
      (Tok -> Bool) -> LP 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
toktype' Bool -> Bool -> Bool
&&
                    Text
txt Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
txt')

    matchPattern :: t Tok -> ParsecT TokStream LaTeXState m ()
matchPattern t Tok
toks = ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m ()
 -> ParsecT TokStream LaTeXState m ())
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ (Tok -> ParsecT TokStream LaTeXState m Tok)
-> t Tok -> ParsecT TokStream LaTeXState m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Tok -> ParsecT TokStream LaTeXState m Tok
forall {m :: * -> *}. PandocMonad m => Tok -> LP m Tok
matchTok t Tok
toks

    getargs :: Map Int [Tok]
-> [ArgSpec] -> ParsecT TokStream LaTeXState m (Map Int [Tok])
getargs Map Int [Tok]
argmap [] = Map Int [Tok] -> ParsecT TokStream LaTeXState m (Map Int [Tok])
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Map Int [Tok]
argmap
    getargs Map Int [Tok]
argmap (Pattern [Tok]
toks : [ArgSpec]
rest) = ParsecT TokStream LaTeXState m (Map Int [Tok])
-> ParsecT TokStream LaTeXState m (Map Int [Tok])
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m (Map Int [Tok])
 -> ParsecT TokStream LaTeXState m (Map Int [Tok]))
-> ParsecT TokStream LaTeXState m (Map Int [Tok])
-> ParsecT TokStream LaTeXState m (Map Int [Tok])
forall a b. (a -> b) -> a -> b
$ do
       [Tok] -> ParsecT TokStream LaTeXState m ()
forall {t :: * -> *} {m :: * -> *}.
(Foldable t, PandocMonad m) =>
t Tok -> ParsecT TokStream LaTeXState m ()
matchPattern [Tok]
toks
       Map Int [Tok]
-> [ArgSpec] -> ParsecT TokStream LaTeXState m (Map Int [Tok])
getargs Map Int [Tok]
argmap [ArgSpec]
rest
    getargs Map Int [Tok]
argmap (ArgNum Int
i : Pattern [Tok]
toks : [ArgSpec]
rest) =
      ParsecT TokStream LaTeXState m (Map Int [Tok])
-> ParsecT TokStream LaTeXState m (Map Int [Tok])
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m (Map Int [Tok])
 -> ParsecT TokStream LaTeXState m (Map Int [Tok]))
-> ParsecT TokStream LaTeXState m (Map Int [Tok])
-> ParsecT TokStream LaTeXState m (Map Int [Tok])
forall a b. (a -> b) -> a -> b
$ do
        [Tok]
x <- [[Tok]] -> [Tok]
forall a. Monoid a => [a] -> a
mconcat ([[Tok]] -> [Tok])
-> ParsecT TokStream LaTeXState m [[Tok]]
-> ParsecT TokStream LaTeXState m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m [[Tok]]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill (ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ((Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
:[]) (Tok -> [Tok])
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok))
                  ([Tok] -> ParsecT TokStream LaTeXState m ()
forall {t :: * -> *} {m :: * -> *}.
(Foldable t, PandocMonad m) =>
t Tok -> ParsecT TokStream LaTeXState m ()
matchPattern [Tok]
toks)
        Map Int [Tok]
-> [ArgSpec] -> ParsecT TokStream LaTeXState m (Map Int [Tok])
getargs (Int -> [Tok] -> Map Int [Tok] -> Map Int [Tok]
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Int
i [Tok]
x Map Int [Tok]
argmap) [ArgSpec]
rest
    getargs Map Int [Tok]
argmap (ArgNum Int
i : [ArgSpec]
rest) = do
      [Tok]
x <- ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m [Tok]
 -> ParsecT TokStream LaTeXState m [Tok])
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
forall a b. (a -> b) -> a -> b
$ ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracedOrToken
      Map Int [Tok]
-> [ArgSpec] -> ParsecT TokStream LaTeXState m (Map Int [Tok])
getargs (Int -> [Tok] -> Map Int [Tok] -> Map Int [Tok]
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Int
i [Tok]
x Map Int [Tok]
argmap) [ArgSpec]
rest

    addTok :: Bool -> Map Int (t Tok) -> SourcePos -> Tok -> [Tok] -> [Tok]
addTok Bool
False Map Int (t Tok)
_args SourcePos
spos (Tok SourcePos
_ (DeferredArg Int
i) Text
txt) [Tok]
acc =
      SourcePos -> TokType -> Text -> Tok
Tok SourcePos
spos (Int -> TokType
Arg Int
i) Text
txt Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: [Tok]
acc
    addTok Bool
False Map Int (t Tok)
args SourcePos
spos (Tok SourcePos
_ (Arg Int
i) Text
_) [Tok]
acc =
       case Int -> Map Int (t Tok) -> Maybe (t Tok)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Int
i Map Int (t Tok)
args of
            Maybe (t Tok)
Nothing -> [Tok]
forall a. [a]
forall (m :: * -> *) a. MonadPlus m => m a
mzero
            Just t Tok
xs -> (Tok -> [Tok] -> [Tok]) -> [Tok] -> t Tok -> [Tok]
forall a b. (a -> b -> b) -> b -> t a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Bool -> Map Int (t Tok) -> SourcePos -> Tok -> [Tok] -> [Tok]
addTok Bool
True Map Int (t Tok)
args SourcePos
spos) [Tok]
acc t Tok
xs
    -- see #4007
    addTok Bool
_ Map Int (t Tok)
_ SourcePos
spos (Tok SourcePos
_ (CtrlSeq Text
x) Text
txt)
           acc :: [Tok]
acc@(Tok SourcePos
_ TokType
Word Text
_ : [Tok]
_)
      | Bool -> Bool
not (Text -> Bool
T.null Text
txt)
      , Char -> Bool
isLetter (HasCallStack => Text -> Char
Text -> Char
T.last Text
txt) =
        SourcePos -> TokType -> Text -> Tok
Tok SourcePos
spos (Text -> TokType
CtrlSeq Text
x) (Text
txt Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" ") Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: [Tok]
acc
    addTok Bool
_ Map Int (t Tok)
_ SourcePos
spos Tok
t [Tok]
acc = SourcePos -> Tok -> Tok
setpos SourcePos
spos Tok
t Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: [Tok]
acc

    handleMacros :: Int
-> SourcePos
-> Text
-> [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
handleMacros Int
n' SourcePos
spos Text
name [Tok]
ts = do
      Bool
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
n' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
20)  -- detect macro expansion loops
        (ParsecT TokStream LaTeXState m ()
 -> ParsecT TokStream LaTeXState m ())
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ PandocError -> ParsecT TokStream LaTeXState m ()
forall a. PandocError -> ParsecT TokStream LaTeXState m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (PandocError -> ParsecT TokStream LaTeXState m ())
-> PandocError -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocMacroLoop Text
name
      (Map Text Macro
macros :| [Map Text Macro]
_ ) <- LaTeXState -> NonEmpty (Map Text Macro)
sMacros (LaTeXState -> NonEmpty (Map Text Macro))
-> ParsecT TokStream LaTeXState m LaTeXState
-> ParsecT TokStream LaTeXState m (NonEmpty (Map Text Macro))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream 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
           Maybe Macro
Nothing -> Text -> [Tok] -> ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => Text -> [Tok] -> LP m [Tok]
trySpecialMacro Text
name [Tok]
ts
           Just (Macro MacroScope
_scope ExpansionPoint
expansionPoint [ArgSpec]
argspecs Maybe [Tok]
optarg [Tok]
newtoks) -> do
             let getargs' :: ParsecT TokStream LaTeXState m (Map Int [Tok], [Tok])
getargs' = do
                   Map Int [Tok]
args <-
                     (case ExpansionPoint
expansionPoint of
                        ExpansionPoint
ExpandWhenUsed    -> LP m (Map Int [Tok]) -> LP m (Map Int [Tok])
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
withVerbatimMode
                        ExpansionPoint
ExpandWhenDefined -> LP m (Map Int [Tok]) -> LP m (Map Int [Tok])
forall a. a -> a
id)
                     (LP m (Map Int [Tok]) -> LP m (Map Int [Tok]))
-> LP m (Map Int [Tok]) -> LP m (Map Int [Tok])
forall a b. (a -> b) -> a -> b
$ case Maybe [Tok]
optarg of
                             Maybe [Tok]
Nothing -> Map Int [Tok] -> [ArgSpec] -> LP m (Map Int [Tok])
forall {m :: * -> *}.
PandocMonad m =>
Map Int [Tok]
-> [ArgSpec] -> ParsecT TokStream LaTeXState m (Map Int [Tok])
getargs Map Int [Tok]
forall k a. Map k a
M.empty [ArgSpec]
argspecs
                             Just [Tok]
o  -> do
                                [Tok]
x <- [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option [Tok]
o ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracketedToks
                                Map Int [Tok] -> [ArgSpec] -> LP m (Map Int [Tok])
forall {m :: * -> *}.
PandocMonad m =>
Map Int [Tok]
-> [ArgSpec] -> ParsecT TokStream LaTeXState m (Map Int [Tok])
getargs (Int -> [Tok] -> Map Int [Tok]
forall k a. k -> a -> Map k a
M.singleton Int
1 [Tok]
x) ([ArgSpec] -> LP m (Map Int [Tok]))
-> [ArgSpec] -> LP m (Map Int [Tok])
forall a b. (a -> b) -> a -> b
$ Int -> [ArgSpec] -> [ArgSpec]
forall a. Int -> [a] -> [a]
drop Int
1 [ArgSpec]
argspecs
                   TokStream Bool
_ [Tok]
rest <- ParsecT TokStream LaTeXState m TokStream
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
                   (Map Int [Tok], [Tok])
-> ParsecT TokStream LaTeXState m (Map Int [Tok], [Tok])
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Map Int [Tok]
args, [Tok]
rest)
             LaTeXState
lstate <- ParsecT TokStream LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
             Either ParseError (Map Int [Tok], [Tok])
res <- m (Either ParseError (Map Int [Tok], [Tok]))
-> ParsecT
     TokStream LaTeXState m (Either ParseError (Map Int [Tok], [Tok]))
forall (m :: * -> *) a.
Monad m =>
m a -> ParsecT TokStream LaTeXState m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either ParseError (Map Int [Tok], [Tok]))
 -> ParsecT
      TokStream LaTeXState m (Either ParseError (Map Int [Tok], [Tok])))
-> m (Either ParseError (Map Int [Tok], [Tok]))
-> ParsecT
     TokStream LaTeXState m (Either ParseError (Map Int [Tok], [Tok]))
forall a b. (a -> b) -> a -> b
$ ParsecT TokStream LaTeXState m (Map Int [Tok], [Tok])
-> LaTeXState
-> [Char]
-> TokStream
-> m (Either ParseError (Map Int [Tok], [Tok]))
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> u -> [Char] -> s -> m (Either ParseError a)
runParserT ParsecT TokStream LaTeXState m (Map Int [Tok], [Tok])
getargs' LaTeXState
lstate [Char]
"args" (TokStream -> m (Either ParseError (Map Int [Tok], [Tok])))
-> TokStream -> m (Either ParseError (Map Int [Tok], [Tok]))
forall a b. (a -> b) -> a -> b
$ Bool -> [Tok] -> TokStream
TokStream Bool
False [Tok]
ts
             case Either ParseError (Map Int [Tok], [Tok])
res of
               Left ParseError
_ -> [Char] -> ParsecT TokStream LaTeXState m [Tok]
forall a. [Char] -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
Prelude.fail ([Char] -> ParsecT TokStream LaTeXState m [Tok])
-> [Char] -> ParsecT TokStream LaTeXState m [Tok]
forall a b. (a -> b) -> a -> b
$ [Char]
"Could not parse arguments for " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++
                                Text -> [Char]
T.unpack Text
name
               Right (Map Int [Tok]
args, [Tok]
rest) -> do
                 -- first boolean param is true if we're tokenizing
                 -- an argument (in which case we don't want to
                 -- expand #1 etc.)
                 let result :: [Tok]
result = (Tok -> [Tok] -> [Tok]) -> [Tok] -> [Tok] -> [Tok]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Bool -> Map Int [Tok] -> SourcePos -> Tok -> [Tok] -> [Tok]
forall {t :: * -> *}.
Foldable t =>
Bool -> Map Int (t Tok) -> SourcePos -> Tok -> [Tok] -> [Tok]
addTok Bool
False Map Int [Tok]
args SourcePos
spos) [Tok]
rest [Tok]
newtoks
                 case ExpansionPoint
expansionPoint of
                   ExpansionPoint
ExpandWhenUsed    -> Int -> [Tok] -> ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => Int -> [Tok] -> LP m [Tok]
doMacros' (Int
n' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [Tok]
result
                   ExpansionPoint
ExpandWhenDefined -> [Tok] -> ParsecT TokStream LaTeXState m [Tok]
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Tok]
result

-- | Certain macros do low-level tex manipulations that can't
-- be represented in our Macro type, so we handle them here.
trySpecialMacro :: PandocMonad m => Text -> [Tok] -> LP m [Tok]
trySpecialMacro :: forall (m :: * -> *). PandocMonad m => Text -> [Tok] -> LP m [Tok]
trySpecialMacro Text
"xspace" [Tok]
ts = do
  [Tok]
ts' <- Int -> [Tok] -> LP m [Tok]
forall (m :: * -> *). PandocMonad m => Int -> [Tok] -> LP m [Tok]
doMacros' Int
1 [Tok]
ts
  case [Tok]
ts' of
    Tok SourcePos
pos TokType
Word Text
t : [Tok]
_
      | Text -> Bool
startsWithAlphaNum Text
t -> [Tok] -> LP m [Tok]
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Tok] -> LP m [Tok]) -> [Tok] -> LP m [Tok]
forall a b. (a -> b) -> a -> b
$ SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Spaces Text
" " Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: [Tok]
ts'
    [Tok]
_ -> [Tok] -> LP m [Tok]
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Tok]
ts'
trySpecialMacro Text
"iftrue" [Tok]
ts = Bool -> [Tok] -> LP m [Tok]
forall (m :: * -> *). PandocMonad m => Bool -> [Tok] -> LP m [Tok]
handleIf Bool
True [Tok]
ts
trySpecialMacro Text
"iffalse" [Tok]
ts = Bool -> [Tok] -> LP m [Tok]
forall (m :: * -> *). PandocMonad m => Bool -> [Tok] -> LP m [Tok]
handleIf Bool
False [Tok]
ts
trySpecialMacro Text
_ [Tok]
_ = LP m [Tok]
forall a. ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. MonadPlus m => m a
mzero

handleIf :: PandocMonad m => Bool -> [Tok] -> LP m [Tok]
handleIf :: forall (m :: * -> *). PandocMonad m => Bool -> [Tok] -> LP m [Tok]
handleIf Bool
b [Tok]
ts = do
  Either ParseError [Tok]
res' <- m (Either ParseError [Tok])
-> ParsecT TokStream LaTeXState m (Either ParseError [Tok])
forall (m :: * -> *) a.
Monad m =>
m a -> ParsecT TokStream LaTeXState m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either ParseError [Tok])
 -> ParsecT TokStream LaTeXState m (Either ParseError [Tok]))
-> m (Either ParseError [Tok])
-> ParsecT TokStream LaTeXState m (Either ParseError [Tok])
forall a b. (a -> b) -> a -> b
$ LP m [Tok]
-> LaTeXState -> [Char] -> TokStream -> m (Either ParseError [Tok])
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> u -> [Char] -> s -> m (Either ParseError a)
runParserT (Bool -> LP m [Tok]
forall (m :: * -> *). PandocMonad m => Bool -> LP m [Tok]
ifParser Bool
b) LaTeXState
defaultLaTeXState [Char]
"tokens"
               (TokStream -> m (Either ParseError [Tok]))
-> TokStream -> m (Either ParseError [Tok])
forall a b. (a -> b) -> a -> b
$ Bool -> [Tok] -> TokStream
TokStream Bool
False [Tok]
ts
  case Either ParseError [Tok]
res' of
    Left ParseError
_ -> [Char] -> LP m [Tok]
forall a. [Char] -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
Prelude.fail [Char]
"Could not parse conditional"
    Right [Tok]
ts' -> [Tok] -> LP m [Tok]
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Tok]
ts'

ifParser :: PandocMonad m => Bool -> LP m [Tok]
ifParser :: forall (m :: * -> *). PandocMonad m => Bool -> LP m [Tok]
ifParser Bool
b = do
  [Tok]
ifToks <- ParsecT TokStream LaTeXState m Tok -> LP m [Tok]
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy (Text -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"else" ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"fi")
                    ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok)
  [Tok]
elseToks <- (Text -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"else" ParsecT TokStream LaTeXState m Tok -> LP m [Tok] -> LP m [Tok]
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok -> LP m [Tok]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok (Text -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"fi"))
                 LP m [Tok] -> LP m [Tok] -> LP m [Tok]
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ([] [Tok] -> ParsecT TokStream LaTeXState m Tok -> LP m [Tok]
forall a b.
a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"fi")
  TokStream Bool
_ [Tok]
rest <- ParsecT TokStream LaTeXState m TokStream
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
  [Tok] -> LP m [Tok]
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Tok] -> LP m [Tok]) -> [Tok] -> LP m [Tok]
forall a b. (a -> b) -> a -> b
$ (if Bool
b then [Tok]
ifToks else [Tok]
elseToks) [Tok] -> [Tok] -> [Tok]
forall a. [a] -> [a] -> [a]
++ [Tok]
rest

startsWithAlphaNum :: Text -> Bool
startsWithAlphaNum :: Text -> Bool
startsWithAlphaNum Text
t =
  case Text -> Maybe (Char, Text)
T.uncons Text
t of
       Just (Char
c, Text
_) | Char -> Bool
isAlphaNum Char
c -> Bool
True
       Maybe (Char, Text)
_           -> Bool
False

setpos :: SourcePos -> Tok -> Tok
setpos :: SourcePos -> Tok -> Tok
setpos SourcePos
spos (Tok SourcePos
_ TokType
tt Text
txt) = SourcePos -> TokType -> Text -> Tok
Tok SourcePos
spos TokType
tt Text
txt

anyControlSeq :: PandocMonad m => LP m Tok
anyControlSeq :: forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq = (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isCtrlSeq

isCtrlSeq :: Tok -> Bool
isCtrlSeq :: Tok -> Bool
isCtrlSeq (Tok SourcePos
_ (CtrlSeq Text
_) Text
_) = Bool
True
isCtrlSeq Tok
_                     = Bool
False

anySymbol :: PandocMonad m => LP m Tok
anySymbol :: forall (m :: * -> *). PandocMonad m => LP m Tok
anySymbol = (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isSymbolTok

isSymbolTok :: Tok -> Bool
isSymbolTok :: Tok -> Bool
isSymbolTok (Tok SourcePos
_ TokType
Symbol Text
_) = Bool
True
isSymbolTok Tok
_                = Bool
False

isWordTok :: Tok -> Bool
isWordTok :: Tok -> Bool
isWordTok (Tok SourcePos
_ TokType
Word Text
_) = Bool
True
isWordTok Tok
_              = Bool
False

isArgTok :: Tok -> Bool
isArgTok :: Tok -> Bool
isArgTok (Tok SourcePos
_ (Arg Int
_) Text
_) = Bool
True
isArgTok Tok
_                 = Bool
False

infile :: PandocMonad m => SourceName -> LP m Tok
infile :: forall (m :: * -> *). PandocMonad m => [Char] -> LP m Tok
infile [Char]
reference = (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok (\(Tok SourcePos
source TokType
_ Text
_) -> (SourcePos -> [Char]
sourceName SourcePos
source) [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
reference)

spaces :: PandocMonad m => LP m ()
spaces :: forall (m :: * -> *). PandocMonad m => LP m ()
spaces = ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany ((Tok -> Bool) -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok ([TokType] -> Tok -> Bool
tokTypeIn [TokType
Comment, TokType
Spaces, TokType
Newline]))

spaces1 :: PandocMonad m => LP m ()
spaces1 :: forall (m :: * -> *). PandocMonad m => LP m ()
spaces1 = ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
skipMany1 ((Tok -> Bool) -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok ([TokType] -> Tok -> Bool
tokTypeIn [TokType
Comment, TokType
Spaces, TokType
Newline]))

tokTypeIn :: [TokType] -> Tok -> Bool
tokTypeIn :: [TokType] -> Tok -> Bool
tokTypeIn [TokType]
toktypes (Tok SourcePos
_ TokType
tt Text
_) = TokType
tt TokType -> [TokType] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [TokType]
toktypes

controlSeq :: PandocMonad m => Text -> LP m Tok
controlSeq :: forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
name = (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isNamed
  where isNamed :: Tok -> Bool
isNamed (Tok SourcePos
_ (CtrlSeq Text
n) Text
_) = Text
n Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
name
        isNamed Tok
_                     = Bool
False

symbol :: PandocMonad m => Char -> LP m Tok
symbol :: forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
c = (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isc
  where isc :: Tok -> Bool
isc (Tok SourcePos
_ TokType
Symbol Text
d) = case Text -> Maybe (Char, Text)
T.uncons Text
d of
                                    Just (Char
c',Text
_) -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
c'
                                    Maybe (Char, Text)
_           -> Bool
False
        isc Tok
_ = Bool
False

symbolIn :: PandocMonad m => [Char] -> LP m Tok
symbolIn :: forall (m :: * -> *). PandocMonad m => [Char] -> LP m Tok
symbolIn [Char]
cs = (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isInCs
  where isInCs :: Tok -> Bool
isInCs (Tok SourcePos
_ TokType
Symbol Text
d) = case Text -> Maybe (Char, Text)
T.uncons Text
d of
                                       Just (Char
c,Text
_) -> Char
c Char -> [Char] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Char]
cs
                                       Maybe (Char, Text)
_          -> Bool
False
        isInCs Tok
_ = Bool
False

sp :: PandocMonad m => LP m ()
sp :: forall (m :: * -> *). PandocMonad m => LP m ()
sp = do
  LP m () -> LP m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional (LP m () -> LP m ()) -> LP m () -> LP m ()
forall a b. (a -> b) -> a -> b
$ LP m () -> LP m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany (LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
whitespace LP m () -> LP m () -> LP m ()
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
comment)
  LP m () -> LP m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional (LP m () -> LP m ()) -> LP m () -> LP m ()
forall a b. (a -> b) -> a -> b
$ LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
endline  LP m () -> LP m () -> LP m ()
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> LP m () -> LP m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany (LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
whitespace LP m () -> LP m () -> LP m ()
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
comment)

whitespace :: PandocMonad m => LP m ()
whitespace :: forall (m :: * -> *). PandocMonad m => LP m ()
whitespace = () ()
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m ()
forall a b.
a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Tok -> Bool) -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isSpaceTok

isSpaceTok :: Tok -> Bool
isSpaceTok :: Tok -> Bool
isSpaceTok (Tok SourcePos
_ TokType
Spaces Text
_) = Bool
True
isSpaceTok Tok
_                = Bool
False

newlineTok :: PandocMonad m => LP m ()
newlineTok :: forall (m :: * -> *). PandocMonad m => LP m ()
newlineTok = () ()
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m ()
forall a b.
a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Tok -> Bool) -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isNewlineTok

isNewlineTok :: Tok -> Bool
isNewlineTok :: Tok -> Bool
isNewlineTok (Tok SourcePos
_ TokType
Newline Text
_) = Bool
True
isNewlineTok Tok
_                 = Bool
False

comment :: PandocMonad m => LP m ()
comment :: forall (m :: * -> *). PandocMonad m => LP m ()
comment = () ()
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m ()
forall a b.
a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Tok -> Bool) -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isCommentTok

isCommentTok :: Tok -> Bool
isCommentTok :: Tok -> Bool
isCommentTok (Tok SourcePos
_ TokType
Comment Text
_) = Bool
True
isCommentTok Tok
_                 = Bool
False

anyTok :: PandocMonad m => LP m Tok
anyTok :: forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok = (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok (Bool -> Tok -> Bool
forall a b. a -> b -> a
const Bool
True)

singleCharTok :: PandocMonad m => LP m Tok
singleCharTok :: forall (m :: * -> *). PandocMonad m => LP m Tok
singleCharTok =
  (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok ((Tok -> Bool) -> LP m Tok) -> (Tok -> Bool) -> LP m Tok
forall a b. (a -> b) -> a -> b
$ \case
     Tok SourcePos
_ TokType
Word  Text
t   -> Text -> Int
T.length Text
t Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1
     Tok SourcePos
_ TokType
Symbol Text
t  -> Bool -> Bool
not ((Char -> Bool) -> Text -> Bool
T.any (Char -> Set Char -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set Char
specialChars) Text
t)
     Tok
_               -> Bool
False

singleChar :: PandocMonad m => LP m Tok
singleChar :: forall (m :: * -> *). PandocMonad m => LP m Tok
singleChar = LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
singleCharTok LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m Tok
singleCharFromWord
 where
  singleCharFromWord :: LP m Tok
singleCharFromWord = do
    Tok SourcePos
pos TokType
toktype Text
t <- LP m Tok -> LP m Tok
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
disablingWithRaw (LP m Tok -> LP m Tok) -> LP m Tok -> LP m Tok
forall a b. (a -> b) -> a -> b
$ (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isWordTok
    let (Text
t1, Text
t2) = (Int -> Text -> Text
T.take Int
1 Text
t, Int -> Text -> Text
T.drop Int
1 Text
t)
    TokStream Bool
macrosExpanded [Tok]
inp <- ParsecT TokStream LaTeXState m TokStream
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
    TokStream -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput (TokStream -> ParsecT TokStream LaTeXState m ())
-> TokStream -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ Bool -> [Tok] -> TokStream
TokStream Bool
macrosExpanded
             ([Tok] -> TokStream) -> [Tok] -> TokStream
forall a b. (a -> b) -> a -> b
$ SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
toktype Text
t1 Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: SourcePos -> TokType -> Text -> Tok
Tok (SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
pos Int
1) TokType
toktype Text
t2 Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: [Tok]
inp
    LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok

specialChars :: Set.Set Char
specialChars :: Set Char
specialChars = [Char] -> Set Char
forall a. Ord a => [a] -> Set a
Set.fromList [Char]
"#$%&~_^\\{}"

endline :: PandocMonad m => LP m ()
endline :: forall (m :: * -> *). PandocMonad m => LP m ()
endline = ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m ()
 -> ParsecT TokStream LaTeXState m ())
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ do
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
newlineTok
  ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok
  ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
blankline

blankline :: PandocMonad m => LP m ()
blankline :: forall (m :: * -> *). PandocMonad m => LP m ()
blankline = ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m ()
 -> ParsecT TokStream LaTeXState m ())
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
whitespace ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
newlineTok

primEscape :: PandocMonad m => LP m Char
primEscape :: forall (m :: * -> *). PandocMonad m => LP m Char
primEscape = do
  Tok SourcePos
_ TokType
toktype Text
t <- (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok ([TokType] -> Tok -> Bool
tokTypeIn [TokType
Esc1, TokType
Esc2])
  case TokType
toktype of
       TokType
Esc1 -> case Text -> Maybe (Char, Text)
T.uncons (Int -> Text -> Text
T.drop Int
2 Text
t) of
                    Just (Char
c, Text
_)
                      | Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'\64' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'\127' -> Char -> LP m Char
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Char
chr (Char -> Int
ord Char
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
64))
                      | Bool
otherwise                 -> Char -> LP m Char
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Char
chr (Char -> Int
ord Char
c Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
64))
                    Maybe (Char, Text)
Nothing -> [Char] -> LP m Char
forall a. [Char] -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
Prelude.fail [Char]
"Empty content of Esc1"
       TokType
Esc2 -> case Text -> Maybe Int
forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead (Text
"0x" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
T.drop Int
2 Text
t) of
                    Just Int
x  -> Char -> LP m Char
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Char
chr Int
x)
                    Maybe Int
Nothing -> [Char] -> LP m Char
forall a. [Char] -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
Prelude.fail ([Char] -> LP m Char) -> [Char] -> LP m Char
forall a b. (a -> b) -> a -> b
$ [Char]
"Could not read: " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
t
       TokType
_    -> [Char] -> LP m Char
forall a. [Char] -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
Prelude.fail [Char]
"Expected an Esc1 or Esc2 token" -- should not happen

bgroup :: PandocMonad m => LP m Tok
bgroup :: forall (m :: * -> *). PandocMonad m => LP m Tok
bgroup = ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m Tok
 -> ParsecT TokStream LaTeXState m Tok)
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
forall a b. (a -> b) -> a -> b
$ do
  ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp
  Tok
t <- Char -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{' ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"bgroup" ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"begingroup"
  -- Add a copy of the macro table to the top of the macro stack,
  -- private for this group. We inherit all the macros defined in
  -- the parent group.
  (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ())
-> (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
s -> LaTeXState
s{ sMacros = NonEmpty.cons (NonEmpty.head (sMacros s))
                                                 (sMacros s) }
  Tok -> ParsecT TokStream LaTeXState m Tok
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Tok
t


egroup :: PandocMonad m => LP m Tok
egroup :: forall (m :: * -> *). PandocMonad m => LP m Tok
egroup = do
  Tok
t <- Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'}' LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState 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
"egroup" LP m Tok -> LP m Tok -> LP m Tok
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState 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
"endgroup"
  -- remove the group's macro table from the stack
  (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ())
-> (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
s -> LaTeXState
s{ sMacros = fromMaybe (sMacros s) $
      NonEmpty.nonEmpty (NonEmpty.tail (sMacros s)) }
  Tok -> LP m Tok
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Tok
t

grouped :: (PandocMonad m,  Monoid a) => LP m a -> LP m a
grouped :: forall (m :: * -> *) a.
(PandocMonad m, Monoid a) =>
LP m a -> LP m a
grouped LP m a
parser = LP m a -> LP m a
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (LP m a -> LP m a) -> LP m a -> LP m a
forall a b. (a -> b) -> a -> b
$ do
  LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
bgroup
  -- first we check for an inner 'grouped', because
  -- {{a,b}} should be parsed the same as {a,b}
  LP m a -> LP m a
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (LP m a -> LP m a
forall (m :: * -> *) a.
(PandocMonad m, Monoid a) =>
LP m a -> LP m a
grouped LP m a
parser LP m a -> LP m Tok -> LP m a
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
egroup) LP m a -> LP m a -> LP m a
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ([a] -> a
forall a. Monoid a => [a] -> a
mconcat ([a] -> a) -> ParsecT TokStream LaTeXState m [a] -> LP m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m a -> LP m Tok -> ParsecT TokStream LaTeXState m [a]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill LP m a
parser LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
egroup)

braced' :: PandocMonad m => LP m Tok -> LP m [Tok]
braced' :: forall (m :: * -> *). PandocMonad m => LP m Tok -> LP m [Tok]
braced' LP m Tok
getTok = Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{' LP m Tok
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int -> ParsecT TokStream LaTeXState m [Tok]
forall {t}.
(Ord t, Num t) =>
t -> ParsecT TokStream LaTeXState m [Tok]
go (Int
1 :: Int)
 where
  go :: t -> ParsecT TokStream LaTeXState m [Tok]
go t
n = do
    Tok
t <- LP m Tok
getTok
    case Tok
t of
      Tok SourcePos
_ TokType
Symbol Text
"}"
        | t
n t -> t -> Bool
forall a. Ord a => a -> a -> Bool
> t
1     -> (Tok
tTok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
:) ([Tok] -> [Tok])
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t -> ParsecT TokStream LaTeXState m [Tok]
go (t
n t -> t -> t
forall a. Num a => a -> a -> a
- t
1)
        | Bool
otherwise -> [Tok] -> ParsecT TokStream LaTeXState m [Tok]
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return []
      Tok SourcePos
_ TokType
Symbol Text
"{" -> (Tok
tTok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
:) ([Tok] -> [Tok])
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t -> ParsecT TokStream LaTeXState m [Tok]
go (t
n t -> t -> t
forall a. Num a => a -> a -> a
+ t
1)
      Tok
_ -> (Tok
tTok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
:) ([Tok] -> [Tok])
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t -> ParsecT TokStream LaTeXState m [Tok]
go t
n

braced :: PandocMonad m => LP m [Tok]
braced :: forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced = LP m Tok -> LP m [Tok]
forall (m :: * -> *). PandocMonad m => LP m Tok -> LP m [Tok]
braced' LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok

-- URLs require special handling, because they can contain %
-- characters.  So we retonenize comments as we go...
bracedUrl :: PandocMonad m => LP m [Tok]
bracedUrl :: forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracedUrl = LP m Tok -> LP m [Tok]
forall (m :: * -> *). PandocMonad m => LP m Tok -> LP m [Tok]
braced' (LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
retokenizeComment LP m () -> LP m Tok -> LP m Tok
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok)

-- For handling URLs, which allow literal % characters...
retokenizeComment :: PandocMonad m => LP m ()
retokenizeComment :: forall (m :: * -> *). PandocMonad m => LP m ()
retokenizeComment = (do
  Tok SourcePos
pos TokType
Comment Text
txt <- (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isCommentTok
  let updPos :: Tok -> Tok
updPos (Tok SourcePos
pos' TokType
toktype' Text
txt') =
        SourcePos -> TokType -> Text -> Tok
Tok (SourcePos -> Int -> SourcePos
incSourceColumn (SourcePos -> Int -> SourcePos
incSourceLine SourcePos
pos' (SourcePos -> Int
sourceLine SourcePos
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))
             (SourcePos -> Int
sourceColumn SourcePos
pos)) TokType
toktype' Text
txt'
  let newtoks :: [Tok]
newtoks = (Tok -> Tok) -> [Tok] -> [Tok]
forall a b. (a -> b) -> [a] -> [b]
map Tok -> Tok
updPos ([Tok] -> [Tok]) -> [Tok] -> [Tok]
forall a b. (a -> b) -> a -> b
$ SourcePos -> Text -> [Tok]
tokenize SourcePos
pos (Text -> [Tok]) -> Text -> [Tok]
forall a b. (a -> b) -> a -> b
$ HasCallStack => Text -> Text
Text -> Text
T.tail Text
txt
  TokStream Bool
macrosExpanded [Tok]
ts <- ParsecT TokStream LaTeXState m TokStream
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
  TokStream -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput (TokStream -> ParsecT TokStream LaTeXState m ())
-> TokStream -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ Bool -> [Tok] -> TokStream
TokStream Bool
macrosExpanded ((SourcePos -> TokType -> Text -> Tok
Tok SourcePos
pos TokType
Symbol Text
"%" Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
: [Tok]
newtoks) [Tok] -> [Tok] -> [Tok]
forall a. [a] -> [a] -> [a]
++ [Tok]
ts))
    ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> () -> ParsecT TokStream LaTeXState m ()
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

bracedOrToken :: PandocMonad m => LP m [Tok]
bracedOrToken :: forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracedOrToken = LP m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced LP m [Tok] -> LP m [Tok] -> LP m [Tok]
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ((Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
:[]) (Tok -> [Tok]) -> ParsecT TokStream LaTeXState m Tok -> LP m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
singleChar))

bracketed :: PandocMonad m => Monoid a => LP m a -> LP m a
bracketed :: forall (m :: * -> *) a.
(PandocMonad m, Monoid a) =>
LP m a -> LP m a
bracketed LP m a
parser = LP m a -> LP m a
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (LP m a -> LP m a) -> LP m a -> LP m a
forall a b. (a -> b) -> a -> b
$ do
  Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'['
  [a] -> a
forall a. Monoid a => [a] -> a
mconcat ([a] -> a) -> ParsecT TokStream LaTeXState m [a] -> LP m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m a -> LP m Tok -> ParsecT TokStream LaTeXState m [a]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill LP m a
parser (Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
']')

bracketedToks :: PandocMonad m => LP m [Tok]
bracketedToks :: forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracketedToks = do
  Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'['
  [[Tok]] -> [Tok]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Tok]] -> [Tok])
-> ParsecT TokStream LaTeXState m [[Tok]] -> LP m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m [Tok] -> LP m Tok -> ParsecT TokStream LaTeXState m [[Tok]]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill ((([Tok], [Tok]) -> [Tok]
forall a b. (a, b) -> b
snd (([Tok], [Tok]) -> [Tok])
-> ParsecT TokStream LaTeXState m ([Tok], [Tok]) -> LP m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m [Tok] -> ParsecT TokStream LaTeXState m ([Tok], [Tok])
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
withRaw (LP m [Tok] -> LP m [Tok]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try LP m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced)) LP m [Tok] -> LP m [Tok] -> LP m [Tok]
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Int -> LP m Tok -> LP m [Tok]
forall s (m :: * -> *) t u a.
Stream s m t =>
Int -> ParsecT s u m a -> ParsecT s u m [a]
count Int
1 LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok)
                      (Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
']')

parenWrapped :: PandocMonad m => Monoid a => LP m a -> LP m a
parenWrapped :: forall (m :: * -> *) a.
(PandocMonad m, Monoid a) =>
LP m a -> LP m a
parenWrapped LP m a
parser = LP m a -> LP m a
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (LP m a -> LP m a) -> LP m a -> LP m a
forall a b. (a -> b) -> a -> b
$ do
  Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'('
  [a] -> a
forall a. Monoid a => [a] -> a
mconcat ([a] -> a) -> ParsecT TokStream LaTeXState m [a] -> LP m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m a -> LP m Tok -> ParsecT TokStream LaTeXState m [a]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill LP m a
parser (Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
')')

dimenarg :: PandocMonad m => LP m Text
dimenarg :: forall (m :: * -> *). PandocMonad m => LP m Text
dimenarg = ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m Text
 -> ParsecT TokStream LaTeXState m Text)
-> ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall a b. (a -> b) -> a -> b
$ do
  ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp
  Bool
ch  <- Bool
-> ParsecT TokStream LaTeXState m Bool
-> ParsecT TokStream LaTeXState m Bool
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Bool
False (ParsecT TokStream LaTeXState m Bool
 -> ParsecT TokStream LaTeXState m Bool)
-> ParsecT TokStream LaTeXState m Bool
-> ParsecT TokStream LaTeXState m Bool
forall a b. (a -> b) -> a -> b
$ Bool
True Bool
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Bool
forall a b.
a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Char -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'='
  Text
minus <- Text
-> ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Text
"" (ParsecT TokStream LaTeXState m Text
 -> ParsecT TokStream LaTeXState m Text)
-> ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall a b. (a -> b) -> a -> b
$ Text
"-" Text
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Text
forall a b.
a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Char -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'-'
  Tok SourcePos
_ TokType
_ Text
s1 <- (Tok -> Bool) -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isWordTok
  Text
s2 <- Text
-> ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Text
"" (ParsecT TokStream LaTeXState m Text
 -> ParsecT TokStream LaTeXState m Text)
-> ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall a b. (a -> b) -> a -> b
$ ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m Text
 -> ParsecT TokStream LaTeXState m Text)
-> ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall a b. (a -> b) -> a -> b
$ do
          Char -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'.'
          Tok SourcePos
_ TokType
_ Text
t <-  (Tok -> Bool) -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isWordTok
          Text -> ParsecT TokStream LaTeXState m Text
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
"." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t)
  let s :: Text
s = Text
s1 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
s2
  let (Text
num, Text
rest) = (Char -> Bool) -> Text -> (Text, Text)
T.span (\Char
c -> Char -> Bool
isDigit Char
c Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.') Text
s
  Bool -> ParsecT TokStream LaTeXState m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> ParsecT TokStream LaTeXState m ())
-> Bool -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ Text -> Int
T.length Text
num Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0
  Bool -> ParsecT TokStream LaTeXState m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> ParsecT TokStream LaTeXState m ())
-> Bool -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ Text
rest Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"", Text
"pt",Text
"pc",Text
"in",Text
"bp",Text
"cm",Text
"mm",Text
"dd",Text
"cc",Text
"sp"]
  Text -> ParsecT TokStream LaTeXState m Text
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> ParsecT TokStream LaTeXState m Text)
-> Text -> ParsecT TokStream LaTeXState m Text
forall a b. (a -> b) -> a -> b
$ [Char] -> Text
T.pack [Char
'=' | Bool
ch] Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
minus Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
s

ignore :: (Monoid a, PandocMonad m) => Text -> ParsecT s u m a
ignore :: forall a (m :: * -> *) s u.
(Monoid a, PandocMonad m) =>
Text -> ParsecT s u m a
ignore Text
raw = do
  SourcePos
pos <- ParsecT s u m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
  LogMessage -> ParsecT s u m ()
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report (LogMessage -> ParsecT s u m ()) -> LogMessage -> ParsecT s u m ()
forall a b. (a -> b) -> a -> b
$ Text -> SourcePos -> LogMessage
SkippedContent Text
raw SourcePos
pos
  a -> ParsecT s u m a
forall a. a -> ParsecT s u m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
forall a. Monoid a => a
mempty

withRaw :: PandocMonad m => LP m a -> LP m (a, [Tok])
withRaw :: forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
withRaw LP m a
parser = do
  IntMap [Tok]
rawTokensMap <- LaTeXState -> IntMap [Tok]
sRawTokens (LaTeXState -> IntMap [Tok])
-> ParsecT TokStream LaTeXState m LaTeXState
-> ParsecT TokStream LaTeXState m (IntMap [Tok])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  let key :: Int
key = case IntMap [Tok] -> Maybe (Int, [Tok])
forall a. IntMap a -> Maybe (Int, a)
IntMap.lookupMax IntMap [Tok]
rawTokensMap of
               Maybe (Int, [Tok])
Nothing     -> Int
0
               Just (Int
n,[Tok]
_)  -> Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
  -- insert empty list at key
  (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ())
-> (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
st -> LaTeXState
st{ sRawTokens =
                             IntMap.insert key [] $ sRawTokens st }
  a
result <- LP m a
parser
  Maybe [Tok]
mbRevToks <- Int -> IntMap [Tok] -> Maybe [Tok]
forall a. Int -> IntMap a -> Maybe a
IntMap.lookup Int
key (IntMap [Tok] -> Maybe [Tok])
-> (LaTeXState -> IntMap [Tok]) -> LaTeXState -> Maybe [Tok]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LaTeXState -> IntMap [Tok]
sRawTokens (LaTeXState -> Maybe [Tok])
-> ParsecT TokStream LaTeXState m LaTeXState
-> ParsecT TokStream LaTeXState m (Maybe [Tok])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  [Tok]
raw <- case Maybe [Tok]
mbRevToks of
           Just [Tok]
revtoks -> do
             (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ())
-> (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
st -> LaTeXState
st{ sRawTokens =
                                        IntMap.delete key $ sRawTokens st}
             [Tok] -> ParsecT TokStream LaTeXState m [Tok]
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Tok] -> ParsecT TokStream LaTeXState m [Tok])
-> [Tok] -> ParsecT TokStream LaTeXState m [Tok]
forall a b. (a -> b) -> a -> b
$ [Tok] -> [Tok]
forall a. [a] -> [a]
reverse [Tok]
revtoks
           Maybe [Tok]
Nothing      ->
             PandocError -> ParsecT TokStream LaTeXState m [Tok]
forall a. PandocError -> ParsecT TokStream LaTeXState m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (PandocError -> ParsecT TokStream LaTeXState m [Tok])
-> PandocError -> ParsecT TokStream LaTeXState m [Tok]
forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocShouldNeverHappenError (Text -> PandocError) -> Text -> PandocError
forall a b. (a -> b) -> a -> b
$
                Text
"sRawTokens has nothing at key " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack (Int -> [Char]
forall a. Show a => a -> [Char]
show Int
key)
  (a, [Tok]) -> LP m (a, [Tok])
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, [Tok]
raw)

keyval :: PandocMonad m => LP m (Text, Text)
keyval :: forall (m :: * -> *). PandocMonad m => LP m (Text, Text)
keyval = ParsecT TokStream LaTeXState m (Text, Text)
-> ParsecT TokStream LaTeXState m (Text, Text)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m (Text, Text)
 -> ParsecT TokStream LaTeXState m (Text, Text))
-> ParsecT TokStream LaTeXState m (Text, Text)
-> ParsecT TokStream LaTeXState m (Text, Text)
forall a b. (a -> b) -> a -> b
$ do
  Text
key <- [Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m [Tok]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy (Char -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'=') ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                         (Char -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'-' ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Char -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'_' ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Tok -> Bool) -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isWordTok))
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp
  Text
val <- Text
-> ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Text
forall a. Monoid a => a
mempty (ParsecT TokStream LaTeXState m Text
 -> ParsecT TokStream LaTeXState m Text)
-> ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall a b. (a -> b) -> a -> b
$ do
           Char -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'='
           ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp
           ([Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced) ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
             ([Text] -> Text
forall a. Monoid a => [a] -> a
mconcat ([Text] -> Text)
-> ParsecT TokStream LaTeXState m [Text]
-> ParsecT TokStream LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m [Text]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (
                 ([Tok] -> Text
untokenize ([Tok] -> Text)
-> (([Tok], [Tok]) -> [Tok]) -> ([Tok], [Tok]) -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Tok], [Tok]) -> [Tok]
forall a b. (a, b) -> b
snd (([Tok], [Tok]) -> Text)
-> ParsecT TokStream LaTeXState m ([Tok], [Tok])
-> ParsecT TokStream LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m ([Tok], [Tok])
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
withRaw ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced)
                 ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                 ([Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream 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 TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok
                         (\case
                                Tok SourcePos
_ TokType
Symbol Text
"]" -> Bool
False
                                Tok SourcePos
_ TokType
Symbol Text
"," -> Bool
False
                                Tok SourcePos
_ TokType
Symbol Text
"{" -> Bool
False
                                Tok SourcePos
_ TokType
Symbol Text
"}" -> Bool
False
                                Tok
_                -> Bool
True)))))
  ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional (Char -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
',')
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp
  (Text, Text) -> ParsecT TokStream LaTeXState m (Text, Text)
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
key, Text -> Text
T.strip Text
val)

keyvals :: PandocMonad m => LP m [(Text, Text)]
keyvals :: forall (m :: * -> *). PandocMonad m => LP m [(Text, Text)]
keyvals = ParsecT TokStream LaTeXState m [(Text, Text)]
-> ParsecT TokStream LaTeXState m [(Text, Text)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m [(Text, Text)]
 -> ParsecT TokStream LaTeXState m [(Text, Text)])
-> ParsecT TokStream LaTeXState m [(Text, Text)]
-> ParsecT TokStream LaTeXState m [(Text, Text)]
forall a b. (a -> b) -> a -> b
$ Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'[' LP m Tok
-> ParsecT TokStream LaTeXState m [(Text, Text)]
-> ParsecT TokStream LaTeXState m [(Text, Text)]
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT TokStream LaTeXState m (Text, Text)
-> LP m Tok -> ParsecT TokStream LaTeXState m [(Text, Text)]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill ParsecT TokStream LaTeXState m (Text, Text)
forall (m :: * -> *). PandocMonad m => LP m (Text, Text)
keyval (Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
']') ParsecT TokStream LaTeXState m [(Text, Text)]
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m [(Text, Text)]
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp

verbEnv :: PandocMonad m => Text -> LP m Text
verbEnv :: forall (m :: * -> *). PandocMonad m => Text -> LP m Text
verbEnv Text
name = LP m Text -> LP m Text
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m a
withVerbatimMode (LP m Text -> LP m Text) -> LP m Text -> LP m Text
forall a b. (a -> b) -> a -> b
$ do
  ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
blankline
  [Tok]
res <- ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m [Tok]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok (Text -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => Text -> LP m ()
end_ Text
name)
  Text -> LP m Text
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> LP m Text) -> Text -> LP m Text
forall a b. (a -> b) -> a -> b
$ Text -> Text
stripTrailingNewline
         (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ [Tok] -> Text
untokenize [Tok]
res

-- Strip single final newline and any spaces following it.
-- Input is unchanged if it doesn't end with newline +
-- optional spaces.
stripTrailingNewline :: Text -> Text
stripTrailingNewline :: Text -> Text
stripTrailingNewline Text
t =
  let (Text
b, Text
e) = HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOnEnd Text
"\n" Text
t
  in  if (Char -> Bool) -> Text -> Bool
T.all (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ') Text
e
         then Int -> Text -> Text
T.dropEnd Int
1 Text
b
         else Text
t

begin_ :: PandocMonad m => Text -> LP m ()
begin_ :: forall (m :: * -> *). PandocMonad m => Text -> LP m ()
begin_ Text
t = ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (do
  Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"begin"
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  Text
txt <- [Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
  Bool -> ParsecT TokStream LaTeXState m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Text
t Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
txt)) ParsecT TokStream LaTeXState m ()
-> [Char] -> ParsecT TokStream LaTeXState m ()
forall s u (m :: * -> *) a.
ParsecT s u m a -> [Char] -> ParsecT s u m a
<?> ([Char]
"\\begin{" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
t [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"}")

end_ :: PandocMonad m => Text -> LP m ()
end_ :: forall (m :: * -> *). PandocMonad m => Text -> LP m ()
end_ Text
t = ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (do
  Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"end"
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  Text
txt <- [Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
  Bool -> ParsecT TokStream LaTeXState m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> ParsecT TokStream LaTeXState m ())
-> Bool -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ Text
t Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
txt) ParsecT TokStream LaTeXState m ()
-> [Char] -> ParsecT TokStream LaTeXState m ()
forall s u (m :: * -> *) a.
ParsecT s u m a -> [Char] -> ParsecT s u m a
<?> ([Char]
"\\end{" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
T.unpack Text
t [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"}")

getRawCommand :: PandocMonad m => Text -> Text -> LP m Text
getRawCommand :: forall (m :: * -> *). PandocMonad m => Text -> Text -> LP m Text
getRawCommand Text
name Text
txt = do
  (()
_, [Tok]
rawargs) <- LP m () -> LP m ((), [Tok])
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
withRaw (LP m () -> LP m ((), [Tok])) -> LP m () -> LP m ((), [Tok])
forall a b. (a -> b) -> a -> b
$
      case Text
name of
           Text
"write" -> do
             ParsecT TokStream LaTeXState m [Tok] -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT TokStream LaTeXState m [Tok] -> LP m ())
-> ParsecT TokStream LaTeXState m [Tok] -> LP m ()
forall a b. (a -> b) -> a -> b
$ ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m [Tok]
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (ParsecT TokStream LaTeXState m Tok
 -> ParsecT TokStream LaTeXState m [Tok])
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m [Tok]
forall a b. (a -> b) -> a -> b
$ (Tok -> Bool) -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isDigitTok -- digits
             ParsecT TokStream LaTeXState m [Tok] -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
           Text
"titleformat" -> do
             ParsecT TokStream LaTeXState m [Tok] -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
             LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
skipopts
             ParsecT TokStream LaTeXState m [[Tok]] -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT TokStream LaTeXState m [[Tok]] -> LP m ())
-> ParsecT TokStream LaTeXState m [[Tok]] -> LP m ()
forall a b. (a -> b) -> a -> b
$ Int
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [[Tok]]
forall s (m :: * -> *) t u a.
Stream s m t =>
Int -> ParsecT s u m a -> ParsecT s u m [a]
count Int
4 ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
           Text
"def" ->
             ParsecT TokStream LaTeXState m [Tok] -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT TokStream LaTeXState m [Tok] -> LP m ())
-> ParsecT TokStream LaTeXState m [Tok] -> LP m ()
forall a b. (a -> b) -> a -> b
$ ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
           Text
"vadjust" ->
             ParsecT TokStream LaTeXState m [Tok] -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced) LP m () -> LP m () -> LP m ()
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                ParsecT TokStream LaTeXState m Tok -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void ((Tok -> Bool) -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isPreTok) -- see #7531
           Text
_ | Text -> Bool
isFontSizeCommand Text
name -> () -> LP m ()
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
             | Text
name Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"hfil", Text
"hfill", Text
"vfil", Text
"vfill",
                            Text
"hfilneg", Text
"vfilneg"] -> () -> LP m ()
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
             | Text
name Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"hskip", Text
"vskip", Text
"mskip"] -> do
                 LP m Text
forall (m :: * -> *). PandocMonad m => LP m Text
dimenarg
                 LP m Text -> LP m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany (LP m Text -> LP m ()) -> LP m Text -> LP m ()
forall a b. (a -> b) -> a -> b
$ LP m Text -> LP m Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (LP m Text -> LP m Text) -> LP m Text -> LP m Text
forall a b. (a -> b) -> a -> b
$ do
                   LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp
                   (Tok -> Bool) -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok ((Tok -> Bool) -> ParsecT TokStream LaTeXState m Tok)
-> (Tok -> Bool) -> ParsecT TokStream LaTeXState m Tok
forall a b. (a -> b) -> a -> b
$
                     \case
                       Tok SourcePos
_ TokType
Word Text
"plus" -> Bool
True
                       Tok SourcePos
_ TokType
Word Text
"minus" -> Bool
True
                       Tok
_ -> Bool
False
                   LP m Text
forall (m :: * -> *). PandocMonad m => LP m Text
dimenarg
             | Bool
otherwise -> do
               LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
skipopts
               Text -> LP m Text -> LP m Text
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Text
"" (LP m Text -> LP m Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try LP m Text
forall (m :: * -> *). PandocMonad m => LP m Text
dimenarg)
               ParsecT TokStream LaTeXState m [[Tok]] -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT TokStream LaTeXState m [[Tok]] -> LP m ())
-> ParsecT TokStream LaTeXState m [[Tok]] -> LP m ()
forall a b. (a -> b) -> a -> b
$ ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [[Tok]]
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
  Text -> LP m Text
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> LP m Text) -> Text -> LP m Text
forall a b. (a -> b) -> a -> b
$ Text
txt Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Tok] -> Text
untokenize [Tok]
rawargs

isPreTok :: Tok -> Bool
isPreTok :: Tok -> Bool
isPreTok (Tok SourcePos
_ TokType
Word Text
"pre") = Bool
True
isPreTok Tok
_ = Bool
False

isDigitTok :: Tok -> Bool
isDigitTok :: Tok -> Bool
isDigitTok (Tok SourcePos
_ TokType
Word Text
t) = (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isDigit Text
t
isDigitTok Tok
_              = Bool
False

skipopts :: PandocMonad m => LP m ()
skipopts :: forall (m :: * -> *). PandocMonad m => LP m ()
skipopts = ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany (ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void ParsecT TokStream LaTeXState m Text
forall (m :: * -> *). PandocMonad m => LP m Text
overlaySpecification ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void ParsecT TokStream LaTeXState m Text
forall (m :: * -> *). PandocMonad m => LP m Text
rawopt)

-- opts in angle brackets are used in beamer
overlaySpecification :: PandocMonad m => LP m Text
overlaySpecification :: forall (m :: * -> *). PandocMonad m => LP m Text
overlaySpecification = ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m Text
 -> ParsecT TokStream LaTeXState m Text)
-> ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall a b. (a -> b) -> a -> b
$ do
  Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'<'
  Text
t <- [Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m Tok -> LP m Tok -> ParsecT TokStream LaTeXState m [Tok]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
overlayTok (Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'>')
  -- see issue #3368
  Bool -> ParsecT TokStream LaTeXState m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> ParsecT TokStream LaTeXState m ())
-> Bool -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not ((Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isLetter Text
t) Bool -> Bool -> Bool
||
          Text
t Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"beamer",Text
"presentation", Text
"trans",
                    Text
"handout",Text
"article", Text
"second"]
  Text -> ParsecT TokStream LaTeXState m Text
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> ParsecT TokStream LaTeXState m Text)
-> Text -> ParsecT TokStream LaTeXState m Text
forall a b. (a -> b) -> a -> b
$ Text
"<" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
">"

overlayTok :: PandocMonad m => LP m Tok
overlayTok :: forall (m :: * -> *). PandocMonad m => LP m Tok
overlayTok =
  (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok (\case
                    Tok SourcePos
_ TokType
Word Text
_       -> Bool
True
                    Tok SourcePos
_ TokType
Spaces Text
_     -> Bool
True
                    Tok SourcePos
_ TokType
Symbol Text
c     -> Text
c Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"-",Text
"+",Text
"@",Text
"|",Text
":",Text
","]
                    Tok
_                  -> Bool
False)

rawopt :: PandocMonad m => LP m Text
rawopt :: forall (m :: * -> *). PandocMonad m => LP m Text
rawopt = ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m Text
 -> ParsecT TokStream LaTeXState m Text)
-> ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m Text
forall a b. (a -> b) -> a -> b
$ do
  LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp
  Text
inner <- [Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracketedToks
  LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp
  Text -> ParsecT TokStream LaTeXState m Text
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> ParsecT TokStream LaTeXState m Text)
-> Text -> ParsecT TokStream LaTeXState m Text
forall a b. (a -> b) -> a -> b
$ Text
"[" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
inner Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"]"

isFontSizeCommand :: Text -> Bool
isFontSizeCommand :: Text -> Bool
isFontSizeCommand Text
"tiny" = Bool
True
isFontSizeCommand Text
"scriptsize" = Bool
True
isFontSizeCommand Text
"footnotesize" = Bool
True
isFontSizeCommand Text
"small" = Bool
True
isFontSizeCommand Text
"normalsize" = Bool
True
isFontSizeCommand Text
"large" = Bool
True
isFontSizeCommand Text
"Large" = Bool
True
isFontSizeCommand Text
"LARGE" = Bool
True
isFontSizeCommand Text
"huge" = Bool
True
isFontSizeCommand Text
"Huge" = Bool
True
isFontSizeCommand Text
_ = Bool
False

getNextNumber :: Monad m
              => (LaTeXState -> DottedNum) -> LP m DottedNum
getNextNumber :: forall (m :: * -> *).
Monad m =>
(LaTeXState -> DottedNum) -> LP m DottedNum
getNextNumber LaTeXState -> DottedNum
getCurrentNum = do
  LaTeXState
st <- ParsecT TokStream LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  let chapnum :: Maybe Int
chapnum =
        case LaTeXState -> DottedNum
sLastHeaderNum LaTeXState
st of
             DottedNum (Int
n:[Int]
_) | LaTeXState -> Bool
sHasChapters LaTeXState
st -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
n
             DottedNum
_                                 -> Maybe Int
forall a. Maybe a
Nothing
  DottedNum -> LP m DottedNum
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (DottedNum -> LP m DottedNum)
-> ([Int] -> DottedNum) -> [Int] -> LP m DottedNum
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> DottedNum
DottedNum ([Int] -> LP m DottedNum) -> [Int] -> LP m DottedNum
forall a b. (a -> b) -> a -> b
$
    case LaTeXState -> DottedNum
getCurrentNum LaTeXState
st of
       DottedNum [Int
m,Int
n]  ->
         case Maybe Int
chapnum of
              Just Int
m' | Int
m' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
m   -> [Int
m, Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1]
                      | Bool
otherwise -> [Int
m', Int
1]
              Maybe Int
Nothing             -> [Int
1]
                                      -- shouldn't happen
       DottedNum [Int
n]   ->
         case Maybe Int
chapnum of
              Just Int
m  -> [Int
m, Int
1]
              Maybe Int
Nothing -> [Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1]
       DottedNum
_               ->
         case Maybe Int
chapnum of
               Just Int
n  -> [Int
n, Int
1]
               Maybe Int
Nothing -> [Int
1]

label :: PandocMonad m => LP m ()
label :: forall (m :: * -> *). PandocMonad m => LP m ()
label = do
  Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"label"
  [Tok]
t <- LP m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
  (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
st -> LaTeXState
st{ sLastLabel = Just $ untokenize t }

setCaption :: PandocMonad m => LP m Inlines -> LP m ()
setCaption :: forall (m :: * -> *). PandocMonad m => LP m Inlines -> LP m ()
setCaption LP m Inlines
inline = ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m ()
 -> ParsecT TokStream LaTeXState m ())
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ do
  Maybe [Inline]
mbshort <- [Inline] -> Maybe [Inline]
forall a. a -> Maybe a
Just ([Inline] -> Maybe [Inline])
-> (Inlines -> [Inline]) -> Inlines -> Maybe [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inlines -> [Inline]
forall a. Many a -> [a]
toList (Inlines -> Maybe [Inline])
-> LP m Inlines -> ParsecT TokStream LaTeXState m (Maybe [Inline])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m Inlines -> LP m Inlines
forall (m :: * -> *) a.
(PandocMonad m, Monoid a) =>
LP m a -> LP m a
bracketed LP m Inlines
inline ParsecT TokStream LaTeXState m (Maybe [Inline])
-> ParsecT TokStream LaTeXState m (Maybe [Inline])
-> ParsecT TokStream LaTeXState m (Maybe [Inline])
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe [Inline] -> ParsecT TokStream LaTeXState m (Maybe [Inline])
forall a. a -> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe [Inline]
forall a. Maybe a
Nothing
  Inlines
ils <- LP m Inlines -> LP m Inlines
forall (m :: * -> *). PandocMonad m => LP m Inlines -> LP m Inlines
tokWith LP m Inlines
inline
  ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional (ParsecT TokStream LaTeXState m ()
 -> ParsecT TokStream LaTeXState m ())
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m ()
 -> ParsecT TokStream LaTeXState m ())
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
label
  (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ())
-> (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
st -> LaTeXState
st{ sCaption = Just $
                              Caption mbshort [Plain $ toList ils] }

resetCaption :: PandocMonad m => LP m ()
resetCaption :: forall (m :: * -> *). PandocMonad m => LP m ()
resetCaption = (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ())
-> (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
st -> LaTeXState
st{ sCaption   = Nothing
                                      , sLastLabel = Nothing }

env :: PandocMonad m => Text -> LP m a -> LP m a
env :: forall (m :: * -> *) a. PandocMonad m => Text -> LP m a -> LP m a
env Text
name LP m a
p = do
  -- environments are groups as far as macros are concerned,
  -- so we need a local copy of the macro table (see above, bgroup, egroup):
  (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ())
-> (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
s -> LaTeXState
s{ sMacros = NonEmpty.cons (NonEmpty.head (sMacros s))
                                                 (sMacros s) }
  a
result <- LP m a
p
  (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ())
-> (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
s -> LaTeXState
s{ sMacros = fromMaybe (sMacros s) $
      NonEmpty.nonEmpty (NonEmpty.tail (sMacros s)) }
  Text -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => Text -> LP m ()
end_ Text
name
  a -> LP m a
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
result

tokWith :: PandocMonad m => LP m Inlines -> LP m Inlines
tokWith :: forall (m :: * -> *). PandocMonad m => LP m Inlines -> LP m Inlines
tokWith LP m Inlines
inlineParser = LP m Inlines -> LP m Inlines
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (LP m Inlines -> LP m Inlines) -> LP m Inlines -> LP m Inlines
forall a b. (a -> b) -> a -> b
$ LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces LP m () -> LP m Inlines -> LP m Inlines
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                                 LP m Inlines -> LP m Inlines
forall (m :: * -> *) a.
(PandocMonad m, Monoid a) =>
LP m a -> LP m a
grouped LP m Inlines
inlineParser
                            LP m Inlines -> LP m Inlines -> LP m Inlines
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyControlSeq ParsecT TokStream LaTeXState m Tok -> LP m Inlines -> LP m Inlines
forall a b.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m b
-> ParsecT TokStream LaTeXState m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> LP m Inlines
inlineParser)
                            LP m Inlines -> LP m Inlines -> LP m Inlines
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m Inlines
singleChar'
  where singleChar' :: LP m Inlines
singleChar' = do
          Tok SourcePos
_ TokType
_ Text
t <- ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
singleChar
          Inlines -> LP m Inlines
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Inlines -> LP m Inlines) -> Inlines -> LP m Inlines
forall a b. (a -> b) -> a -> b
$ Text -> Inlines
str Text
t

addMeta :: PandocMonad m => ToMetaValue a => Text -> a -> LP m ()
addMeta :: forall (m :: * -> *) a.
(PandocMonad m, ToMetaValue a) =>
Text -> a -> LP m ()
addMeta Text
field a
val = (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ())
-> (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ \LaTeXState
st ->
   LaTeXState
st{ sMeta = addMetaField field val $ sMeta st }

-- remove label spans to avoid duplicated identifier
removeLabel :: Walkable [Inline] a => Text -> a -> a
removeLabel :: forall a. Walkable [Inline] a => Text -> a -> a
removeLabel Text
lbl = ([Inline] -> [Inline]) -> a -> a
forall a b. Walkable a b => (a -> a) -> b -> b
walk [Inline] -> [Inline]
go
 where
  go :: [Inline] -> [Inline]
go (Span (Text
_,[Text]
_,[(Text, Text)]
kvs) [Inline]
_ : [Inline]
rest)
    | Just Text
lbl' <- Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"label" [(Text, Text)]
kvs
    , Text
lbl' Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
lbl = [Inline] -> [Inline]
go ((Inline -> Bool) -> [Inline] -> [Inline]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Inline -> Bool
isSpaceOrSoftBreak [Inline]
rest)
  go (Inline
x:[Inline]
xs) = Inline
x Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline] -> [Inline]
go [Inline]
xs
  go [] = []
  isSpaceOrSoftBreak :: Inline -> Bool
isSpaceOrSoftBreak Inline
Space = Bool
True
  isSpaceOrSoftBreak Inline
SoftBreak = Bool
True
  isSpaceOrSoftBreak Inline
_ = Bool
False