{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ViewPatterns      #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Text.CSL.Input.Bibtex
-- Copyright   :  (c) John MacFarlane
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  John MacFarlane <fiddlosopher@gmail.com>
-- Stability   :  unstable-- Portability :  unportable
--
-----------------------------------------------------------------------------

module Text.CSL.Input.Bibtex
    ( readBibtex
    , readBibtexString
    , Lang(..)
    , langToLocale
    , getLangFromEnv
    )
    where

import Prelude
import           Control.Applicative
import qualified Control.Exception      as E
import           Control.Monad
import           Control.Monad.RWS      hiding ((<>))
import qualified Data.ByteString        as B
import           Data.Char              (isAlphaNum, isDigit, isUpper, toLower,
                                         toUpper)
import           Data.List              (foldl', intercalate)
import           Data.List.Split        (splitOn, splitWhen, wordsBy)
import qualified Data.Map               as Map
import           Data.Maybe
import           Data.Text              (Text)
import qualified Data.Text              as T
import           Data.Text.Encoding     (decodeUtf8)
import           System.Environment     (getEnvironment)
import           Text.CSL.Compat.Pandoc (readLaTeX)
import           Text.CSL.Exception     (CiteprocException (ErrorReadingBib, ErrorReadingBibFile))
import           Text.CSL.Parser        (parseLocale)
import           Text.CSL.Reference
import           Text.CSL.Style         (Agent (..), emptyAgent, CslTerm (..),
                                         Formatted (..), Locale (..))
import           Text.CSL.Util          (onBlocks, protectCase, safeRead,
                                         splitWhen, splitStrWhen, trim,
                                         unTitlecase, addSpaceAfterPeriod)
import           Text.Pandoc.Definition
import qualified Text.Pandoc.Walk       as Walk
import Text.Parsec hiding (State, many, (<|>))

blocksToFormatted  :: [Block]  -> Bib Formatted
blocksToFormatted :: [Block] -> Bib Formatted
blocksToFormatted [Block]
bs =
  case [Block]
bs of
       [Plain [Inline]
xs] -> [Inline] -> Bib Formatted
inlinesToFormatted [Inline]
xs
       [Para  [Inline]
xs] -> [Inline] -> Bib Formatted
inlinesToFormatted [Inline]
xs
       [Block]
_          -> [Inline] -> Bib Formatted
inlinesToFormatted ([Inline] -> Bib Formatted) -> [Inline] -> Bib Formatted
forall a b. (a -> b) -> a -> b
$ (Inline -> [Inline]) -> [Block] -> [Inline]
forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
Walk.query (Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:[]) [Block]
bs

adjustSpans :: Lang -> Inline -> [Inline]
adjustSpans :: Lang -> Inline -> [Inline]
adjustSpans Lang
_ (Span (Text
"",[],[]) [Inline]
xs) = [Inline]
xs
adjustSpans Lang
lang (RawInline (Format Text
"latex") Text
s)
  | Text
s Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"\\hyphen" Bool -> Bool -> Bool
|| Text
s Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"\\hyphen " = [Text -> Inline
Str Text
"-"]
  | Bool
otherwise = ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall a b. Walkable a b => (a -> a) -> b -> b
Walk.walk ((Inline -> [Inline]) -> [Inline] -> [Inline]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Lang -> Inline -> [Inline]
adjustSpans Lang
lang))
                ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall a b. (a -> b) -> a -> b
$ Lang -> Text -> [Inline]
parseRawLaTeX Lang
lang Text
s
adjustSpans Lang
_ Inline
x = [Inline
x]

parseRawLaTeX :: Lang -> Text -> [Inline]
parseRawLaTeX :: Lang -> Text -> [Inline]
parseRawLaTeX Lang
lang (Text -> Text -> Maybe Text
T.stripPrefix Text
"\\" -> Just Text
xs) =
  case Text -> [Block]
latex' Text
contents of
       [Para [Inline]
ys]  -> Text -> [Inline] -> [Inline]
forall a. (Eq a, IsString a) => a -> [Inline] -> [Inline]
f Text
command [Inline]
ys
       [Plain [Inline]
ys] -> Text -> [Inline] -> [Inline]
forall a. (Eq a, IsString a) => a -> [Inline] -> [Inline]
f Text
command [Inline]
ys
       [Block]
_          -> []
   where (Text
command', Text
contents') = (Char -> Bool) -> Text -> (Text, Text)
T.break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
'{') Text
xs
         command :: Text
command  = Text -> Text
trim Text
command'
         contents :: Text
contents = Int -> Text -> Text
T.drop Int
1 (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.dropEnd Int
1 Text
contents'
         f :: a -> [Inline] -> [Inline]
f a
"mkbibquote"    [Inline]
ils = [QuoteType -> [Inline] -> Inline
Quoted QuoteType
DoubleQuote [Inline]
ils]
         f a
"mkbibemph"     [Inline]
ils = [[Inline] -> Inline
Emph [Inline]
ils]
         f a
"mkbibitalic"   [Inline]
ils = [[Inline] -> Inline
Emph [Inline]
ils] -- TODO: italic/=emph
         f a
"mkbibbold"     [Inline]
ils = [[Inline] -> Inline
Strong [Inline]
ils]
         f a
"mkbibparens"   [Inline]
ils = [Text -> Inline
Str Text
"("] [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Inline]
ils [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Text -> Inline
Str Text
")"] -- TODO: ...
         f a
"mkbibbrackets" [Inline]
ils = [Text -> Inline
Str Text
"["] [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Inline]
ils [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Text -> Inline
Str Text
"]"] -- TODO: ...
         -- ... both should be nestable & should work in year fields
         f a
"autocap"    [Inline]
ils    = [Inline]
ils  -- TODO: should work in year fields
         f a
"textnormal" [Inline]
ils    = [(Text, [Text], [(Text, Text)]) -> [Inline] -> Inline
Span (Text
"",[Text
"nodecor"],[]) [Inline]
ils]
         f a
"bibstring" [Str Text
s] = [Text -> Inline
Str (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ Lang -> Text -> Text
resolveKey' Lang
lang Text
s]
         f a
_            [Inline]
ils    = [(Text, [Text], [(Text, Text)]) -> [Inline] -> Inline
Span (Text, [Text], [(Text, Text)])
nullAttr [Inline]
ils]
parseRawLaTeX Lang
_ Text
_ = []

inlinesToFormatted :: [Inline] -> Bib Formatted
inlinesToFormatted :: [Inline] -> Bib Formatted
inlinesToFormatted [Inline]
ils = do
  Lang
lang <- (BibState -> Lang) -> RWST Item () BibState Maybe Lang
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets BibState -> Lang
localeLanguage
  Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return (Formatted -> Bib Formatted) -> Formatted -> Bib Formatted
forall a b. (a -> b) -> a -> b
$ [Inline] -> Formatted
Formatted ([Inline] -> Formatted) -> [Inline] -> Formatted
forall a b. (a -> b) -> a -> b
$ ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall a b. Walkable a b => (a -> a) -> b -> b
Walk.walk ((Inline -> [Inline]) -> [Inline] -> [Inline]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Lang -> Inline -> [Inline]
adjustSpans Lang
lang)) [Inline]
ils

data Item = Item{ Item -> Text
identifier :: Text
                , Item -> Text
entryType  :: Text
                , Item -> Map Text Text
fields     :: Map.Map Text Text
                }

-- | Get 'Lang' from the environment variable LANG, defaulting to en-US.
getLangFromEnv :: IO Lang
getLangFromEnv :: IO Lang
getLangFromEnv = do
  [(String, String)]
env <- IO [(String, String)]
getEnvironment
  Lang -> IO Lang
forall (m :: * -> *) a. Monad m => a -> m a
return (Lang -> IO Lang) -> Lang -> IO Lang
forall a b. (a -> b) -> a -> b
$ case String -> [(String, String)] -> Maybe String
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup String
"LANG" [(String, String)]
env of
    Just String
x  -> case (Char -> Bool) -> Text -> [Text]
Text.CSL.Util.splitWhen (\Char
c -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'_' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-')
                    ((Char -> Bool) -> Text -> Text
T.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/=Char
'.') (String -> Text
T.pack String
x)) of
                 (Text
w:Text
z:[Text]
_) -> Text -> Text -> Lang
Lang Text
w Text
z
                 [Text
w]     | Bool -> Bool
not (Text -> Bool
T.null Text
w) -> Text -> Text -> Lang
Lang Text
w Text
forall a. Monoid a => a
mempty
                 [Text]
_       -> Text -> Text -> Lang
Lang Text
"en" Text
"US"
    Maybe String
Nothing -> Text -> Text -> Lang
Lang Text
"en" Text
"US"

-- | Parse a BibTeX or BibLaTeX file into a list of 'Reference's.
-- The first parameter is a predicate to filter identifiers.
-- If the second parameter is true, the file will be treated as
-- BibTeX; otherwise as BibLaTeX.  If the third parameter is
-- true, an "untitlecase" transformation will be performed.
readBibtex :: (Text -> Bool) -> Bool -> Bool -> FilePath -> IO [Reference]
readBibtex :: (Text -> Bool) -> Bool -> Bool -> String -> IO [Reference]
readBibtex Text -> Bool
idpred Bool
isBibtex Bool
caseTransform String
f = do
  Text
contents <- ByteString -> Text
decodeUtf8 (ByteString -> Text) -> IO ByteString -> IO Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> IO ByteString
B.readFile String
f
  IO [Reference]
-> (CiteprocException -> IO [Reference]) -> IO [Reference]
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
E.catch ((Text -> Bool) -> Bool -> Bool -> Text -> IO [Reference]
readBibtexString Text -> Bool
idpred Bool
isBibtex Bool
caseTransform Text
contents)
        (\CiteprocException
e -> case CiteprocException
e of
                  ErrorReadingBib String
es -> CiteprocException -> IO [Reference]
forall e a. Exception e => e -> IO a
E.throwIO (CiteprocException -> IO [Reference])
-> CiteprocException -> IO [Reference]
forall a b. (a -> b) -> a -> b
$ String -> String -> CiteprocException
ErrorReadingBibFile String
f String
es
                  CiteprocException
_                  -> CiteprocException -> IO [Reference]
forall e a. Exception e => e -> IO a
E.throwIO CiteprocException
e)

-- | Like 'readBibtex' but operates on Text rather than a file.
readBibtexString :: (Text -> Bool) -> Bool -> Bool -> Text
                 -> IO [Reference]
readBibtexString :: (Text -> Bool) -> Bool -> Bool -> Text -> IO [Reference]
readBibtexString Text -> Bool
idpred Bool
isBibtex Bool
caseTransform Text
contents = do
  Lang
lang <- IO Lang
getLangFromEnv
  Locale
locale <- Text -> IO Locale
parseLocale (Lang -> Text
langToLocale Lang
lang)
  case Parsec Text (Map Text Text) [Item]
-> Map Text Text -> String -> Text -> Either ParseError [Item]
forall s t u a.
Stream s Identity t =>
Parsec s u a -> u -> String -> s -> Either ParseError a
runParser (Parsec Text (Map Text Text) [Item]
bibEntries Parsec Text (Map Text Text) [Item]
-> ParsecT Text (Map Text Text) Identity ()
-> Parsec Text (Map Text Text) [Item]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m ()
eof) Map Text Text
forall k a. Map k a
Map.empty String
"stdin" Text
contents of
                      -- drop 8 to remove "stdin" + space
          Left ParseError
err -> CiteprocException -> IO [Reference]
forall e a. Exception e => e -> IO a
E.throwIO (CiteprocException -> IO [Reference])
-> CiteprocException -> IO [Reference]
forall a b. (a -> b) -> a -> b
$ String -> CiteprocException
ErrorReadingBib (String -> CiteprocException) -> String -> CiteprocException
forall a b. (a -> b) -> a -> b
$ Int -> String -> String
forall a. Int -> [a] -> [a]
drop Int
8 (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ ParseError -> String
forall a. Show a => a -> String
show ParseError
err
          Right [Item]
xs -> [Reference] -> IO [Reference]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Reference] -> IO [Reference]) -> [Reference] -> IO [Reference]
forall a b. (a -> b) -> a -> b
$ (Item -> Maybe Reference) -> [Item] -> [Reference]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe
            (Lang -> Locale -> Bool -> Bool -> Item -> Maybe Reference
itemToReference Lang
lang Locale
locale Bool
isBibtex Bool
caseTransform)
            ((Item -> Bool) -> [Item] -> [Item]
forall a. (a -> Bool) -> [a] -> [a]
filter (Text -> Bool
idpred (Text -> Bool) -> (Item -> Text) -> Item -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Item -> Text
identifier)
              (Bool -> [Item] -> [Item]
resolveCrossRefs Bool
isBibtex
                [Item]
xs))

type BibParser = Parsec Text (Map.Map Text Text)

bibEntries :: BibParser [Item]
bibEntries :: Parsec Text (Map Text Text) [Item]
bibEntries = do
  ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany ParsecT Text (Map Text Text) Identity ()
nonEntry
  ParsecT Text (Map Text Text) Identity Item
-> Parsec Text (Map Text Text) [Item]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (ParsecT Text (Map Text Text) Identity Item
bibItem ParsecT Text (Map Text Text) Identity Item
-> ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity Item
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany ParsecT Text (Map Text Text) Identity ()
nonEntry)
 where nonEntry :: ParsecT Text (Map Text Text) Identity ()
nonEntry = ParsecT Text (Map Text Text) Identity ()
bibSkip ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                  ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'@' ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                       (ParsecT Text (Map Text Text) Identity ()
bibComment ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Text (Map Text Text) Identity ()
bibPreamble ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Text (Map Text Text) Identity ()
bibString))

bibSkip :: BibParser ()
bibSkip :: ParsecT Text (Map Text Text) Identity ()
bibSkip = ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
skipMany1 ((Char -> Bool) -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
(Char -> Bool) -> ParsecT s u m Char
satisfy (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/=Char
'@'))

bibComment :: BibParser ()
bibComment :: ParsecT Text (Map Text Text) Identity ()
bibComment = do
  Text -> BibParser Text
cistring Text
"comment"
  ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces
  BibParser Text -> ParsecT Text (Map Text Text) Identity ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void BibParser Text
inBraces ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Text (Map Text Text) Identity ()
bibSkip ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> () -> ParsecT Text (Map Text Text) Identity ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

bibPreamble :: BibParser ()
bibPreamble :: ParsecT Text (Map Text Text) Identity ()
bibPreamble = do
  Text -> BibParser Text
cistring Text
"preamble"
  ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces
  BibParser Text -> ParsecT Text (Map Text Text) Identity ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void BibParser Text
inBraces

bibString :: BibParser ()
bibString :: ParsecT Text (Map Text Text) Identity ()
bibString = do
  Text -> BibParser Text
cistring Text
"string"
  ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces
  Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'{'
  ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces
  (Text
k,Text
v) <- BibParser (Text, Text)
entField
  Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'}'
  (Map Text Text -> Map Text Text)
-> ParsecT Text (Map Text Text) Identity ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState (Text -> Text -> Map Text Text -> Map Text Text
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Text
k Text
v)
  () -> ParsecT Text (Map Text Text) Identity ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

inBraces :: BibParser Text
inBraces :: BibParser Text
inBraces = BibParser Text -> BibParser Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (BibParser Text -> BibParser Text)
-> BibParser Text -> BibParser Text
forall a b. (a -> b) -> a -> b
$ do
  Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'{'
  [Text]
res <- BibParser Text
-> ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity [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
         (  (String -> Text
T.pack (String -> Text)
-> ParsecT Text (Map Text Text) Identity String -> BibParser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity String
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (String -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
"{}\\"))
        BibParser Text -> BibParser Text -> BibParser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'\\' ParsecT Text (Map Text Text) Identity Char
-> BibParser Text -> BibParser Text
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (  (Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'{' ParsecT Text (Map Text Text) Identity Char
-> BibParser Text -> BibParser Text
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> BibParser Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
"\\{")
                         BibParser Text -> BibParser Text -> BibParser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'}' ParsecT Text (Map Text Text) Identity Char
-> BibParser Text -> BibParser Text
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> BibParser Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
"\\}")
                         BibParser Text -> BibParser Text -> BibParser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> BibParser Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
"\\"))
        BibParser Text -> BibParser Text -> BibParser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Text -> Text
braced (Text -> Text) -> BibParser Text -> BibParser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BibParser Text
inBraces)
         ) (Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'}')
  Text -> BibParser Text
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> BibParser Text) -> Text -> BibParser Text
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.concat [Text]
res

braced :: Text -> Text
braced :: Text -> Text
braced = Char -> Text -> Text
T.cons Char
'{' (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Char -> Text) -> Char -> Text -> Text
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> Char -> Text
T.snoc Char
'}'

inQuotes :: BibParser Text
inQuotes :: BibParser Text
inQuotes = do
  Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'"'
  [Text] -> Text
T.concat ([Text] -> Text)
-> ParsecT Text (Map Text Text) Identity [Text] -> BibParser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BibParser Text
-> ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity [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 (  (String -> Text
T.pack (String -> Text)
-> ParsecT Text (Map Text Text) Identity String -> BibParser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity String
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (String -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf String
"\"\\{"))
                       BibParser Text -> BibParser Text -> BibParser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'\\' ParsecT Text (Map Text Text) Identity Char
-> BibParser Text -> BibParser Text
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> Text -> Text
T.cons Char
'\\' (Text -> Text) -> (Char -> Text) -> Char -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Text
T.singleton (Char -> Text)
-> ParsecT Text (Map Text Text) Identity Char -> BibParser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
anyChar)
                       BibParser Text -> BibParser Text -> BibParser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Text
braced (Text -> Text) -> BibParser Text -> BibParser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BibParser Text
inBraces
                        ) (Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'"')

fieldName :: BibParser Text
fieldName :: BibParser Text
fieldName = Text -> Text
resolveAlias (Text -> Text) -> (String -> Text) -> String -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
T.toLower (Text -> Text) -> (String -> Text) -> String -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack
  (String -> Text)
-> ParsecT Text (Map Text Text) Identity String -> BibParser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity String
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
letter ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity Char
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
digit ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity Char
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> String -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
oneOf String
"-_:+")

isBibtexKeyChar :: Char -> Bool
isBibtexKeyChar :: Char -> Bool
isBibtexKeyChar Char
c = Char -> Bool
isAlphaNum Char
c Bool -> Bool -> Bool
|| Char
c Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (String
".:;?!`'()/*@_+=-[]*&" :: String)

bibItem :: BibParser Item
bibItem :: ParsecT Text (Map Text Text) Identity Item
bibItem = do
  Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'@'
  String
enttype <- (Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower (String -> String)
-> ParsecT Text (Map Text Text) Identity String
-> ParsecT Text (Map Text Text) Identity String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity String
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
letter
  ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces
  Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'{'
  ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces
  String
entid <- ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity String
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ((Char -> Bool) -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
(Char -> Bool) -> ParsecT s u m Char
satisfy Char -> Bool
isBibtexKeyChar)
  ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces
  Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
','
  ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces
  [(Text, Text)]
entfields <- BibParser (Text, Text)
entField BibParser (Text, Text)
-> ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity [(Text, Text)]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
`sepEndBy` (Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
',' ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces)
  ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces
  Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'}'
  Item -> ParsecT Text (Map Text Text) Identity Item
forall (m :: * -> *) a. Monad m => a -> m a
return (Item -> ParsecT Text (Map Text Text) Identity Item)
-> Item -> ParsecT Text (Map Text Text) Identity Item
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Map Text Text -> Item
Item (String -> Text
T.pack String
entid) (String -> Text
T.pack String
enttype) ([(Text, Text)] -> Map Text Text
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Text, Text)]
entfields)

entField :: BibParser (Text, Text)
entField :: BibParser (Text, Text)
entField = do
  Text
k <- BibParser Text
fieldName
  ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces
  Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'='
  ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces
  [Text]
vs <- (BibParser Text
expandString BibParser Text -> BibParser Text -> BibParser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> BibParser Text
inQuotes BibParser Text -> BibParser Text -> BibParser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> BibParser Text
inBraces BibParser Text -> BibParser Text -> BibParser Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> BibParser Text
rawWord) BibParser Text
-> ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity [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]
`sepBy`
            ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity Char
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Char -> ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'#' ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity ()
-> ParsecT Text (Map Text Text) Identity ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces)
  ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces
  (Text, Text) -> BibParser (Text, Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
k, [Text] -> Text
T.concat [Text]
vs)

resolveAlias :: Text -> Text
resolveAlias :: Text -> Text
resolveAlias Text
"archiveprefix" = Text
"eprinttype"
resolveAlias Text
"primaryclass" = Text
"eprintclass"
resolveAlias Text
s = Text
s

rawWord :: BibParser Text
rawWord :: BibParser Text
rawWord = String -> Text
T.pack (String -> Text)
-> ParsecT Text (Map Text Text) Identity String -> BibParser Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text (Map Text Text) Identity Char
-> ParsecT Text (Map Text Text) Identity String
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT Text (Map Text Text) Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
alphaNum

expandString :: BibParser Text
expandString :: BibParser Text
expandString = do
  Text
k <- BibParser Text
fieldName
  Map Text Text
strs <- ParsecT Text (Map Text Text) Identity (Map Text Text)
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  case Text -> Map Text Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
k Map Text Text
strs of
       Just Text
v  -> Text -> BibParser Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
v
       Maybe Text
Nothing -> Text -> BibParser Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
k -- return raw key if not found

cistring :: Text -> BibParser Text
cistring :: Text -> BibParser Text
cistring Text
s = BibParser Text -> BibParser Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (Text -> BibParser Text
forall s (m :: * -> *) u.
Stream s m Char =>
Text -> ParsecT s u m Text
go Text
s)
 where go :: Text -> ParsecT s u m Text
go Text
t = case Text -> Maybe (Char, Text)
T.uncons Text
t of
         Maybe (Char, Text)
Nothing     -> Text -> ParsecT s u m Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
""
         Just (Char
c,Text
cs) -> do
           Char
x <- Char -> ParsecT s u m Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char (Char -> Char
toLower Char
c) ParsecT s u m Char -> ParsecT s u m Char -> ParsecT s u m Char
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Char -> ParsecT s u m Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char (Char -> Char
toUpper Char
c)
           Text
xs <- Text -> ParsecT s u m Text
go Text
cs
           Text -> ParsecT s u m Text
forall (m :: * -> *) a. Monad m => a -> m a
return (Char -> Text -> Text
T.cons Char
x Text
xs)

resolveCrossRefs :: Bool -> [Item] -> [Item]
resolveCrossRefs :: Bool -> [Item] -> [Item]
resolveCrossRefs Bool
isBibtex [Item]
entries =
  (Item -> Item) -> [Item] -> [Item]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> [Item] -> Item -> Item
resolveCrossRef Bool
isBibtex [Item]
entries) [Item]
entries

splitKeys :: Text -> [Text]
splitKeys :: Text -> [Text]
splitKeys = (Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Text -> Bool) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Bool
T.null) ([Text] -> [Text]) -> (Text -> [Text]) -> Text -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> Text -> [Text]
T.split (\Char
c -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
',')

getXrefFields :: Bool -> Item -> [Item] -> Text -> [(Text, Text)]
getXrefFields :: Bool -> Item -> [Item] -> Text -> [(Text, Text)]
getXrefFields Bool
isBibtex Item
baseEntry [Item]
entries Text
keys = do
  let keys' :: [Text]
keys' = Text -> [Text]
splitKeys Text
keys
  Item
xrefEntry <- [Item
e | Item
e <- [Item]
entries, Item -> Text
identifier Item
e Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
keys']
  (Text
k, Text
v) <- Map Text Text -> [(Text, Text)]
forall k a. Map k a -> [(k, a)]
Map.toList (Map Text Text -> [(Text, Text)])
-> Map Text Text -> [(Text, Text)]
forall a b. (a -> b) -> a -> b
$ Item -> Map Text Text
fields Item
xrefEntry
  if Text
k Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"crossref" Bool -> Bool -> Bool
|| Text
k Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"xdata"
     then do
       [(Text, Text)]
xs <- (Text -> [(Text, Text)]) -> [Text] -> [[(Text, Text)]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Bool -> Item -> [Item] -> Text -> [(Text, Text)]
getXrefFields Bool
isBibtex Item
baseEntry [Item]
entries)
                   (Text -> [Text]
splitKeys Text
v)
       (Text
x, Text
y) <- [(Text, Text)]
xs
       Bool -> [()]
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> [()]) -> Bool -> [()]
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Bool
forall a. Maybe a -> Bool
isNothing (Maybe Text -> Bool) -> Maybe Text -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Map Text Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
x (Map Text Text -> Maybe Text) -> Map Text Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ Item -> Map Text Text
fields Item
xrefEntry
       (Text, Text) -> [(Text, Text)]
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
x, Text
y)
     else do
       Text
k' <- if Bool
isBibtex
                then Text -> [Text]
forall (m :: * -> *) a. Monad m => a -> m a
return Text
k
                else Text -> Text -> Text -> [Text]
transformKey (Item -> Text
entryType Item
xrefEntry) (Item -> Text
entryType Item
baseEntry) Text
k
       Bool -> [()]
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> [()]) -> Bool -> [()]
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Bool
forall a. Maybe a -> Bool
isNothing (Maybe Text -> Bool) -> Maybe Text -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Map Text Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
k' (Map Text Text -> Maybe Text) -> Map Text Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ Item -> Map Text Text
fields Item
baseEntry
       (Text, Text) -> [(Text, Text)]
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
k',Text
v)

resolveCrossRef :: Bool -> [Item] -> Item -> Item
resolveCrossRef :: Bool -> [Item] -> Item -> Item
resolveCrossRef Bool
isBibtex [Item]
entries Item
entry =
  (Text -> Text -> Item -> Item) -> Item -> Map Text Text -> Item
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey Text -> Text -> Item -> Item
forall a. (Eq a, IsString a) => a -> Text -> Item -> Item
go Item
entry (Item -> Map Text Text
fields Item
entry)
  where go :: a -> Text -> Item -> Item
go a
key Text
val Item
entry' =
          if a
key a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
"crossref" Bool -> Bool -> Bool
|| a
key a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
"xdata"
          then Item
entry'{ fields :: Map Text Text
fields = Item -> Map Text Text
fields Item
entry' Map Text Text -> Map Text Text -> Map Text Text
forall a. Semigroup a => a -> a -> a
<>
                          [(Text, Text)] -> Map Text Text
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList (Bool -> Item -> [Item] -> Text -> [(Text, Text)]
getXrefFields Bool
isBibtex
                                        Item
entry [Item]
entries Text
val) }
          else Item
entry'

-- transformKey source target key
-- derived from Appendix C of bibtex manual
transformKey :: Text -> Text -> Text -> [Text]
transformKey :: Text -> Text -> Text -> [Text]
transformKey Text
_ Text
_ Text
"ids"            = []
transformKey Text
_ Text
_ Text
"crossref"       = []
transformKey Text
_ Text
_ Text
"xref"           = []
transformKey Text
_ Text
_ Text
"entryset"       = []
transformKey Text
_ Text
_ Text
"entrysubtype"   = []
transformKey Text
_ Text
_ Text
"execute"        = []
transformKey Text
_ Text
_ Text
"label"          = []
transformKey Text
_ Text
_ Text
"options"        = []
transformKey Text
_ Text
_ Text
"presort"        = []
transformKey Text
_ Text
_ Text
"related"        = []
transformKey Text
_ Text
_ Text
"relatedoptions" = []
transformKey Text
_ Text
_ Text
"relatedstring"  = []
transformKey Text
_ Text
_ Text
"relatedtype"    = []
transformKey Text
_ Text
_ Text
"shorthand"      = []
transformKey Text
_ Text
_ Text
"shorthandintro" = []
transformKey Text
_ Text
_ Text
"sortkey"        = []
transformKey Text
x Text
y Text
"author"
  | Text
x Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"mvbook", Text
"book"] Bool -> Bool -> Bool
&&
    Text
y Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"inbook", Text
"bookinbook", Text
"suppbook"] = [Text
"bookauthor", Text
"author"]
-- note: this next clause is not in the biblatex manual, but it makes
-- sense in the context of CSL conversion:
transformKey Text
x Text
y Text
"author"
  | Text
x Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"mvbook" Bool -> Bool -> Bool
&& Text
y Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"book" = [Text
"bookauthor", Text
"author"]
transformKey Text
"mvbook" Text
y Text
z
  | Text
y Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"book", Text
"inbook", Text
"bookinbook", Text
"suppbook"] = Text -> [Text]
standardTrans Text
z
transformKey Text
x Text
y Text
z
  | Text
x Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"mvcollection", Text
"mvreference"] Bool -> Bool -> Bool
&&
    Text
y Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"collection", Text
"reference", Text
"incollection", Text
"inreference",
               Text
"suppcollection"] = Text -> [Text]
standardTrans Text
z
transformKey Text
"mvproceedings" Text
y Text
z
  | Text
y Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"proceedings", Text
"inproceedings"] = Text -> [Text]
standardTrans Text
z
transformKey Text
"book" Text
y Text
z
  | Text
y Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"inbook", Text
"bookinbook", Text
"suppbook"] = Text -> [Text]
bookTrans Text
z
transformKey Text
x Text
y Text
z
  | Text
x Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"collection", Text
"reference"] Bool -> Bool -> Bool
&&
    Text
y Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"incollection", Text
"inreference", Text
"suppcollection"] = Text -> [Text]
bookTrans Text
z
transformKey Text
"proceedings" Text
"inproceedings" Text
z = Text -> [Text]
bookTrans Text
z
transformKey Text
"periodical" Text
y Text
z
  | Text
y Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"article", Text
"suppperiodical"] =
  case Text
z of
       Text
"title"          -> [Text
"journaltitle"]
       Text
"subtitle"       -> [Text
"journalsubtitle"]
       Text
"shorttitle"     -> []
       Text
"sorttitle"      -> []
       Text
"indextitle"     -> []
       Text
"indexsorttitle" -> []
       Text
_                -> [Text
z]
transformKey Text
_ Text
_ Text
x                = [Text
x]

standardTrans :: Text -> [Text]
standardTrans :: Text -> [Text]
standardTrans Text
z =
  case Text
z of
       Text
"title"          -> [Text
"maintitle"]
       Text
"subtitle"       -> [Text
"mainsubtitle"]
       Text
"titleaddon"     -> [Text
"maintitleaddon"]
       Text
"shorttitle"     -> []
       Text
"sorttitle"      -> []
       Text
"indextitle"     -> []
       Text
"indexsorttitle" -> []
       Text
_                -> [Text
z]

bookTrans :: Text -> [Text]
bookTrans :: Text -> [Text]
bookTrans Text
z =
  case Text
z of
       Text
"title"          -> [Text
"booktitle"]
       Text
"subtitle"       -> [Text
"booksubtitle"]
       Text
"titleaddon"     -> [Text
"booktitleaddon"]
       Text
"shorttitle"     -> []
       Text
"sorttitle"      -> []
       Text
"indextitle"     -> []
       Text
"indexsorttitle" -> []
       Text
_                -> [Text
z]

-- | A representation of a language and localization.
data Lang = Lang Text Text  -- e.g. "en" "US"

-- | Prints a 'Lang' in BCP 47 format.
langToLocale :: Lang -> Text
langToLocale :: Lang -> Text
langToLocale (Lang Text
x Text
y) = Text
x Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (if Text -> Bool
T.null Text
y then Text
"" else Char -> Text -> Text
T.cons Char
'-' Text
y)

-- Biblatex Localization Keys (see Biblatex manual)
-- Currently we only map a subset likely to be used in Biblatex *databases*
-- (in fields such as `type`, and via `\bibstring{}` commands).

resolveKey :: Lang -> Formatted -> Formatted
resolveKey :: Lang -> Formatted -> Formatted
resolveKey Lang
lang (Formatted [Inline]
ils) = [Inline] -> Formatted
Formatted ((Inline -> Inline) -> [Inline] -> [Inline]
forall a b. Walkable a b => (a -> a) -> b -> b
Walk.walk Inline -> Inline
go [Inline]
ils)
  where go :: Inline -> Inline
go (Str Text
s) = Text -> Inline
Str (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ Lang -> Text -> Text
resolveKey' Lang
lang Text
s
        go Inline
x       = Inline
x

-- biblatex localization keys, from files at
-- http://github.com/plk/biblatex/tree/master/tex/latex/biblatex/lbx
-- Some keys missing in these were added from csl locale files at
-- http://github.com/citation-style-language/locales -- labeled "csl"
resolveKey' :: Lang -> Text -> Text
resolveKey' :: Lang -> Text -> Text
resolveKey' (Lang Text
"ca" Text
"AD") Text
k =
    case Text -> Text
T.toLower Text
k of
       Text
"inpreparation" -> Text
"en preparació"
       Text
"submitted"     -> Text
"enviat"
       Text
"forthcoming"   -> Text
"disponible en breu"
       Text
"inpress"       -> Text
"a impremta"
       Text
"prepublished"  -> Text
"pre-publicat"
       Text
"mathesis"      -> Text
"tesi de màster"
       Text
"phdthesis"     -> Text
"tesi doctoral"
       Text
"candthesis"    -> Text
"tesi de candidatura"
       Text
"techreport"    -> Text
"informe tècnic"
       Text
"resreport"     -> Text
"informe de recerca"
       Text
"software"      -> Text
"programari"
       Text
"datacd"        -> Text
"CD de dades"
       Text
"audiocd"       -> Text
"CD d’àudio"
       Text
"patent"        -> Text
"patent"
       Text
"patentde"      -> Text
"patent alemana"
       Text
"patenteu"      -> Text
"patent europea"
       Text
"patentfr"      -> Text
"patent francesa"
       Text
"patentuk"      -> Text
"patent britànica"
       Text
"patentus"      -> Text
"patent estatunidenca"
       Text
"patreq"        -> Text
"soŀlicitud de patent"
       Text
"patreqde"      -> Text
"soŀlicitud de patent alemana"
       Text
"patreqeu"      -> Text
"soŀlicitud de patent europea"
       Text
"patreqfr"      -> Text
"soŀlicitud de patent francesa"
       Text
"patrequk"      -> Text
"soŀlicitud de patent britànica"
       Text
"patrequs"      -> Text
"soŀlicitud de patent estatunidenca"
       Text
"countryde"     -> Text
"Alemanya"
       Text
"countryeu"     -> Text
"Unió Europea"
       Text
"countryep"     -> Text
"Unió Europea"
       Text
"countryfr"     -> Text
"França"
       Text
"countryuk"     -> Text
"Regne Unit"
       Text
"countryus"     -> Text
"Estats Units d’Amèrica"
       Text
"newseries"     -> Text
"sèrie nova"
       Text
"oldseries"     -> Text
"sèrie antiga"
       Text
_               -> Text
k
resolveKey' (Lang Text
"da" Text
"DK") Text
k =
    case Text -> Text
T.toLower Text
k of
       -- "inpreparation" -> "" -- missing
       -- "submitted"     -> "" -- missing
       Text
"forthcoming" -> Text
"kommende" -- csl
       Text
"inpress"     -> Text
"i tryk"   -- csl
       -- "prepublished"  -> "" -- missing
       Text
"mathesis"    -> Text
"speciale"
       Text
"phdthesis"   -> Text
"ph.d.-afhandling"
       Text
"candthesis"  -> Text
"kandidatafhandling"
       Text
"techreport"  -> Text
"teknisk rapport"
       Text
"resreport"   -> Text
"forskningsrapport"
       Text
"software"    -> Text
"software"
       Text
"datacd"      -> Text
"data-cd"
       Text
"audiocd"     -> Text
"lyd-cd"
       Text
"patent"      -> Text
"patent"
       Text
"patentde"    -> Text
"tysk patent"
       Text
"patenteu"    -> Text
"europæisk patent"
       Text
"patentfr"    -> Text
"fransk patent"
       Text
"patentuk"    -> Text
"britisk patent"
       Text
"patentus"    -> Text
"amerikansk patent"
       Text
"patreq"      -> Text
"ansøgning om patent"
       Text
"patreqde"    -> Text
"ansøgning om tysk patent"
       Text
"patreqeu"    -> Text
"ansøgning om europæisk patent"
       Text
"patreqfr"    -> Text
"ansøgning om fransk patent"
       Text
"patrequk"    -> Text
"ansøgning om britisk patent"
       Text
"patrequs"    -> Text
"ansøgning om amerikansk patent"
       Text
"countryde"   -> Text
"Tyskland"
       Text
"countryeu"   -> Text
"Europæiske Union"
       Text
"countryep"   -> Text
"Europæiske Union"
       Text
"countryfr"   -> Text
"Frankrig"
       Text
"countryuk"   -> Text
"Storbritanien"
       Text
"countryus"   -> Text
"USA"
       Text
"newseries"   -> Text
"ny serie"
       Text
"oldseries"   -> Text
"gammel serie"
       Text
_             -> Text
k
resolveKey' (Lang Text
"de" Text
"DE") Text
k =
    case Text -> Text
T.toLower Text
k of
       Text
"inpreparation" -> Text
"in Vorbereitung"
       Text
"submitted"     -> Text
"eingereicht"
       Text
"forthcoming"   -> Text
"im Erscheinen"
       Text
"inpress"       -> Text
"im Druck"
       Text
"prepublished"  -> Text
"Vorveröffentlichung"
       Text
"mathesis"      -> Text
"Magisterarbeit"
       Text
"phdthesis"     -> Text
"Dissertation"
       -- "candthesis" -> "" -- missing
       Text
"techreport"    -> Text
"Technischer Bericht"
       Text
"resreport"     -> Text
"Forschungsbericht"
       Text
"software"      -> Text
"Computer-Software"
       Text
"datacd"        -> Text
"CD-ROM"
       Text
"audiocd"       -> Text
"Audio-CD"
       Text
"patent"        -> Text
"Patent"
       Text
"patentde"      -> Text
"deutsches Patent"
       Text
"patenteu"      -> Text
"europäisches Patent"
       Text
"patentfr"      -> Text
"französisches Patent"
       Text
"patentuk"      -> Text
"britisches Patent"
       Text
"patentus"      -> Text
"US-Patent"
       Text
"patreq"        -> Text
"Patentanmeldung"
       Text
"patreqde"      -> Text
"deutsche Patentanmeldung"
       Text
"patreqeu"      -> Text
"europäische Patentanmeldung"
       Text
"patreqfr"      -> Text
"französische Patentanmeldung"
       Text
"patrequk"      -> Text
"britische Patentanmeldung"
       Text
"patrequs"      -> Text
"US-Patentanmeldung"
       Text
"countryde"     -> Text
"Deutschland"
       Text
"countryeu"     -> Text
"Europäische Union"
       Text
"countryep"     -> Text
"Europäische Union"
       Text
"countryfr"     -> Text
"Frankreich"
       Text
"countryuk"     -> Text
"Großbritannien"
       Text
"countryus"     -> Text
"USA"
       Text
"newseries"     -> Text
"neue Folge"
       Text
"oldseries"     -> Text
"alte Folge"
       Text
_               -> Text
k
resolveKey' (Lang Text
"en" Text
"US") Text
k =
  case Text -> Text
T.toLower Text
k of
       Text
"audiocd"        -> Text
"audio CD"
       Text
"by"             -> Text
"by"
       Text
"candthesis"     -> Text
"Candidate thesis"
       Text
"countryde"      -> Text
"Germany"
       Text
"countryep"      -> Text
"European Union"
       Text
"countryeu"      -> Text
"European Union"
       Text
"countryfr"      -> Text
"France"
       Text
"countryuk"      -> Text
"United Kingdom"
       Text
"countryus"      -> Text
"United States of America"
       Text
"datacd"         -> Text
"data CD"
       Text
"edition"        -> Text
"ed."
       Text
"forthcoming"    -> Text
"forthcoming"
       Text
"inpreparation"  -> Text
"in preparation"
       Text
"inpress"        -> Text
"in press"
       Text
"introduction"   -> Text
"introduction"
       Text
"jourser"        -> Text
"ser."
       Text
"mathesis"       -> Text
"Master’s thesis"
       Text
"newseries"      -> Text
"new series"
       Text
"nodate"         -> Text
"n. d."
       Text
"number"         -> Text
"no."
       Text
"numbers"        -> Text
"nos."
       Text
"oldseries"      -> Text
"old series"
       Text
"patent"         -> Text
"patent"
       Text
"patentde"       -> Text
"German patent"
       Text
"patenteu"       -> Text
"European patent"
       Text
"patentfr"       -> Text
"French patent"
       Text
"patentuk"       -> Text
"British patent"
       Text
"patentus"       -> Text
"U.S. patent"
       Text
"patreq"         -> Text
"patent request"
       Text
"patreqde"       -> Text
"German patent request"
       Text
"patreqeu"       -> Text
"European patent request"
       Text
"patreqfr"       -> Text
"French patent request"
       Text
"patrequk"       -> Text
"British patent request"
       Text
"patrequs"       -> Text
"U.S. patent request"
       Text
"phdthesis"      -> Text
"PhD thesis"
       Text
"prepublished"   -> Text
"pre-published"
       Text
"pseudonym"      -> Text
"pseud."
       Text
"recorded"       -> Text
"recorded"
       Text
"resreport"      -> Text
"research report"
       Text
"reviewof"       -> Text
"Review of"
       Text
"revisededition" -> Text
"rev. ed."
       Text
"software"       -> Text
"computer software"
       Text
"submitted"      -> Text
"submitted"
       Text
"techreport"     -> Text
"technical report"
       Text
"volume"         -> Text
"vol."
       Text
_                -> Text
k
resolveKey' (Lang Text
"es" Text
"ES") Text
k =
    case Text -> Text
T.toLower Text
k of
       -- "inpreparation" -> "" -- missing
       -- "submitted"     -> "" -- missing
       Text
"forthcoming" -> Text
"previsto"    -- csl
       Text
"inpress"     -> Text
"en imprenta" -- csl
       -- "prepublished"  -> "" -- missing
       Text
"mathesis"    -> Text
"Tesis de licenciatura"
       Text
"phdthesis"   -> Text
"Tesis doctoral"
       -- "candthesis" -> "" -- missing
       Text
"techreport"  -> Text
"informe técnico"
       -- "resreport"  -> "" -- missing
       -- "software"   -> "" -- missing
       -- "datacd"     -> "" -- missing
       -- "audiocd"    -> "" -- missing
       Text
"patent"      -> Text
"patente"
       Text
"patentde"    -> Text
"patente alemana"
       Text
"patenteu"    -> Text
"patente europea"
       Text
"patentfr"    -> Text
"patente francesa"
       Text
"patentuk"    -> Text
"patente británica"
       Text
"patentus"    -> Text
"patente americana"
       Text
"patreq"      -> Text
"solicitud de patente"
       Text
"patreqde"    -> Text
"solicitud de patente alemana"
       Text
"patreqeu"    -> Text
"solicitud de patente europea"
       Text
"patreqfr"    -> Text
"solicitud de patente francesa"
       Text
"patrequk"    -> Text
"solicitud de patente británica"
       Text
"patrequs"    -> Text
"solicitud de patente americana"
       Text
"countryde"   -> Text
"Alemania"
       Text
"countryeu"   -> Text
"Unión Europea"
       Text
"countryep"   -> Text
"Unión Europea"
       Text
"countryfr"   -> Text
"Francia"
       Text
"countryuk"   -> Text
"Reino Unido"
       Text
"countryus"   -> Text
"Estados Unidos de América"
       Text
"newseries"   -> Text
"nueva época"
       Text
"oldseries"   -> Text
"antigua época"
       Text
_             -> Text
k
resolveKey' (Lang Text
"fi" Text
"FI") Text
k =
    case Text -> Text
T.toLower Text
k of
       -- "inpreparation" -> ""      -- missing
       -- "submitted"     -> ""      -- missing
       Text
"forthcoming" -> Text
"tulossa"  -- csl
       Text
"inpress"     -> Text
"painossa" -- csl
       -- "prepublished"  -> ""      -- missing
       Text
"mathesis"    -> Text
"tutkielma"
       Text
"phdthesis"   -> Text
"tohtorinväitöskirja"
       Text
"candthesis"  -> Text
"kandidat"
       Text
"techreport"  -> Text
"tekninen raportti"
       Text
"resreport"   -> Text
"tutkimusraportti"
       Text
"software"    -> Text
"ohjelmisto"
       Text
"datacd"      -> Text
"data-CD"
       Text
"audiocd"     -> Text
"ääni-CD"
       Text
"patent"      -> Text
"patentti"
       Text
"patentde"    -> Text
"saksalainen patentti"
       Text
"patenteu"    -> Text
"Euroopan Unionin patentti"
       Text
"patentfr"    -> Text
"ranskalainen patentti"
       Text
"patentuk"    -> Text
"englantilainen patentti"
       Text
"patentus"    -> Text
"yhdysvaltalainen patentti"
       Text
"patreq"      -> Text
"patenttihakemus"
       Text
"patreqde"    -> Text
"saksalainen patenttihakemus"
       Text
"patreqeu"    -> Text
"Euroopan Unionin patenttihakemus"
       Text
"patreqfr"    -> Text
"ranskalainen patenttihakemus"
       Text
"patrequk"    -> Text
"englantilainen patenttihakemus"
       Text
"patrequs"    -> Text
"yhdysvaltalainen patenttihakemus"
       Text
"countryde"   -> Text
"Saksa"
       Text
"countryeu"   -> Text
"Euroopan Unioni"
       Text
"countryep"   -> Text
"Euroopan Unioni"
       Text
"countryfr"   -> Text
"Ranska"
       Text
"countryuk"   -> Text
"Iso-Britannia"
       Text
"countryus"   -> Text
"Yhdysvallat"
       Text
"newseries"   -> Text
"uusi sarja"
       Text
"oldseries"   -> Text
"vanha sarja"
       Text
_             -> Text
k

resolveKey' (Lang Text
"fr" Text
"FR") Text
k =
    case Text -> Text
T.toLower Text
k of
       Text
"inpreparation" -> Text
"en préparation"
       Text
"submitted"     -> Text
"soumis"
       Text
"forthcoming"   -> Text
"à paraître"
       Text
"inpress"       -> Text
"sous presse"
       Text
"prepublished"  -> Text
"prépublié"
       Text
"mathesis"      -> Text
"mémoire de master"
       Text
"phdthesis"     -> Text
"thèse de doctorat"
       Text
"candthesis"    -> Text
"thèse de candidature"
       Text
"techreport"    -> Text
"rapport technique"
       Text
"resreport"     -> Text
"rapport scientifique"
       Text
"software"      -> Text
"logiciel"
       Text
"datacd"        -> Text
"cédérom"
       Text
"audiocd"       -> Text
"disque compact audio"
       Text
"patent"        -> Text
"brevet"
       Text
"patentde"      -> Text
"brevet allemand"
       Text
"patenteu"      -> Text
"brevet européen"
       Text
"patentfr"      -> Text
"brevet français"
       Text
"patentuk"      -> Text
"brevet britannique"
       Text
"patentus"      -> Text
"brevet américain"
       Text
"patreq"        -> Text
"demande de brevet"
       Text
"patreqde"      -> Text
"demande de brevet allemand"
       Text
"patreqeu"      -> Text
"demande de brevet européen"
       Text
"patreqfr"      -> Text
"demande de brevet français"
       Text
"patrequk"      -> Text
"demande de brevet britannique"
       Text
"patrequs"      -> Text
"demande de brevet américain"
       Text
"countryde"     -> Text
"Allemagne"
       Text
"countryeu"     -> Text
"Union européenne"
       Text
"countryep"     -> Text
"Union européenne"
       Text
"countryfr"     -> Text
"France"
       Text
"countryuk"     -> Text
"Royaume-Uni"
       Text
"countryus"     -> Text
"États-Unis"
       Text
"newseries"     -> Text
"nouvelle série"
       Text
"oldseries"     -> Text
"ancienne série"
       Text
_               -> Text
k
resolveKey' (Lang Text
"it" Text
"IT") Text
k =
    case Text -> Text
T.toLower Text
k of
       -- "inpreparation" -> "" -- missing
       -- "submitted"     -> "" -- missing
       Text
"forthcoming" -> Text
"futuro" -- csl
       Text
"inpress"     -> Text
"in stampa"
       -- "prepublished"  -> "" -- missing
       Text
"mathesis"    -> Text
"tesi di laurea magistrale"
       Text
"phdthesis"   -> Text
"tesi di dottorato"
       -- "candthesis" -> "" -- missing
       Text
"techreport"  -> Text
"rapporto tecnico"
       Text
"resreport"   -> Text
"rapporto di ricerca"
       -- "software"   -> "" -- missing
       -- "datacd"     -> "" -- missing
       -- "audiocd"    -> "" -- missing
       Text
"patent"      -> Text
"brevetto"
       Text
"patentde"    -> Text
"brevetto tedesco"
       Text
"patenteu"    -> Text
"brevetto europeo"
       Text
"patentfr"    -> Text
"brevetto francese"
       Text
"patentuk"    -> Text
"brevetto britannico"
       Text
"patentus"    -> Text
"brevetto americano"
       Text
"patreq"      -> Text
"brevetto richiesto"
       Text
"patreqde"    -> Text
"brevetto tedesco richiesto"
       Text
"patreqeu"    -> Text
"brevetto europeo richiesto"
       Text
"patreqfr"    -> Text
"brevetto francese richiesto"
       Text
"patrequk"    -> Text
"brevetto britannico richiesto"
       Text
"patrequs"    -> Text
"brevetto U.S.A. richiesto"
       Text
"countryde"   -> Text
"Germania"
       Text
"countryeu"   -> Text
"Unione Europea"
       Text
"countryep"   -> Text
"Unione Europea"
       Text
"countryfr"   -> Text
"Francia"
       Text
"countryuk"   -> Text
"Regno Unito"
       Text
"countryus"   -> Text
"Stati Uniti d’America"
       Text
"newseries"   -> Text
"nuova serie"
       Text
"oldseries"   -> Text
"vecchia serie"
       Text
_             -> Text
k
resolveKey' (Lang Text
"nl" Text
"NL") Text
k =
    case Text -> Text
T.toLower Text
k of
       Text
"inpreparation" -> Text
"in voorbereiding"
       Text
"submitted"     -> Text
"ingediend"
       Text
"forthcoming"   -> Text
"onderweg"
       Text
"inpress"       -> Text
"in druk"
       Text
"prepublished"  -> Text
"voorpublicatie"
       Text
"mathesis"      -> Text
"masterscriptie"
       Text
"phdthesis"     -> Text
"proefschrift"
       -- "candthesis" -> "" -- missing
       Text
"techreport"    -> Text
"technisch rapport"
       Text
"resreport"     -> Text
"onderzoeksrapport"
       Text
"software"      -> Text
"computersoftware"
       Text
"datacd"        -> Text
"cd-rom"
       Text
"audiocd"       -> Text
"audio-cd"
       Text
"patent"        -> Text
"patent"
       Text
"patentde"      -> Text
"Duits patent"
       Text
"patenteu"      -> Text
"Europees patent"
       Text
"patentfr"      -> Text
"Frans patent"
       Text
"patentuk"      -> Text
"Brits patent"
       Text
"patentus"      -> Text
"Amerikaans patent"
       Text
"patreq"        -> Text
"patentaanvraag"
       Text
"patreqde"      -> Text
"Duitse patentaanvraag"
       Text
"patreqeu"      -> Text
"Europese patentaanvraag"
       Text
"patreqfr"      -> Text
"Franse patentaanvraag"
       Text
"patrequk"      -> Text
"Britse patentaanvraag"
       Text
"patrequs"      -> Text
"Amerikaanse patentaanvraag"
       Text
"countryde"     -> Text
"Duitsland"
       Text
"countryeu"     -> Text
"Europese Unie"
       Text
"countryep"     -> Text
"Europese Unie"
       Text
"countryfr"     -> Text
"Frankrijk"
       Text
"countryuk"     -> Text
"Verenigd Koninkrijk"
       Text
"countryus"     -> Text
"Verenigde Staten van Amerika"
       Text
"newseries"     -> Text
"nieuwe reeks"
       Text
"oldseries"     -> Text
"oude reeks"
       Text
_               -> Text
k
resolveKey' (Lang Text
"pl" Text
"PL") Text
k =
    case Text -> Text
T.toLower Text
k of
       Text
"inpreparation" -> Text
"przygotowanie"
       Text
"submitted"     -> Text
"prezentacja"
       Text
"forthcoming"   -> Text
"przygotowanie"
       Text
"inpress"       -> Text
"wydrukowane"
       Text
"prepublished"  -> Text
"przedwydanie"
       Text
"mathesis"      -> Text
"praca magisterska"
       Text
"phdthesis"     -> Text
"praca doktorska"
       Text
"techreport"    -> Text
"sprawozdanie techniczne"
       Text
"resreport"     -> Text
"sprawozdanie naukowe"
       Text
"software"      -> Text
"oprogramowanie"
       Text
"datacd"        -> Text
"CD-ROM"
       Text
"audiocd"       -> Text
"audio CD"
       Text
"patent"        -> Text
"patent"
       Text
"patentde"      -> Text
"patent Niemiec"
       Text
"patenteu"      -> Text
"patent Europy"
       Text
"patentfr"      -> Text
"patent Francji"
       Text
"patentuk"      -> Text
"patent Wielkiej Brytanji"
       Text
"patentus"      -> Text
"patent USA"
       Text
"patreq"        -> Text
"podanie na patent"
       Text
"patreqeu"      -> Text
"podanie na patent Europy"
       Text
"patrequs"      -> Text
"podanie na patent USA"
       Text
"countryde"     -> Text
"Niemcy"
       Text
"countryeu"     -> Text
"Unia Europejska"
       Text
"countryep"     -> Text
"Unia Europejska"
       Text
"countryfr"     -> Text
"Francja"
       Text
"countryuk"     -> Text
"Wielka Brytania"
       Text
"countryus"     -> Text
"Stany Zjednoczone Ameryki"
       Text
"newseries"     -> Text
"nowa serja"
       Text
"oldseries"     -> Text
"stara serja"
       Text
_               -> Text
k
resolveKey' (Lang Text
"pt" Text
"PT") Text
k =
    case Text -> Text
T.toLower Text
k of
       -- "candthesis" -> "" -- missing
       Text
"techreport"  -> Text
"relatório técnico"
       Text
"resreport"   -> Text
"relatório de pesquisa"
       Text
"software"    -> Text
"software"
       Text
"datacd"      -> Text
"CD-ROM"
       Text
"patent"      -> Text
"patente"
       Text
"patentde"    -> Text
"patente alemã"
       Text
"patenteu"    -> Text
"patente européia"
       Text
"patentfr"    -> Text
"patente francesa"
       Text
"patentuk"    -> Text
"patente britânica"
       Text
"patentus"    -> Text
"patente americana"
       Text
"patreq"      -> Text
"pedido de patente"
       Text
"patreqde"    -> Text
"pedido de patente alemã"
       Text
"patreqeu"    -> Text
"pedido de patente européia"
       Text
"patreqfr"    -> Text
"pedido de patente francesa"
       Text
"patrequk"    -> Text
"pedido de patente britânica"
       Text
"patrequs"    -> Text
"pedido de patente americana"
       Text
"countryde"   -> Text
"Alemanha"
       Text
"countryeu"   -> Text
"União Europeia"
       Text
"countryep"   -> Text
"União Europeia"
       Text
"countryfr"   -> Text
"França"
       Text
"countryuk"   -> Text
"Reino Unido"
       Text
"countryus"   -> Text
"Estados Unidos da América"
       Text
"newseries"   -> Text
"nova série"
       Text
"oldseries"   -> Text
"série antiga"
       -- "inpreparation" -> "" -- missing
       Text
"forthcoming" -> Text
"a publicar" -- csl
       Text
"inpress"     -> Text
"na imprensa"
       -- "prepublished"  -> "" -- missing
       Text
"mathesis"    -> Text
"tese de mestrado"
       Text
"phdthesis"   -> Text
"tese de doutoramento"
       Text
"audiocd"     -> Text
"CD áudio"
       Text
_             -> Text
k
resolveKey' (Lang Text
"pt" Text
"BR") Text
k =
    case Text -> Text
T.toLower Text
k of
       -- "candthesis" -> "" -- missing
       Text
"techreport"    -> Text
"relatório técnico"
       Text
"resreport"     -> Text
"relatório de pesquisa"
       Text
"software"      -> Text
"software"
       Text
"datacd"        -> Text
"CD-ROM"
       Text
"patent"        -> Text
"patente"
       Text
"patentde"      -> Text
"patente alemã"
       Text
"patenteu"      -> Text
"patente européia"
       Text
"patentfr"      -> Text
"patente francesa"
       Text
"patentuk"      -> Text
"patente britânica"
       Text
"patentus"      -> Text
"patente americana"
       Text
"patreq"        -> Text
"pedido de patente"
       Text
"patreqde"      -> Text
"pedido de patente alemã"
       Text
"patreqeu"      -> Text
"pedido de patente européia"
       Text
"patreqfr"      -> Text
"pedido de patente francesa"
       Text
"patrequk"      -> Text
"pedido de patente britânica"
       Text
"patrequs"      -> Text
"pedido de patente americana"
       Text
"countryde"     -> Text
"Alemanha"
       Text
"countryeu"     -> Text
"União Europeia"
       Text
"countryep"     -> Text
"União Europeia"
       Text
"countryfr"     -> Text
"França"
       Text
"countryuk"     -> Text
"Reino Unido"
       Text
"countryus"     -> Text
"Estados Unidos da América"
       Text
"newseries"     -> Text
"nova série"
       Text
"oldseries"     -> Text
"série antiga"
       Text
"inpreparation" -> Text
"em preparação"
       Text
"forthcoming"   -> Text
"aceito para publicação"
       Text
"inpress"       -> Text
"no prelo"
       Text
"prepublished"  -> Text
"pré-publicado"
       Text
"mathesis"      -> Text
"dissertação de mestrado"
       Text
"phdthesis"     -> Text
"tese de doutorado"
       Text
"audiocd"       -> Text
"CD de áudio"
       Text
_               -> Text
k
resolveKey' (Lang Text
"sv" Text
"SE") Text
k =
    case Text -> Text
T.toLower Text
k of
       -- "inpreparation" -> "" -- missing
       -- "submitted"     -> "" -- missing
       Text
"forthcoming" -> Text
"kommande" -- csl
       Text
"inpress"     -> Text
"i tryck"  -- csl
       -- "prepublished"  -> "" -- missing
       Text
"mathesis"    -> Text
"examensarbete"
       Text
"phdthesis"   -> Text
"doktorsavhandling"
       Text
"candthesis"  -> Text
"kandidatavhandling"
       Text
"techreport"  -> Text
"teknisk rapport"
       Text
"resreport"   -> Text
"forskningsrapport"
       Text
"software"    -> Text
"datorprogram"
       Text
"datacd"      -> Text
"data-cd"
       Text
"audiocd"     -> Text
"ljud-cd"
       Text
"patent"      -> Text
"patent"
       Text
"patentde"    -> Text
"tyskt patent"
       Text
"patenteu"    -> Text
"europeiskt patent"
       Text
"patentfr"    -> Text
"franskt patent"
       Text
"patentuk"    -> Text
"brittiskt patent"
       Text
"patentus"    -> Text
"amerikanskt patent"
       Text
"patreq"      -> Text
"patentansökan"
       Text
"patreqde"    -> Text
"ansökan om tyskt patent"
       Text
"patreqeu"    -> Text
"ansökan om europeiskt patent"
       Text
"patreqfr"    -> Text
"ansökan om franskt patent"
       Text
"patrequk"    -> Text
"ansökan om brittiskt patent"
       Text
"patrequs"    -> Text
"ansökan om amerikanskt patent"
       Text
"countryde"   -> Text
"Tyskland"
       Text
"countryeu"   -> Text
"Europeiska unionen"
       Text
"countryep"   -> Text
"Europeiska unionen"
       Text
"countryfr"   -> Text
"Frankrike"
       Text
"countryuk"   -> Text
"Storbritannien"
       Text
"countryus"   -> Text
"USA"
       Text
"newseries"   -> Text
"ny följd"
       Text
"oldseries"   -> Text
"gammal följd"
       Text
_             -> Text
k
resolveKey' Lang
_ Text
k = Lang -> Text -> Text
resolveKey' (Text -> Text -> Lang
Lang Text
"en" Text
"US") Text
k

parseMonth :: Text -> Maybe Int
parseMonth :: Text -> Maybe Int
parseMonth Text
s =
  case Text -> Text
T.toLower Text
s of
         Text
"jan" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
1
         Text
"feb" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
2
         Text
"mar" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
3
         Text
"apr" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
4
         Text
"may" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
5
         Text
"jun" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
6
         Text
"jul" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
7
         Text
"aug" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
8
         Text
"sep" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
9
         Text
"oct" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
10
         Text
"nov" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
11
         Text
"dec" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
12
         Text
_     -> Text -> Maybe Int
forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
s

data BibState = BibState{
           BibState -> Bool
untitlecase    :: Bool
         , BibState -> Lang
localeLanguage :: Lang
         }

type Bib = RWST Item () BibState Maybe

notFound :: Text -> Bib a
notFound :: Text -> Bib a
notFound Text
f = String -> Bib a
forall (m :: * -> *) a. MonadFail m => String -> m a
Prelude.fail (String -> Bib a) -> String -> Bib a
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
f String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" not found"

getField :: Text -> Bib Formatted
getField :: Text -> Bib Formatted
getField Text
f = do
  Map Text Text
fs <- (Item -> Map Text Text)
-> RWST Item () BibState Maybe (Map Text Text)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Item -> Map Text Text
fields
  case Text -> Map Text Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
f Map Text Text
fs of
       Just Text
x  -> Text -> Bib Formatted
latex Text
x
       Maybe Text
Nothing -> Text -> Bib Formatted
forall a. Text -> Bib a
notFound Text
f

getPeriodicalTitle :: Text -> Bib Formatted
getPeriodicalTitle :: Text -> Bib Formatted
getPeriodicalTitle Text
f = do
  Map Text Text
fs <- (Item -> Map Text Text)
-> RWST Item () BibState Maybe (Map Text Text)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Item -> Map Text Text
fields
  case Text -> Map Text Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
f Map Text Text
fs of
       Just Text
x  -> [Block] -> Bib Formatted
blocksToFormatted ([Block] -> Bib Formatted) -> [Block] -> Bib Formatted
forall a b. (a -> b) -> a -> b
$ ([Inline] -> [Inline]) -> [Block] -> [Block]
onBlocks [Inline] -> [Inline]
protectCase ([Block] -> [Block]) -> [Block] -> [Block]
forall a b. (a -> b) -> a -> b
$ Text -> [Block]
latex' (Text -> [Block]) -> Text -> [Block]
forall a b. (a -> b) -> a -> b
$ Text -> Text
trim Text
x
       Maybe Text
Nothing -> Text -> Bib Formatted
forall a. Text -> Bib a
notFound Text
f

getTitle :: Text -> Bib Formatted
getTitle :: Text -> Bib Formatted
getTitle Text
f = do
  Map Text Text
fs <- (Item -> Map Text Text)
-> RWST Item () BibState Maybe (Map Text Text)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Item -> Map Text Text
fields
  case Text -> Map Text Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
f Map Text Text
fs of
       Just Text
x  -> Text -> Bib Formatted
latexTitle Text
x
       Maybe Text
Nothing -> Text -> Bib Formatted
forall a. Text -> Bib a
notFound Text
f

getShortTitle :: Bool -> Text -> Bib Formatted
getShortTitle :: Bool -> Text -> Bib Formatted
getShortTitle Bool
requireColon Text
f = do
  Map Text Text
fs <- (Item -> Map Text Text)
-> RWST Item () BibState Maybe (Map Text Text)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Item -> Map Text Text
fields
  Bool
utc <- (BibState -> Bool) -> RWST Item () BibState Maybe Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets BibState -> Bool
untitlecase
  let processTitle :: [Block] -> [Block]
processTitle = if Bool
utc then ([Inline] -> [Inline]) -> [Block] -> [Block]
onBlocks [Inline] -> [Inline]
unTitlecase else [Block] -> [Block]
forall a. a -> a
id
  case Text -> Map Text Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
f Map Text Text
fs of
       Just Text
x  -> case [Block] -> [Block]
processTitle ([Block] -> [Block]) -> [Block] -> [Block]
forall a b. (a -> b) -> a -> b
$ Text -> [Block]
latex' Text
x of
                       [Block]
bs | Bool -> Bool
not Bool
requireColon Bool -> Bool -> Bool
|| [Block] -> Bool
containsColon [Block]
bs ->
                                  [Block] -> Bib Formatted
blocksToFormatted ([Block] -> Bib Formatted) -> [Block] -> Bib Formatted
forall a b. (a -> b) -> a -> b
$ [Block] -> [Block]
upToColon [Block]
bs
                          | Bool
otherwise -> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
       Maybe Text
Nothing -> Text -> Bib Formatted
forall a. Text -> Bib a
notFound Text
f

containsColon :: [Block] -> Bool
containsColon :: [Block] -> Bool
containsColon [Para  [Inline]
xs] = Text -> Inline
Str Text
":" Inline -> [Inline] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Inline]
xs
containsColon [Plain [Inline]
xs] = [Block] -> Bool
containsColon [[Inline] -> Block
Para [Inline]
xs]
containsColon [Block]
_          = Bool
False

upToColon :: [Block] -> [Block]
upToColon :: [Block] -> [Block]
upToColon [Para  [Inline]
xs] = [[Inline] -> Block
Para ([Inline] -> Block) -> [Inline] -> Block
forall a b. (a -> b) -> a -> b
$ (Inline -> Bool) -> [Inline] -> [Inline]
forall a. (a -> Bool) -> [a] -> [a]
takeWhile (Inline -> Inline -> Bool
forall a. Eq a => a -> a -> Bool
/= Text -> Inline
Str Text
":") [Inline]
xs]
upToColon [Plain [Inline]
xs] = [Block] -> [Block]
upToColon [[Inline] -> Block
Para [Inline]
xs]
upToColon [Block]
bs         = [Block]
bs

getDates :: Text -> Bib [RefDate]
getDates :: Text -> Bib [RefDate]
getDates Text
f = Text -> [RefDate]
parseEDTFDate (Text -> [RefDate])
-> RWST Item () BibState Maybe Text -> Bib [RefDate]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> RWST Item () BibState Maybe Text
getRawField Text
f

isNumber :: Text -> Bool
isNumber :: Text -> Bool
isNumber Text
t = case Text -> Maybe (Char, Text)
T.uncons Text
t of
  Just (Char
'-', Text
ds) -> (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isDigit Text
ds
  Just (Char, Text)
_         -> (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isDigit Text
t
  Maybe (Char, Text)
Nothing        -> Bool
False

-- A negative (BC) year might be written with -- or --- in bibtex:
fixLeadingDash :: Text -> Text
fixLeadingDash :: Text -> Text
fixLeadingDash Text
t = case Text -> Maybe (Char, Text)
T.uncons Text
t of
  Just (Char
c, Text
ds) | (Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'–' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'—') Bool -> Bool -> Bool
&& Text -> Bool
firstIsDigit Text
ds -> Char -> Text -> Text
T.cons Char
'–' Text
ds
  Maybe (Char, Text)
_ -> Text
t
 where firstIsDigit :: Text -> Bool
firstIsDigit = Bool -> ((Char, Text) -> Bool) -> Maybe (Char, Text) -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Char -> Bool
isDigit (Char -> Bool) -> ((Char, Text) -> Char) -> (Char, Text) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char, Text) -> Char
forall a b. (a, b) -> a
fst) (Maybe (Char, Text) -> Bool)
-> (Text -> Maybe (Char, Text)) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Maybe (Char, Text)
T.uncons

getOldDates :: Text -> Bib [RefDate]
getOldDates :: Text -> Bib [RefDate]
getOldDates Text
prefix = do
  Text
year' <- Text -> Text
fixLeadingDash (Text -> Text)
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> RWST Item () BibState Maybe Text
getRawField (Text
prefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"year")
           RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
""
  Maybe Int
month' <- (Text -> Maybe Int
parseMonth (Text -> Maybe Int)
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> RWST Item () BibState Maybe Text
getRawField (Text
prefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"month"))
            RWST Item () BibState Maybe (Maybe Int)
-> RWST Item () BibState Maybe (Maybe Int)
-> RWST Item () BibState Maybe (Maybe Int)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe Int -> RWST Item () BibState Maybe (Maybe Int)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Int
forall a. Maybe a
Nothing
  Maybe Int
day' <- (Text -> Maybe Int
forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead (Text -> Maybe Int)
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> RWST Item () BibState Maybe Text
getRawField (Text
prefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"day"))
          RWST Item () BibState Maybe (Maybe Int)
-> RWST Item () BibState Maybe (Maybe Int)
-> RWST Item () BibState Maybe (Maybe Int)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe Int -> RWST Item () BibState Maybe (Maybe Int)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Int
forall a. Maybe a
Nothing
  Text
endyear' <- (Text -> Text
fixLeadingDash (Text -> Text)
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> RWST Item () BibState Maybe Text
getRawField (Text
prefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"endyear"))
            RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
""
  Maybe Int
endmonth' <- (Text -> Maybe Int
parseMonth (Text -> Maybe Int)
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> RWST Item () BibState Maybe Text
getRawField (Text
prefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"endmonth"))
            RWST Item () BibState Maybe (Maybe Int)
-> RWST Item () BibState Maybe (Maybe Int)
-> RWST Item () BibState Maybe (Maybe Int)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe Int -> RWST Item () BibState Maybe (Maybe Int)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Int
forall a. Maybe a
Nothing
  Maybe Int
endday' <- (Text -> Maybe Int
forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead (Text -> Maybe Int)
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> RWST Item () BibState Maybe Text
getRawField (Text
prefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"endday"))
             RWST Item () BibState Maybe (Maybe Int)
-> RWST Item () BibState Maybe (Maybe Int)
-> RWST Item () BibState Maybe (Maybe Int)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe Int -> RWST Item () BibState Maybe (Maybe Int)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Int
forall a. Maybe a
Nothing
  let start' :: RefDate
start' = RefDate :: Maybe Int
-> Maybe Int
-> Maybe Season
-> Maybe Int
-> Literal
-> Bool
-> RefDate
RefDate { year :: Maybe Int
year   = Text -> Maybe Int
forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
year'
                       , month :: Maybe Int
month  = Maybe Int
month'
                       , season :: Maybe Season
season = Maybe Season
forall a. Maybe a
Nothing
                       , day :: Maybe Int
day    = Maybe Int
day'
                       , other :: Literal
other  = Text -> Literal
Literal (Text -> Literal) -> Text -> Literal
forall a b. (a -> b) -> a -> b
$ if Text -> Bool
isNumber Text
year' then Text
"" else Text
year'
                       , circa :: Bool
circa  = Bool
False
                       }
  let end' :: RefDate
end' = RefDate :: Maybe Int
-> Maybe Int
-> Maybe Season
-> Maybe Int
-> Literal
-> Bool
-> RefDate
RefDate { year :: Maybe Int
year     = Text -> Maybe Int
forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
endyear'
                       , month :: Maybe Int
month  = Maybe Int
endmonth'
                       , day :: Maybe Int
day    = Maybe Int
endday'
                       , season :: Maybe Season
season = Maybe Season
forall a. Maybe a
Nothing
                       , other :: Literal
other  = Text -> Literal
Literal (Text -> Literal) -> Text -> Literal
forall a b. (a -> b) -> a -> b
$ if Text -> Bool
isNumber Text
endyear' then Text
"" else Text
endyear'
                       , circa :: Bool
circa  = Bool
False
                       }
  let hasyear :: RefDate -> Bool
hasyear RefDate
r = Maybe Int -> Bool
forall a. Maybe a -> Bool
isJust (RefDate -> Maybe Int
year RefDate
r)
  [RefDate] -> Bib [RefDate]
forall (m :: * -> *) a. Monad m => a -> m a
return ([RefDate] -> Bib [RefDate]) -> [RefDate] -> Bib [RefDate]
forall a b. (a -> b) -> a -> b
$ (RefDate -> Bool) -> [RefDate] -> [RefDate]
forall a. (a -> Bool) -> [a] -> [a]
filter RefDate -> Bool
hasyear [RefDate
start', RefDate
end']

getRawField :: Text -> Bib Text
getRawField :: Text -> RWST Item () BibState Maybe Text
getRawField Text
f = do
  Map Text Text
fs <- (Item -> Map Text Text)
-> RWST Item () BibState Maybe (Map Text Text)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Item -> Map Text Text
fields
  case Text -> Map Text Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
f Map Text Text
fs of
       Just Text
x  -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
x
       Maybe Text
Nothing -> Text -> RWST Item () BibState Maybe Text
forall a. Text -> Bib a
notFound Text
f

getAuthorList :: Options -> Text -> Bib [Agent]
getAuthorList :: [(Text, Text)] -> Text -> Bib [Agent]
getAuthorList [(Text, Text)]
opts  Text
f = do
  Map Text Text
fs <- (Item -> Map Text Text)
-> RWST Item () BibState Maybe (Map Text Text)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Item -> Map Text Text
fields
  case Text -> Map Text Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
f Map Text Text
fs of
       Just Text
x  -> [(Text, Text)] -> Text -> Bib [Agent]
latexAuthors [(Text, Text)]
opts Text
x
       Maybe Text
Nothing -> Text -> Bib [Agent]
forall a. Text -> Bib a
notFound Text
f

getLiteralList :: Text -> Bib [Formatted]
getLiteralList :: Text -> Bib [Formatted]
getLiteralList Text
f = do
  Map Text Text
fs <- (Item -> Map Text Text)
-> RWST Item () BibState Maybe (Map Text Text)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Item -> Map Text Text
fields
  case Text -> Map Text Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Text
f Map Text Text
fs of
       Just Text
x  -> [Block] -> Bib [Formatted]
toLiteralList ([Block] -> Bib [Formatted]) -> [Block] -> Bib [Formatted]
forall a b. (a -> b) -> a -> b
$ Text -> [Block]
latex' Text
x
       Maybe Text
Nothing -> Text -> Bib [Formatted]
forall a. Text -> Bib a
notFound Text
f

-- separates items with semicolons
getLiteralList' :: Text -> Bib Formatted
getLiteralList' :: Text -> Bib Formatted
getLiteralList' Text
f = [Inline] -> Formatted
Formatted ([Inline] -> Formatted)
-> ([Formatted] -> [Inline]) -> [Formatted] -> Formatted
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> [[Inline]] -> [Inline]
forall a. [a] -> [[a]] -> [a]
intercalate [Text -> Inline
Str Text
";", Inline
Space] ([[Inline]] -> [Inline])
-> ([Formatted] -> [[Inline]]) -> [Formatted] -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Formatted -> [Inline]) -> [Formatted] -> [[Inline]]
forall a b. (a -> b) -> [a] -> [b]
map Formatted -> [Inline]
unFormatted
  ([Formatted] -> Formatted) -> Bib [Formatted] -> Bib Formatted
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Bib [Formatted]
getLiteralList Text
f

splitByAnd :: [Inline] -> [[Inline]]
splitByAnd :: [Inline] -> [[Inline]]
splitByAnd = [Inline] -> [Inline] -> [[Inline]]
forall a. Eq a => [a] -> [a] -> [[a]]
splitOn [Inline
Space, Text -> Inline
Str Text
"and", Inline
Space]

toLiteralList :: [Block] -> Bib [Formatted]
toLiteralList :: [Block] -> Bib [Formatted]
toLiteralList [Para [Inline]
xs] =
  ([Inline] -> Bib Formatted) -> [[Inline]] -> Bib [Formatted]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM [Inline] -> Bib Formatted
inlinesToFormatted ([[Inline]] -> Bib [Formatted]) -> [[Inline]] -> Bib [Formatted]
forall a b. (a -> b) -> a -> b
$ [Inline] -> [[Inline]]
splitByAnd [Inline]
xs
toLiteralList [Plain [Inline]
xs] = [Block] -> Bib [Formatted]
toLiteralList [[Inline] -> Block
Para [Inline]
xs]
toLiteralList [Block]
_ = Bib [Formatted]
forall (m :: * -> *) a. MonadPlus m => m a
mzero

toAuthorList :: Options -> [Block] -> Bib [Agent]
toAuthorList :: [(Text, Text)] -> [Block] -> Bib [Agent]
toAuthorList [(Text, Text)]
opts [Para [Inline]
xs] =
  (Agent -> Bool) -> [Agent] -> [Agent]
forall a. (a -> Bool) -> [a] -> [a]
filter (Agent -> Agent -> Bool
forall a. Eq a => a -> a -> Bool
/= Agent
emptyAgent) ([Agent] -> [Agent]) -> Bib [Agent] -> Bib [Agent]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Inline] -> RWST Item () BibState Maybe Agent)
-> [[Inline]] -> Bib [Agent]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ([(Text, Text)] -> [Inline] -> RWST Item () BibState Maybe Agent
toAuthor [(Text, Text)]
opts ([Inline] -> RWST Item () BibState Maybe Agent)
-> ([Inline] -> [Inline])
-> [Inline]
-> RWST Item () BibState Maybe Agent
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> [Inline]
addSpaceAfterPeriod)
                                    ([Inline] -> [[Inline]]
splitByAnd [Inline]
xs)
toAuthorList [(Text, Text)]
opts [Plain [Inline]
xs] = [(Text, Text)] -> [Block] -> Bib [Agent]
toAuthorList [(Text, Text)]
opts [[Inline] -> Block
Para [Inline]
xs]
toAuthorList [(Text, Text)]
_ [Block]
_ = Bib [Agent]
forall (m :: * -> *) a. MonadPlus m => m a
mzero

toAuthor :: Options -> [Inline] -> Bib Agent
toAuthor :: [(Text, Text)] -> [Inline] -> RWST Item () BibState Maybe Agent
toAuthor [(Text, Text)]
_ [Str Text
"others"] =
  Agent -> RWST Item () BibState Maybe Agent
forall (m :: * -> *) a. Monad m => a -> m a
return
    Agent :: [Formatted]
-> Formatted
-> Formatted
-> Formatted
-> Formatted
-> Formatted
-> Bool
-> Bool
-> Agent
Agent { givenName :: [Formatted]
givenName       = []
          , droppingPart :: Formatted
droppingPart    = Formatted
forall a. Monoid a => a
mempty
          , nonDroppingPart :: Formatted
nonDroppingPart = Formatted
forall a. Monoid a => a
mempty
          , familyName :: Formatted
familyName      = Formatted
forall a. Monoid a => a
mempty
          , nameSuffix :: Formatted
nameSuffix      = Formatted
forall a. Monoid a => a
mempty
          , literal :: Formatted
literal         = [Inline] -> Formatted
Formatted [Text -> Inline
Str Text
"others"]
          , commaSuffix :: Bool
commaSuffix     = Bool
False
          , parseNames :: Bool
parseNames      = Bool
False
          }
toAuthor [(Text, Text)]
_ [Span (Text
"",[],[]) [Inline]
ils] =
  Agent -> RWST Item () BibState Maybe Agent
forall (m :: * -> *) a. Monad m => a -> m a
return -- corporate author
    Agent :: [Formatted]
-> Formatted
-> Formatted
-> Formatted
-> Formatted
-> Formatted
-> Bool
-> Bool
-> Agent
Agent { givenName :: [Formatted]
givenName       = []
          , droppingPart :: Formatted
droppingPart    = Formatted
forall a. Monoid a => a
mempty
          , nonDroppingPart :: Formatted
nonDroppingPart = Formatted
forall a. Monoid a => a
mempty
          , familyName :: Formatted
familyName      = Formatted
forall a. Monoid a => a
mempty
          , nameSuffix :: Formatted
nameSuffix      = Formatted
forall a. Monoid a => a
mempty
          , literal :: Formatted
literal         = [Inline] -> Formatted
Formatted [Inline]
ils
          , commaSuffix :: Bool
commaSuffix     = Bool
False
          , parseNames :: Bool
parseNames      = Bool
False
          }
 -- extended BibLaTeX name format - see #266
toAuthor [(Text, Text)]
_ ils :: [Inline]
ils@(Str Text
ys:[Inline]
_) | (Char -> Bool) -> Text -> Bool
T.any (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'=') Text
ys = do
  let commaParts :: [[Inline]]
commaParts = (Inline -> Bool) -> [Inline] -> [[Inline]]
forall a. (a -> Bool) -> [a] -> [[a]]
Data.List.Split.splitWhen (Inline -> Inline -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Inline
Str Text
",")
                   ([Inline] -> [[Inline]])
-> ([Inline] -> [Inline]) -> [Inline] -> [[Inline]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> [Inline] -> [Inline]
splitStrWhen (\Char
c -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
',' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'=' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\160')
                   ([Inline] -> [[Inline]]) -> [Inline] -> [[Inline]]
forall a b. (a -> b) -> a -> b
$ [Inline]
ils
  let addPart :: Agent -> [Inline] -> Agent
addPart Agent
ag (Str Text
"given" : Str Text
"=" : [Inline]
xs) =
        Agent
ag{ givenName :: [Formatted]
givenName = Agent -> [Formatted]
givenName Agent
ag [Formatted] -> [Formatted] -> [Formatted]
forall a. [a] -> [a] -> [a]
++ [[Inline] -> Formatted
Formatted [Inline]
xs] }
      addPart Agent
ag (Str Text
"family" : Str Text
"=" : [Inline]
xs) =
        Agent
ag{ familyName :: Formatted
familyName = [Inline] -> Formatted
Formatted [Inline]
xs }
      addPart Agent
ag (Str Text
"prefix" : Str Text
"=" : [Inline]
xs) =
        Agent
ag{ droppingPart :: Formatted
droppingPart = [Inline] -> Formatted
Formatted [Inline]
xs }
      addPart Agent
ag (Str Text
"useprefix" : Str Text
"=" : Str Text
"true" : [Inline]
_) =
        Agent
ag{ nonDroppingPart :: Formatted
nonDroppingPart = Agent -> Formatted
droppingPart Agent
ag, droppingPart :: Formatted
droppingPart = Formatted
forall a. Monoid a => a
mempty }
      addPart Agent
ag (Str Text
"suffix" : Str Text
"=" : [Inline]
xs) =
        Agent
ag{ nameSuffix :: Formatted
nameSuffix = [Inline] -> Formatted
Formatted [Inline]
xs }
      addPart Agent
ag (Inline
Space : [Inline]
xs) = Agent -> [Inline] -> Agent
addPart Agent
ag [Inline]
xs
      addPart Agent
ag [Inline]
_ = Agent
ag
  Agent -> RWST Item () BibState Maybe Agent
forall (m :: * -> *) a. Monad m => a -> m a
return (Agent -> RWST Item () BibState Maybe Agent)
-> Agent -> RWST Item () BibState Maybe Agent
forall a b. (a -> b) -> a -> b
$ (Agent -> [Inline] -> Agent) -> Agent -> [[Inline]] -> Agent
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Agent -> [Inline] -> Agent
addPart Agent
emptyAgent [[Inline]]
commaParts

-- First von Last
-- von Last, First
-- von Last, Jr ,First
-- NOTE: biblatex and bibtex differ on:
-- Drummond de Andrade, Carlos
-- bibtex takes "Drummond de" as the von;
-- biblatex takes the whole as a last name.
-- See https://github.com/plk/biblatex/issues/236
-- Here we implement the more sensible biblatex behavior.
toAuthor [(Text, Text)]
opts [Inline]
ils = do
  let useprefix :: Bool
useprefix = Text -> [(Text, Text)] -> Bool
optionSet Text
"useprefix" [(Text, Text)]
opts
  let usecomma :: Bool
usecomma  = Text -> [(Text, Text)] -> Bool
optionSet Text
"juniorcomma" [(Text, Text)]
opts
  let bibtex :: Bool
bibtex    = Text -> [(Text, Text)] -> Bool
optionSet Text
"bibtex" [(Text, Text)]
opts
  let words' :: [Inline] -> [[Inline]]
words' = (Inline -> Bool) -> [Inline] -> [[Inline]]
forall a. (a -> Bool) -> [a] -> [[a]]
wordsBy (\Inline
x -> Inline
x Inline -> Inline -> Bool
forall a. Eq a => a -> a -> Bool
== Inline
Space Bool -> Bool -> Bool
|| Inline
x Inline -> Inline -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Inline
Str Text
"\160")
  let commaParts :: [[[Inline]]]
commaParts = ([Inline] -> [[Inline]]) -> [[Inline]] -> [[[Inline]]]
forall a b. (a -> b) -> [a] -> [b]
map [Inline] -> [[Inline]]
words' ([[Inline]] -> [[[Inline]]]) -> [[Inline]] -> [[[Inline]]]
forall a b. (a -> b) -> a -> b
$ (Inline -> Bool) -> [Inline] -> [[Inline]]
forall a. (a -> Bool) -> [a] -> [[a]]
Data.List.Split.splitWhen (Inline -> Inline -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Inline
Str Text
",")
                              ([Inline] -> [[Inline]]) -> [Inline] -> [[Inline]]
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> [Inline] -> [Inline]
splitStrWhen (\Char
c -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
',' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\160') [Inline]
ils
  let ([[Inline]]
first, [[Inline]]
vonlast, [[Inline]]
jr) =
          case [[[Inline]]]
commaParts of
               --- First is the longest sequence of white-space separated
               -- words starting with an uppercase and that is not the
               -- whole string. von is the longest sequence of whitespace
               -- separated words whose last word starts with lower case
               -- and that is not the whole string.
               [[[Inline]]
fvl]      -> let ([[Inline]]
caps', [[Inline]]
rest') = ([Inline] -> Bool) -> [[Inline]] -> ([[Inline]], [[Inline]])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span [Inline] -> Bool
isCapitalized [[Inline]]
fvl
                             in  if [[Inline]] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Inline]]
rest' Bool -> Bool -> Bool
&& Bool -> Bool
not ([[Inline]] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Inline]]
caps')
                                 then ([[Inline]] -> [[Inline]]
forall a. [a] -> [a]
init [[Inline]]
caps', [[[Inline]] -> [Inline]
forall a. [a] -> a
last [[Inline]]
caps'], [])
                                 else ([[Inline]]
caps', [[Inline]]
rest', [])
               [[[Inline]]
vl,[[Inline]]
f]     -> ([[Inline]]
f, [[Inline]]
vl, [])
               ([[Inline]]
vl:[[Inline]]
j:[[Inline]]
f:[[[Inline]]]
_) -> ([[Inline]]
f, [[Inline]]
vl, [[Inline]]
j )
               []         -> ([], [], [])

  let ([[Inline]]
von, [[Inline]]
lastname) =
         if Bool
bibtex
            then case ([Inline] -> Bool) -> [[Inline]] -> ([[Inline]], [[Inline]])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span [Inline] -> Bool
isCapitalized ([[Inline]] -> ([[Inline]], [[Inline]]))
-> [[Inline]] -> ([[Inline]], [[Inline]])
forall a b. (a -> b) -> a -> b
$ [[Inline]] -> [[Inline]]
forall a. [a] -> [a]
reverse [[Inline]]
vonlast of
                        ([],[Inline]
w:[[Inline]]
ws) -> ([[Inline]] -> [[Inline]]
forall a. [a] -> [a]
reverse [[Inline]]
ws, [[Inline]
w])
                        ([[Inline]]
vs, [[Inline]]
ws)    -> ([[Inline]] -> [[Inline]]
forall a. [a] -> [a]
reverse [[Inline]]
ws, [[Inline]] -> [[Inline]]
forall a. [a] -> [a]
reverse [[Inline]]
vs)
            else case ([Inline] -> Bool) -> [[Inline]] -> ([[Inline]], [[Inline]])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break [Inline] -> Bool
isCapitalized [[Inline]]
vonlast of
                        (vs :: [[Inline]]
vs@([Inline]
_:[[Inline]]
_), []) -> ([[Inline]] -> [[Inline]]
forall a. [a] -> [a]
init [[Inline]]
vs, [[[Inline]] -> [Inline]
forall a. [a] -> a
last [[Inline]]
vs])
                        ([[Inline]]
vs, [[Inline]]
ws)       -> ([[Inline]]
vs, [[Inline]]
ws)
  let prefix :: Formatted
prefix = [Inline] -> Formatted
Formatted ([Inline] -> Formatted) -> [Inline] -> Formatted
forall a b. (a -> b) -> a -> b
$ [Inline] -> [[Inline]] -> [Inline]
forall a. [a] -> [[a]] -> [a]
intercalate [Inline
Space] [[Inline]]
von
  let family :: Formatted
family = [Inline] -> Formatted
Formatted ([Inline] -> Formatted) -> [Inline] -> Formatted
forall a b. (a -> b) -> a -> b
$ [Inline] -> [[Inline]] -> [Inline]
forall a. [a] -> [[a]] -> [a]
intercalate [Inline
Space] [[Inline]]
lastname
  let suffix :: Formatted
suffix = [Inline] -> Formatted
Formatted ([Inline] -> Formatted) -> [Inline] -> Formatted
forall a b. (a -> b) -> a -> b
$ [Inline] -> [[Inline]] -> [Inline]
forall a. [a] -> [[a]] -> [a]
intercalate [Inline
Space] [[Inline]]
jr
  let givens :: [Formatted]
givens = ([Inline] -> Formatted) -> [[Inline]] -> [Formatted]
forall a b. (a -> b) -> [a] -> [b]
map [Inline] -> Formatted
Formatted [[Inline]]
first

  Agent -> RWST Item () BibState Maybe Agent
forall (m :: * -> *) a. Monad m => a -> m a
return Agent :: [Formatted]
-> Formatted
-> Formatted
-> Formatted
-> Formatted
-> Formatted
-> Bool
-> Bool
-> Agent
Agent
          { givenName :: [Formatted]
givenName       = [Formatted]
givens
          , droppingPart :: Formatted
droppingPart    = if Bool
useprefix then Formatted
forall a. Monoid a => a
mempty else Formatted
prefix
          , nonDroppingPart :: Formatted
nonDroppingPart = if Bool
useprefix then Formatted
prefix else Formatted
forall a. Monoid a => a
mempty
          , familyName :: Formatted
familyName      = Formatted
family
          , nameSuffix :: Formatted
nameSuffix      = Formatted
suffix
          , literal :: Formatted
literal         = Formatted
forall a. Monoid a => a
mempty
          , commaSuffix :: Bool
commaSuffix     = Bool
usecomma
          , parseNames :: Bool
parseNames      = Bool
False
          }

isCapitalized :: [Inline] -> Bool
isCapitalized :: [Inline] -> Bool
isCapitalized (Str (Text -> Maybe (Char, Text)
T.uncons -> Just (Char
c,Text
cs)) : [Inline]
rest)
  | Char -> Bool
isUpper Char
c = Bool
True
  | Char -> Bool
isDigit Char
c = [Inline] -> Bool
isCapitalized (Text -> Inline
Str Text
cs Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline]
rest)
  | Bool
otherwise = Bool
False
isCapitalized (Inline
_:[Inline]
rest) = [Inline] -> Bool
isCapitalized [Inline]
rest
isCapitalized [] = Bool
True

optionSet :: Text -> Options -> Bool
optionSet :: Text -> [(Text, Text)] -> Bool
optionSet Text
key [(Text, Text)]
opts = case Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
key [(Text, Text)]
opts of
                      Just Text
"true" -> Bool
True
                      Just Text
s      -> Text
s Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
forall a. Monoid a => a
mempty
                      Maybe Text
_           -> Bool
False

latex' :: Text -> [Block]
latex' :: Text -> [Block]
latex' Text
s = (Inline -> Inline) -> [Block] -> [Block]
forall a b. Walkable a b => (a -> a) -> b -> b
Walk.walk Inline -> Inline
removeSoftBreak [Block]
bs
  where Pandoc Meta
_ [Block]
bs = Text -> Pandoc
readLaTeX Text
s

removeSoftBreak :: Inline -> Inline
removeSoftBreak :: Inline -> Inline
removeSoftBreak Inline
SoftBreak = Inline
Space
removeSoftBreak Inline
x         = Inline
x

latex :: Text -> Bib Formatted
latex :: Text -> Bib Formatted
latex Text
s = [Block] -> Bib Formatted
blocksToFormatted ([Block] -> Bib Formatted) -> [Block] -> Bib Formatted
forall a b. (a -> b) -> a -> b
$ Text -> [Block]
latex' (Text -> [Block]) -> Text -> [Block]
forall a b. (a -> b) -> a -> b
$ Text -> Text
trim Text
s

latexTitle :: Text -> Bib Formatted
latexTitle :: Text -> Bib Formatted
latexTitle Text
s = do
  Bool
utc <- (BibState -> Bool) -> RWST Item () BibState Maybe Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets BibState -> Bool
untitlecase
  let processTitle :: [Block] -> [Block]
processTitle = if Bool
utc then ([Inline] -> [Inline]) -> [Block] -> [Block]
onBlocks [Inline] -> [Inline]
unTitlecase else [Block] -> [Block]
forall a. a -> a
id
  [Block] -> Bib Formatted
blocksToFormatted ([Block] -> Bib Formatted) -> [Block] -> Bib Formatted
forall a b. (a -> b) -> a -> b
$ [Block] -> [Block]
processTitle ([Block] -> [Block]) -> [Block] -> [Block]
forall a b. (a -> b) -> a -> b
$ Text -> [Block]
latex' Text
s

latexAuthors :: Options -> Text -> Bib [Agent]
latexAuthors :: [(Text, Text)] -> Text -> Bib [Agent]
latexAuthors [(Text, Text)]
opts = [(Text, Text)] -> [Block] -> Bib [Agent]
toAuthorList [(Text, Text)]
opts ([Block] -> Bib [Agent])
-> (Text -> [Block]) -> Text -> Bib [Agent]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Block]
latex' (Text -> [Block]) -> (Text -> Text) -> Text -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
trim

bib :: Bib Reference -> Item -> Maybe Reference
bib :: Bib Reference -> Item -> Maybe Reference
bib Bib Reference
m Item
entry = (Reference, ()) -> Reference
forall a b. (a, b) -> a
fst ((Reference, ()) -> Reference)
-> Maybe (Reference, ()) -> Maybe Reference
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Control.Applicative.<$> Bib Reference -> Item -> BibState -> Maybe (Reference, ())
forall (m :: * -> *) r w s a.
Monad m =>
RWST r w s m a -> r -> s -> m (a, w)
evalRWST Bib Reference
m Item
entry (Bool -> Lang -> BibState
BibState Bool
True (Text -> Text -> Lang
Lang Text
"en" Text
"US"))

toLocale :: Text -> Text
toLocale :: Text -> Text
toLocale Text
"english"         = Text
"en-US" -- "en-EN" unavailable in CSL
toLocale Text
"usenglish"       = Text
"en-US"
toLocale Text
"american"        = Text
"en-US"
toLocale Text
"british"         = Text
"en-GB"
toLocale Text
"ukenglish"       = Text
"en-GB"
toLocale Text
"canadian"        = Text
"en-US" -- "en-CA" unavailable in CSL
toLocale Text
"australian"      = Text
"en-GB" -- "en-AU" unavailable in CSL
toLocale Text
"newzealand"      = Text
"en-GB" -- "en-NZ" unavailable in CSL
toLocale Text
"afrikaans"       = Text
"af-ZA"
toLocale Text
"arabic"          = Text
"ar"
toLocale Text
"basque"          = Text
"eu"
toLocale Text
"bulgarian"       = Text
"bg-BG"
toLocale Text
"catalan"         = Text
"ca-AD"
toLocale Text
"croatian"        = Text
"hr-HR"
toLocale Text
"czech"           = Text
"cs-CZ"
toLocale Text
"danish"          = Text
"da-DK"
toLocale Text
"dutch"           = Text
"nl-NL"
toLocale Text
"estonian"        = Text
"et-EE"
toLocale Text
"finnish"         = Text
"fi-FI"
toLocale Text
"canadien"        = Text
"fr-CA"
toLocale Text
"acadian"         = Text
"fr-CA"
toLocale Text
"french"          = Text
"fr-FR"
toLocale Text
"francais"        = Text
"fr-FR"
toLocale Text
"austrian"        = Text
"de-AT"
toLocale Text
"naustrian"       = Text
"de-AT"
toLocale Text
"german"          = Text
"de-DE"
toLocale Text
"germanb"         = Text
"de-DE"
toLocale Text
"ngerman"         = Text
"de-DE"
toLocale Text
"greek"           = Text
"el-GR"
toLocale Text
"polutonikogreek" = Text
"el-GR"
toLocale Text
"hebrew"          = Text
"he-IL"
toLocale Text
"hungarian"       = Text
"hu-HU"
toLocale Text
"icelandic"       = Text
"is-IS"
toLocale Text
"italian"         = Text
"it-IT"
toLocale Text
"japanese"        = Text
"ja-JP"
toLocale Text
"latvian"         = Text
"lv-LV"
toLocale Text
"lithuanian"      = Text
"lt-LT"
toLocale Text
"magyar"          = Text
"hu-HU"
toLocale Text
"mongolian"       = Text
"mn-MN"
toLocale Text
"norsk"           = Text
"nb-NO"
toLocale Text
"nynorsk"         = Text
"nn-NO"
toLocale Text
"farsi"           = Text
"fa-IR"
toLocale Text
"polish"          = Text
"pl-PL"
toLocale Text
"brazil"          = Text
"pt-BR"
toLocale Text
"brazilian"       = Text
"pt-BR"
toLocale Text
"portugues"       = Text
"pt-PT"
toLocale Text
"portuguese"      = Text
"pt-PT"
toLocale Text
"romanian"        = Text
"ro-RO"
toLocale Text
"russian"         = Text
"ru-RU"
toLocale Text
"serbian"         = Text
"sr-RS"
toLocale Text
"serbianc"        = Text
"sr-RS"
toLocale Text
"slovak"          = Text
"sk-SK"
toLocale Text
"slovene"         = Text
"sl-SL"
toLocale Text
"spanish"         = Text
"es-ES"
toLocale Text
"swedish"         = Text
"sv-SE"
toLocale Text
"thai"            = Text
"th-TH"
toLocale Text
"turkish"         = Text
"tr-TR"
toLocale Text
"ukrainian"       = Text
"uk-UA"
toLocale Text
"vietnamese"      = Text
"vi-VN"
toLocale Text
"latin"           = Text
"la"
toLocale Text
x                 = Text
x

concatWith :: Char -> [Formatted] -> Formatted
concatWith :: Char -> [Formatted] -> Formatted
concatWith Char
sep = [Inline] -> Formatted
Formatted ([Inline] -> Formatted)
-> ([Formatted] -> [Inline]) -> [Formatted] -> Formatted
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Inline] -> [Inline] -> [Inline])
-> [Inline] -> [[Inline]] -> [Inline]
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' [Inline] -> [Inline] -> [Inline]
go [Inline]
forall a. Monoid a => a
mempty ([[Inline]] -> [Inline])
-> ([Formatted] -> [[Inline]]) -> [Formatted] -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Formatted -> [Inline]) -> [Formatted] -> [[Inline]]
forall a b. (a -> b) -> [a] -> [b]
map Formatted -> [Inline]
unFormatted
  where go :: [Inline] -> [Inline] -> [Inline]
        go :: [Inline] -> [Inline] -> [Inline]
go [Inline]
accum [] = [Inline]
accum
        go [Inline]
accum [Inline]
s  = case [Inline] -> [Inline]
forall a. [a] -> [a]
reverse [Inline]
accum of
                           []    -> [Inline]
s
                           (Str Text
x:[Inline]
_)
                             | Bool -> Bool
not (Text -> Bool
T.null Text
x) Bool -> Bool -> Bool
&& Text -> Char
T.last Text
x Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (String
"!?.,:;" :: String)
                                          -> [Inline]
accum [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ (Inline
Space Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline]
s)
                           [Inline]
_     -> [Inline]
accum [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ (Text -> Inline
Str (Char -> Text
T.singleton Char
sep) Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: Inline
Space Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline]
s)

type Options = [(Text, Text)]

parseOptions :: Text -> Options
parseOptions :: Text -> [(Text, Text)]
parseOptions = (Text -> (Text, Text)) -> [Text] -> [(Text, Text)]
forall a b. (a -> b) -> [a] -> [b]
map Text -> (Text, Text)
breakOpt ([Text] -> [(Text, Text)])
-> (Text -> [Text]) -> Text -> [(Text, Text)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> [Text]
T.splitOn Text
","
  where breakOpt :: Text -> (Text, Text)
breakOpt Text
x = case (Char -> Bool) -> Text -> (Text, Text)
T.break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
'=') Text
x of
                          (Text
w,Text
v) -> (Text -> Text
T.toLower (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Text
trim Text
w,
                                    Text -> Text
T.toLower (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Text
trim (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Int -> Text -> Text
T.drop Int
1 Text
v)

ordinalize :: Locale -> Text -> Text
ordinalize :: Locale -> Text -> Text
ordinalize Locale
locale Text
n =
  case [CslTerm -> Text
termSingular CslTerm
c | CslTerm
c <- [CslTerm]
terms, CslTerm -> Text
cslTerm CslTerm
c Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== (Text
"ordinal-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
pad0 Text
n)] [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++
       [CslTerm -> Text
termSingular CslTerm
c | CslTerm
c <- [CslTerm]
terms, CslTerm -> Text
cslTerm CslTerm
c Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"ordinal"] of
       (Text
suff:[Text]
_) -> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
suff
       []       -> Text
n
    where pad0 :: Text -> Text
pad0 Text
s = case Text -> Maybe (Char, Text)
T.uncons Text
s of
            Just (Char
c,Text
"") -> String -> Text
T.pack [Char
'0', Char
c]
            Maybe (Char, Text)
_           -> Text
s
          terms :: [CslTerm]
terms = Locale -> [CslTerm]
localeTerms Locale
locale

itemToReference :: Lang -> Locale -> Bool -> Bool -> Item -> Maybe Reference
itemToReference :: Lang -> Locale -> Bool -> Bool -> Item -> Maybe Reference
itemToReference Lang
lang Locale
locale Bool
bibtex Bool
caseTransform = Bib Reference -> Item -> Maybe Reference
bib (Bib Reference -> Item -> Maybe Reference)
-> Bib Reference -> Item -> Maybe Reference
forall a b. (a -> b) -> a -> b
$ do
  (BibState -> BibState) -> RWST Item () BibState Maybe ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((BibState -> BibState) -> RWST Item () BibState Maybe ())
-> (BibState -> BibState) -> RWST Item () BibState Maybe ()
forall a b. (a -> b) -> a -> b
$ \BibState
st -> BibState
st{ localeLanguage :: Lang
localeLanguage = Lang
lang,
                      untitlecase :: Bool
untitlecase = case Lang
lang of
                                         Lang Text
"en" Text
_ -> Bool
caseTransform
                                         Lang
_           -> Bool
False }
  Text
id' <- (Item -> Text) -> RWST Item () BibState Maybe Text
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Item -> Text
identifier
  [Text]
otherIds <- ((Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
trim ([Text] -> [Text]) -> (Text -> [Text]) -> Text -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> [Text]
T.splitOn Text
"," (Text -> [Text])
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> RWST Item () BibState Maybe Text
getRawField Text
"ids")
                RWST Item () BibState Maybe [Text]
-> RWST Item () BibState Maybe [Text]
-> RWST Item () BibState Maybe [Text]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Text] -> RWST Item () BibState Maybe [Text]
forall (m :: * -> *) a. Monad m => a -> m a
return []
  Text
et <- (Item -> Text) -> RWST Item () BibState Maybe Text
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Item -> Text
entryType
  Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> RWST Item () BibState Maybe ())
-> Bool -> RWST Item () BibState Maybe ()
forall a b. (a -> b) -> a -> b
$ Text
et Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
"xdata"
  [(Text, Text)]
opts <- (Text -> [(Text, Text)]
parseOptions (Text -> [(Text, Text)])
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe [(Text, Text)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> RWST Item () BibState Maybe Text
getRawField Text
"options") RWST Item () BibState Maybe [(Text, Text)]
-> RWST Item () BibState Maybe [(Text, Text)]
-> RWST Item () BibState Maybe [(Text, Text)]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [(Text, Text)] -> RWST Item () BibState Maybe [(Text, Text)]
forall (m :: * -> *) a. Monad m => a -> m a
return []
  let getAuthorList' :: Text -> Bib [Agent]
getAuthorList' = [(Text, Text)] -> Text -> Bib [Agent]
getAuthorList
         ((Text
"bibtex", Text -> Text
T.toLower (Text -> Text) -> (String -> Text) -> String -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Bool -> String
forall a. Show a => a -> String
show Bool
bibtex)(Text, Text) -> [(Text, Text)] -> [(Text, Text)]
forall a. a -> [a] -> [a]
:[(Text, Text)]
opts)
  Text
st <- Text -> RWST Item () BibState Maybe Text
getRawField Text
"entrysubtype" RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
forall a. Monoid a => a
mempty
  Bool
isEvent <- (Bool
True Bool
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Bool
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Text -> RWST Item () BibState Maybe Text
getRawField Text
"eventdate"
                     RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
getRawField Text
"eventtitle"
                     RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
getRawField Text
"venue")) RWST Item () BibState Maybe Bool
-> RWST Item () BibState Maybe Bool
-> RWST Item () BibState Maybe Bool
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Bool -> RWST Item () BibState Maybe Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
  Formatted
reftype' <- Lang -> Formatted -> Formatted
resolveKey Lang
lang (Formatted -> Formatted) -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Bib Formatted
getField Text
"type" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  let (RefType
reftype, Formatted
refgenre) = case Text
et of
       Text
"article"
         | Text
st Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"magazine"  -> (RefType
ArticleMagazine,Formatted
forall a. Monoid a => a
mempty)
         | Text
st Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"newspaper" -> (RefType
ArticleNewspaper,Formatted
forall a. Monoid a => a
mempty)
         | Bool
otherwise         -> (RefType
ArticleJournal,Formatted
forall a. Monoid a => a
mempty)
       Text
"book"            -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       Text
"booklet"         -> (RefType
Pamphlet,Formatted
forall a. Monoid a => a
mempty)
       Text
"bookinbook"      -> (RefType
Chapter,Formatted
forall a. Monoid a => a
mempty)
       Text
"collection"      -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       Text
"dataset"         -> (RefType
Dataset,Formatted
forall a. Monoid a => a
mempty)
       Text
"electronic"      -> (RefType
Webpage,Formatted
forall a. Monoid a => a
mempty)
       Text
"inbook"          -> (RefType
Chapter,Formatted
forall a. Monoid a => a
mempty)
       Text
"incollection"    -> (RefType
Chapter,Formatted
forall a. Monoid a => a
mempty)
       Text
"inreference"     -> (RefType
EntryEncyclopedia,Formatted
forall a. Monoid a => a
mempty)
       Text
"inproceedings"   -> (RefType
PaperConference,Formatted
forall a. Monoid a => a
mempty)
       Text
"manual"          -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       Text
"mastersthesis"   -> (RefType
Thesis, if Formatted
reftype' Formatted -> Formatted -> Bool
forall a. Eq a => a -> a -> Bool
== Formatted
forall a. Monoid a => a
mempty
                                        then [Inline] -> Formatted
Formatted [Text -> Inline
Str (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ Lang -> Text -> Text
resolveKey' Lang
lang Text
"mathesis"]
                                        else Formatted
reftype')
       Text
"misc"            -> (RefType
NoType,Formatted
forall a. Monoid a => a
mempty)
       Text
"mvbook"          -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       Text
"mvcollection"    -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       Text
"mvproceedings"   -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       Text
"mvreference"     -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       Text
"online"          -> (RefType
Webpage,Formatted
forall a. Monoid a => a
mempty)
       Text
"patent"          -> (RefType
Patent,Formatted
forall a. Monoid a => a
mempty)
       Text
"periodical"
         | Text
st Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"magazine"  -> (RefType
ArticleMagazine,Formatted
forall a. Monoid a => a
mempty)
         | Text
st Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"newspaper" -> (RefType
ArticleNewspaper,Formatted
forall a. Monoid a => a
mempty)
         | Bool
otherwise         -> (RefType
ArticleJournal,Formatted
forall a. Monoid a => a
mempty)
       Text
"phdthesis"       -> (RefType
Thesis, if Formatted
reftype' Formatted -> Formatted -> Bool
forall a. Eq a => a -> a -> Bool
== Formatted
forall a. Monoid a => a
mempty
                                        then [Inline] -> Formatted
Formatted [Text -> Inline
Str (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ Lang -> Text -> Text
resolveKey' Lang
lang Text
"phdthesis"]
                                        else Formatted
reftype')
       Text
"proceedings"     -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       Text
"reference"       -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       Text
"report"          -> (RefType
Report,Formatted
forall a. Monoid a => a
mempty)
       Text
"software"        -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)         -- no "software" type in CSL
       Text
"suppbook"        -> (RefType
Chapter,Formatted
forall a. Monoid a => a
mempty)
       Text
"suppcollection"  -> (RefType
Chapter,Formatted
forall a. Monoid a => a
mempty)
       Text
"suppperiodical"
         | Text
st Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"magazine"  -> (RefType
ArticleMagazine,Formatted
forall a. Monoid a => a
mempty)
         | Text
st Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"newspaper" -> (RefType
ArticleNewspaper,Formatted
forall a. Monoid a => a
mempty)
         | Bool
otherwise         -> (RefType
ArticleJournal,Formatted
forall a. Monoid a => a
mempty)
       Text
"techreport"      -> (RefType
Report,Formatted
forall a. Monoid a => a
mempty)
       Text
"thesis"          -> (RefType
Thesis,Formatted
forall a. Monoid a => a
mempty)
       Text
"unpublished"     -> (if Bool
isEvent then RefType
Speech else RefType
Manuscript,Formatted
forall a. Monoid a => a
mempty)
       Text
"www"             -> (RefType
Webpage,Formatted
forall a. Monoid a => a
mempty)
       -- biblatex, "unsupported"
       Text
"artwork"         -> (RefType
Graphic,Formatted
forall a. Monoid a => a
mempty)
       Text
"audio"           -> (RefType
Song,Formatted
forall a. Monoid a => a
mempty)         -- for audio *recordings*
       Text
"commentary"      -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       Text
"image"           -> (RefType
Graphic,Formatted
forall a. Monoid a => a
mempty)      -- or "figure" ?
       Text
"jurisdiction"    -> (RefType
LegalCase,Formatted
forall a. Monoid a => a
mempty)
       Text
"legislation"     -> (RefType
Legislation,Formatted
forall a. Monoid a => a
mempty)  -- or "bill" ?
       Text
"legal"           -> (RefType
Treaty,Formatted
forall a. Monoid a => a
mempty)
       Text
"letter"          -> (RefType
PersonalCommunication,Formatted
forall a. Monoid a => a
mempty)
       Text
"movie"           -> (RefType
MotionPicture,Formatted
forall a. Monoid a => a
mempty)
       Text
"music"           -> (RefType
Song,Formatted
forall a. Monoid a => a
mempty)         -- for musical *recordings*
       Text
"performance"     -> (RefType
Speech,Formatted
forall a. Monoid a => a
mempty)
       Text
"review"          -> (RefType
Review,Formatted
forall a. Monoid a => a
mempty)       -- or "review-book" ?
       Text
"standard"        -> (RefType
Legislation,Formatted
forall a. Monoid a => a
mempty)
       Text
"video"           -> (RefType
MotionPicture,Formatted
forall a. Monoid a => a
mempty)
       -- biblatex-apa:
       Text
"data"            -> (RefType
Dataset,Formatted
forall a. Monoid a => a
mempty)
       Text
"letters"         -> (RefType
PersonalCommunication,Formatted
forall a. Monoid a => a
mempty)
       Text
"newsarticle"     -> (RefType
ArticleNewspaper,Formatted
forall a. Monoid a => a
mempty)
       Text
_                 -> (RefType
NoType,Formatted
forall a. Monoid a => a
mempty)

  -- hyphenation:
  let defaultHyphenation :: Text
defaultHyphenation = case Lang
lang of
                                Lang Text
x Text
y -> Text
x Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
y
  let getLangId :: RWST Item () BibState Maybe Text
getLangId = do
           Text
langid <- Text -> Text
trim (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
T.toLower (Text -> Text)
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> RWST Item () BibState Maybe Text
getRawField Text
"langid"
           Text
idopts <- Text -> Text
trim (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
T.toLower (Text -> Text)
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                         Text -> RWST Item () BibState Maybe Text
getRawField Text
"langidopts" RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
""
           case (Text
langid, Text
idopts) of
                (Text
"english",Text
"variant=british")    -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
"british"
                (Text
"english",Text
"variant=american")   -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
"american"
                (Text
"english",Text
"variant=us")         -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
"american"
                (Text
"english",Text
"variant=usmax")      -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
"american"
                (Text
"english",Text
"variant=uk")         -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
"british"
                (Text
"english",Text
"variant=australian") -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
"australian"
                (Text
"english",Text
"variant=newzealand") -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
"newzealand"
                (Text
x,Text
_)                            -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
x
  Text
hyphenation <- (Text -> Text
toLocale (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
T.toLower (Text -> Text)
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                   (RWST Item () BibState Maybe Text
getLangId RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
getRawField Text
"hyphenation"))
                RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
forall a. Monoid a => a
mempty

  -- authors:
  [Agent]
author' <- Text -> Bib [Agent]
getAuthorList' Text
"author" Bib [Agent] -> Bib [Agent] -> Bib [Agent]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Agent] -> Bib [Agent]
forall (m :: * -> *) a. Monad m => a -> m a
return []
  [Agent]
containerAuthor' <- Text -> Bib [Agent]
getAuthorList' Text
"bookauthor" Bib [Agent] -> Bib [Agent] -> Bib [Agent]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Agent] -> Bib [Agent]
forall (m :: * -> *) a. Monad m => a -> m a
return []
  [Agent]
translator' <- Text -> Bib [Agent]
getAuthorList' Text
"translator" Bib [Agent] -> Bib [Agent] -> Bib [Agent]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Agent] -> Bib [Agent]
forall (m :: * -> *) a. Monad m => a -> m a
return []
  Text
editortype <- Text -> RWST Item () BibState Maybe Text
getRawField Text
"editortype" RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
forall a. Monoid a => a
mempty
  [Agent]
editor'' <- Text -> Bib [Agent]
getAuthorList' Text
"editor" Bib [Agent] -> Bib [Agent] -> Bib [Agent]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Agent] -> Bib [Agent]
forall (m :: * -> *) a. Monad m => a -> m a
return []
  [Agent]
director'' <- Text -> Bib [Agent]
getAuthorList' Text
"director" Bib [Agent] -> Bib [Agent] -> Bib [Agent]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Agent] -> Bib [Agent]
forall (m :: * -> *) a. Monad m => a -> m a
return []
  let ([Agent]
editor', [Agent]
director') = case Text
editortype of
                                  Text
"director" -> ([], [Agent]
editor'')
                                  Text
_          -> ([Agent]
editor'', [Agent]
director'')
  -- FIXME: add same for editora, editorb, editorc

  -- titles
  let isArticle :: Bool
isArticle = Text
et Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"article", Text
"periodical", Text
"suppperiodical", Text
"review"]
  let isPeriodical :: Bool
isPeriodical = Text
et Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"periodical"
  let isChapterlike :: Bool
isChapterlike = Text
et Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem`
         [Text
"inbook",Text
"incollection",Text
"inproceedings",Text
"inreference",Text
"bookinbook"]
  Bool
hasMaintitle <- (Bool
True Bool
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Bool
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Text -> RWST Item () BibState Maybe Text
getRawField Text
"maintitle") RWST Item () BibState Maybe Bool
-> RWST Item () BibState Maybe Bool
-> RWST Item () BibState Maybe Bool
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Bool -> RWST Item () BibState Maybe Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
  let hyphenation' :: Text
hyphenation' = if Text -> Bool
T.null Text
hyphenation
                     then Text
defaultHyphenation
                     else Text
hyphenation
  let la :: Text
la = case Text -> Text -> [Text]
T.splitOn Text
"-" Text
hyphenation' of
                      (Text
x:[Text]
_) -> Text
x
                      []    -> Text
forall a. Monoid a => a
mempty
  (BibState -> BibState) -> RWST Item () BibState Maybe ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((BibState -> BibState) -> RWST Item () BibState Maybe ())
-> (BibState -> BibState) -> RWST Item () BibState Maybe ()
forall a b. (a -> b) -> a -> b
$ \BibState
s -> BibState
s{ untitlecase :: Bool
untitlecase = Bool
caseTransform Bool -> Bool -> Bool
&& Text
la Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"en" }

  Formatted
title' <- (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
isPeriodical RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"issuetitle")
            Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
hasMaintitle RWST Item () BibState Maybe ()
-> RWST Item () BibState Maybe () -> RWST Item () BibState Maybe ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not Bool
isChapterlike) RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"maintitle")
            Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getTitle Text
"title"
            Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
subtitle' <- (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
isPeriodical RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"issuesubtitle")
               Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
hasMaintitle RWST Item () BibState Maybe ()
-> RWST Item () BibState Maybe () -> RWST Item () BibState Maybe ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not Bool
isChapterlike) RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"mainsubtitle")
               Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getTitle Text
"subtitle"
               Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
titleaddon' <- (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
hasMaintitle RWST Item () BibState Maybe ()
-> RWST Item () BibState Maybe () -> RWST Item () BibState Maybe ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not Bool
isChapterlike) RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"maintitleaddon")
                 Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getTitle Text
"titleaddon"
                 Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
volumeTitle' <- (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
hasMaintitle RWST Item () BibState Maybe ()
-> RWST Item () BibState Maybe () -> RWST Item () BibState Maybe ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not Bool
isChapterlike) RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"title")
                  Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
hasMaintitle RWST Item () BibState Maybe ()
-> RWST Item () BibState Maybe () -> RWST Item () BibState Maybe ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
isChapterlike RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"booktitle")
                  Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
volumeSubtitle' <- (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
hasMaintitle RWST Item () BibState Maybe ()
-> RWST Item () BibState Maybe () -> RWST Item () BibState Maybe ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not Bool
isChapterlike) RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"subtitle")
                     Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
hasMaintitle RWST Item () BibState Maybe ()
-> RWST Item () BibState Maybe () -> RWST Item () BibState Maybe ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
isChapterlike RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"booksubtitle")
                     Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
volumeTitleAddon' <- (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
hasMaintitle RWST Item () BibState Maybe ()
-> RWST Item () BibState Maybe () -> RWST Item () BibState Maybe ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not Bool
isChapterlike) RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"titleaddon")
                       Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
hasMaintitle RWST Item () BibState Maybe ()
-> RWST Item () BibState Maybe () -> RWST Item () BibState Maybe ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
isChapterlike RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"booktitleaddon")
                       Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
containerTitle' <- (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
isPeriodical RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getPeriodicalTitle Text
"title")
                     Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
isChapterlike RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"maintitle")
                     Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
isChapterlike RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"booktitle")
                     Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getPeriodicalTitle Text
"journaltitle"
                     Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getPeriodicalTitle Text
"journal"
                     Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
containerSubtitle' <- (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
isPeriodical RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getPeriodicalTitle Text
"subtitle")
                        Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
isChapterlike RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"mainsubtitle")
                        Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
isChapterlike RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"booksubtitle")
                        Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getPeriodicalTitle Text
"journalsubtitle"
                        Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
containerTitleAddon' <- (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
isPeriodical RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getPeriodicalTitle Text
"titleaddon")
                          Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
isChapterlike RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"maintitleaddon")
                          Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
isChapterlike RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getTitle Text
"booktitleaddon")
                          Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
containerTitleShort' <- (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
isPeriodical RWST Item () BibState Maybe ()
-> RWST Item () BibState Maybe () -> RWST Item () BibState Maybe ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not Bool
hasMaintitle)
                       RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> Bib Formatted
getField Text
"shorttitle")
                        Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getPeriodicalTitle Text
"shortjournal"
                        Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  -- change numerical series title to e.g. 'series 3'
  let fixSeriesTitle :: Formatted -> Formatted
fixSeriesTitle (Formatted [Str Text
xs]) | (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isDigit Text
xs =
         [Inline] -> Formatted
Formatted [Text -> Inline
Str (Locale -> Text -> Text
ordinalize Locale
locale Text
xs),
                    Inline
Space, Text -> Inline
Str (Lang -> Text -> Text
resolveKey' Lang
lang Text
"ser.")]
      fixSeriesTitle Formatted
x = Formatted
x
  Formatted
seriesTitle' <- Formatted -> Formatted
fixSeriesTitle (Formatted -> Formatted)
-> (Formatted -> Formatted) -> Formatted -> Formatted
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Lang -> Formatted -> Formatted
resolveKey Lang
lang (Formatted -> Formatted) -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                      Text -> Bib Formatted
getTitle Text
"series" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
shortTitle' <- (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not Bool
hasMaintitle Bool -> Bool -> Bool
|| Bool
isChapterlike) RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                        Text -> Bib Formatted
getTitle Text
"shorttitle")
               Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> if (Formatted
subtitle' Formatted -> Formatted -> Bool
forall a. Eq a => a -> a -> Bool
/= Formatted
forall a. Monoid a => a
mempty Bool -> Bool -> Bool
|| Formatted
titleaddon' Formatted -> Formatted -> Bool
forall a. Eq a => a -> a -> Bool
/= Formatted
forall a. Monoid a => a
mempty) Bool -> Bool -> Bool
&&
                      Bool -> Bool
not Bool
hasMaintitle
                      then Bool -> Text -> Bib Formatted
getShortTitle Bool
False Text
"title"
                      else Bool -> Text -> Bib Formatted
getShortTitle Bool
True  Text
"title"
               Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty

  Formatted
eventTitle' <- Text -> Bib Formatted
getTitle Text
"eventtitle" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
origTitle' <- Text -> Bib Formatted
getTitle Text
"origtitle" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty

  -- publisher
  [Maybe Formatted]
pubfields <- (Text -> RWST Item () BibState Maybe (Maybe Formatted))
-> [Text] -> RWST Item () BibState Maybe [Maybe Formatted]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\Text
f -> Formatted -> Maybe Formatted
forall a. a -> Maybe a
Just (Formatted -> Maybe Formatted)
-> Bib Formatted -> RWST Item () BibState Maybe (Maybe Formatted)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap`
                       (if Bool
bibtex Bool -> Bool -> Bool
|| Text
f Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"howpublished"
                        then Text -> Bib Formatted
getField Text
f
                        else Text -> Bib Formatted
getLiteralList' Text
f)
                      RWST Item () BibState Maybe (Maybe Formatted)
-> RWST Item () BibState Maybe (Maybe Formatted)
-> RWST Item () BibState Maybe (Maybe Formatted)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe Formatted -> RWST Item () BibState Maybe (Maybe Formatted)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Formatted
forall a. Maybe a
Nothing)
         [Text
"school",Text
"institution",Text
"organization", Text
"howpublished",Text
"publisher"]
  let publisher' :: Formatted
publisher' = Char -> [Formatted] -> Formatted
concatWith Char
';' ([Formatted] -> Formatted) -> [Formatted] -> Formatted
forall a b. (a -> b) -> a -> b
$ [Maybe Formatted] -> [Formatted]
forall a. [Maybe a] -> [a]
catMaybes [Maybe Formatted]
pubfields
  Formatted
origpublisher' <- Text -> Bib Formatted
getField Text
"origpublisher" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty

-- places
  Formatted
venue' <- Text -> Bib Formatted
getField Text
"venue" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
address' <- (if Bool
bibtex
               then Text -> Bib Formatted
getField Text
"address"
               else Text -> Bib Formatted
getLiteralList' Text
"address"
                     Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Text
et Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
"patent") RWST Item () BibState Maybe () -> Bib Formatted -> Bib Formatted
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                          Text -> Bib Formatted
getLiteralList' Text
"location"))
              Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
origLocation' <- (if Bool
bibtex
                    then Text -> Bib Formatted
getField Text
"origlocation"
                    else Text -> Bib Formatted
getLiteralList' Text
"origlocation")
                  Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
jurisdiction' <- if Text
et Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"patent"
                   then (Char -> [Formatted] -> Formatted
concatWith Char
';' ([Formatted] -> Formatted)
-> ([Formatted] -> [Formatted]) -> [Formatted] -> Formatted
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Formatted -> Formatted) -> [Formatted] -> [Formatted]
forall a b. (a -> b) -> [a] -> [b]
map (Lang -> Formatted -> Formatted
resolveKey Lang
lang) ([Formatted] -> Formatted) -> Bib [Formatted] -> Bib Formatted
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                           Text -> Bib [Formatted]
getLiteralList Text
"location") Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
                   else Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty

  -- locators
  Formatted
pages' <- Text -> Bib Formatted
getField Text
"pages" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
volume' <- Text -> Bib Formatted
getField Text
"volume" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
part' <- Text -> Bib Formatted
getField Text
"part" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
volumes' <- Text -> Bib Formatted
getField Text
"volumes" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
pagetotal' <- Text -> Bib Formatted
getField Text
"pagetotal" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
chapter' <- Text -> Bib Formatted
getField Text
"chapter" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
edition' <- Text -> Bib Formatted
getField Text
"edition" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
version' <- Text -> Bib Formatted
getField Text
"version" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  (Formatted
number', Formatted
collectionNumber', Formatted
issue') <-
     (Text -> Bib Formatted
getField Text
"number" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty) Bib Formatted
-> (Formatted
    -> RWST Item () BibState Maybe (Formatted, Formatted, Formatted))
-> RWST Item () BibState Maybe (Formatted, Formatted, Formatted)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Formatted
x ->
       if Text
et Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
"book",Text
"collection",Text
"proceedings",Text
"reference",
                     Text
"mvbook",Text
"mvcollection",Text
"mvproceedings", Text
"mvreference",
                     Text
"bookinbook",Text
"inbook", Text
"incollection",Text
"inproceedings",
                     Text
"inreference", Text
"suppbook",Text
"suppcollection"]
       then (Formatted, Formatted, Formatted)
-> RWST Item () BibState Maybe (Formatted, Formatted, Formatted)
forall (m :: * -> *) a. Monad m => a -> m a
return (Formatted
forall a. Monoid a => a
mempty,Formatted
x,Formatted
forall a. Monoid a => a
mempty)
       else if Bool
isArticle
            then (Text -> Bib Formatted
getField Text
"issue" Bib Formatted
-> (Formatted
    -> RWST Item () BibState Maybe (Formatted, Formatted, Formatted))
-> RWST Item () BibState Maybe (Formatted, Formatted, Formatted)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Formatted
y ->
                                    (Formatted, Formatted, Formatted)
-> RWST Item () BibState Maybe (Formatted, Formatted, Formatted)
forall (m :: * -> *) a. Monad m => a -> m a
return (Formatted
forall a. Monoid a => a
mempty,Formatted
forall a. Monoid a => a
mempty,Char -> [Formatted] -> Formatted
concatWith Char
',' [Formatted
x,Formatted
y]))
               RWST Item () BibState Maybe (Formatted, Formatted, Formatted)
-> RWST Item () BibState Maybe (Formatted, Formatted, Formatted)
-> RWST Item () BibState Maybe (Formatted, Formatted, Formatted)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Formatted, Formatted, Formatted)
-> RWST Item () BibState Maybe (Formatted, Formatted, Formatted)
forall (m :: * -> *) a. Monad m => a -> m a
return (Formatted
forall a. Monoid a => a
mempty,Formatted
forall a. Monoid a => a
mempty,Formatted
x)
            else (Formatted, Formatted, Formatted)
-> RWST Item () BibState Maybe (Formatted, Formatted, Formatted)
forall (m :: * -> *) a. Monad m => a -> m a
return (Formatted
x,Formatted
forall a. Monoid a => a
mempty,Formatted
forall a. Monoid a => a
mempty)

  -- dates
  [RefDate]
issued' <- Text -> Bib [RefDate]
getDates Text
"date" Bib [RefDate] -> Bib [RefDate] -> Bib [RefDate]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib [RefDate]
getOldDates Text
forall a. Monoid a => a
mempty Bib [RefDate] -> Bib [RefDate] -> Bib [RefDate]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [RefDate] -> Bib [RefDate]
forall (m :: * -> *) a. Monad m => a -> m a
return []
  [RefDate]
eventDate' <- Text -> Bib [RefDate]
getDates Text
"eventdate" Bib [RefDate] -> Bib [RefDate] -> Bib [RefDate]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib [RefDate]
getOldDates Text
"event"
              Bib [RefDate] -> Bib [RefDate] -> Bib [RefDate]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [RefDate] -> Bib [RefDate]
forall (m :: * -> *) a. Monad m => a -> m a
return []
  [RefDate]
origDate' <- Text -> Bib [RefDate]
getDates Text
"origdate" Bib [RefDate] -> Bib [RefDate] -> Bib [RefDate]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib [RefDate]
getOldDates Text
"orig"
              Bib [RefDate] -> Bib [RefDate] -> Bib [RefDate]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [RefDate] -> Bib [RefDate]
forall (m :: * -> *) a. Monad m => a -> m a
return []
  [RefDate]
accessed' <- Text -> Bib [RefDate]
getDates Text
"urldate" Bib [RefDate] -> Bib [RefDate] -> Bib [RefDate]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib [RefDate]
getOldDates Text
"url" Bib [RefDate] -> Bib [RefDate] -> Bib [RefDate]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [RefDate] -> Bib [RefDate]
forall (m :: * -> *) a. Monad m => a -> m a
return []

  -- url, doi, isbn, etc.:
  -- note that with eprinttype = arxiv, we take eprint to be a partial url
  -- archivePrefix is an alias for eprinttype
  Text
url' <- (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Text
et Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"online" Bool -> Bool -> Bool
|| Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"url" [(Text, Text)]
opts Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"false")
           RWST Item () BibState Maybe ()
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> RWST Item () BibState Maybe Text
getRawField Text
"url")
       RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (do Text
etype <- Text -> RWST Item () BibState Maybe Text
getRawField Text
"eprinttype"
               Text
eprint <- Text -> RWST Item () BibState Maybe Text
getRawField Text
"eprint"
               let baseUrl :: Text
baseUrl =
                     case Text -> Text
T.toLower Text
etype of
                       Text
"arxiv"       -> Text
"http://arxiv.org/abs/"
                       Text
"jstor"       -> Text
"http://www.jstor.org/stable/"
                       Text
"pubmed"      -> Text
"http://www.ncbi.nlm.nih.gov/pubmed/"
                       Text
"googlebooks" -> Text
"http://books.google.com?id="
                       Text
_             -> Text
""
               if Text -> Bool
T.null Text
baseUrl
                  then RWST Item () BibState Maybe Text
forall (m :: * -> *) a. MonadPlus m => m a
mzero
                  else Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> RWST Item () BibState Maybe Text)
-> Text -> RWST Item () BibState Maybe Text
forall a b. (a -> b) -> a -> b
$ Text
baseUrl Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
eprint)
       RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
forall a. Monoid a => a
mempty
  Text
doi' <- (Bool -> RWST Item () BibState Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"doi" [(Text, Text)]
opts Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"false") RWST Item () BibState Maybe ()
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Text -> RWST Item () BibState Maybe Text
getRawField Text
"doi")
         RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
forall a. Monoid a => a
mempty
  Text
isbn' <- Text -> RWST Item () BibState Maybe Text
getRawField Text
"isbn" RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
forall a. Monoid a => a
mempty
  Text
issn' <- Text -> RWST Item () BibState Maybe Text
getRawField Text
"issn" RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
forall a. Monoid a => a
mempty
  Text
pmid' <- Text -> RWST Item () BibState Maybe Text
getRawField Text
"pmid" RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
forall a. Monoid a => a
mempty
  Text
pmcid' <- Text -> RWST Item () BibState Maybe Text
getRawField Text
"pmcid" RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
forall a. Monoid a => a
mempty
  Text
callNumber' <- Text -> RWST Item () BibState Maybe Text
getRawField Text
"library" RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
-> RWST Item () BibState Maybe Text
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
forall a. Monoid a => a
mempty

  -- notes
  Formatted
annotation' <- Text -> Bib Formatted
getField Text
"annotation" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getField Text
"annote"
                   Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
abstract' <- Text -> Bib Formatted
getField Text
"abstract" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
keywords' <- Text -> Bib Formatted
getField Text
"keywords" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
note' <- if Text
et Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"periodical"
           then Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
           else Text -> Bib Formatted
getField Text
"note" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
addendum' <- if Bool
bibtex
               then Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
               else Text -> Bib Formatted
getField Text
"addendum"
                 Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
  Formatted
pubstate' <- Lang -> Formatted -> Formatted
resolveKey Lang
lang (Formatted -> Formatted) -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap`
                 (  Text -> Bib Formatted
getField Text
"pubstate"
                Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> case [RefDate]
issued' of
                         (RefDate
x:[RefDate]
_) | RefDate -> Literal
other RefDate
x Literal -> Literal -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Literal
Literal Text
"forthcoming" ->
                                     Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return ([Inline] -> Formatted
Formatted [Text -> Inline
Str Text
"forthcoming"])
                         [RefDate]
_ -> Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return Formatted
forall a. Monoid a => a
mempty
                 )

  let convertEnDash :: Inline -> Inline
convertEnDash (Str Text
s) = Text -> Inline
Str ((Char -> Char) -> Text -> Text
T.map (\Char
c -> if Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'–' then Char
'-' else Char
c) Text
s)
      convertEnDash Inline
x       = Inline
x

  let takeDigits :: [Inline] -> [Inline]
takeDigits (Str Text
xs : [Inline]
_) =
         case (Char -> Bool) -> Text -> Text
T.takeWhile Char -> Bool
isDigit Text
xs of
              Text
"" -> []
              Text
ds -> [Text -> Inline
Str Text
ds]
      takeDigits [Inline]
x             = [Inline]
x

  Reference -> Bib Reference
forall (m :: * -> *) a. Monad m => a -> m a
return (Reference -> Bib Reference) -> Reference -> Bib Reference
forall a b. (a -> b) -> a -> b
$ Reference
emptyReference
         { refId :: Literal
refId               = Text -> Literal
Literal Text
id'
         , refOtherIds :: [Literal]
refOtherIds         = (Text -> Literal) -> [Text] -> [Literal]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Literal
Literal [Text]
otherIds
         , refType :: RefType
refType             = RefType
reftype
         , author :: [Agent]
author              = [Agent]
author'
         , editor :: [Agent]
editor              = [Agent]
editor'
         , translator :: [Agent]
translator          = [Agent]
translator'
         -- , recipient           = undefined -- :: [Agent]
         -- , interviewer         = undefined -- :: [Agent]
         -- , composer            = undefined -- :: [Agent]
         , director :: [Agent]
director            = [Agent]
director'
         -- , illustrator         = undefined -- :: [Agent]
         -- , originalAuthor      = undefined -- :: [Agent]
         , containerAuthor :: [Agent]
containerAuthor     = [Agent]
containerAuthor'
         -- , collectionEditor    = undefined -- :: [Agent]
         -- , editorialDirector   = undefined -- :: [Agent]
         -- , reviewedAuthor      = undefined -- :: [Agent]

         , issued :: [RefDate]
issued              = [RefDate]
issued'
         , eventDate :: [RefDate]
eventDate           = [RefDate]
eventDate'
         , accessed :: [RefDate]
accessed            = [RefDate]
accessed'
         -- , container           = undefined -- :: [RefDate]
         , originalDate :: [RefDate]
originalDate        = [RefDate]
origDate'
         -- , submitted           = undefined -- :: [RefDate]
         , title :: Formatted
title               = Char -> [Formatted] -> Formatted
concatWith Char
'.' [
                                    Char -> [Formatted] -> Formatted
concatWith Char
':' [Formatted
title', Formatted
subtitle']
                                  , Formatted
titleaddon' ]
         , titleShort :: Formatted
titleShort          = Formatted
shortTitle'
         -- , reviewedTitle       = undefined -- :: String
         , containerTitle :: Formatted
containerTitle      = Char -> [Formatted] -> Formatted
concatWith Char
'.' [
                                      Char -> [Formatted] -> Formatted
concatWith Char
':' [ Formatted
containerTitle'
                                                     , Formatted
containerSubtitle']
                                    , Formatted
containerTitleAddon' ]
         , collectionTitle :: Formatted
collectionTitle     = Formatted
seriesTitle'
         , volumeTitle :: Formatted
volumeTitle         = Char -> [Formatted] -> Formatted
concatWith Char
'.' [
                                      Char -> [Formatted] -> Formatted
concatWith Char
':' [ Formatted
volumeTitle'
                                                     , Formatted
volumeSubtitle']
                                    , Formatted
volumeTitleAddon' ]
         , containerTitleShort :: Formatted
containerTitleShort = Formatted
containerTitleShort'
         , collectionNumber :: Formatted
collectionNumber    = Formatted
collectionNumber'
         , originalTitle :: Formatted
originalTitle       = Formatted
origTitle'
         , publisher :: Formatted
publisher           = Formatted
publisher'
         , originalPublisher :: Formatted
originalPublisher   = Formatted
origpublisher'
         , publisherPlace :: Formatted
publisherPlace      = Formatted
address'
         , originalPublisherPlace :: Formatted
originalPublisherPlace = Formatted
origLocation'
         , jurisdiction :: Formatted
jurisdiction        = Formatted
jurisdiction'
         , event :: Formatted
event               = Formatted
eventTitle'
         , eventPlace :: Formatted
eventPlace          = Formatted
venue'
         , page :: Formatted
page                = [Inline] -> Formatted
Formatted ([Inline] -> Formatted) -> [Inline] -> Formatted
forall a b. (a -> b) -> a -> b
$
                                 (Inline -> Inline) -> [Inline] -> [Inline]
forall a b. Walkable a b => (a -> a) -> b -> b
Walk.walk Inline -> Inline
convertEnDash ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall a b. (a -> b) -> a -> b
$ Formatted -> [Inline]
unFormatted Formatted
pages'
         , pageFirst :: Formatted
pageFirst           = [Inline] -> Formatted
Formatted ([Inline] -> Formatted) -> [Inline] -> Formatted
forall a b. (a -> b) -> a -> b
$ [Inline] -> [Inline]
takeDigits ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall a b. (a -> b) -> a -> b
$ Formatted -> [Inline]
unFormatted Formatted
pages'
         , numberOfPages :: Formatted
numberOfPages       = Formatted
pagetotal'
         , version :: Formatted
version             = Formatted
version'
         , volume :: Formatted
volume              = [Inline] -> Formatted
Formatted ([Inline] -> Formatted) -> [Inline] -> Formatted
forall a b. (a -> b) -> a -> b
$ [Inline] -> [[Inline]] -> [Inline]
forall a. [a] -> [[a]] -> [a]
intercalate [Text -> Inline
Str Text
"."]
                                 ([[Inline]] -> [Inline]) -> [[Inline]] -> [Inline]
forall a b. (a -> b) -> a -> b
$ ([Inline] -> Bool) -> [[Inline]] -> [[Inline]]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ([Inline] -> Bool) -> [Inline] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null)
                                 [Formatted -> [Inline]
unFormatted Formatted
volume', Formatted -> [Inline]
unFormatted Formatted
part']
         , numberOfVolumes :: Formatted
numberOfVolumes     = Formatted
volumes'
         , issue :: Formatted
issue               = Formatted
issue'
         , chapterNumber :: Formatted
chapterNumber       = Formatted
chapter'
         -- , medium              = undefined -- :: String
         , status :: Formatted
status              = Formatted
pubstate'
         , edition :: Formatted
edition             = Formatted
edition'
         -- , section             = undefined -- :: String
         -- , source              = undefined -- :: String
         , genre :: Formatted
genre               = if Formatted
refgenre Formatted -> Formatted -> Bool
forall a. Eq a => a -> a -> Bool
== Formatted
forall a. Monoid a => a
mempty
                                    then Formatted
reftype'
                                    else Formatted
refgenre
         , note :: Formatted
note                = Char -> [Formatted] -> Formatted
concatWith Char
'.' [Formatted
note', Formatted
addendum']
         , annote :: Formatted
annote              = Formatted
annotation'
         , abstract :: Formatted
abstract            = Formatted
abstract'
         , keyword :: Formatted
keyword             = Formatted
keywords'
         , number :: Formatted
number              = Formatted
number'
         , url :: Literal
url                 = Text -> Literal
Literal Text
url'
         , doi :: Literal
doi                 = Text -> Literal
Literal Text
doi'
         , isbn :: Literal
isbn                = Text -> Literal
Literal Text
isbn'
         , issn :: Literal
issn                = Text -> Literal
Literal Text
issn'
         , pmcid :: Literal
pmcid               = Text -> Literal
Literal Text
pmcid'
         , pmid :: Literal
pmid                = Text -> Literal
Literal Text
pmid'
         , language :: Literal
language            = Text -> Literal
Literal Text
hyphenation
         , callNumber :: Literal
callNumber          = Text -> Literal
Literal Text
callNumber'
         }