{-# 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 bs :: [Block]
bs =
  case [Block]
bs of
       [Plain xs :: [Inline]
xs] -> [Inline] -> Bib Formatted
inlinesToFormatted [Inline]
xs
       [Para  xs :: [Inline]
xs] -> [Inline] -> Bib Formatted
inlinesToFormatted [Inline]
xs
       _          -> [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 _ (Span ("",[],[]) xs :: [Inline]
xs) = [Inline]
xs
adjustSpans lang :: Lang
lang (RawInline (Format "latex") s :: Text
s)
  | Text
s Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "\\hyphen" Bool -> Bool -> Bool
|| Text
s Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "\\hyphen " = [Text -> Inline
Str "-"]
  | 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 _ x :: Inline
x = [Inline
x]

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

inlinesToFormatted :: [Inline] -> Bib Formatted
inlinesToFormatted :: [Inline] -> Bib Formatted
inlinesToFormatted ils :: [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 "LANG" [(String, String)]
env of
    Just x :: String
x  -> case (Char -> Bool) -> Text -> [Text]
Text.CSL.Util.splitWhen (\c :: Char
c -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== '_' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== '-')
                    ((Char -> Bool) -> Text -> Text
T.takeWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/='.') (String -> Text
T.pack String
x)) of
                 (w :: Text
w:z :: Text
z:_) -> Text -> Text -> Lang
Lang Text
w Text
z
                 [w :: 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 -> Lang
Lang "en" "US"
    Nothing -> Text -> Text -> Lang
Lang "en" "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 idpred :: Text -> Bool
idpred isBibtex :: Bool
isBibtex caseTransform :: Bool
caseTransform f :: 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)
        (\e :: CiteprocException
e -> case CiteprocException
e of
                  ErrorReadingBib es :: 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 -> 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 idpred :: Text -> Bool
idpred isBibtex :: Bool
isBibtex caseTransform :: Bool
caseTransform contents :: 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 "stdin" Text
contents of
                      -- drop 8 to remove "stdin" + space
          Left err :: 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 8 (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ ParseError -> String
forall a. Show a => a -> String
show ParseError
err
          Right xs :: [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 '@' 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
/='@'))

bibComment :: BibParser ()
bibComment :: ParsecT Text (Map Text Text) Identity ()
bibComment = do
  Text -> BibParser Text
cistring "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 "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 "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 '{'
  ParsecT Text (Map Text Text) Identity ()
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m ()
spaces
  (k :: Text
k,v :: 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 '}'
  (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 '{'
  [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 "{}\\"))
        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 '\\' 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 '{' 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 "\\{")
                         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 '}' 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 "\\}")
                         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 "\\"))
        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 '}')
  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 '{' (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 '}'

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 '"'
  [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 "\"\\{"))
                       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 '\\' 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 '\\' (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 '"')

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 "-_:+")

isBibtexKeyChar :: Char -> Bool
isBibtexKeyChar :: Char -> Bool
isBibtexKeyChar c :: 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)

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 '@'
  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 '{'
  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 ','
  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 ',' 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 '}'
  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 '='
  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 '#' 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 "archiveprefix" = "eprinttype"
resolveAlias "primaryclass" = "eprintclass"
resolveAlias s :: 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 v :: Text
v  -> Text -> BibParser Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
v
       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 s :: 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 t :: Text
t = case Text -> Maybe (Char, Text)
T.uncons Text
t of
         Nothing     -> Text -> ParsecT s u m Text
forall (m :: * -> *) a. Monad m => a -> m a
return ""
         Just (c :: Char
c,cs :: 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 isBibtex :: Bool
isBibtex entries :: [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 (\c :: Char
c -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== ' ' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== ',')

getXrefFields :: Bool -> Item -> [Item] -> Text -> [(Text, Text)]
getXrefFields :: Bool -> Item -> [Item] -> Text -> [(Text, Text)]
getXrefFields isBibtex :: Bool
isBibtex baseEntry :: Item
baseEntry entries :: [Item]
entries keys :: 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']
  (k :: Text
k, v :: 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
== "crossref" Bool -> Bool -> Bool
|| Text
k Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "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)
       (x :: Text
x, y :: 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 isBibtex :: Bool
isBibtex entries :: [Item]
entries entry :: 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 key :: a
key val :: Text
val entry' :: Item
entry' =
          if a
key a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== "crossref" Bool -> Bool -> Bool
|| a
key a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== "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 _ _ "ids"            = []
transformKey _ _ "crossref"       = []
transformKey _ _ "xref"           = []
transformKey _ _ "entryset"       = []
transformKey _ _ "entrysubtype"   = []
transformKey _ _ "execute"        = []
transformKey _ _ "label"          = []
transformKey _ _ "options"        = []
transformKey _ _ "presort"        = []
transformKey _ _ "related"        = []
transformKey _ _ "relatedoptions" = []
transformKey _ _ "relatedstring"  = []
transformKey _ _ "relatedtype"    = []
transformKey _ _ "shorthand"      = []
transformKey _ _ "shorthandintro" = []
transformKey _ _ "sortkey"        = []
transformKey x :: Text
x y :: Text
y "author"
  | Text
x Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ["mvbook", "book"] Bool -> Bool -> Bool
&&
    Text
y Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ["inbook", "bookinbook", "suppbook"] = ["bookauthor", "author"]
-- note: this next clause is not in the biblatex manual, but it makes
-- sense in the context of CSL conversion:
transformKey x :: Text
x y :: Text
y "author"
  | Text
x Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "mvbook" Bool -> Bool -> Bool
&& Text
y Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "book" = ["bookauthor", "author"]
transformKey "mvbook" y :: Text
y z :: Text
z
  | Text
y Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ["book", "inbook", "bookinbook", "suppbook"] = Text -> [Text]
standardTrans Text
z
transformKey x :: Text
x y :: Text
y z :: Text
z
  | Text
x Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ["mvcollection", "mvreference"] Bool -> Bool -> Bool
&&
    Text
y Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ["collection", "reference", "incollection", "inreference",
               "suppcollection"] = Text -> [Text]
standardTrans Text
z
transformKey "mvproceedings" y :: Text
y z :: Text
z
  | Text
y Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ["proceedings", "inproceedings"] = Text -> [Text]
standardTrans Text
z
transformKey "book" y :: Text
y z :: Text
z
  | Text
y Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ["inbook", "bookinbook", "suppbook"] = Text -> [Text]
bookTrans Text
z
transformKey x :: Text
x y :: Text
y z :: Text
z
  | Text
x Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ["collection", "reference"] Bool -> Bool -> Bool
&&
    Text
y Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ["incollection", "inreference", "suppcollection"] = Text -> [Text]
bookTrans Text
z
transformKey "proceedings" "inproceedings" z :: Text
z = Text -> [Text]
bookTrans Text
z
transformKey "periodical" y :: Text
y z :: Text
z
  | Text
y Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ["article", "suppperiodical"] =
  case Text
z of
       "title"          -> ["journaltitle"]
       "subtitle"       -> ["journalsubtitle"]
       "shorttitle"     -> []
       "sorttitle"      -> []
       "indextitle"     -> []
       "indexsorttitle" -> []
       _                -> [Text
z]
transformKey _ _ x :: Text
x                = [Text
x]

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

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

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

parseMonth :: Text -> Maybe Int
parseMonth :: Text -> Maybe Int
parseMonth s :: Text
s =
  case Text -> Text
T.toLower Text
s of
         "jan" -> Int -> Maybe Int
forall a. a -> Maybe a
Just 1
         "feb" -> Int -> Maybe Int
forall a. a -> Maybe a
Just 2
         "mar" -> Int -> Maybe Int
forall a. a -> Maybe a
Just 3
         "apr" -> Int -> Maybe Int
forall a. a -> Maybe a
Just 4
         "may" -> Int -> Maybe Int
forall a. a -> Maybe a
Just 5
         "jun" -> Int -> Maybe Int
forall a. a -> Maybe a
Just 6
         "jul" -> Int -> Maybe Int
forall a. a -> Maybe a
Just 7
         "aug" -> Int -> Maybe Int
forall a. a -> Maybe a
Just 8
         "sep" -> Int -> Maybe Int
forall a. a -> Maybe a
Just 9
         "oct" -> Int -> Maybe Int
forall a. a -> Maybe a
Just 10
         "nov" -> Int -> Maybe Int
forall a. a -> Maybe a
Just 11
         "dec" -> Int -> Maybe Int
forall a. a -> Maybe a
Just 12
         _     -> 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 f :: 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]
++ " not found"

getField :: Text -> Bib Formatted
getField :: Text -> Bib Formatted
getField f :: 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 x :: Text
x  -> Text -> Bib Formatted
latex Text
x
       Nothing -> Text -> Bib Formatted
forall a. Text -> Bib a
notFound Text
f

getPeriodicalTitle :: Text -> Bib Formatted
getPeriodicalTitle :: Text -> Bib Formatted
getPeriodicalTitle f :: 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 x :: 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
       Nothing -> Text -> Bib Formatted
forall a. Text -> Bib a
notFound Text
f

getTitle :: Text -> Bib Formatted
getTitle :: Text -> Bib Formatted
getTitle f :: 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 x :: Text
x  -> Text -> Bib Formatted
latexTitle Text
x
       Nothing -> Text -> Bib Formatted
forall a. Text -> Bib a
notFound Text
f

getShortTitle :: Bool -> Text -> Bib Formatted
getShortTitle :: Bool -> Text -> Bib Formatted
getShortTitle requireColon :: Bool
requireColon f :: 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 x :: Text
x  -> case [Block] -> [Block]
processTitle ([Block] -> [Block]) -> [Block] -> [Block]
forall a b. (a -> b) -> a -> b
$ Text -> [Block]
latex' Text
x of
                       bs :: [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
       Nothing -> Text -> Bib Formatted
forall a. Text -> Bib a
notFound Text
f

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

upToColon :: [Block] -> [Block]
upToColon :: [Block] -> [Block]
upToColon [Para  xs :: [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 ":") [Inline]
xs]
upToColon [Plain xs :: [Inline]
xs] = [Block] -> [Block]
upToColon [[Inline] -> Block
Para [Inline]
xs]
upToColon bs :: [Block]
bs         = [Block]
bs

getDates :: Text -> Bib [RefDate]
getDates :: Text -> Bib [RefDate]
getDates f :: 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 t :: Text
t = case Text -> Maybe (Char, Text)
T.uncons Text
t of
  Just ('-', ds :: Text
ds) -> (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isDigit Text
ds
  Just _         -> (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isDigit Text
t
  Nothing        -> Bool
False

-- A negative (BC) year might be written with -- or --- in bibtex:
fixLeadingDash :: Text -> Text
fixLeadingDash :: Text -> Text
fixLeadingDash t :: Text
t = case Text -> Maybe (Char, Text)
T.uncons Text
t of
  Just (c :: Char
c, ds :: Text
ds) | (Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== '–' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== '—') Bool -> Bool -> Bool
&& Text -> Bool
firstIsDigit Text
ds -> Char -> Text -> Text
T.cons '–' Text
ds
  _ -> 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 prefix :: 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
<> "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 ""
  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
<> "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
<> "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
<> "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 ""
  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
<> "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
<> "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 "" 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 "" else Text
endyear'
                       , circa :: Bool
circa  = Bool
False
                       }
  let hasyear :: RefDate -> Bool
hasyear r :: 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 f :: 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 x :: Text
x  -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return Text
x
       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 opts :: [(Text, Text)]
opts  f :: 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 x :: Text
x  -> [(Text, Text)] -> Text -> Bib [Agent]
latexAuthors [(Text, Text)]
opts Text
x
       Nothing -> Text -> Bib [Agent]
forall a. Text -> Bib a
notFound Text
f

getLiteralList :: Text -> Bib [Formatted]
getLiteralList :: Text -> Bib [Formatted]
getLiteralList f :: 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 x :: Text
x  -> [Block] -> Bib [Formatted]
toLiteralList ([Block] -> Bib [Formatted]) -> [Block] -> Bib [Formatted]
forall a b. (a -> b) -> a -> b
$ Text -> [Block]
latex' Text
x
       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' f :: 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 ";", 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 "and", Inline
Space]

toLiteralList :: [Block] -> Bib [Formatted]
toLiteralList :: [Block] -> Bib [Formatted]
toLiteralList [Para xs :: [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 xs :: [Inline]
xs] = [Block] -> Bib [Formatted]
toLiteralList [[Inline] -> Block
Para [Inline]
xs]
toLiteralList _ = Bib [Formatted]
forall (m :: * -> *) a. MonadPlus m => m a
mzero

toAuthorList :: Options -> [Block] -> Bib [Agent]
toAuthorList :: [(Text, Text)] -> [Block] -> Bib [Agent]
toAuthorList opts :: [(Text, Text)]
opts [Para xs :: [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 opts :: [(Text, Text)]
opts [Plain xs :: [Inline]
xs] = [(Text, Text)] -> [Block] -> Bib [Agent]
toAuthorList [(Text, Text)]
opts [[Inline] -> Block
Para [Inline]
xs]
toAuthorList _ _ = 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 _ [Str "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 "others"]
          , commaSuffix :: Bool
commaSuffix     = Bool
False
          , parseNames :: Bool
parseNames      = Bool
False
          }
toAuthor _ [Span ("",[],[]) ils :: [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 _ ils :: [Inline]
ils@(Str ys :: Text
ys:_) | (Char -> Bool) -> Text -> Bool
T.any (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== '=') 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 ",")
                   ([Inline] -> [[Inline]])
-> ([Inline] -> [Inline]) -> [Inline] -> [[Inline]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> [Inline] -> [Inline]
splitStrWhen (\c :: Char
c -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== ',' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== '=' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== '\160')
                   ([Inline] -> [[Inline]]) -> [Inline] -> [[Inline]]
forall a b. (a -> b) -> a -> b
$ [Inline]
ils
  let addPart :: Agent -> [Inline] -> Agent
addPart ag :: Agent
ag (Str "given" : Str "=" : xs :: [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 ag :: Agent
ag (Str "family" : Str "=" : xs :: [Inline]
xs) =
        Agent
ag{ familyName :: Formatted
familyName = [Inline] -> Formatted
Formatted [Inline]
xs }
      addPart ag :: Agent
ag (Str "prefix" : Str "=" : xs :: [Inline]
xs) =
        Agent
ag{ droppingPart :: Formatted
droppingPart = [Inline] -> Formatted
Formatted [Inline]
xs }
      addPart ag :: Agent
ag (Str "useprefix" : Str "=" : Str "true" : _) =
        Agent
ag{ nonDroppingPart :: Formatted
nonDroppingPart = Agent -> Formatted
droppingPart Agent
ag, droppingPart :: Formatted
droppingPart = Formatted
forall a. Monoid a => a
mempty }
      addPart ag :: Agent
ag (Str "suffix" : Str "=" : xs :: [Inline]
xs) =
        Agent
ag{ nameSuffix :: Formatted
nameSuffix = [Inline] -> Formatted
Formatted [Inline]
xs }
      addPart ag :: Agent
ag (Space : xs :: [Inline]
xs) = Agent -> [Inline] -> Agent
addPart Agent
ag [Inline]
xs
      addPart ag :: Agent
ag _ = 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 opts :: [(Text, Text)]
opts ils :: [Inline]
ils = do
  let useprefix :: Bool
useprefix = Text -> [(Text, Text)] -> Bool
optionSet "useprefix" [(Text, Text)]
opts
  let usecomma :: Bool
usecomma  = Text -> [(Text, Text)] -> Bool
optionSet "juniorcomma" [(Text, Text)]
opts
  let bibtex :: Bool
bibtex    = Text -> [(Text, Text)] -> Bool
optionSet "bibtex" [(Text, Text)]
opts
  let words' :: [Inline] -> [[Inline]]
words' = (Inline -> Bool) -> [Inline] -> [[Inline]]
forall a. (a -> Bool) -> [a] -> [[a]]
wordsBy (\x :: 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 "\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 ",")
                              ([Inline] -> [[Inline]]) -> [Inline] -> [[Inline]]
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> [Inline] -> [Inline]
splitStrWhen (\c :: Char
c -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== ',' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== '\160') [Inline]
ils
  let (first :: [[Inline]]
first, vonlast :: [[Inline]]
vonlast, jr :: [[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.
               [fvl :: [[Inline]]
fvl]      -> let (caps' :: [[Inline]]
caps', rest' :: [[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', [])
               [vl :: [[Inline]]
vl,f :: [[Inline]]
f]     -> ([[Inline]]
f, [[Inline]]
vl, [])
               (vl :: [[Inline]]
vl:j :: [[Inline]]
j:f :: [[Inline]]
f:_) -> ([[Inline]]
f, [[Inline]]
vl, [[Inline]]
j )
               []         -> ([], [], [])

  let (von :: [[Inline]]
von, lastname :: [[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
                        ([],w :: [Inline]
w:ws :: [[Inline]]
ws) -> ([[Inline]] -> [[Inline]]
forall a. [a] -> [a]
reverse [[Inline]]
ws, [[Inline]
w])
                        (vs :: [[Inline]]
vs, ws :: [[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]]
forall a. [a] -> [a]
init [[Inline]]
vs, [[[Inline]] -> [Inline]
forall a. [a] -> a
last [[Inline]]
vs])
                        (vs :: [[Inline]]
vs, ws :: [[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 (c :: Char
c,cs :: Text
cs)) : rest :: [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 (_:rest :: [Inline]
rest) = [Inline] -> Bool
isCapitalized [Inline]
rest
isCapitalized [] = Bool
True

optionSet :: Text -> Options -> Bool
optionSet :: Text -> [(Text, Text)] -> Bool
optionSet key :: Text
key opts :: [(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 "true" -> Bool
True
                      Just s :: Text
s      -> Text
s Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
forall a. Monoid a => a
mempty
                      _           -> Bool
False

latex' :: Text -> [Block]
latex' :: Text -> [Block]
latex' s :: 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 _ bs :: [Block]
bs = Text -> Pandoc
readLaTeX Text
s

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

latex :: Text -> Bib Formatted
latex :: Text -> Bib Formatted
latex s :: 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 s :: 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 opts :: [(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 m :: Bib Reference
m entry :: 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 "en" "US"))

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

concatWith :: Char -> [Formatted] -> Formatted
concatWith :: Char -> [Formatted] -> Formatted
concatWith sep :: 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 accum :: [Inline]
accum [] = [Inline]
accum
        go accum :: [Inline]
accum s :: [Inline]
s  = case [Inline] -> [Inline]
forall a. [a] -> [a]
reverse [Inline]
accum of
                           []    -> [Inline]
s
                           (Str x :: Text
x:_)
                             | 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)
                                          -> [Inline]
accum [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ (Inline
Space Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline]
s)
                           _     -> [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 ","
  where breakOpt :: Text -> (Text, Text)
breakOpt x :: Text
x = case (Char -> Bool) -> Text -> (Text, Text)
T.break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
=='=') Text
x of
                          (w :: Text
w,v :: 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 1 Text
v)

ordinalize :: Locale -> Text -> Text
ordinalize :: Locale -> Text -> Text
ordinalize locale :: Locale
locale n :: 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
== ("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
== "ordinal"] of
       (suff :: Text
suff:_) -> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
suff
       []       -> Text
n
    where pad0 :: Text -> Text
pad0 s :: Text
s = case Text -> Maybe (Char, Text)
T.uncons Text
s of
            Just (c :: Char
c,"") -> String -> Text
T.pack ['0', Char
c]
            _           -> 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
lang locale :: Locale
locale bibtex :: Bool
bibtex caseTransform :: 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
$ \st :: BibState
st -> BibState
st{ localeLanguage :: Lang
localeLanguage = Lang
lang,
                      untitlecase :: Bool
untitlecase = case Lang
lang of
                                         Lang "en" _ -> Bool
caseTransform
                                         _           -> 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])
-> 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 "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
/= "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 "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
         (("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 "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 "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 "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 "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 "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
reftype, refgenre :: Formatted
refgenre) = case Text
et of
       "article"
         | Text
st Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "magazine"  -> (RefType
ArticleMagazine,Formatted
forall a. Monoid a => a
mempty)
         | Text
st Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "newspaper" -> (RefType
ArticleNewspaper,Formatted
forall a. Monoid a => a
mempty)
         | Bool
otherwise         -> (RefType
ArticleJournal,Formatted
forall a. Monoid a => a
mempty)
       "book"            -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       "booklet"         -> (RefType
Pamphlet,Formatted
forall a. Monoid a => a
mempty)
       "bookinbook"      -> (RefType
Chapter,Formatted
forall a. Monoid a => a
mempty)
       "collection"      -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       "dataset"         -> (RefType
Dataset,Formatted
forall a. Monoid a => a
mempty)
       "electronic"      -> (RefType
Webpage,Formatted
forall a. Monoid a => a
mempty)
       "inbook"          -> (RefType
Chapter,Formatted
forall a. Monoid a => a
mempty)
       "incollection"    -> (RefType
Chapter,Formatted
forall a. Monoid a => a
mempty)
       "inreference"     -> (RefType
EntryEncyclopedia,Formatted
forall a. Monoid a => a
mempty)
       "inproceedings"   -> (RefType
PaperConference,Formatted
forall a. Monoid a => a
mempty)
       "manual"          -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       "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 "mathesis"]
                                        else Formatted
reftype')
       "misc"            -> (RefType
NoType,Formatted
forall a. Monoid a => a
mempty)
       "mvbook"          -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       "mvcollection"    -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       "mvproceedings"   -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       "mvreference"     -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       "online"          -> (RefType
Webpage,Formatted
forall a. Monoid a => a
mempty)
       "patent"          -> (RefType
Patent,Formatted
forall a. Monoid a => a
mempty)
       "periodical"
         | Text
st Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "magazine"  -> (RefType
ArticleMagazine,Formatted
forall a. Monoid a => a
mempty)
         | Text
st Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "newspaper" -> (RefType
ArticleNewspaper,Formatted
forall a. Monoid a => a
mempty)
         | Bool
otherwise         -> (RefType
ArticleJournal,Formatted
forall a. Monoid a => a
mempty)
       "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 "phdthesis"]
                                        else Formatted
reftype')
       "proceedings"     -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       "reference"       -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       "report"          -> (RefType
Report,Formatted
forall a. Monoid a => a
mempty)
       "software"        -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)         -- no "software" type in CSL
       "suppbook"        -> (RefType
Chapter,Formatted
forall a. Monoid a => a
mempty)
       "suppcollection"  -> (RefType
Chapter,Formatted
forall a. Monoid a => a
mempty)
       "suppperiodical"
         | Text
st Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "magazine"  -> (RefType
ArticleMagazine,Formatted
forall a. Monoid a => a
mempty)
         | Text
st Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "newspaper" -> (RefType
ArticleNewspaper,Formatted
forall a. Monoid a => a
mempty)
         | Bool
otherwise         -> (RefType
ArticleJournal,Formatted
forall a. Monoid a => a
mempty)
       "techreport"      -> (RefType
Report,Formatted
forall a. Monoid a => a
mempty)
       "thesis"          -> (RefType
Thesis,Formatted
forall a. Monoid a => a
mempty)
       "unpublished"     -> (if Bool
isEvent then RefType
Speech else RefType
Manuscript,Formatted
forall a. Monoid a => a
mempty)
       "www"             -> (RefType
Webpage,Formatted
forall a. Monoid a => a
mempty)
       -- biblatex, "unsupported"
       "artwork"         -> (RefType
Graphic,Formatted
forall a. Monoid a => a
mempty)
       "audio"           -> (RefType
Song,Formatted
forall a. Monoid a => a
mempty)         -- for audio *recordings*
       "commentary"      -> (RefType
Book,Formatted
forall a. Monoid a => a
mempty)
       "image"           -> (RefType
Graphic,Formatted
forall a. Monoid a => a
mempty)      -- or "figure" ?
       "jurisdiction"    -> (RefType
LegalCase,Formatted
forall a. Monoid a => a
mempty)
       "legislation"     -> (RefType
Legislation,Formatted
forall a. Monoid a => a
mempty)  -- or "bill" ?
       "legal"           -> (RefType
Treaty,Formatted
forall a. Monoid a => a
mempty)
       "letter"          -> (RefType
PersonalCommunication,Formatted
forall a. Monoid a => a
mempty)
       "movie"           -> (RefType
MotionPicture,Formatted
forall a. Monoid a => a
mempty)
       "music"           -> (RefType
Song,Formatted
forall a. Monoid a => a
mempty)         -- for musical *recordings*
       "performance"     -> (RefType
Speech,Formatted
forall a. Monoid a => a
mempty)
       "review"          -> (RefType
Review,Formatted
forall a. Monoid a => a
mempty)       -- or "review-book" ?
       "standard"        -> (RefType
Legislation,Formatted
forall a. Monoid a => a
mempty)
       "video"           -> (RefType
MotionPicture,Formatted
forall a. Monoid a => a
mempty)
       -- biblatex-apa:
       "data"            -> (RefType
Dataset,Formatted
forall a. Monoid a => a
mempty)
       "letters"         -> (RefType
PersonalCommunication,Formatted
forall a. Monoid a => a
mempty)
       "newsarticle"     -> (RefType
ArticleNewspaper,Formatted
forall a. Monoid a => a
mempty)
       _                 -> (RefType
NoType,Formatted
forall a. Monoid a => a
mempty)

  -- hyphenation:
  let defaultHyphenation :: Text
defaultHyphenation = case Lang
lang of
                                Lang x :: Text
x y :: Text
y -> Text
x Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> "-" 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 "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 "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 ""
           case (Text
langid, Text
idopts) of
                ("english","variant=british")    -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return "british"
                ("english","variant=american")   -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return "american"
                ("english","variant=us")         -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return "american"
                ("english","variant=usmax")      -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return "american"
                ("english","variant=uk")         -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return "british"
                ("english","variant=australian") -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return "australian"
                ("english","variant=newzealand") -> Text -> RWST Item () BibState Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return "newzealand"
                (x :: Text
x,_)                            -> 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 "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' "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' "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' "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 "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' "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' "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 (editor' :: [Agent]
editor', director' :: [Agent]
director') = case Text
editortype of
                                  "director" -> ([], [Agent]
editor'')
                                  _          -> ([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` ["article", "periodical", "suppperiodical", "review"]
  let isPeriodical :: Bool
isPeriodical = Text
et Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "periodical"
  let isChapterlike :: Bool
isChapterlike = Text
et Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem`
         ["inbook","incollection","inproceedings","inreference","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 "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
hyphenation' of
                      (x :: Text
x:_) -> 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
$ \s :: BibState
s -> BibState
s{ untitlecase :: Bool
untitlecase = Bool
caseTransform Bool -> Bool -> Bool
&& Text
la Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "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 "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 "maintitle")
            Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getTitle "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 "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 "mainsubtitle")
               Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getTitle "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 "maintitleaddon")
                 Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getTitle "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 "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 "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 "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 "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 "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 "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 "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 "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 "booktitle")
                     Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getPeriodicalTitle "journaltitle"
                     Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getPeriodicalTitle "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 "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 "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 "booksubtitle")
                        Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getPeriodicalTitle "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 "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 "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 "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 "shorttitle")
                        Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getPeriodicalTitle "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 xs :: 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 "ser.")]
      fixSeriesTitle x :: 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 "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 "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 "title"
                      else Bool -> Text -> Bib Formatted
getShortTitle Bool
True  "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 "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 "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 (\f :: 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
== "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)
         ["school","institution","organization", "howpublished","publisher"]
  let publisher' :: Formatted
publisher' = Char -> [Formatted] -> Formatted
concatWith ';' ([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 "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 "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 "address"
               else Text -> Bib Formatted
getLiteralList' "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
/= "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' "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 "origlocation"
                    else Text -> Bib Formatted
getLiteralList' "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
== "patent"
                   then (Char -> [Formatted] -> Formatted
concatWith ';' ([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 "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 "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 "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 "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 "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 "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 "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 "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 "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
  (number' :: Formatted
number', collectionNumber' :: Formatted
collectionNumber', issue' :: Formatted
issue') <-
     (Text -> Bib Formatted
getField "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
>>= \x :: Formatted
x ->
       if Text
et Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ["book","collection","proceedings","reference",
                     "mvbook","mvcollection","mvproceedings", "mvreference",
                     "bookinbook","inbook", "incollection","inproceedings",
                     "inreference", "suppbook","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 "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
>>= \y :: 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 ',' [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 "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 "eventdate" Bib [RefDate] -> Bib [RefDate] -> Bib [RefDate]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib [RefDate]
getOldDates "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 "origdate" Bib [RefDate] -> Bib [RefDate] -> Bib [RefDate]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib [RefDate]
getOldDates "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 "urldate" Bib [RefDate] -> Bib [RefDate] -> Bib [RefDate]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib [RefDate]
getOldDates "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
== "online" Bool -> Bool -> Bool
|| Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup "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 "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 "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 "eprinttype"
               Text
eprint <- Text -> RWST Item () BibState Maybe Text
getRawField "eprint"
               let baseUrl :: Text
baseUrl =
                     case Text -> Text
T.toLower Text
etype of
                       "arxiv"       -> "http://arxiv.org/abs/"
                       "jstor"       -> "http://www.jstor.org/stable/"
                       "pubmed"      -> "http://www.ncbi.nlm.nih.gov/pubmed/"
                       "googlebooks" -> "http://books.google.com?id="
                       _             -> ""
               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 "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 "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 "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 "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 "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 "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 "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 "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 "annotation" Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Bib Formatted
getField "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 "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 "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
== "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 "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 "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 "pubstate"
                Bib Formatted -> Bib Formatted -> Bib Formatted
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> case [RefDate]
issued' of
                         (x :: RefDate
x:_) | RefDate -> Literal
other RefDate
x Literal -> Literal -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Literal
Literal "forthcoming" ->
                                     Formatted -> Bib Formatted
forall (m :: * -> *) a. Monad m => a -> m a
return ([Inline] -> Formatted
Formatted [Text -> Inline
Str "forthcoming"])
                         _ -> 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 s :: Text
s) = Text -> Inline
Str ((Char -> Char) -> Text -> Text
T.map (\c :: Char
c -> if Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== '–' then '-' else Char
c) Text
s)
      convertEnDash x :: Inline
x       = Inline
x

  let takeDigits :: [Inline] -> [Inline]
takeDigits (Str xs :: Text
xs : _) =
         case (Char -> Bool) -> Text -> Text
T.takeWhile Char -> Bool
isDigit Text
xs of
              "" -> []
              ds :: Text
ds -> [Text -> Inline
Str Text
ds]
      takeDigits x :: [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 -> [Formatted] -> Formatted
concatWith ':' [Formatted
title', Formatted
subtitle']
                                  , Formatted
titleaddon' ]
         , titleShort :: Formatted
titleShort          = Formatted
shortTitle'
         -- , reviewedTitle       = undefined -- :: String
         , containerTitle :: Formatted
containerTitle      = Char -> [Formatted] -> Formatted
concatWith '.' [
                                      Char -> [Formatted] -> Formatted
concatWith ':' [ Formatted
containerTitle'
                                                     , Formatted
containerSubtitle']
                                    , Formatted
containerTitleAddon' ]
         , collectionTitle :: Formatted
collectionTitle     = Formatted
seriesTitle'
         , volumeTitle :: Formatted
volumeTitle         = Char -> [Formatted] -> Formatted
concatWith '.' [
                                      Char -> [Formatted] -> Formatted
concatWith ':' [ 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 "."]
                                 ([[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 '.' [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'
         }