{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Text.Pandoc.Readers.BibTeX
   Copyright   : Copyright (C) 2020 John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley.edu>
   Stability   : alpha
   Portability : portable

Parses BibTeX or BibLaTeX bibliographies into a Pandoc document
with empty body and `references` and `nocite` fields
in the metadata.  A wildcard `nocite` is used so that
if the document is rendered in another format, the
entire bibliography will be printed.
-}
module Text.Pandoc.Readers.BibTeX
  ( readBibTeX
  , readBibLaTeX
  )
where

import Text.Pandoc.Options
import Text.Pandoc.Definition
import Text.Pandoc.Builder (setMeta, cite, str)
import Data.Text (Text)
import Citeproc (Lang(..), parseLang)
import Citeproc.Locale (getLocale)
import Text.Pandoc.Error (PandocError(..))
import Text.Pandoc.Class (PandocMonad, lookupEnv)
import Text.Pandoc.Citeproc.BibTeX as BibTeX
import Text.Pandoc.Citeproc.MetaValue (referenceToMetaValue)
import Control.Monad.Except (throwError)

-- | Read BibTeX from an input string and return a Pandoc document.
-- The document will have only metadata, with an empty body.
-- The metadata will contain a `references` field with the
-- bibliography entries, and a `nocite` field with the wildcard `[@*]`.
readBibTeX :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
readBibTeX :: ReaderOptions -> Text -> m Pandoc
readBibTeX = Variant -> ReaderOptions -> Text -> m Pandoc
forall (m :: * -> *).
PandocMonad m =>
Variant -> ReaderOptions -> Text -> m Pandoc
readBibTeX' Variant
BibTeX.Bibtex

-- | Read BibLaTeX from an input string and return a Pandoc document.
-- The document will have only metadata, with an empty body.
-- The metadata will contain a `references` field with the
-- bibliography entries, and a `nocite` field with the wildcard `[@*]`.
readBibLaTeX :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
readBibLaTeX :: ReaderOptions -> Text -> m Pandoc
readBibLaTeX = Variant -> ReaderOptions -> Text -> m Pandoc
forall (m :: * -> *).
PandocMonad m =>
Variant -> ReaderOptions -> Text -> m Pandoc
readBibTeX' Variant
BibTeX.Biblatex

readBibTeX' :: PandocMonad m => Variant -> ReaderOptions -> Text -> m Pandoc
readBibTeX' :: Variant -> ReaderOptions -> Text -> m Pandoc
readBibTeX' Variant
variant ReaderOptions
_opts Text
t = do
  Lang
lang <- Lang -> (Text -> Lang) -> Maybe Text -> Lang
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Text -> Maybe Text -> Lang
Lang Text
"en" (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"US")) Text -> Lang
parseLang
             (Maybe Text -> Lang) -> m (Maybe Text) -> m Lang
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> m (Maybe Text)
forall (m :: * -> *). PandocMonad m => Text -> m (Maybe Text)
lookupEnv Text
"LANG"
  Locale
locale <- case Lang -> Either CiteprocError Locale
getLocale Lang
lang of
               Left CiteprocError
e  ->
                 case Lang -> Either CiteprocError Locale
getLocale (Text -> Maybe Text -> Lang
Lang Text
"en" (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"US")) of
                   Right Locale
l -> Locale -> m Locale
forall (m :: * -> *) a. Monad m => a -> m a
return Locale
l
                   Left CiteprocError
_  -> PandocError -> m Locale
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (PandocError -> m Locale) -> PandocError -> m Locale
forall a b. (a -> b) -> a -> b
$ CiteprocError -> PandocError
PandocCiteprocError CiteprocError
e
               Right Locale
l -> Locale -> m Locale
forall (m :: * -> *) a. Monad m => a -> m a
return Locale
l
  case Variant
-> Locale
-> (Text -> Bool)
-> Text
-> Either ParseError [Reference Inlines]
BibTeX.readBibtexString Variant
variant Locale
locale (Bool -> Text -> Bool
forall a b. a -> b -> a
const Bool
True) Text
t of
    Left ParseError
e -> PandocError -> m Pandoc
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (PandocError -> m Pandoc) -> PandocError -> m Pandoc
forall a b. (a -> b) -> a -> b
$ Text -> ParseError -> PandocError
PandocParsecError Text
t ParseError
e
    Right [Reference Inlines]
refs -> Pandoc -> m Pandoc
forall (m :: * -> *) a. Monad m => a -> m a
return (Pandoc -> m Pandoc) -> Pandoc -> m Pandoc
forall a b. (a -> b) -> a -> b
$ Text -> [MetaValue] -> Pandoc -> Pandoc
forall a b. (HasMeta a, ToMetaValue b) => Text -> b -> a -> a
setMeta Text
"references"
                              ((Reference Inlines -> MetaValue)
-> [Reference Inlines] -> [MetaValue]
forall a b. (a -> b) -> [a] -> [b]
map Reference Inlines -> MetaValue
referenceToMetaValue [Reference Inlines]
refs)
                         (Pandoc -> Pandoc) -> (Pandoc -> Pandoc) -> Pandoc -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Inlines -> Pandoc -> Pandoc
forall a b. (HasMeta a, ToMetaValue b) => Text -> b -> a -> a
setMeta Text
"nocite"
                            ([Citation] -> Inlines -> Inlines
cite [Citation :: Text
-> [Inline] -> [Inline] -> CitationMode -> Int -> Int -> Citation
Citation {citationId :: Text
citationId = Text
"*"
                                            , citationPrefix :: [Inline]
citationPrefix = []
                                            , citationSuffix :: [Inline]
citationSuffix = []
                                            , citationMode :: CitationMode
citationMode = CitationMode
NormalCitation
                                            , citationNoteNum :: Int
citationNoteNum = Int
0
                                            , citationHash :: Int
citationHash = Int
0}]
                                            (Text -> Inlines
str Text
"[@*]"))
                        (Pandoc -> Pandoc) -> Pandoc -> Pandoc
forall a b. (a -> b) -> a -> b
$ Meta -> [Block] -> Pandoc
Pandoc Meta
nullMeta []