{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{- |
   Module      : Text.Pandoc.Readers.LaTeX.Parsing
   Copyright   : Copyright (C) 2006-2020 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
  , withVerbatimMode
  , rawLaTeXParser
  , applyMacros
  , tokenize
  , untokenize
  , untoken
  , totoks
  , toksToString
  , satisfyTok
  , doMacros
  , doMacros'
  , setpos
  , anyControlSeq
  , anySymbol
  , isNewlineTok
  , isWordTok
  , isArgTok
  , spaces
  , spaces1
  , tokTypeIn
  , controlSeq
  , symbol
  , symbolIn
  , sp
  , whitespace
  , newlineTok
  , comment
  , anyTok
  , singleChar
  , 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
  ) 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.Map as M
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as T
import Text.Pandoc.Builder
import Text.Pandoc.Class.PandocMonad (PandocMonad, report)
import Text.Pandoc.Error (PandocError (PandocMacroLoop))
import Text.Pandoc.Logging
import Text.Pandoc.Options
import Text.Pandoc.Parsing hiding (blankline, many, mathDisplay, mathInline,
                            space, spaces, withRaw, (<|>))
import Text.Pandoc.Readers.LaTeX.Types (ExpansionPoint (..), Macro (..),
                                        ArgSpec (..), Tok (..), TokType (..))
import Text.Pandoc.Shared
import Text.Parsec.Pos
-- import Debug.Trace

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

renderDottedNum :: DottedNum -> T.Text
renderDottedNum :: DottedNum -> Text
renderDottedNum (DottedNum [Int]
xs) = String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$
  String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"." ((Int -> String) -> [Int] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Int -> String
forall a. Show a => a -> String
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 -> String
(Int -> TheoremStyle -> ShowS)
-> (TheoremStyle -> String)
-> ([TheoremStyle] -> ShowS)
-> Show TheoremStyle
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TheoremStyle] -> ShowS
$cshowList :: [TheoremStyle] -> ShowS
show :: TheoremStyle -> String
$cshow :: TheoremStyle -> String
showsPrec :: Int -> TheoremStyle -> ShowS
$cshowsPrec :: Int -> TheoremStyle -> ShowS
Show, TheoremStyle -> TheoremStyle -> Bool
(TheoremStyle -> TheoremStyle -> Bool)
-> (TheoremStyle -> TheoremStyle -> Bool) -> Eq TheoremStyle
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TheoremStyle -> TheoremStyle -> Bool
$c/= :: TheoremStyle -> TheoremStyle -> Bool
== :: TheoremStyle -> TheoremStyle -> Bool
$c== :: 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 -> String
(Int -> TheoremSpec -> ShowS)
-> (TheoremSpec -> String)
-> ([TheoremSpec] -> ShowS)
-> Show TheoremSpec
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TheoremSpec] -> ShowS
$cshowList :: [TheoremSpec] -> ShowS
show :: TheoremSpec -> String
$cshow :: TheoremSpec -> String
showsPrec :: Int -> TheoremSpec -> ShowS
$cshowsPrec :: Int -> TheoremSpec -> ShowS
Show, TheoremSpec -> TheoremSpec -> Bool
(TheoremSpec -> TheoremSpec -> Bool)
-> (TheoremSpec -> TheoremSpec -> Bool) -> Eq TheoremSpec
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TheoremSpec -> TheoremSpec -> Bool
$c/= :: TheoremSpec -> TheoremSpec -> Bool
== :: TheoremSpec -> TheoremSpec -> Bool
$c== :: TheoremSpec -> TheoremSpec -> Bool
Eq)

data LaTeXState = LaTeXState{ LaTeXState -> ReaderOptions
sOptions       :: ReaderOptions
                            , LaTeXState -> Meta
sMeta          :: Meta
                            , LaTeXState -> QuoteContext
sQuoteContext  :: QuoteContext
                            , LaTeXState -> Map Text Macro
sMacros        :: 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 Inlines
sCaption       :: Maybe Inlines
                            , LaTeXState -> Bool
sInListItem    :: Bool
                            , LaTeXState -> Bool
sInTableCell   :: Bool
                            , LaTeXState -> DottedNum
sLastHeaderNum :: DottedNum
                            , LaTeXState -> DottedNum
sLastFigureNum :: DottedNum
                            , LaTeXState -> DottedNum
sLastTableNum  :: DottedNum
                            , 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 -> Bool
sExpanded      :: Bool
                            }
     deriving Int -> LaTeXState -> ShowS
[LaTeXState] -> ShowS
LaTeXState -> String
(Int -> LaTeXState -> ShowS)
-> (LaTeXState -> String)
-> ([LaTeXState] -> ShowS)
-> Show LaTeXState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LaTeXState] -> ShowS
$cshowList :: [LaTeXState] -> ShowS
show :: LaTeXState -> String
$cshow :: LaTeXState -> String
showsPrec :: Int -> LaTeXState -> ShowS
$cshowsPrec :: Int -> LaTeXState -> ShowS
Show

defaultLaTeXState :: LaTeXState
defaultLaTeXState :: LaTeXState
defaultLaTeXState = LaTeXState :: ReaderOptions
-> Meta
-> QuoteContext
-> Map Text Macro
-> [Text]
-> [LogMessage]
-> Set Text
-> Bool
-> Maybe Inlines
-> Bool
-> Bool
-> DottedNum
-> DottedNum
-> DottedNum
-> Map Text TheoremSpec
-> TheoremStyle
-> Maybe Text
-> Map Text [Inline]
-> Bool
-> Map Text Bool
-> Bool
-> LaTeXState
LaTeXState{ sOptions :: ReaderOptions
sOptions       = ReaderOptions
forall a. Default a => a
def
                              , sMeta :: Meta
sMeta          = Meta
nullMeta
                              , sQuoteContext :: QuoteContext
sQuoteContext  = QuoteContext
NoQuote
                              , sMacros :: Map Text Macro
sMacros        = Map Text Macro
forall k a. Map k a
M.empty
                              , 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 Inlines
sCaption       = Maybe Inlines
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 []
                              , 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
                              , sExpanded :: Bool
sExpanded      = Bool
False
                              }

instance PandocMonad m => HasQuoteContext LaTeXState m where
  getQuoteContext :: 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 :: 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 :: QuoteContext
sQuoteContext = QuoteContext
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 :: QuoteContext
sQuoteContext = QuoteContext
oldQuoteContext }
    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 :: [LogMessage]
sLogMessages = LogMessage
msg LogMessage -> [LogMessage] -> [LogMessage]
forall a. a -> [a] -> [a]
: LaTeXState -> [LogMessage]
sLogMessages LaTeXState
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 :: Set Text
sIdentifiers = Set Text -> Set Text
f (Set Text -> Set Text) -> Set Text -> Set Text
forall a b. (a -> b) -> a -> b
$ LaTeXState -> Set Text
sIdentifiers LaTeXState
st }

instance HasIncludeFiles LaTeXState where
  getIncludeFiles :: LaTeXState -> [Text]
getIncludeFiles = LaTeXState -> [Text]
sContainers
  addIncludeFile :: Text -> LaTeXState -> LaTeXState
addIncludeFile Text
f LaTeXState
s = LaTeXState
s{ sContainers :: [Text]
sContainers = Text
f Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: LaTeXState -> [Text]
sContainers LaTeXState
s }
  dropLatestIncludeFile :: LaTeXState -> LaTeXState
dropLatestIncludeFile LaTeXState
s = LaTeXState
s { sContainers :: [Text]
sContainers = Int -> [Text] -> [Text]
forall a. Int -> [a] -> [a]
drop Int
1 ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ LaTeXState -> [Text]
sContainers LaTeXState
s }

instance HasMacros LaTeXState where
  extractMacros :: LaTeXState -> Map Text Macro
extractMacros  LaTeXState
st  = LaTeXState -> 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 :: Map Text Macro
sMacros = Map Text Macro -> Map Text Macro
f (LaTeXState -> Map Text Macro
sMacros LaTeXState
st) }

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

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

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

type LP m = ParserT [Tok] LaTeXState m

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

rawLaTeXParser :: (PandocMonad m, HasMacros s, HasReaderOptions s)
               => [Tok] -> Bool -> LP m a -> LP m a
               -> ParserT Text s m (a, Text)
rawLaTeXParser :: [Tok] -> Bool -> LP m a -> LP m a -> ParserT Text s m (a, Text)
rawLaTeXParser [Tok]
toks Bool
retokenize LP m a
parser LP m a
valParser = do
  s
pstate <- ParsecT Text 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 :: ReaderOptions
sOptions = s -> ReaderOptions
forall st. HasReaderOptions st => st -> ReaderOptions
extractReaderOptions s
pstate }
  let lstate' :: LaTeXState
lstate' = LaTeXState
lstate { sMacros :: Map Text Macro
sMacros = s -> Map Text Macro
forall st. HasMacros st => st -> Map Text Macro
extractMacros s
pstate }
  let rawparser :: ParsecT [Tok] LaTeXState m ((a, [Tok]), LaTeXState)
rawparser = (,) ((a, [Tok]) -> LaTeXState -> ((a, [Tok]), LaTeXState))
-> ParsecT [Tok] LaTeXState m (a, [Tok])
-> ParsecT
     [Tok] LaTeXState m (LaTeXState -> ((a, [Tok]), LaTeXState))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m a -> ParsecT [Tok] LaTeXState m (a, [Tok])
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
withRaw LP m a
valParser ParsecT [Tok] LaTeXState m (LaTeXState -> ((a, [Tok]), LaTeXState))
-> ParsecT [Tok] LaTeXState m LaTeXState
-> ParsecT [Tok] LaTeXState m ((a, [Tok]), LaTeXState)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT [Tok] LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  Either ParseError [Tok]
res' <- m (Either ParseError [Tok])
-> ParsecT Text s m (Either ParseError [Tok])
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either ParseError [Tok])
 -> ParsecT Text s m (Either ParseError [Tok]))
-> m (Either ParseError [Tok])
-> ParsecT Text s m (Either ParseError [Tok])
forall a b. (a -> b) -> a -> b
$ ParsecT [Tok] LaTeXState m [Tok]
-> LaTeXState -> String -> [Tok] -> m (Either ParseError [Tok])
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> u -> String -> s -> m (Either ParseError a)
runParserT ((a, [Tok]) -> [Tok]
forall a b. (a, b) -> b
snd ((a, [Tok]) -> [Tok])
-> ParsecT [Tok] LaTeXState m (a, [Tok])
-> ParsecT [Tok] LaTeXState m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m a -> ParsecT [Tok] LaTeXState m (a, [Tok])
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
withRaw LP m a
parser) LaTeXState
lstate String
"chunk" [Tok]
toks
  case Either ParseError [Tok]
res' of
       Left ParseError
_    -> ParserT Text s m (a, Text)
forall (m :: * -> *) a. MonadPlus m => m a
mzero
       Right [Tok]
toks' -> do
         Either ParseError ((a, [Tok]), LaTeXState)
res <- m (Either ParseError ((a, [Tok]), LaTeXState))
-> ParsecT Text s m (Either ParseError ((a, [Tok]), LaTeXState))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either ParseError ((a, [Tok]), LaTeXState))
 -> ParsecT Text s m (Either ParseError ((a, [Tok]), LaTeXState)))
-> m (Either ParseError ((a, [Tok]), LaTeXState))
-> ParsecT Text s m (Either ParseError ((a, [Tok]), LaTeXState))
forall a b. (a -> b) -> a -> b
$ ParsecT [Tok] LaTeXState m ((a, [Tok]), LaTeXState)
-> LaTeXState
-> String
-> [Tok]
-> m (Either ParseError ((a, [Tok]), LaTeXState))
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> u -> String -> s -> m (Either ParseError a)
runParserT (do Bool
-> ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
retokenize (ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ())
-> ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ do
                                        -- retokenize, applying macros
                                        ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
doMacros
                                        [Tok]
ts <- ParsecT [Tok] LaTeXState m Tok -> ParsecT [Tok] LaTeXState m [Tok]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many ((Tok -> Bool) -> ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok (Bool -> Tok -> Bool
forall a b. a -> b -> a
const Bool
True))
                                        [Tok] -> ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput [Tok]
ts
                                      ParsecT [Tok] LaTeXState m ((a, [Tok]), LaTeXState)
rawparser)
                        LaTeXState
lstate' String
"chunk" [Tok]
toks'
         case Either ParseError ((a, [Tok]), LaTeXState)
res of
              Left ParseError
_    -> ParserT Text s m (a, Text)
forall (m :: * -> *) a. MonadPlus m => m a
mzero
              Right ((a
val, [Tok]
raw), LaTeXState
st) -> do
                (s -> s) -> ParsecT Text 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 (LaTeXState -> Map Text Macro
sMacros LaTeXState
st Map Text Macro -> Map Text Macro -> Map Text Macro
forall a. Semigroup a => a -> a -> a
<>))
                Text
_ <- Int -> ParserT Text s m Text
forall (m :: * -> *) st. Monad m => Int -> ParserT Text st m Text
takeP (Text -> Int
T.length ([Tok] -> Text
untokenize [Tok]
toks'))
                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) -> ParserT Text s m (a, Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
val, Text
result')

applyMacros :: (PandocMonad m, HasMacros s, HasReaderOptions s)
            => Text -> ParserT Text s m Text
applyMacros :: Text -> ParserT Text s m Text
applyMacros Text
s = (Extension -> ParserT Text s m ()
forall s (m :: * -> *) a st.
(Stream s m a, HasReaderOptions st) =>
Extension -> ParserT s st m ()
guardDisabled Extension
Ext_latex_macros ParserT Text s m ()
-> ParserT Text s m Text -> ParserT Text s m Text
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> ParserT Text s m Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
s) ParserT Text s m Text
-> ParserT Text s m Text -> ParserT Text s m Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
   do let retokenize :: ParsecT [Tok] LaTeXState (ParsecT Text s m) Text
retokenize = [Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT [Tok] LaTeXState (ParsecT Text s m) [Tok]
-> ParsecT [Tok] LaTeXState (ParsecT Text s m) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState (ParsecT Text s m) Tok
-> ParsecT [Tok] LaTeXState (ParsecT Text s m) [Tok]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many ((Tok -> Bool) -> ParsecT [Tok] LaTeXState (ParsecT Text s m) Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok (Bool -> Tok -> Bool
forall a b. a -> b -> a
const Bool
True))
      s
pstate <- ParsecT Text 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 :: ReaderOptions
sOptions = s -> ReaderOptions
forall st. HasReaderOptions st => st -> ReaderOptions
extractReaderOptions s
pstate
                      , sMacros :: Map Text Macro
sMacros  = s -> Map Text Macro
forall st. HasMacros st => st -> Map Text Macro
extractMacros s
pstate }
      Either ParseError Text
res <- ParsecT [Tok] LaTeXState (ParsecT Text s m) Text
-> LaTeXState
-> String
-> [Tok]
-> ParsecT Text s m (Either ParseError Text)
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> u -> String -> s -> m (Either ParseError a)
runParserT ParsecT [Tok] LaTeXState (ParsecT Text s m) Text
retokenize LaTeXState
lstate String
"math" (String -> Text -> [Tok]
tokenize String
"math" Text
s)
      case Either ParseError Text
res of
           Left ParseError
e   -> String -> ParserT Text s m Text
forall (m :: * -> *) a. MonadFail m => String -> m a
Prelude.fail (ParseError -> String
forall a. Show a => a -> String
show ParseError
e)
           Right Text
s' -> Text -> ParserT Text s m Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
s'

tokenize :: SourceName -> Text -> [Tok]
tokenize :: String -> Text -> [Tok]
tokenize String
sourcename = SourcePos -> Text -> [Tok]
totoks (String -> SourcePos
initialPos String
sourcename)

totoks :: SourcePos -> Text -> [Tok]
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)
                                                  -> (String -> Text
T.pack String
"\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)) (String -> 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
'#' ->
           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
t2
         | 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 (String -> 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 (String -> 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 (String -> 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 (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

toksToString :: [Tok] -> String
toksToString :: [Tok] -> String
toksToString = Text -> String
T.unpack (Text -> String) -> ([Tok] -> Text) -> [Tok] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Tok] -> Text
untokenize

satisfyTok :: PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok :: (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 -> String)
-> (SourcePos -> Tok -> [Tok] -> SourcePos)
-> (Tok -> Maybe Tok)
-> LP m Tok
forall s (m :: * -> *) t a u.
Stream s m t =>
(t -> String)
-> (SourcePos -> t -> s -> SourcePos)
-> (t -> Maybe a)
-> ParsecT s u m a
tokenPrim (Text -> String
T.unpack (Text -> String) -> (Tok -> Text) -> Tok -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Tok -> Text
untoken) SourcePos -> Tok -> [Tok] -> 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 -> LaTeXState
st{ sExpanded :: Bool
sExpanded = Bool
False }
    Tok -> LP m Tok
forall (m :: * -> *) a. Monad m => a -> m a
return 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 -> [Tok] -> SourcePos
        updatePos :: SourcePos -> Tok -> [Tok] -> SourcePos
updatePos SourcePos
_spos Tok
_ (Tok SourcePos
pos TokType
_ Text
_ : [Tok]
_) = SourcePos
pos
        updatePos SourcePos
spos Tok
_ []                 = SourcePos -> Int -> SourcePos
incSourceColumn SourcePos
spos Int
1

doMacros :: PandocMonad m => LP m ()
doMacros :: LP m ()
doMacros = do
  Bool
expanded <- LaTeXState -> Bool
sExpanded (LaTeXState -> Bool)
-> ParsecT [Tok] LaTeXState m LaTeXState
-> ParsecT [Tok] LaTeXState m Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  Bool
verbatimMode <- LaTeXState -> Bool
sVerbatimMode (LaTeXState -> Bool)
-> ParsecT [Tok] LaTeXState m LaTeXState
-> ParsecT [Tok] LaTeXState m Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  Bool -> LP m () -> LP m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Bool
expanded Bool -> Bool -> Bool
|| Bool
verbatimMode) (LP m () -> LP m ()) -> LP m () -> LP m ()
forall a b. (a -> b) -> a -> b
$ do
      ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput ParsecT [Tok] LaTeXState m [Tok]
-> ([Tok] -> ParsecT [Tok] LaTeXState m [Tok])
-> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> [Tok] -> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => Int -> [Tok] -> LP m [Tok]
doMacros' Int
1 ParsecT [Tok] LaTeXState m [Tok] -> ([Tok] -> LP m ()) -> LP m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [Tok] -> LP m ()
forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput
      (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{ sExpanded :: Bool
sExpanded = Bool
True }

doMacros' :: PandocMonad m => Int -> [Tok] -> LP m [Tok]
doMacros' :: 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 [Tok] LaTeXState m [Tok]
handleMacros Int
n SourcePos
spos Text
name [Tok]
ts
     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 [Tok] LaTeXState m [Tok]
handleMacros Int
n SourcePos
spos (Text
"end" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name) [Tok]
ts
     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 [Tok] LaTeXState m [Tok]
handleMacros Int
n SourcePos
spos Text
name [Tok]
ts
     [Tok]
_ -> [Tok] -> LP m [Tok]
forall (m :: * -> *) a. Monad m => a -> m a
return [Tok]
inp
   LP m [Tok] -> LP m [Tok] -> LP m [Tok]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Tok] -> LP m [Tok]
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 [Tok] LaTeXState m ()
matchPattern t Tok
toks = ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ())
-> ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ (Tok -> ParsecT [Tok] LaTeXState m Tok)
-> t Tok -> ParsecT [Tok] LaTeXState m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Tok -> ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Tok -> LP m Tok
matchTok t Tok
toks

    getargs :: Map Int [Tok]
-> [ArgSpec] -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
getargs Map Int [Tok]
argmap [] = Map Int [Tok] -> ParsecT [Tok] LaTeXState m (Map Int [Tok])
forall (m :: * -> *) a. Monad m => a -> m a
return Map Int [Tok]
argmap
    getargs Map Int [Tok]
argmap (Pattern [Tok]
toks : [ArgSpec]
rest) = ParsecT [Tok] LaTeXState m (Map Int [Tok])
-> ParsecT [Tok] LaTeXState m (Map Int [Tok])
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT [Tok] LaTeXState m (Map Int [Tok])
 -> ParsecT [Tok] LaTeXState m (Map Int [Tok]))
-> ParsecT [Tok] LaTeXState m (Map Int [Tok])
-> ParsecT [Tok] LaTeXState m (Map Int [Tok])
forall a b. (a -> b) -> a -> b
$ do
       [Tok] -> ParsecT [Tok] LaTeXState m ()
forall (t :: * -> *) (m :: * -> *).
(Foldable t, PandocMonad m) =>
t Tok -> ParsecT [Tok] LaTeXState m ()
matchPattern [Tok]
toks
       Map Int [Tok]
-> [ArgSpec] -> ParsecT [Tok] 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 [Tok] LaTeXState m (Map Int [Tok])
-> ParsecT [Tok] LaTeXState m (Map Int [Tok])
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT [Tok] LaTeXState m (Map Int [Tok])
 -> ParsecT [Tok] LaTeXState m (Map Int [Tok]))
-> ParsecT [Tok] LaTeXState m (Map Int [Tok])
-> ParsecT [Tok] 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 [Tok] LaTeXState m [[Tok]]
-> ParsecT [Tok] LaTeXState m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m ()
-> ParsecT [Tok] 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 [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m [Tok]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ((Tok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
:[]) (Tok -> [Tok])
-> ParsecT [Tok] LaTeXState m Tok
-> ParsecT [Tok] LaTeXState m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok))
                  ([Tok] -> ParsecT [Tok] LaTeXState m ()
forall (t :: * -> *) (m :: * -> *).
(Foldable t, PandocMonad m) =>
t Tok -> ParsecT [Tok] LaTeXState m ()
matchPattern [Tok]
toks)
        Map Int [Tok]
-> [ArgSpec] -> ParsecT [Tok] 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 [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m [Tok]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT [Tok] LaTeXState m [Tok]
 -> ParsecT [Tok] LaTeXState m [Tok])
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m [Tok]
forall a b. (a -> b) -> a -> b
$ ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces ParsecT [Tok] LaTeXState m ()
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracedOrToken
      Map Int [Tok]
-> [ArgSpec] -> ParsecT [Tok] 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
_ (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 (m :: * -> *) a. MonadPlus m => m a
mzero
            Just t Tok
xs -> (Tok -> [Tok] -> [Tok]) -> [Tok] -> t Tok -> [Tok]
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 (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 [Tok] LaTeXState m [Tok]
handleMacros Int
n' SourcePos
spos Text
name [Tok]
ts = do
      Bool
-> ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] 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 [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ())
-> ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ PandocError -> ParsecT [Tok] LaTeXState m ()
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (PandocError -> ParsecT [Tok] LaTeXState m ())
-> PandocError -> ParsecT [Tok] LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocMacroLoop Text
name
      Map Text Macro
macros <- LaTeXState -> Map Text Macro
sMacros (LaTeXState -> Map Text Macro)
-> ParsecT [Tok] LaTeXState m LaTeXState
-> ParsecT [Tok] LaTeXState m (Map Text Macro)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
      case Text -> Map Text Macro -> Maybe Macro
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Text
name Map Text Macro
macros of
           Maybe Macro
Nothing -> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *) a. MonadPlus m => m a
mzero
           Just (Macro ExpansionPoint
expansionPoint [ArgSpec]
argspecs Maybe [Tok]
optarg [Tok]
newtoks) -> do
             let getargs' :: ParsecT [Tok] 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 [Tok] 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 [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] 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 [Tok] 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 [Tok] 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
                   [Tok]
rest <- ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
                   (Map Int [Tok], [Tok])
-> ParsecT [Tok] LaTeXState m (Map Int [Tok], [Tok])
forall (m :: * -> *) a. Monad m => a -> m a
return (Map Int [Tok]
args, [Tok]
rest)
             LaTeXState
lstate <- ParsecT [Tok] 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
     [Tok] LaTeXState m (Either ParseError (Map Int [Tok], [Tok]))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either ParseError (Map Int [Tok], [Tok]))
 -> ParsecT
      [Tok] LaTeXState m (Either ParseError (Map Int [Tok], [Tok])))
-> m (Either ParseError (Map Int [Tok], [Tok]))
-> ParsecT
     [Tok] LaTeXState m (Either ParseError (Map Int [Tok], [Tok]))
forall a b. (a -> b) -> a -> b
$ ParsecT [Tok] LaTeXState m (Map Int [Tok], [Tok])
-> LaTeXState
-> String
-> [Tok]
-> m (Either ParseError (Map Int [Tok], [Tok]))
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> u -> String -> s -> m (Either ParseError a)
runParserT ParsecT [Tok] LaTeXState m (Map Int [Tok], [Tok])
getargs' LaTeXState
lstate String
"args" [Tok]
ts
             case Either ParseError (Map Int [Tok], [Tok])
res of
               Left ParseError
_ -> String -> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *) a. MonadFail m => String -> m a
Prelude.fail (String -> ParsecT [Tok] LaTeXState m [Tok])
-> String -> ParsecT [Tok] LaTeXState m [Tok]
forall a b. (a -> b) -> a -> b
$ String
"Could not parse arguments for " String -> ShowS
forall a. [a] -> [a] -> [a]
++
                                Text -> String
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 (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 [Tok] 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 [Tok] LaTeXState m [Tok]
forall (m :: * -> *) a. Monad m => a -> m a
return [Tok]
result

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 :: 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 :: 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

spaces :: PandocMonad m => LP m ()
spaces :: LP m ()
spaces = ParsecT [Tok] LaTeXState m Tok -> LP m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany ((Tok -> Bool) -> ParsecT [Tok] 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 :: LP m ()
spaces1 = ParsecT [Tok] LaTeXState m Tok -> LP m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
skipMany1 ((Tok -> Bool) -> ParsecT [Tok] 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 (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [TokType]
toktypes

controlSeq :: PandocMonad m => Text -> LP m Tok
controlSeq :: 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 :: 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 :: String -> LP m Tok
symbolIn String
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 -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` String
cs
                                       Maybe (Char, Text)
_          -> Bool
False
        isInCs Tok
_ = Bool
False

sp :: PandocMonad m => LP m ()
sp :: 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 (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 (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 (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 :: LP m ()
whitespace = () () -> ParsecT [Tok] LaTeXState m Tok -> LP m ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Tok -> Bool) -> ParsecT [Tok] 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 :: LP m ()
newlineTok = () () -> ParsecT [Tok] LaTeXState m Tok -> LP m ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Tok -> Bool) -> ParsecT [Tok] 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 :: LP m ()
comment = () () -> ParsecT [Tok] LaTeXState m Tok -> LP m ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Tok -> Bool) -> ParsecT [Tok] 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 :: 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)

singleChar :: PandocMonad m => LP m Tok
singleChar :: LP m Tok
singleChar = 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 -> LP m Tok) -> LP m Tok -> LP m Tok
forall a b. (a -> b) -> a -> b
$ do
  Tok SourcePos
pos TokType
toktype Text
t <- (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok ([TokType] -> Tok -> Bool
tokTypeIn [TokType
Word, TokType
Symbol])
  Bool -> ParsecT [Tok] LaTeXState m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> ParsecT [Tok] LaTeXState m ())
-> Bool -> ParsecT [Tok] LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ TokType
toktype TokType -> TokType -> Bool
forall a. Eq a => a -> a -> Bool
== TokType
Symbol Bool -> Bool -> Bool
&&
                (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
  if Text -> Int
T.length Text
t Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1
     then do
       let (Text
t1, Text
t2) = (Int -> Text -> Text
T.take Int
1 Text
t, Int -> Text -> Text
T.drop Int
1 Text
t)
       [Tok]
inp <- ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
       [Tok] -> ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput ([Tok] -> ParsecT [Tok] LaTeXState m ())
-> [Tok] -> ParsecT [Tok] LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ 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
       Tok -> LP m Tok
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
toktype Text
t1
     else Tok -> LP m Tok
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
toktype Text
t

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

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

blankline :: PandocMonad m => LP m ()
blankline :: LP m ()
blankline = LP m () -> LP m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (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 (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
newlineTok

primEscape :: PandocMonad m => LP m Char
primEscape :: 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 (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 (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 -> String -> LP m Char
forall (m :: * -> *) a. MonadFail m => String -> m a
Prelude.fail String
"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 (m :: * -> *) a. Monad m => a -> m a
return (Int -> Char
chr Int
x)
                    Maybe Int
Nothing -> String -> LP m Char
forall (m :: * -> *) a. MonadFail m => String -> m a
Prelude.fail (String -> LP m Char) -> String -> LP m Char
forall a b. (a -> b) -> a -> b
$ String
"Could not read: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> String
T.unpack Text
t
       TokType
_    -> String -> LP m Char
forall (m :: * -> *) a. MonadFail m => String -> m a
Prelude.fail String
"Expected an Esc1 or Esc2 token" -- should not happen

bgroup :: PandocMonad m => LP m Tok
bgroup :: LP m Tok
bgroup = 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 -> LP m Tok) -> LP m Tok -> LP m Tok
forall a b. (a -> b) -> a -> b
$ do
  ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp
  Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{' LP m Tok -> LP m Tok -> LP m Tok
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"bgroup" LP m Tok -> LP m Tok -> LP m Tok
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"begingroup"

egroup :: PandocMonad m => LP m Tok
egroup :: LP m Tok
egroup = Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'}' LP m Tok -> LP m Tok -> LP m Tok
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"egroup" LP m Tok -> LP m Tok -> LP m Tok
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"endgroup"

grouped :: (PandocMonad m,  Monoid a) => LP m a -> LP m a
grouped :: 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 (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 (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ([a] -> a
forall a. Monoid a => [a] -> a
mconcat ([a] -> a) -> ParsecT [Tok] 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 [Tok] 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 -> Int -> LP m [Tok]
braced' :: LP m Tok -> Int -> LP m [Tok]
braced' LP m Tok
getTok Int
n =
  LP m [Tok]
handleEgroup LP m [Tok] -> LP m [Tok] -> LP m [Tok]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m [Tok]
handleBgroup LP m [Tok] -> LP m [Tok] -> LP m [Tok]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m [Tok]
handleOther
  where handleEgroup :: LP m [Tok]
handleEgroup = do
          Tok
t <- Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'}'
          if Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1
             then [Tok] -> LP m [Tok]
forall (m :: * -> *) a. Monad m => a -> m a
return []
             else (Tok
tTok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
:) ([Tok] -> [Tok]) -> LP m [Tok] -> LP m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m Tok -> Int -> LP m [Tok]
forall (m :: * -> *).
PandocMonad m =>
LP m Tok -> Int -> LP m [Tok]
braced' LP m Tok
getTok (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
        handleBgroup :: LP m [Tok]
handleBgroup = do
          Tok
t <- Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{'
          (Tok
tTok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
:) ([Tok] -> [Tok]) -> LP m [Tok] -> LP m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m Tok -> Int -> LP m [Tok]
forall (m :: * -> *).
PandocMonad m =>
LP m Tok -> Int -> LP m [Tok]
braced' LP m Tok
getTok (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
        handleOther :: LP m [Tok]
handleOther = do
          Tok
t <- LP m Tok
getTok
          (Tok
tTok -> [Tok] -> [Tok]
forall a. a -> [a] -> [a]
:) ([Tok] -> [Tok]) -> LP m [Tok] -> LP m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m Tok -> Int -> LP m [Tok]
forall (m :: * -> *).
PandocMonad m =>
LP m Tok -> Int -> LP m [Tok]
braced' LP m Tok
getTok Int
n

braced :: PandocMonad m => LP m [Tok]
braced :: LP m [Tok]
braced = Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{' LP m Tok -> LP m [Tok] -> LP m [Tok]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> LP m Tok -> Int -> LP m [Tok]
forall (m :: * -> *).
PandocMonad m =>
LP m Tok -> Int -> LP m [Tok]
braced' LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok Int
1

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

-- For handling URLs, which allow literal % characters...
retokenizeComment :: PandocMonad m => LP m ()
retokenizeComment :: 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
$ String -> Text -> [Tok]
tokenize (SourcePos -> String
sourceName SourcePos
pos) (Text -> [Tok]) -> Text -> [Tok]
forall a b. (a -> b) -> a -> b
$ Text -> Text
T.tail Text
txt
  ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput ParsecT [Tok] LaTeXState m [Tok] -> ([Tok] -> LP m ()) -> LP m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [Tok] -> LP m ()
forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput ([Tok] -> LP m ()) -> ([Tok] -> [Tok]) -> [Tok] -> LP m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((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]
++))
    LP m () -> LP m () -> LP m ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> () -> LP m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

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

bracketed :: PandocMonad m => Monoid a => LP m a -> LP m a
bracketed :: 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 [Tok] 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 [Tok] 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 :: 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 [Tok] 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 [Tok] 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 [Tok] LaTeXState m ([Tok], [Tok]) -> LP m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m [Tok] -> ParsecT [Tok] 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 (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 :: 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 [Tok] 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 [Tok] 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 :: LP m Text
dimenarg = 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
  ParsecT [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp
  Bool
ch  <- Bool
-> ParsecT [Tok] LaTeXState m Bool
-> ParsecT [Tok] 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 [Tok] LaTeXState m Bool
 -> ParsecT [Tok] LaTeXState m Bool)
-> ParsecT [Tok] LaTeXState m Bool
-> ParsecT [Tok] LaTeXState m Bool
forall a b. (a -> b) -> a -> b
$ Bool
True Bool
-> ParsecT [Tok] LaTeXState m Tok
-> ParsecT [Tok] LaTeXState m Bool
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Char -> ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'='
  Text
minus <- 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) -> LP m Text -> LP m Text
forall a b. (a -> b) -> a -> b
$ Text
"-" Text -> ParsecT [Tok] LaTeXState m Tok -> LP m Text
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Char -> ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'-'
  Tok SourcePos
_ TokType
_ Text
s1 <- (Tok -> Bool) -> ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isWordTok
  Text
s2 <- 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) -> LP m Text -> LP m Text
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
          Char -> ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'.'
          Tok SourcePos
_ TokType
_ Text
t <-  (Tok -> Bool) -> ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isWordTok
          Text -> LP m Text
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 [Tok] LaTeXState m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> ParsecT [Tok] LaTeXState m ())
-> Bool -> ParsecT [Tok] 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 [Tok] LaTeXState m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> ParsecT [Tok] LaTeXState m ())
-> Bool -> ParsecT [Tok] LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ Text
rest Text -> [Text] -> 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 -> LP m Text
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> LP m Text) -> Text -> LP m Text
forall a b. (a -> b) -> a -> b
$ String -> 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 -> ParserT s u m a
ignore :: Text -> ParserT 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 -> ParserT 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 :: LP m a -> LP m (a, [Tok])
withRaw LP m a
parser = do
  [Tok]
inp <- ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
  a
result <- LP m a
parser
  Maybe SourcePos
nxtpos <- Maybe SourcePos
-> ParsecT [Tok] LaTeXState m (Maybe SourcePos)
-> ParsecT [Tok] LaTeXState m (Maybe SourcePos)
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Maybe SourcePos
forall a. Maybe a
Nothing ((\(Tok SourcePos
pos' TokType
_ Text
_) -> SourcePos -> Maybe SourcePos
forall a. a -> Maybe a
Just SourcePos
pos') (Tok -> Maybe SourcePos)
-> ParsecT [Tok] LaTeXState m Tok
-> ParsecT [Tok] LaTeXState m (Maybe SourcePos)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m Tok -> ParsecT [Tok] LaTeXState m Tok
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok)
  let raw :: [Tok]
raw = (Tok -> Bool) -> [Tok] -> [Tok]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (\(Tok SourcePos
pos TokType
_ Text
_) -> Bool -> (SourcePos -> Bool) -> Maybe SourcePos -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True
                  (\SourcePos
p -> SourcePos -> String
sourceName SourcePos
p String -> String -> Bool
forall a. Eq a => a -> a -> Bool
/= SourcePos -> String
sourceName SourcePos
pos Bool -> Bool -> Bool
|| SourcePos
pos SourcePos -> SourcePos -> Bool
forall a. Ord a => a -> a -> Bool
< SourcePos
p) Maybe SourcePos
nxtpos) [Tok]
inp
  (a, [Tok]) -> LP m (a, [Tok])
forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, [Tok]
raw)

keyval :: PandocMonad m => LP m (Text, Text)
keyval :: LP m (Text, Text)
keyval = LP m (Text, Text) -> LP m (Text, Text)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (LP m (Text, Text) -> LP m (Text, Text))
-> LP m (Text, Text) -> LP m (Text, Text)
forall a b. (a -> b) -> a -> b
$ do
  Tok SourcePos
_ TokType
Word Text
key <- (Tok -> Bool) -> LP m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isWordTok
  LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp
  Text
val <- Text
-> ParsecT [Tok] LaTeXState m Text
-> ParsecT [Tok] 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 [Tok] LaTeXState m Text
 -> ParsecT [Tok] LaTeXState m Text)
-> ParsecT [Tok] LaTeXState m Text
-> ParsecT [Tok] 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
'='
           LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp
           ([Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced) ParsecT [Tok] LaTeXState m Text
-> ParsecT [Tok] LaTeXState m Text
-> ParsecT [Tok] LaTeXState m Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
             ([Text] -> Text
forall a. Monoid a => [a] -> a
mconcat ([Text] -> Text)
-> ParsecT [Tok] LaTeXState m [Text]
-> ParsecT [Tok] LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m Text
-> ParsecT [Tok] 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 [Tok] LaTeXState m ([Tok], [Tok])
-> ParsecT [Tok] LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m ([Tok], [Tok])
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
withRaw ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced)
                 ParsecT [Tok] LaTeXState m Text
-> ParsecT [Tok] LaTeXState m Text
-> ParsecT [Tok] LaTeXState m Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                 ([Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m Tok -> ParsecT [Tok] LaTeXState m [Tok]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1
                      ((Tok -> Bool) -> LP 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)))))
  LP m Tok -> LP m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional (Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
',')
  LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp
  (Text, Text) -> LP m (Text, Text)
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 :: LP m [(Text, Text)]
keyvals = LP m [(Text, Text)] -> LP m [(Text, Text)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (LP m [(Text, Text)] -> LP m [(Text, Text)])
-> LP m [(Text, Text)] -> LP 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 -> LP m [(Text, Text)] -> LP m [(Text, Text)]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT [Tok] LaTeXState m (Text, Text)
-> LP m Tok -> LP 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 [Tok] 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
']') LP m [(Text, Text)]
-> ParsecT [Tok] LaTeXState m () -> LP m [(Text, Text)]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp

verbEnv :: PandocMonad m => Text -> LP m Text
verbEnv :: 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 [Tok] LaTeXState m () -> ParsecT [Tok] LaTeXState m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
blankline
  [Tok]
res <- ParsecT [Tok] LaTeXState m Tok
-> ParsecT [Tok] LaTeXState m ()
-> ParsecT [Tok] 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 [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok (Text -> ParsecT [Tok] LaTeXState m ()
forall (m :: * -> *). PandocMonad m => Text -> LP m ()
end_ Text
name)
  Text -> LP m Text
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) = 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_ :: Text -> LP m ()
begin_ Text
t = LP m () -> LP 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"
  LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  Text
txt <- [Tok] -> Text
untokenize ([Tok] -> Text)
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
  Bool -> LP m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Text
t Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
txt)) LP m () -> String -> LP m ()
forall s u (m :: * -> *) a.
ParsecT s u m a -> String -> ParsecT s u m a
<?> (String
"\\begin{" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Text -> String
T.unpack Text
t String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"}")

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

getRawCommand :: PandocMonad m => Text -> Text -> LP m Text
getRawCommand :: 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 [Tok] LaTeXState m Tok -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT [Tok] LaTeXState m Tok -> LP m ())
-> ParsecT [Tok] LaTeXState m Tok -> LP m ()
forall a b. (a -> b) -> a -> b
$ (Tok -> Bool) -> ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
isWordTok -- digits
             ParsecT [Tok] LaTeXState m [Tok] -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
           Text
"titleformat" -> do
             ParsecT [Tok] LaTeXState m [Tok] -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
             LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
skipopts
             ParsecT [Tok] LaTeXState m [[Tok]] -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT [Tok] LaTeXState m [[Tok]] -> LP m ())
-> ParsecT [Tok] LaTeXState m [[Tok]] -> LP m ()
forall a b. (a -> b) -> a -> b
$ Int
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] 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 [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
           Text
"def" ->
             ParsecT [Tok] LaTeXState m [Tok] -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT [Tok] LaTeXState m [Tok] -> LP m ())
-> ParsecT [Tok] LaTeXState m [Tok] -> LP m ()
forall a b. (a -> b) -> a -> b
$ ParsecT [Tok] LaTeXState m Tok
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] 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 [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
           Text
_ | Text -> Bool
isFontSizeCommand Text
name -> () -> LP m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
             | 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 [Tok] LaTeXState m [[Tok]] -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT [Tok] LaTeXState m [[Tok]] -> LP m ())
-> ParsecT [Tok] LaTeXState m [[Tok]] -> LP m ()
forall a b. (a -> b) -> a -> b
$ ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m [[Tok]]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
  Text -> LP m Text
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

skipopts :: PandocMonad m => LP m ()
skipopts :: LP m ()
skipopts = LP m () -> LP m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany (ParsecT [Tok] LaTeXState m Text -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void ParsecT [Tok] LaTeXState m Text
forall (m :: * -> *). PandocMonad m => LP m Text
overlaySpecification LP m () -> LP m () -> LP m ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT [Tok] LaTeXState m Text -> LP m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void ParsecT [Tok] 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 :: LP m Text
overlaySpecification = 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
  Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'<'
  Text
t <- [Tok] -> Text
untokenize ([Tok] -> Text) -> ParsecT [Tok] LaTeXState m [Tok] -> LP m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m Tok -> LP m Tok -> ParsecT [Tok] 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 [Tok] LaTeXState m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> ParsecT [Tok] LaTeXState m ())
-> Bool -> ParsecT [Tok] 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 (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"beamer",Text
"presentation", Text
"trans",
                    Text
"handout",Text
"article", Text
"second"]
  Text -> LP m Text
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 -> 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 :: 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 (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 :: LP m Text
rawopt = 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
  Text
inner <- [Tok] -> Text
untokenize ([Tok] -> Text) -> ParsecT [Tok] LaTeXState m [Tok] -> LP m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracketedToks
  LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp
  Text -> LP m Text
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 -> 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 :: (LaTeXState -> DottedNum) -> LP m DottedNum
getNextNumber LaTeXState -> DottedNum
getCurrentNum = do
  LaTeXState
st <- ParsecT [Tok] 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 (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]