{-# 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.Error (PandocError(PandocParsecError))
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 :: LP m Inlines -> Map Text (LP m Inlines)
citationCommands inline :: 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 (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
  [ ("cite", Text -> CitationMode -> Bool -> LP m Inlines
citation "cite" CitationMode
NormalCitation Bool
False)
  , ("Cite", Text -> CitationMode -> Bool -> LP m Inlines
citation "Cite" CitationMode
NormalCitation Bool
False)
  , ("citep", Text -> CitationMode -> Bool -> LP m Inlines
citation "citep" CitationMode
NormalCitation Bool
False)
  , ("citep*", Text -> CitationMode -> Bool -> LP m Inlines
citation "citep*" CitationMode
NormalCitation Bool
False)
  , ("citeal", Text -> CitationMode -> Bool -> LP m Inlines
citation "citeal" CitationMode
NormalCitation Bool
False)
  , ("citealp", Text -> CitationMode -> Bool -> LP m Inlines
citation "citealp" CitationMode
NormalCitation Bool
False)
  , ("citealp*", Text -> CitationMode -> Bool -> LP m Inlines
citation "citealp*" CitationMode
NormalCitation Bool
False)
  , ("autocite", Text -> CitationMode -> Bool -> LP m Inlines
citation "autocite" CitationMode
NormalCitation Bool
False)
  , ("smartcite", Text -> CitationMode -> Bool -> LP m Inlines
citation "smartcite" CitationMode
NormalCitation Bool
False)
  , ("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 "footcite" CitationMode
NormalCitation Bool
False)
  , ("parencite", Text -> CitationMode -> Bool -> LP m Inlines
citation "parencite" CitationMode
NormalCitation Bool
False)
  , ("supercite", Text -> CitationMode -> Bool -> LP m Inlines
citation "supercite" CitationMode
NormalCitation Bool
False)
  , ("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 "footcitetext" CitationMode
NormalCitation Bool
False)
  , ("citeyearpar", Text -> CitationMode -> Bool -> LP m Inlines
citation "citeyearpar" CitationMode
SuppressAuthor Bool
False)
  , ("citeyear", Text -> CitationMode -> Bool -> LP m Inlines
citation "citeyear" CitationMode
SuppressAuthor Bool
False)
  , ("autocite*", Text -> CitationMode -> Bool -> LP m Inlines
citation "autocite*" CitationMode
SuppressAuthor Bool
False)
  , ("cite*", Text -> CitationMode -> Bool -> LP m Inlines
citation "cite*" CitationMode
SuppressAuthor Bool
False)
  , ("parencite*", Text -> CitationMode -> Bool -> LP m Inlines
citation "parencite*" CitationMode
SuppressAuthor Bool
False)
  , ("textcite", Text -> CitationMode -> Bool -> LP m Inlines
citation "textcite" CitationMode
AuthorInText Bool
False)
  , ("citet", Text -> CitationMode -> Bool -> LP m Inlines
citation "citet" CitationMode
AuthorInText Bool
False)
  , ("citet*", Text -> CitationMode -> Bool -> LP m Inlines
citation "citet*" CitationMode
AuthorInText Bool
False)
  , ("citealt", Text -> CitationMode -> Bool -> LP m Inlines
citation "citealt" CitationMode
AuthorInText Bool
False)
  , ("citealt*", Text -> CitationMode -> Bool -> LP m Inlines
citation "citealt*" CitationMode
AuthorInText Bool
False)
  , ("textcites", Text -> CitationMode -> Bool -> LP m Inlines
citation "textcites" CitationMode
AuthorInText Bool
True)
  , ("cites", Text -> CitationMode -> Bool -> LP m Inlines
citation "cites" CitationMode
NormalCitation Bool
True)
  , ("autocites", Text -> CitationMode -> Bool -> LP m Inlines
citation "autocites" CitationMode
NormalCitation Bool
True)
  , ("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 "footcites" CitationMode
NormalCitation Bool
True)
  , ("parencites", Text -> CitationMode -> Bool -> LP m Inlines
citation "parencites" CitationMode
NormalCitation Bool
True)
  , ("supercites", Text -> CitationMode -> Bool -> LP m Inlines
citation "supercites" CitationMode
NormalCitation Bool
True)
  , ("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 "footcitetexts" CitationMode
NormalCitation Bool
True)
  , ("Autocite", Text -> CitationMode -> Bool -> LP m Inlines
citation "Autocite" CitationMode
NormalCitation Bool
False)
  , ("Smartcite", Text -> CitationMode -> Bool -> LP m Inlines
citation "Smartcite" CitationMode
NormalCitation Bool
False)
  , ("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 "Footcite" CitationMode
NormalCitation Bool
False)
  , ("Parencite", Text -> CitationMode -> Bool -> LP m Inlines
citation "Parencite" CitationMode
NormalCitation Bool
False)
  , ("Supercite", Text -> CitationMode -> Bool -> LP m Inlines
citation "Supercite" CitationMode
NormalCitation Bool
False)
  , ("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 "Footcitetext" CitationMode
NormalCitation Bool
False)
  , ("Citeyearpar", Text -> CitationMode -> Bool -> LP m Inlines
citation "Citeyearpar" CitationMode
SuppressAuthor Bool
False)
  , ("Citeyear", Text -> CitationMode -> Bool -> LP m Inlines
citation "Citeyear" CitationMode
SuppressAuthor Bool
False)
  , ("Autocite*", Text -> CitationMode -> Bool -> LP m Inlines
citation "Autocite*" CitationMode
SuppressAuthor Bool
False)
  , ("Cite*", Text -> CitationMode -> Bool -> LP m Inlines
citation "Cite*" CitationMode
SuppressAuthor Bool
False)
  , ("Parencite*", Text -> CitationMode -> Bool -> LP m Inlines
citation "Parencite*" CitationMode
SuppressAuthor Bool
False)
  , ("Textcite", Text -> CitationMode -> Bool -> LP m Inlines
citation "Textcite" CitationMode
AuthorInText Bool
False)
  , ("Textcites", Text -> CitationMode -> Bool -> LP m Inlines
citation "Textcites" CitationMode
AuthorInText Bool
True)
  , ("Cites", Text -> CitationMode -> Bool -> LP m Inlines
citation "Cites" CitationMode
NormalCitation Bool
True)
  , ("Autocites", Text -> CitationMode -> Bool -> LP m Inlines
citation "Autocites" CitationMode
NormalCitation Bool
True)
  , ("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 "Footcites" CitationMode
NormalCitation Bool
True)
  , ("Parencites", Text -> CitationMode -> Bool -> LP m Inlines
citation "Parencites" CitationMode
NormalCitation Bool
True)
  , ("Supercites", Text -> CitationMode -> Bool -> LP m Inlines
citation "Supercites" CitationMode
NormalCitation Bool
True)
  , ("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 "Footcitetexts" CitationMode
NormalCitation Bool
True)
  , ("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)
  , ("citeauthor", (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 (LP m Inlines
tok LP m Inlines -> LP m () -> LP m ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp LP m ()
-> ParsecT [Tok] LaTeXState m Tok -> ParsecT [Tok] LaTeXState m Tok
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Text -> ParsecT [Tok] LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq "citetext") ParsecT [Tok] LaTeXState m Tok -> LP m Inlines -> LP m Inlines
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 (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> CitationMode -> Bool -> LP m Inlines
citation "citeauthor" CitationMode
AuthorInText Bool
False)
  , ("nocite", Inlines
forall a. Monoid a => a
mempty Inlines -> LP m () -> LP m Inlines
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Text -> CitationMode -> Bool -> LP m Inlines
citation "nocite" CitationMode
NormalCitation Bool
False LP m Inlines -> (Inlines -> LP m ()) -> LP m ()
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 "nocite"))
  ]

-- citations

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

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

simpleCiteArgs :: forall m . PandocMonad m => LP m Inlines -> LP m [Citation]
simpleCiteArgs :: LP m Inlines -> LP m [Citation]
simpleCiteArgs inline :: LP m Inlines
inline = LP m [Citation] -> LP m [Citation]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (LP m [Citation] -> LP m [Citation])
-> LP m [Citation] -> LP m [Citation]
forall a b. (a -> b) -> a -> b
$ do
  Maybe [Inline]
first  <- ParsecT [Tok] LaTeXState m [Inline]
-> ParsecT [Tok] 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 [Tok] LaTeXState m [Inline]
 -> ParsecT [Tok] LaTeXState m (Maybe [Inline]))
-> ParsecT [Tok] LaTeXState m [Inline]
-> ParsecT [Tok] 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 [Tok] LaTeXState m [Inline]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m Inlines
PandocMonad m => LP m Inlines
opt
  Maybe [Inline]
second <- ParsecT [Tok] LaTeXState m [Inline]
-> ParsecT [Tok] 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 [Tok] LaTeXState m [Inline]
 -> ParsecT [Tok] LaTeXState m (Maybe [Inline]))
-> ParsecT [Tok] LaTeXState m [Inline]
-> ParsecT [Tok] 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 [Tok] LaTeXState m [Inline]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m Inlines
PandocMonad m => LP m Inlines
opt
  [Text]
keys <- ParsecT [Tok] LaTeXState m [Text]
-> ParsecT [Tok] LaTeXState m [Text]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (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
$ LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
bgroup LP m Tok
-> ParsecT [Tok] LaTeXState m [Text]
-> ParsecT [Tok] LaTeXState m [Text]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT [Tok] LaTeXState m Text
-> LP m Tok -> ParsecT [Tok] LaTeXState m [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
forall (m :: * -> *). PandocMonad m => LP m Text
citationLabel LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
egroup
  let (pre :: [Inline]
pre, suf :: [Inline]
suf) = case (Maybe [Inline]
first  , Maybe [Inline]
second ) of
        (Just s :: [Inline]
s , Nothing) -> ([Inline]
forall a. Monoid a => a
mempty, [Inline]
s )
        (Just s :: [Inline]
s , Just t :: [Inline]
t ) -> ([Inline]
s , [Inline]
t )
        _                  -> ([Inline]
forall a. Monoid a => a
mempty, [Inline]
forall a. Monoid a => a
mempty)
      conv :: Text -> Citation
conv k :: Text
k = $WCitation :: Text
-> [Inline] -> [Inline] -> CitationMode -> Int -> Int -> Citation
Citation { citationId :: Text
citationId      = Text
k
                        , citationPrefix :: [Inline]
citationPrefix  = []
                        , citationSuffix :: [Inline]
citationSuffix  = []
                        , citationMode :: CitationMode
citationMode    = CitationMode
NormalCitation
                        , citationHash :: Int
citationHash    = 0
                        , citationNoteNum :: Int
citationNoteNum = 0
                        }
  [Citation] -> LP m [Citation]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Citation] -> LP m [Citation]) -> [Citation] -> LP m [Citation]
forall a b. (a -> b) -> a -> b
$ [Inline] -> [Citation] -> [Citation]
addPrefix [Inline]
pre ([Citation] -> [Citation]) -> [Citation] -> [Citation]
forall a b. (a -> b) -> a -> b
$ [Inline] -> [Citation] -> [Citation]
addSuffix [Inline]
suf ([Citation] -> [Citation]) -> [Citation] -> [Citation]
forall a b. (a -> b) -> a -> b
$ (Text -> Citation) -> [Text] -> [Citation]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Citation
conv [Text]
keys
 where
  opt :: PandocMonad m => LP m Inlines
  opt :: LP m Inlines
opt = do
    [Tok]
toks <- 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 (LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
sp LP m ()
-> ParsecT [Tok] LaTeXState m [Tok]
-> ParsecT [Tok] LaTeXState m [Tok]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT [Tok] LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracketedToks ParsecT [Tok] LaTeXState m [Tok]
-> LP m () -> ParsecT [Tok] LaTeXState m [Tok]
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
    LaTeXState
st <- ParsecT [Tok] LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
    Either ParseError Inlines
parsed <- m (Either ParseError Inlines)
-> ParsecT [Tok] LaTeXState m (Either ParseError Inlines)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either ParseError Inlines)
 -> ParsecT [Tok] LaTeXState m (Either ParseError Inlines))
-> m (Either ParseError Inlines)
-> ParsecT [Tok] LaTeXState m (Either ParseError Inlines)
forall a b. (a -> b) -> a -> b
$
      LP m Inlines
-> LaTeXState
-> SourceName
-> [Tok]
-> m (Either ParseError Inlines)
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> u -> SourceName -> s -> m (Either ParseError a)
runParserT ([Inlines] -> Inlines
forall a. Monoid a => [a] -> a
mconcat ([Inlines] -> Inlines)
-> ParsecT [Tok] LaTeXState m [Inlines] -> LP m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m Inlines -> ParsecT [Tok] LaTeXState m [Inlines]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many LP m Inlines
inline) LaTeXState
st "bracketed option" [Tok]
toks
    case Either ParseError Inlines
parsed of
      Right result :: Inlines
result -> Inlines -> LP m Inlines
forall (m :: * -> *) a. Monad m => a -> m a
return Inlines
result
      Left e :: ParseError
e       -> PandocError -> LP m Inlines
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
PandocParsecError ([Tok] -> Sources
forall a. ToSources a => a -> Sources
toSources [Tok]
toks) ParseError
e



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

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

handleCitationPart :: Inlines -> [Citation]
handleCitationPart :: Inlines -> [Citation]
handleCitationPart ils :: Inlines
ils =
  let isCite :: Inline -> Bool
isCite Cite{} = Bool
True
      isCite _      = Bool
False
      (pref :: [Inline]
pref, rest :: [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 cs :: [Citation]
cs _:suff :: [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
          _                -> []

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

inNote :: Inlines -> Inlines
inNote :: Inlines -> Inlines
inNote ils :: 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 "."