{-# 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 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
  [ (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 [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 Text
"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 Text
"citeauthor" CitationMode
AuthorInText Bool
False)
  , (Text
"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 Text
"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 Text
"nocite"))
  ]

-- citations

addPrefix :: [Inline] -> [Citation] -> [Citation]
addPrefix :: [Inline] -> [Citation] -> [Citation]
addPrefix [Inline]
p (Citation
k:[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 [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. [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 [Inline]
_ [Citation]
_ = []

simpleCiteArgs :: forall m . PandocMonad m => LP m Inlines -> LP m [Citation]
simpleCiteArgs :: LP m Inlines -> LP m [Citation]
simpleCiteArgs 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 ([Inline]
pre, [Inline]
suf) = case (Maybe [Inline]
first  , Maybe [Inline]
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 -> Citation
conv Text
k = Citation :: 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    = Int
0
                        , citationNoteNum :: Int
citationNoteNum = Int
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 SourceName
"bracketed option" [Tok]
toks
    case Either ParseError Inlines
parsed of
      Right Inlines
result -> Inlines -> LP m Inlines
forall (m :: * -> *) a. Monad m => a -> m a
return Inlines
result
      Left 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
$ Text -> ParseError -> PandocError
PandocParsecError ([Tok] -> Text
untokenize [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 Char
',')
          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 = SourceName
".:;?!`'()/*@_+=-&[]" :: [Char]

cites :: PandocMonad m
      => LP m Inlines -> CitationMode -> Bool -> LP m [Citation]
cites :: LP m Inlines -> CitationMode -> Bool -> LP m [Citation]
cites LP m Inlines
inline CitationMode
mode 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 ([Inline]
pre, [Inline]
suf) = case (Maybe [Inline]
multiprenote, Maybe [Inline]
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)
               [[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
                 ([Citation]
k:[[Citation]]
ks) -> case [[Citation]]
ks of
                             ([Citation]
_:[[Citation]]
_) -> [[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]]
_ -> [[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]]
_ -> [[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 Int
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
        CitationMode
AuthorInText -> case [Citation]
cs of
                             (Citation
c:[Citation]
rest) -> Citation
c {citationMode :: CitationMode
citationMode = 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 :: CitationMode
citationMode = 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 :: LP m Inlines -> Text -> CitationMode -> Bool -> LP m Inlines
citationWith LP m Inlines
inline Text
name CitationMode
mode Bool
multi = do
  ([Citation]
c,[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 Text
"latex" (Text -> Inlines) -> Text -> Inlines
forall a b. (a -> b) -> a -> b
$ Text
"\\" 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 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 :: 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
  ([Citation]
cs, [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 Char
';') 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 Char
';'
      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
       (Citation
c:[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 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
"."