{-# LANGUAGE OverloadedStrings #-}
module Text.Pandoc.Writers.BibTeX
( writeBibTeX
, writeBibLaTeX
)
where
import Text.Pandoc.Options
import Text.Pandoc.Definition
import Data.Text (Text)
import Data.Maybe (mapMaybe)
import Citeproc (parseLang)
import Text.Pandoc.Class (PandocMonad)
import Text.Pandoc.Citeproc.BibTeX as BibTeX
import Text.Pandoc.Citeproc.MetaValue (metaValueToReference)
import Text.Pandoc.Writers.Shared (lookupMetaString)
writeBibTeX :: PandocMonad m => WriterOptions -> Pandoc -> m Text
writeBibTeX :: WriterOptions -> Pandoc -> m Text
writeBibTeX = Variant -> WriterOptions -> Pandoc -> m Text
forall (m :: * -> *).
PandocMonad m =>
Variant -> WriterOptions -> Pandoc -> m Text
writeBibTeX' Variant
BibTeX.Bibtex
writeBibLaTeX :: PandocMonad m => WriterOptions -> Pandoc -> m Text
writeBibLaTeX :: WriterOptions -> Pandoc -> m Text
writeBibLaTeX = Variant -> WriterOptions -> Pandoc -> m Text
forall (m :: * -> *).
PandocMonad m =>
Variant -> WriterOptions -> Pandoc -> m Text
writeBibTeX' Variant
BibTeX.Biblatex
writeBibTeX' :: PandocMonad m => Variant -> WriterOptions -> Pandoc -> m Text
writeBibTeX' :: Variant -> WriterOptions -> Pandoc -> m Text
writeBibTeX' Variant
variant WriterOptions
opts (Pandoc Meta
meta [Block]
_) = do
let mblang :: Maybe Lang
mblang = case Text -> Meta -> Text
lookupMetaString Text
"lang" Meta
meta of
Text
"" -> Maybe Lang
forall a. Maybe a
Nothing
Text
t -> Lang -> Maybe Lang
forall a. a -> Maybe a
Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
forall a b. (a -> b) -> a -> b
$ Text -> Lang
parseLang Text
t
let refs :: [Reference Inlines]
refs = case Text -> Meta -> Maybe MetaValue
lookupMeta Text
"references" Meta
meta of
Just (MetaList [MetaValue]
xs) -> (MetaValue -> Maybe (Reference Inlines))
-> [MetaValue] -> [Reference Inlines]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe MetaValue -> Maybe (Reference Inlines)
metaValueToReference [MetaValue]
xs
Maybe MetaValue
_ -> []
Text -> m Text
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> m Text) -> Text -> m Text
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$
(Reference Inlines -> Text) -> [Reference Inlines] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (WriterOptions -> Variant -> Maybe Lang -> Reference Inlines -> Text
BibTeX.writeBibtexString WriterOptions
opts Variant
variant Maybe Lang
mblang) [Reference Inlines]
refs