    {-# LANGUAGE ScopedTypeVariables #-}
    {-# LANGUAGE OverloadedStrings #-}
    module Text.Pandoc.Readers.LaTeX.Citation
      ( citationCommands
      , cites
      )
    where

    import Text.Pandoc.Class
    import Text.Pandoc.Readers.LaTeX.Parsing
    import Text.Pandoc.Builder as B
    import qualified Data.Map as M
    import Data.Text (Text)
    import Control.Applicative ((<|>), optional, many)
    import Control.Monad (mzero)
    import Control.Monad.Trans (lift)
    import Control.Monad.Except (throwError)
    import Text.Pandoc.Parsing hiding (blankline, many, mathDisplay, mathInline,
                                optional, space, spaces, withRaw, (<|>))

    citationCommands :: PandocMonad m => LP m Inlines -> M.Map Text (LP m Inlines)
    citationCommands :: forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> Map Text (LP m Inlines)
    citationCommands LP m Inlines
    inline =
      let citation :: Text -> CitationMode -> Bool -> LP m Inlines
    citation = LP m Inlines -> Text -> CitationMode -> Bool -> LP m Inlines
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> Text -> CitationMode -> Bool -> LP m Inlines
    citationWith LP m Inlines
    inline
          tok :: LP m Inlines
    tok = LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    spaces LP m () -> LP m Inlines -> LP m Inlines
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> LP m Inlines -> LP m Inlines
    forall (m :: * -> *) a.
    (PandocMonad m, Monoid a) =>
    LP m a -> LP m a
    grouped LP m Inlines
    inline
       in [(Text, LP m Inlines)] -> Map Text (LP m Inlines)
    forall k a. Ord k => [(k, a)] -> Map k a
    M.fromList
      [ (Text
    "cite", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "cite" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "Cite", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Cite" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "citep", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "citep" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "citep*", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "citep*" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "citeal", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "citeal" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "citealp", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "citealp" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "citealp*", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "citealp*" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "autocite", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "autocite" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "smartcite", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "smartcite" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "footcite", Inlines -> Inlines
    inNote (Inlines -> Inlines) -> LP m Inlines -> LP m Inlines
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "footcite" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "parencite", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "parencite" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "supercite", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "supercite" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "footcitetext", Inlines -> Inlines
    inNote (Inlines -> Inlines) -> LP m Inlines -> LP m Inlines
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "footcitetext" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "citeyearpar", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "citeyearpar" CitationMode
    SuppressAuthor Bool
    False)
      , (Text
    "citeyear", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "citeyear" CitationMode
    SuppressAuthor Bool
    False)
      , (Text
    "autocite*", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "autocite*" CitationMode
    SuppressAuthor Bool
    False)
      , (Text
    "cite*", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "cite*" CitationMode
    SuppressAuthor Bool
    False)
      , (Text
    "parencite*", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "parencite*" CitationMode
    SuppressAuthor Bool
    False)
      , (Text
    "textcite", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "textcite" CitationMode
    AuthorInText Bool
    False)
      , (Text
    "citet", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "citet" CitationMode
    AuthorInText Bool
    False)
      , (Text
    "citet*", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "citet*" CitationMode
    AuthorInText Bool
    False)
      , (Text
    "citealt", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "citealt" CitationMode
    AuthorInText Bool
    False)
      , (Text
    "citealt*", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "citealt*" CitationMode
    AuthorInText Bool
    False)
      , (Text
    "textcites", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "textcites" CitationMode
    AuthorInText Bool
    True)
      , (Text
    "cites", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "cites" CitationMode
    NormalCitation Bool
    True)
      , (Text
    "autocites", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "autocites" CitationMode
    NormalCitation Bool
    True)
      , (Text
    "footcites", Inlines -> Inlines
    inNote (Inlines -> Inlines) -> LP m Inlines -> LP m Inlines
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "footcites" CitationMode
    NormalCitation Bool
    True)
      , (Text
    "parencites", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "parencites" CitationMode
    NormalCitation Bool
    True)
      , (Text
    "supercites", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "supercites" CitationMode
    NormalCitation Bool
    True)
      , (Text
    "footcitetexts", Inlines -> Inlines
    inNote (Inlines -> Inlines) -> LP m Inlines -> LP m Inlines
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "footcitetexts" CitationMode
    NormalCitation Bool
    True)
      , (Text
    "Autocite", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Autocite" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "Smartcite", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Smartcite" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "Footcite", Inlines -> Inlines
    inNote (Inlines -> Inlines) -> LP m Inlines -> LP m Inlines
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Footcite" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "Parencite", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Parencite" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "Supercite", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Supercite" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "Footcitetext", Inlines -> Inlines
    inNote (Inlines -> Inlines) -> LP m Inlines -> LP m Inlines
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Footcitetext" CitationMode
    NormalCitation Bool
    False)
      , (Text
    "Citeyearpar", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Citeyearpar" CitationMode
    SuppressAuthor Bool
    False)
      , (Text
    "Citeyear", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Citeyear" CitationMode
    SuppressAuthor Bool
    False)
      , (Text
    "Autocite*", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Autocite*" CitationMode
    SuppressAuthor Bool
    False)
      , (Text
    "Cite*", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Cite*" CitationMode
    SuppressAuthor Bool
    False)
      , (Text
    "Parencite*", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Parencite*" CitationMode
    SuppressAuthor Bool
    False)
      , (Text
    "Textcite", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Textcite" CitationMode
    AuthorInText Bool
    False)
      , (Text
    "Textcites", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Textcites" CitationMode
    AuthorInText Bool
    True)
      , (Text
    "Cites", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Cites" CitationMode
    NormalCitation Bool
    True)
      , (Text
    "Autocites", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Autocites" CitationMode
    NormalCitation Bool
    True)
      , (Text
    "Footcites", Inlines -> Inlines
    inNote (Inlines -> Inlines) -> LP m Inlines -> LP m Inlines
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Footcites" CitationMode
    NormalCitation Bool
    True)
      , (Text
    "Parencites", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Parencites" CitationMode
    NormalCitation Bool
    True)
      , (Text
    "Supercites", Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Supercites" CitationMode
    NormalCitation Bool
    True)
      , (Text
    "Footcitetexts", Inlines -> Inlines
    inNote (Inlines -> Inlines) -> LP m Inlines -> LP m Inlines
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "Footcitetexts" CitationMode
    NormalCitation Bool
    True)
      , (Text
    "citetext", LP m Inlines -> CitationMode -> LP m Inlines
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> CitationMode -> LP m Inlines
    complexNatbibCitation LP m Inlines
    inline CitationMode
    NormalCitation)
      , (Text
    "citeauthor", (ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Tok
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (LP m Inlines
    tok LP m Inlines -> LP m () -> LP m ()
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    sp LP m ()
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Tok
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> Text -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq Text
    "citetext") ParsecT TokStream LaTeXState m Tok -> LP m Inlines -> LP m Inlines
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *>
                            LP m Inlines -> CitationMode -> LP m Inlines
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> CitationMode -> LP m Inlines
    complexNatbibCitation LP m Inlines
    inline CitationMode
    AuthorInText)
                       LP m Inlines -> LP m Inlines -> LP m Inlines
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "citeauthor" CitationMode
    AuthorInText Bool
    False)
      , (Text
    "nocite", Inlines
    forall a. Monoid a => a
    mempty Inlines -> LP m () -> LP m Inlines
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ (Text -> CitationMode -> Bool -> LP m Inlines
    citation Text
    "nocite" CitationMode
    NormalCitation Bool
    False LP m Inlines -> (Inlines -> LP m ()) -> LP m ()
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> (a -> ParsecT TokStream LaTeXState m b)
    -> ParsecT TokStream LaTeXState m b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>=
                              Text -> Inlines -> LP m ()
    forall (m :: * -> *) a.
    (PandocMonad m, ToMetaValue a) =>
    Text -> a -> LP m ()
    addMeta Text
    "nocite"))
      ]

    -- citations

    addPrefix :: [Inline] -> [Citation] -> [Citation]
    addPrefix :: [Inline] -> [Citation] -> [Citation]
    addPrefix [Inline]
    p (Citation
    k:[Citation]
    ks) = Citation
    k {citationPrefix = p ++ citationPrefix k} Citation -> [Citation] -> [Citation]
    forall a. a -> [a] -> [a]
    : [Citation]
    ks
    addPrefix [Inline]
    _ [Citation]
    _      = []

    addSuffix :: [Inline] -> [Citation] -> [Citation]
    addSuffix :: [Inline] -> [Citation] -> [Citation]
    addSuffix [Inline]
    s ks :: [Citation]
    ks@(Citation
    _:[Citation]
    _) =
      let k :: Citation
    k = [Citation] -> Citation
    forall a. HasCallStack => [a] -> a
    last [Citation]
    ks
      in  [Citation] -> [Citation]
    forall a. HasCallStack => [a] -> [a]
    init [Citation]
    ks [Citation] -> [Citation] -> [Citation]
    forall a. [a] -> [a] -> [a]
    ++ [Citation
    k {citationSuffix = citationSuffix k ++ s}]
    addSuffix [Inline]
    _ [Citation]
    _ = []

    simpleCiteArgs :: forall m . PandocMonad m => LP m Inlines -> LP m [Citation]
    simpleCiteArgs :: forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> LP m [Citation]
    simpleCiteArgs LP m Inlines
    inline = ParsecT TokStream LaTeXState m [Citation]
    -> ParsecT TokStream LaTeXState m [Citation]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT TokStream LaTeXState m [Citation]
     -> ParsecT TokStream LaTeXState m [Citation])
    -> ParsecT TokStream LaTeXState m [Citation]
    -> ParsecT TokStream LaTeXState m [Citation]
    forall a b. (a -> b) -> a -> b
    $ do
      first  <- ParsecT TokStream LaTeXState m [Inline]
    -> ParsecT TokStream LaTeXState m (Maybe [Inline])
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m (Maybe a)
    optionMaybe (ParsecT TokStream LaTeXState m [Inline]
     -> ParsecT TokStream LaTeXState m (Maybe [Inline]))
    -> ParsecT TokStream LaTeXState m [Inline]
    -> ParsecT TokStream LaTeXState m (Maybe [Inline])
    forall a b. (a -> b) -> a -> b
    $ Inlines -> [Inline]
    forall a. Many a -> [a]
    toList (Inlines -> [Inline])
    -> LP m Inlines -> ParsecT TokStream LaTeXState m [Inline]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m Inlines
    PandocMonad m => LP m Inlines
    opt
      second <- optionMaybe $ toList <$> opt
      keys <- try $ bgroup *> manyTill citationLabel egroup
      let (pre, suf) = case (first  , second ) of
            (Just [Inline]
    s , Maybe [Inline]
    Nothing) -> ([Inline]
    forall a. Monoid a => a
    mempty, [Inline]
    s )
            (Just [Inline]
    s , Just [Inline]
    t ) -> ([Inline]
    s , [Inline]
    t )
            (Maybe [Inline], Maybe [Inline])
    _                  -> ([Inline]
    forall a. Monoid a => a
    mempty, [Inline]
    forall a. Monoid a => a
    mempty)
          conv Text
    k = Citation { citationId :: Text
    citationId      = Text
    k
                            , citationPrefix :: [Inline]
    citationPrefix  = []
                            , citationSuffix :: [Inline]
    citationSuffix  = []
                            , citationMode :: CitationMode
    citationMode    = CitationMode
    NormalCitation
                            , citationHash :: Int
    citationHash    = Int
    0
                            , citationNoteNum :: Int
    citationNoteNum = Int
    0
                            }
      return $ addPrefix pre $ addSuffix suf $ map conv keys
     where
      opt :: PandocMonad m => LP m Inlines
      opt :: PandocMonad m => LP m Inlines
    opt = do
        toks <- ParsecT TokStream LaTeXState m [Tok]
    -> ParsecT TokStream LaTeXState m [Tok]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    sp LP m ()
    -> ParsecT TokStream LaTeXState m [Tok]
    -> ParsecT TokStream LaTeXState m [Tok]
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> ParsecT TokStream LaTeXState m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    bracketedToks ParsecT TokStream LaTeXState m [Tok]
    -> LP m () -> ParsecT TokStream LaTeXState m [Tok]
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    sp)
        -- now parse the toks as inlines
        st <- getState
        parsed <- lift $
          runParserT (mconcat <$> many inline) st "bracketed option"
           (TokStream False toks)
        case parsed of
          Right Inlines
    result -> Inlines -> LP m Inlines
    forall a. a -> ParsecT TokStream LaTeXState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Inlines
    result
          Left ParseError
    e       -> PandocError -> LP m Inlines
    forall a. PandocError -> ParsecT TokStream LaTeXState m a
    forall e (m :: * -> *) a. MonadError e m => e -> m a
    throwError (PandocError -> LP m Inlines) -> PandocError -> LP m Inlines
    forall a b. (a -> b) -> a -> b
    $ Sources -> ParseError -> PandocError
    fromParsecError ([Tok] -> Sources
    forall a. ToSources a => a -> Sources
    toSources [Tok]
    toks) ParseError
    e

    citationLabel :: PandocMonad m => LP m Text
    citationLabel :: forall (m :: * -> *). PandocMonad m => LP m Text
    citationLabel  = do
      LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    sp
      [Tok] -> Text
    untokenize ([Tok] -> Text)
    -> ParsecT TokStream LaTeXState m [Tok]
    -> ParsecT TokStream LaTeXState m Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
        (ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m [Tok]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
    many1 ((Tok -> Bool) -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
    satisfyTok Tok -> Bool
    isWordTok ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Tok
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> String -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => String -> LP m Tok
    symbolIn String
    bibtexKeyChar)
              ParsecT TokStream LaTeXState m [Tok]
    -> LP m () -> ParsecT TokStream LaTeXState m [Tok]
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    sp
              ParsecT TokStream LaTeXState m [Tok]
    -> ParsecT TokStream LaTeXState m (Maybe Tok)
    -> ParsecT TokStream LaTeXState m [Tok]
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m (Maybe Tok)
    forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
    optional (Char -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    ',')
              ParsecT TokStream LaTeXState m [Tok]
    -> LP m () -> ParsecT TokStream LaTeXState m [Tok]
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    sp)
      where bibtexKeyChar :: String
    bibtexKeyChar = String
    ".:;?!`'()/*@_+=-&[]" :: [Char]

    cites :: PandocMonad m
          => LP m Inlines -> CitationMode -> Bool -> LP m [Citation]
    cites :: forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> CitationMode -> Bool -> LP m [Citation]
    cites LP m Inlines
    inline CitationMode
    mode Bool
    multi = ParsecT TokStream LaTeXState m [Citation]
    -> ParsecT TokStream LaTeXState m [Citation]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT TokStream LaTeXState m [Citation]
     -> ParsecT TokStream LaTeXState m [Citation])
    -> ParsecT TokStream LaTeXState m [Citation]
    -> ParsecT TokStream LaTeXState m [Citation]
    forall a b. (a -> b) -> a -> b
    $ do
      let paropt :: LP m Inlines
    paropt = LP m Inlines -> LP m Inlines
    forall (m :: * -> *) a.
    (PandocMonad m, Monoid a) =>
    LP m a -> LP m a
    parenWrapped LP m Inlines
    inline
      cits <- if Bool
    multi
                 then do
                   multiprenote <- ParsecT TokStream LaTeXState m [Inline]
    -> ParsecT TokStream LaTeXState m (Maybe [Inline])
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m (Maybe a)
    optionMaybe (ParsecT TokStream LaTeXState m [Inline]
     -> ParsecT TokStream LaTeXState m (Maybe [Inline]))
    -> ParsecT TokStream LaTeXState m [Inline]
    -> ParsecT TokStream LaTeXState m (Maybe [Inline])
    forall a b. (a -> b) -> a -> b
    $ Inlines -> [Inline]
    forall a. Many a -> [a]
    toList (Inlines -> [Inline])
    -> LP m Inlines -> ParsecT TokStream LaTeXState m [Inline]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m Inlines
    paropt
                   multipostnote <- optionMaybe $ toList <$> paropt
                   let (pre, suf) = case (multiprenote, multipostnote) of
                         (Just [Inline]
    s , Maybe [Inline]
    Nothing) -> ([Inline]
    forall a. Monoid a => a
    mempty, [Inline]
    s)
                         (Maybe [Inline]
    Nothing , Just [Inline]
    t) -> ([Inline]
    forall a. Monoid a => a
    mempty, [Inline]
    t)
                         (Just [Inline]
    s , Just [Inline]
    t ) -> ([Inline]
    s, [Inline]
    t)
                         (Maybe [Inline], Maybe [Inline])
    _                  -> ([Inline]
    forall a. Monoid a => a
    mempty, [Inline]
    forall a. Monoid a => a
    mempty)
                   tempCits <- many1 $ simpleCiteArgs inline
                   case tempCits of
                     ([Citation]
    k:[[Citation]]
    ks) -> case [[Citation]]
    ks of
                                 ([Citation]
    _:[[Citation]]
    _) -> [[Citation]] -> ParsecT TokStream LaTeXState m [[Citation]]
    forall a. a -> ParsecT TokStream LaTeXState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ([[Citation]] -> ParsecT TokStream LaTeXState m [[Citation]])
    -> [[Citation]] -> ParsecT TokStream LaTeXState m [[Citation]]
    forall a b. (a -> b) -> a -> b
    $ ([Inline] -> [Citation] -> [Citation]
    addMprenote [Inline]
    pre [Citation]
    k [Citation] -> [[Citation]] -> [[Citation]]
    forall a. a -> [a] -> [a]
    : [[Citation]] -> [[Citation]]
    forall a. HasCallStack => [a] -> [a]
    init [[Citation]]
    ks) [[Citation]] -> [[Citation]] -> [[Citation]]
    forall a. [a] -> [a] -> [a]
    ++
                                                     [[Inline] -> [Citation] -> [Citation]
    addMpostnote [Inline]
    suf ([[Citation]] -> [Citation]
    forall a. HasCallStack => [a] -> a
    last [[Citation]]
    ks)]
                                 [[Citation]]
    _ -> [[Citation]] -> ParsecT TokStream LaTeXState m [[Citation]]
    forall a. a -> ParsecT TokStream LaTeXState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return [[Inline] -> [Citation] -> [Citation]
    addMprenote [Inline]
    pre ([Inline] -> [Citation] -> [Citation]
    addMpostnote [Inline]
    suf [Citation]
    k)]
                     [[Citation]]
    _ -> [[Citation]] -> ParsecT TokStream LaTeXState m [[Citation]]
    forall a. a -> ParsecT TokStream LaTeXState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return [[]]
                 else Int
    -> ParsecT TokStream LaTeXState m [Citation]
    -> ParsecT TokStream LaTeXState m [[Citation]]
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    Int -> ParsecT s u m a -> ParsecT s u m [a]
    count Int
    1 (ParsecT TokStream LaTeXState m [Citation]
     -> ParsecT TokStream LaTeXState m [[Citation]])
    -> ParsecT TokStream LaTeXState m [Citation]
    -> ParsecT TokStream LaTeXState m [[Citation]]
    forall a b. (a -> b) -> a -> b
    $ LP m Inlines -> ParsecT TokStream LaTeXState m [Citation]
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> LP m [Citation]
    simpleCiteArgs LP m Inlines
    inline
      let cs = [[Citation]] -> [Citation]
    forall (t :: * -> *) a. Foldable t => t [a] -> [a]
    concat [[Citation]]
    cits
      return $ case mode of
            CitationMode
    AuthorInText -> case [Citation]
    cs of
                                 (Citation
    c:[Citation]
    rest) -> Citation
    c {citationMode = mode} Citation -> [Citation] -> [Citation]
    forall a. a -> [a] -> [a]
    : [Citation]
    rest
                                 []       -> []
            CitationMode
    _            -> (Citation -> Citation) -> [Citation] -> [Citation]
    forall a b. (a -> b) -> [a] -> [b]
    map (\Citation
    a -> Citation
    a {citationMode = mode}) [Citation]
    cs
      where mprenote :: [Inline] -> [Inline]
    mprenote (Inline
    k:[Inline]
    ks) = (Inline
    kInline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    :[Inline]
    ks) [Inline] -> [Inline] -> [Inline]
    forall a. [a] -> [a] -> [a]
    ++ [Inline
    Space]
            mprenote [Inline]
    _ = [Inline]
    forall a. Monoid a => a
    mempty
            mpostnote :: [Inline] -> [Inline]
    mpostnote (Inline
    k:[Inline]
    ks) = [Text -> Inline
    Str Text
    ",", Inline
    Space] [Inline] -> [Inline] -> [Inline]
    forall a. [a] -> [a] -> [a]
    ++ (Inline
    kInline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    :[Inline]
    ks)
            mpostnote [Inline]
    _ = [Inline]
    forall a. Monoid a => a
    mempty
            addMprenote :: [Inline] -> [Citation] -> [Citation]
    addMprenote [Inline]
    mpn (Citation
    k:[Citation]
    ks) =
              let mpnfinal :: [Inline]
    mpnfinal = case Citation -> [Inline]
    citationPrefix Citation
    k of
                               (Inline
    _:[Inline]
    _) -> [Inline] -> [Inline]
    mprenote [Inline]
    mpn
                               [Inline]
    _ -> [Inline]
    mpn
              in [Inline] -> [Citation] -> [Citation]
    addPrefix [Inline]
    mpnfinal (Citation
    kCitation -> [Citation] -> [Citation]
    forall a. a -> [a] -> [a]
    :[Citation]
    ks)
            addMprenote [Inline]
    _ [Citation]
    _ = []
            addMpostnote :: [Inline] -> [Citation] -> [Citation]
    addMpostnote = [Inline] -> [Citation] -> [Citation]
    addSuffix ([Inline] -> [Citation] -> [Citation])
    -> ([Inline] -> [Inline]) -> [Inline] -> [Citation] -> [Citation]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Inline] -> [Inline]
    mpostnote

    citationWith :: PandocMonad m
                 => LP m Inlines -> Text -> CitationMode -> Bool -> LP m Inlines
    citationWith :: forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> Text -> CitationMode -> Bool -> LP m Inlines
    citationWith LP m Inlines
    inline Text
    name CitationMode
    mode Bool
    multi = do
      (c,raw) <- LP m [Citation] -> LP m ([Citation], [Tok])
    forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
    withRaw (LP m [Citation] -> LP m ([Citation], [Tok]))
    -> LP m [Citation] -> LP m ([Citation], [Tok])
    forall a b. (a -> b) -> a -> b
    $ LP m Inlines -> CitationMode -> Bool -> LP m [Citation]
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> CitationMode -> Bool -> LP m [Citation]
    cites LP m Inlines
    inline CitationMode
    mode Bool
    multi
      return $ cite c (rawInline "latex" $ "\\" <> name <> untokenize raw)

    handleCitationPart :: Inlines -> [Citation]
    handleCitationPart :: Inlines -> [Citation]
    handleCitationPart Inlines
    ils =
      let isCite :: Inline -> Bool
    isCite Cite{} = Bool
    True
          isCite Inline
    _      = Bool
    False
          ([Inline]
    pref, [Inline]
    rest) = (Inline -> Bool) -> [Inline] -> ([Inline], [Inline])
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    break Inline -> Bool
    isCite (Inlines -> [Inline]
    forall a. Many a -> [a]
    toList Inlines
    ils)
      in case [Inline]
    rest of
              (Cite [Citation]
    cs [Inline]
    _:[Inline]
    suff) -> [Inline] -> [Citation] -> [Citation]
    addPrefix [Inline]
    pref ([Citation] -> [Citation]) -> [Citation] -> [Citation]
    forall a b. (a -> b) -> a -> b
    $ [Inline] -> [Citation] -> [Citation]
    addSuffix [Inline]
    suff [Citation]
    cs
              [Inline]
    _                -> []

    complexNatbibCitation :: PandocMonad m
                          => LP m Inlines -> CitationMode -> LP m Inlines
    complexNatbibCitation :: forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> CitationMode -> LP m Inlines
    complexNatbibCitation LP m Inlines
    inline CitationMode
    mode = LP m Inlines -> LP m Inlines
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (LP m Inlines -> LP m Inlines) -> LP m Inlines -> LP m Inlines
    forall a b. (a -> b) -> a -> b
    $ do
      (cs, raw) <-
        LP m [Citation] -> LP m ([Citation], [Tok])
    forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
    withRaw (LP m [Citation] -> LP m ([Citation], [Tok]))
    -> LP m [Citation] -> LP m ([Citation], [Tok])
    forall a b. (a -> b) -> a -> b
    $ [[Citation]] -> [Citation]
    forall (t :: * -> *) a. Foldable t => t [a] -> [a]
    concat ([[Citation]] -> [Citation])
    -> ParsecT TokStream LaTeXState m [[Citation]] -> LP m [Citation]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> do
          LP m Tok
    forall (m :: * -> *). PandocMonad m => LP m Tok
    bgroup
          items <- [[Inlines]] -> [Inlines]
    forall a. Monoid a => [a] -> a
    mconcat ([[Inlines]] -> [Inlines])
    -> ParsecT TokStream LaTeXState m [[Inlines]]
    -> ParsecT TokStream LaTeXState m [Inlines]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
                    LP m Inlines -> ParsecT TokStream LaTeXState m [Inlines]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
    many1 (LP m Tok -> ParsecT TokStream LaTeXState m ()
    forall s (m :: * -> *) t a u.
    (Stream s m t, Show a) =>
    ParsecT s u m a -> ParsecT s u m ()
    notFollowedBy (Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    ';') ParsecT TokStream LaTeXState m () -> LP m Inlines -> LP m Inlines
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> LP m Inlines
    inline)
                      ParsecT TokStream LaTeXState m [Inlines]
    -> LP m Tok -> ParsecT TokStream LaTeXState m [[Inlines]]
    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]
    `sepBy1` Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    ';'
          egroup
          return $ map handleCitationPart items
      case cs of
           []       -> LP m Inlines
    forall a. ParsecT TokStream LaTeXState m a
    forall (m :: * -> *) a. MonadPlus m => m a
    mzero
           (Citation
    c:[Citation]
    cits) -> Inlines -> LP m Inlines
    forall a. a -> ParsecT TokStream LaTeXState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Inlines -> LP m Inlines) -> Inlines -> LP m Inlines
    forall a b. (a -> b) -> a -> b
    $ [Citation] -> Inlines -> Inlines
    cite (Citation
    c{ citationMode = mode }Citation -> [Citation] -> [Citation]
    forall a. a -> [a] -> [a]
    :[Citation]
    cits)
                          (Text -> Text -> Inlines
    rawInline Text
    "latex" (Text -> Inlines) -> Text -> Inlines
    forall a b. (a -> b) -> a -> b
    $ Text
    "\\citetext" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> [Tok] -> Text
    untokenize [Tok]
    raw)

    inNote :: Inlines -> Inlines
    inNote :: Inlines -> Inlines
    inNote Inlines
    ils =
      Blocks -> Inlines
    note (Blocks -> Inlines) -> Blocks -> Inlines
    forall a b. (a -> b) -> a -> b
    $ Inlines -> Blocks
    para (Inlines -> Blocks) -> Inlines -> Blocks
    forall a b. (a -> b) -> a -> b
    $ Inlines
    ils Inlines -> Inlines -> Inlines
    forall a. Semigroup a => a -> a -> a
    <> Text -> Inlines
    str Text
    "."