{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
module Text.Pandoc.Citeproc
( processCitations,
getReferences,
)
where
import Citeproc
import Citeproc.Pandoc ()
import Text.Pandoc.Citeproc.Locator (parseLocator, toLocatorMap,
LocatorInfo(..))
import Text.Pandoc.Citeproc.CslJson (cslJsonToReferences)
import Text.Pandoc.Citeproc.BibTeX (readBibtexString, Variant(..))
import Text.Pandoc.Citeproc.MetaValue (metaValueToReference, metaValueToText)
import Text.Pandoc.Readers.Markdown (yamlToRefs)
import Text.Pandoc.Builder (Inlines, Many(..), deleteMeta, setMeta)
import qualified Text.Pandoc.Builder as B
import Text.Pandoc.Definition as Pandoc
import Text.Pandoc.Class (PandocMonad(..), getResourcePath, getUserDataDir,
fetchItem, readDataFile, report, setResourcePath)
import Text.Pandoc.Error (PandocError(..))
import Text.Pandoc.Extensions (pandocExtensions)
import Text.Pandoc.Logging (LogMessage(..))
import Text.Pandoc.Options (ReaderOptions(..))
import Text.Pandoc.Shared (stringify, ordNub, tshow)
import qualified Text.Pandoc.UTF8 as UTF8
import Text.Pandoc.Walk (query, walk, walkM)
import Control.Applicative ((<|>))
import Control.Monad.Except (catchError, throwError)
import Control.Monad.State (State, evalState, get, put, runState)
import Data.Aeson (eitherDecode)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as L
import Data.Char (isPunctuation, isUpper)
import Data.Default (Default(def))
import qualified Data.Foldable as Foldable
import qualified Data.Map as M
import Data.Maybe (mapMaybe, fromMaybe)
import Data.Ord ()
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as T
import System.FilePath (takeExtension)
import Safe (lastMay, initSafe)
processCitations :: PandocMonad m => Pandoc -> m Pandoc
processCitations :: Pandoc -> m Pandoc
processCitations (Pandoc meta :: Meta
meta bs :: [Block]
bs) = do
Style Inlines
style <- Pandoc -> m (Style Inlines)
forall (m :: * -> *). PandocMonad m => Pandoc -> m (Style Inlines)
getStyle (Meta -> [Block] -> Pandoc
Pandoc Meta
meta [Block]
bs)
Maybe Lang
mblang <- Meta -> m (Maybe Lang)
forall (m :: * -> *). PandocMonad m => Meta -> m (Maybe Lang)
getCiteprocLang Meta
meta
let locale :: Locale
locale = Maybe Lang -> Style Inlines -> Locale
forall a. Maybe Lang -> Style a -> Locale
Citeproc.mergeLocales Maybe Lang
mblang Style Inlines
style
let addQuoteSpan :: Inline -> Inline
addQuoteSpan (Quoted _ xs :: [Inline]
xs) = Attr -> [Inline] -> Inline
Span ("",["csl-quoted"],[]) [Inline]
xs
addQuoteSpan x :: Inline
x = Inline
x
[Reference Inlines]
refs <- (Reference Inlines -> Reference Inlines)
-> [Reference Inlines] -> [Reference Inlines]
forall a b. (a -> b) -> [a] -> [b]
map ((Inline -> Inline) -> Reference Inlines -> Reference Inlines
forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
addQuoteSpan) ([Reference Inlines] -> [Reference Inlines])
-> m [Reference Inlines] -> m [Reference Inlines]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
Maybe Locale -> Pandoc -> m [Reference Inlines]
forall (m :: * -> *).
PandocMonad m =>
Maybe Locale -> Pandoc -> m [Reference Inlines]
getReferences (Locale -> Maybe Locale
forall a. a -> Maybe a
Just Locale
locale) (Meta -> [Block] -> Pandoc
Pandoc Meta
meta [Block]
bs)
let otherIdsMap :: Map Text ItemId
otherIdsMap = (Reference Inlines -> Map Text ItemId -> Map Text ItemId)
-> Map Text ItemId -> [Reference Inlines] -> Map Text ItemId
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\ref :: Reference Inlines
ref m :: Map Text ItemId
m ->
case Text -> [Text]
T.words (Text -> [Text]) -> (Val Inlines -> Text) -> Val Inlines -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Val Inlines -> Text
extractText (Val Inlines -> [Text]) -> Maybe (Val Inlines) -> Maybe [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
Variable -> Map Variable (Val Inlines) -> Maybe (Val Inlines)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup "other-ids"
(Reference Inlines -> Map Variable (Val Inlines)
forall a. Reference a -> Map Variable (Val a)
referenceVariables Reference Inlines
ref) of
Nothing -> Map Text ItemId
m
Just ids :: [Text]
ids -> (Text -> Map Text ItemId -> Map Text ItemId)
-> Map Text ItemId -> [Text] -> Map Text ItemId
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr
(\id' :: Text
id' ->
Text -> ItemId -> Map Text ItemId -> Map Text ItemId
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
id' (Reference Inlines -> ItemId
forall a. Reference a -> ItemId
referenceId Reference Inlines
ref)) Map Text ItemId
m [Text]
ids)
Map Text ItemId
forall k a. Map k a
M.empty [Reference Inlines]
refs
let meta' :: Meta
meta' = Text -> Meta -> Meta
forall a. HasMeta a => Text -> a -> a
deleteMeta "nocite" Meta
meta
let citations :: [Citation Inlines]
citations = Locale -> Map Text ItemId -> Pandoc -> [Citation Inlines]
getCitations Locale
locale Map Text ItemId
otherIdsMap (Pandoc -> [Citation Inlines]) -> Pandoc -> [Citation Inlines]
forall a b. (a -> b) -> a -> b
$ Meta -> [Block] -> Pandoc
Pandoc Meta
meta' [Block]
bs
let linkCites :: Bool
linkCites = Bool -> (MetaValue -> Bool) -> Maybe MetaValue -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False MetaValue -> Bool
truish (Maybe MetaValue -> Bool) -> Maybe MetaValue -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Meta -> Maybe MetaValue
lookupMeta "link-citations" Meta
meta
let linkBib :: Bool
linkBib = Bool -> (MetaValue -> Bool) -> Maybe MetaValue -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True MetaValue -> Bool
truish (Maybe MetaValue -> Bool) -> Maybe MetaValue -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Meta -> Maybe MetaValue
lookupMeta "link-bibliography" Meta
meta
let opts :: CiteprocOptions
opts = CiteprocOptions
defaultCiteprocOptions{ linkCitations :: Bool
linkCitations = Bool
linkCites
, linkBibliography :: Bool
linkBibliography = Bool
linkBib }
let result :: Result Inlines
result = CiteprocOptions
-> Style Inlines
-> Maybe Lang
-> [Reference Inlines]
-> [Citation Inlines]
-> Result Inlines
forall a.
CiteprocOutput a =>
CiteprocOptions
-> Style a
-> Maybe Lang
-> [Reference a]
-> [Citation a]
-> Result a
Citeproc.citeproc CiteprocOptions
opts Style Inlines
style Maybe Lang
mblang [Reference Inlines]
refs [Citation Inlines]
citations
(Text -> m ()) -> [Text] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (LogMessage -> m ()
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report (LogMessage -> m ()) -> (Text -> LogMessage) -> Text -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> LogMessage
CiteprocWarning) (Result Inlines -> [Text]
forall a. Result a -> [Text]
resultWarnings Result Inlines
result)
let sopts :: StyleOptions
sopts = Style Inlines -> StyleOptions
forall a. Style a -> StyleOptions
styleOptions Style Inlines
style
let classes :: [Text]
classes = "references" Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
:
"csl-bib-body" Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
:
["hanging-indent" | StyleOptions -> Bool
styleHangingIndent StyleOptions
sopts]
let refkvs :: [(Text, Text)]
refkvs = (case StyleOptions -> Maybe Int
styleEntrySpacing StyleOptions
sopts of
Just es :: Int
es | Int
es Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0 -> (("entry-spacing",String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show Int
es)(Text, Text) -> [(Text, Text)] -> [(Text, Text)]
forall a. a -> [a] -> [a]
:)
_ -> [(Text, Text)] -> [(Text, Text)]
forall a. a -> a
id) ([(Text, Text)] -> [(Text, Text)])
-> ([(Text, Text)] -> [(Text, Text)])
-> [(Text, Text)]
-> [(Text, Text)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
(case StyleOptions -> Maybe Int
styleLineSpacing StyleOptions
sopts of
Just ls :: Int
ls | Int
ls Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 1 -> (("line-spacing",String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show Int
ls)(Text, Text) -> [(Text, Text)] -> [(Text, Text)]
forall a. a -> [a] -> [a]
:)
_ -> [(Text, Text)] -> [(Text, Text)]
forall a. a -> a
id) ([(Text, Text)] -> [(Text, Text)])
-> [(Text, Text)] -> [(Text, Text)]
forall a b. (a -> b) -> a -> b
$ []
let bibs :: Blocks
bibs = [Blocks] -> Blocks
forall a. Monoid a => [a] -> a
mconcat ([Blocks] -> Blocks) -> [Blocks] -> Blocks
forall a b. (a -> b) -> a -> b
$ ((Text, Inlines) -> Blocks) -> [(Text, Inlines)] -> [Blocks]
forall a b. (a -> b) -> [a] -> [b]
map (\(ident :: Text
ident, out :: Inlines
out) ->
Attr -> Blocks -> Blocks
B.divWith ("ref-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
ident,["csl-entry"],[]) (Blocks -> Blocks) -> (Inlines -> Blocks) -> Inlines -> Blocks
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inlines -> Blocks
B.para (Inlines -> Blocks) -> (Inlines -> Inlines) -> Inlines -> Blocks
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Inlines -> Inlines
insertSpace (Inlines -> Blocks) -> Inlines -> Blocks
forall a b. (a -> b) -> a -> b
$ Inlines
out)
(Result Inlines -> [(Text, Inlines)]
forall a. Result a -> [(Text, a)]
resultBibliography Result Inlines
result)
let moveNotes :: Bool
moveNotes = Bool -> (MetaValue -> Bool) -> Maybe MetaValue -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (StyleOptions -> Bool
styleIsNoteStyle StyleOptions
sopts) MetaValue -> Bool
truish
(Text -> Meta -> Maybe MetaValue
lookupMeta "notes-after-punctuation" Meta
meta)
let cits :: [Inlines]
cits = Result Inlines -> [Inlines]
forall a. Result a -> [a]
resultCitations Result Inlines
result
let metanocites :: Maybe MetaValue
metanocites = Text -> Meta -> Maybe MetaValue
lookupMeta "nocite" Meta
meta
let Pandoc meta'' :: Meta
meta'' bs' :: [Block]
bs' =
(Pandoc -> Pandoc)
-> (MetaValue -> Pandoc -> Pandoc)
-> Maybe MetaValue
-> Pandoc
-> Pandoc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Pandoc -> Pandoc
forall a. a -> a
id (Text -> MetaValue -> Pandoc -> Pandoc
forall a b. (HasMeta a, ToMetaValue b) => Text -> b -> a -> a
setMeta "nocite") Maybe MetaValue
metanocites (Pandoc -> Pandoc) -> ([Inlines] -> Pandoc) -> [Inlines] -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
([Inline] -> [Inline]) -> Pandoc -> Pandoc
forall a b. Walkable a b => (a -> a) -> b -> b
walk (Bool -> Locale -> [Inline] -> [Inline]
mvPunct Bool
moveNotes Locale
locale) (Pandoc -> Pandoc) -> ([Inlines] -> Pandoc) -> [Inlines] -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
(if StyleOptions -> Bool
styleIsNoteStyle StyleOptions
sopts
then (Inline -> Inline) -> Pandoc -> Pandoc
forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
addNote (Pandoc -> Pandoc) -> (Pandoc -> Pandoc) -> Pandoc -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Inline -> Inline) -> Pandoc -> Pandoc
forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
deNote
else Pandoc -> Pandoc
forall a. a -> a
id) (Pandoc -> Pandoc) -> ([Inlines] -> Pandoc) -> [Inlines] -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
State [Inlines] Pandoc -> [Inlines] -> Pandoc
forall s a. State s a -> s -> a
evalState ((Inline -> StateT [Inlines] Identity Inline)
-> Pandoc -> State [Inlines] Pandoc
forall a b (m :: * -> *).
(Walkable a b, Monad m, Applicative m, Functor m) =>
(a -> m a) -> b -> m b
walkM Inline -> StateT [Inlines] Identity Inline
insertResolvedCitations (Pandoc -> State [Inlines] Pandoc)
-> Pandoc -> State [Inlines] Pandoc
forall a b. (a -> b) -> a -> b
$ Meta -> [Block] -> Pandoc
Pandoc Meta
meta' [Block]
bs)
([Inlines] -> Pandoc) -> [Inlines] -> Pandoc
forall a b. (a -> b) -> a -> b
$ [Inlines]
cits
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
$ (Inline -> Inline) -> Pandoc -> Pandoc
forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
removeQuoteSpan
(Pandoc -> Pandoc) -> Pandoc -> Pandoc
forall a b. (a -> b) -> a -> b
$ Meta -> [Block] -> Pandoc
Pandoc Meta
meta''
([Block] -> Pandoc) -> [Block] -> Pandoc
forall a b. (a -> b) -> a -> b
$ [(Text, Text)] -> [Text] -> Meta -> [Block] -> [Block] -> [Block]
insertRefs [(Text, Text)]
refkvs [Text]
classes Meta
meta'' (Blocks -> [Block]
forall a. Many a -> [a]
B.toList Blocks
bibs) [Block]
bs'
removeQuoteSpan :: Inline -> Inline
removeQuoteSpan :: Inline -> Inline
removeQuoteSpan (Span ("",["csl-quoted"],[]) xs :: [Inline]
xs) = Attr -> [Inline] -> Inline
Span Attr
nullAttr [Inline]
xs
removeQuoteSpan x :: Inline
x = Inline
x
getStyle :: PandocMonad m => Pandoc -> m (Style Inlines)
getStyle :: Pandoc -> m (Style Inlines)
getStyle (Pandoc meta :: Meta
meta _) = do
let cslfile :: Maybe Text
cslfile = (Text -> Meta -> Maybe MetaValue
lookupMeta "csl" Meta
meta Maybe MetaValue -> Maybe MetaValue -> Maybe MetaValue
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Meta -> Maybe MetaValue
lookupMeta "citation-style" Meta
meta)
Maybe MetaValue -> (MetaValue -> Maybe Text) -> Maybe Text
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MetaValue -> Maybe Text
metaValueToText
let getFile :: Text -> Text -> m ByteString
getFile defaultExtension :: Text
defaultExtension fp :: Text
fp = do
[String]
oldRp <- m [String]
forall (m :: * -> *). PandocMonad m => m [String]
getResourcePath
Maybe String
mbUdd <- m (Maybe String)
forall (m :: * -> *). PandocMonad m => m (Maybe String)
getUserDataDir
[String] -> m ()
forall (m :: * -> *). PandocMonad m => [String] -> m ()
setResourcePath ([String] -> m ()) -> [String] -> m ()
forall a b. (a -> b) -> a -> b
$ [String]
oldRp [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [String] -> (String -> [String]) -> Maybe String -> [String]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe []
(\u :: String
u -> [String
u String -> String -> String
forall a. Semigroup a => a -> a -> a
<> "/csl",
String
u String -> String -> String
forall a. Semigroup a => a -> a -> a
<> "/csl/dependent"]) Maybe String
mbUdd
let fp' :: Text
fp' = if (Char -> Bool) -> Text -> Bool
T.any (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
=='.') Text
fp Bool -> Bool -> Bool
|| "data:" Text -> Text -> Bool
`T.isPrefixOf` Text
fp
then Text
fp
else Text
fp Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
defaultExtension
(result :: ByteString
result, _) <- Text -> m (ByteString, Maybe Text)
forall (m :: * -> *).
PandocMonad m =>
Text -> m (ByteString, Maybe Text)
fetchItem Text
fp'
[String] -> m ()
forall (m :: * -> *). PandocMonad m => [String] -> m ()
setResourcePath [String]
oldRp
ByteString -> m ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
result
let getCslDefault :: m ByteString
getCslDefault = String -> m ByteString
forall (m :: * -> *). PandocMonad m => String -> m ByteString
readDataFile "default.csl"
Text
cslContents <- ByteString -> Text
UTF8.toText (ByteString -> Text) -> m ByteString -> m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m ByteString
-> (Text -> m ByteString) -> Maybe Text -> m ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe m ByteString
getCslDefault (Text -> Text -> m ByteString
forall (m :: * -> *). PandocMonad m => Text -> Text -> m ByteString
getFile ".csl") Maybe Text
cslfile
let abbrevFile :: Maybe Text
abbrevFile = Text -> Meta -> Maybe MetaValue
lookupMeta "citation-abbreviations" Meta
meta Maybe MetaValue -> (MetaValue -> Maybe Text) -> Maybe Text
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MetaValue -> Maybe Text
metaValueToText
Maybe Abbreviations
mbAbbrevs <- case Maybe Text
abbrevFile of
Nothing -> Maybe Abbreviations -> m (Maybe Abbreviations)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Abbreviations
forall a. Maybe a
Nothing
Just fp :: Text
fp -> do
ByteString
rawAbbr <- Text -> Text -> m ByteString
forall (m :: * -> *). PandocMonad m => Text -> Text -> m ByteString
getFile ".json" Text
fp
case ByteString -> Either String Abbreviations
forall a. FromJSON a => ByteString -> Either String a
eitherDecode (ByteString -> ByteString
L.fromStrict ByteString
rawAbbr) of
Left err :: String
err -> PandocError -> m (Maybe Abbreviations)
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (PandocError -> m (Maybe Abbreviations))
-> PandocError -> m (Maybe Abbreviations)
forall a b. (a -> b) -> a -> b
$ CiteprocError -> PandocError
PandocCiteprocError (CiteprocError -> PandocError) -> CiteprocError -> PandocError
forall a b. (a -> b) -> a -> b
$
Text -> CiteprocError
CiteprocParseError (Text -> CiteprocError) -> Text -> CiteprocError
forall a b. (a -> b) -> a -> b
$
"Could not parse abbreviations file " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
fp
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> "\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack String
err
Right abbr :: Abbreviations
abbr -> Maybe Abbreviations -> m (Maybe Abbreviations)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Abbreviations -> m (Maybe Abbreviations))
-> Maybe Abbreviations -> m (Maybe Abbreviations)
forall a b. (a -> b) -> a -> b
$ Abbreviations -> Maybe Abbreviations
forall a. a -> Maybe a
Just Abbreviations
abbr
let getParentStyle :: Text -> f Text
getParentStyle url :: Text
url = do
let basename :: Text
basename = (Char -> Bool) -> Text -> Text
T.takeWhileEnd (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/='/') Text
url
ByteString -> Text
UTF8.toText (ByteString -> Text) -> f ByteString -> f Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
f ByteString -> (PandocError -> f ByteString) -> f ByteString
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
catchError (Text -> Text -> f ByteString
forall (m :: * -> *). PandocMonad m => Text -> Text -> m ByteString
getFile ".csl" Text
basename) (\_ -> (ByteString, Maybe Text) -> ByteString
forall a b. (a, b) -> a
fst ((ByteString, Maybe Text) -> ByteString)
-> f (ByteString, Maybe Text) -> f ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> f (ByteString, Maybe Text)
forall (m :: * -> *).
PandocMonad m =>
Text -> m (ByteString, Maybe Text)
fetchItem Text
url)
Either CiteprocError (Style Inlines)
styleRes <- (Text -> m Text)
-> Text -> m (Either CiteprocError (Style Inlines))
forall (m :: * -> *) a.
Monad m =>
(Text -> m Text) -> Text -> m (Either CiteprocError (Style a))
Citeproc.parseStyle Text -> m Text
forall (f :: * -> *). PandocMonad f => Text -> f Text
getParentStyle Text
cslContents
case Either CiteprocError (Style Inlines)
styleRes of
Left err :: CiteprocError
err -> PandocError -> m (Style Inlines)
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (PandocError -> m (Style Inlines))
-> PandocError -> m (Style Inlines)
forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocAppError (Text -> PandocError) -> Text -> PandocError
forall a b. (a -> b) -> a -> b
$ CiteprocError -> Text
prettyCiteprocError CiteprocError
err
Right style :: Style Inlines
style -> Style Inlines -> m (Style Inlines)
forall (m :: * -> *) a. Monad m => a -> m a
return Style Inlines
style{ styleAbbreviations :: Maybe Abbreviations
styleAbbreviations = Maybe Abbreviations
mbAbbrevs }
getCiteprocLang :: PandocMonad m => Meta -> m (Maybe Lang)
getCiteprocLang :: Meta -> m (Maybe Lang)
getCiteprocLang meta :: Meta
meta = m (Maybe Lang)
-> (Text -> m (Maybe Lang)) -> Maybe Text -> m (Maybe Lang)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Maybe Lang -> m (Maybe Lang)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Lang
forall a. Maybe a
Nothing) Text -> m (Maybe Lang)
forall (m :: * -> *). PandocMonad m => Text -> m (Maybe Lang)
bcp47LangToIETF
((Text -> Meta -> Maybe MetaValue
lookupMeta "lang" Meta
meta Maybe MetaValue -> Maybe MetaValue -> Maybe MetaValue
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> Meta -> Maybe MetaValue
lookupMeta "locale" Meta
meta) Maybe MetaValue -> (MetaValue -> Maybe Text) -> Maybe Text
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MetaValue -> Maybe Text
metaValueToText)
getReferences :: PandocMonad m
=> Maybe Locale -> Pandoc -> m [Reference Inlines]
getReferences :: Maybe Locale -> Pandoc -> m [Reference Inlines]
getReferences mblocale :: Maybe Locale
mblocale (Pandoc meta :: Meta
meta bs :: [Block]
bs) = do
Locale
locale <- case Maybe Locale
mblocale of
Just l :: Locale
l -> Locale -> m Locale
forall (m :: * -> *) a. Monad m => a -> m a
return Locale
l
Nothing -> do
Maybe Lang
mblang <- Meta -> m (Maybe Lang)
forall (m :: * -> *). PandocMonad m => Meta -> m (Maybe Lang)
getCiteprocLang Meta
meta
case Maybe Lang
mblang of
Just lang :: Lang
lang -> Locale -> m Locale
forall (m :: * -> *) a. Monad m => a -> m a
return (Locale -> m Locale) -> Locale -> m Locale
forall a b. (a -> b) -> a -> b
$ (CiteprocError -> Locale)
-> (Locale -> Locale) -> Either CiteprocError Locale -> Locale
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either CiteprocError -> Locale
forall a. Monoid a => a
mempty Locale -> Locale
forall a. a -> a
id (Either CiteprocError Locale -> Locale)
-> Either CiteprocError Locale -> Locale
forall a b. (a -> b) -> a -> b
$ Lang -> Either CiteprocError Locale
getLocale Lang
lang
Nothing -> Locale -> m Locale
forall (m :: * -> *) a. Monad m => a -> m a
return Locale
forall a. Monoid a => a
mempty
let getCiteId :: Inline -> Set Text
getCiteId (Cite cs :: [Citation]
cs _) = [Text] -> Set Text
forall a. Ord a => [a] -> Set a
Set.fromList ([Text] -> Set Text) -> [Text] -> Set Text
forall a b. (a -> b) -> a -> b
$ (Citation -> Text) -> [Citation] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Citation -> Text
B.citationId [Citation]
cs
getCiteId _ = Set Text
forall a. Monoid a => a
mempty
let metanocites :: Maybe MetaValue
metanocites = Text -> Meta -> Maybe MetaValue
lookupMeta "nocite" Meta
meta
let nocites :: Set Text
nocites = Set Text -> (MetaValue -> Set Text) -> Maybe MetaValue -> Set Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Set Text
forall a. Monoid a => a
mempty ((Inline -> Set Text) -> MetaValue -> Set Text
forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
query Inline -> Set Text
getCiteId) Maybe MetaValue
metanocites
let citeIds :: Set Text
citeIds = (Inline -> Set Text) -> Pandoc -> Set Text
forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
query Inline -> Set Text
getCiteId (Meta -> [Block] -> Pandoc
Pandoc Meta
meta [Block]
bs)
let idpred :: Text -> Bool
idpred = if "*" Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set Text
nocites
then Bool -> Text -> Bool
forall a b. a -> b -> a
const Bool
True
else (Text -> Set Text -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set Text
citeIds)
let inlineRefs :: [Reference Inlines]
inlineRefs = case Text -> Meta -> Maybe MetaValue
lookupMeta "references" Meta
meta of
Just (MetaList rs :: [MetaValue]
rs) ->
(Reference Inlines -> Bool)
-> [Reference Inlines] -> [Reference Inlines]
forall a. (a -> Bool) -> [a] -> [a]
filter (Text -> Bool
idpred (Text -> Bool)
-> (Reference Inlines -> Text) -> Reference Inlines -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ItemId -> Text
unItemId (ItemId -> Text)
-> (Reference Inlines -> ItemId) -> Reference Inlines -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Reference Inlines -> ItemId
forall a. Reference a -> ItemId
referenceId)
([Reference Inlines] -> [Reference Inlines])
-> [Reference Inlines] -> [Reference Inlines]
forall a b. (a -> b) -> a -> b
$ (MetaValue -> Maybe (Reference Inlines))
-> [MetaValue] -> [Reference Inlines]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe MetaValue -> Maybe (Reference Inlines)
metaValueToReference [MetaValue]
rs
_ -> []
[Reference Inlines]
externalRefs <- case Text -> Meta -> Maybe MetaValue
lookupMeta "bibliography" Meta
meta of
Just (MetaList xs :: [MetaValue]
xs) ->
[[Reference Inlines]] -> [Reference Inlines]
forall a. Monoid a => [a] -> a
mconcat ([[Reference Inlines]] -> [Reference Inlines])
-> m [[Reference Inlines]] -> m [Reference Inlines]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
(Text -> m [Reference Inlines])
-> [Text] -> m [[Reference Inlines]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Locale -> (Text -> Bool) -> Text -> m [Reference Inlines]
forall (m :: * -> *).
PandocMonad m =>
Locale -> (Text -> Bool) -> Text -> m [Reference Inlines]
getRefsFromBib Locale
locale Text -> Bool
idpred)
((MetaValue -> Maybe Text) -> [MetaValue] -> [Text]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe MetaValue -> Maybe Text
metaValueToText [MetaValue]
xs)
Just x :: MetaValue
x ->
case MetaValue -> Maybe Text
metaValueToText MetaValue
x of
Just fp :: Text
fp -> Locale -> (Text -> Bool) -> Text -> m [Reference Inlines]
forall (m :: * -> *).
PandocMonad m =>
Locale -> (Text -> Bool) -> Text -> m [Reference Inlines]
getRefsFromBib Locale
locale Text -> Bool
idpred Text
fp
Nothing -> [Reference Inlines] -> m [Reference Inlines]
forall (m :: * -> *) a. Monad m => a -> m a
return []
Nothing -> [Reference Inlines] -> m [Reference Inlines]
forall (m :: * -> *) a. Monad m => a -> m a
return []
[Reference Inlines] -> m [Reference Inlines]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Reference Inlines] -> m [Reference Inlines])
-> [Reference Inlines] -> m [Reference Inlines]
forall a b. (a -> b) -> a -> b
$ (Reference Inlines -> Reference Inlines)
-> [Reference Inlines] -> [Reference Inlines]
forall a b. (a -> b) -> [a] -> [b]
map Reference Inlines -> Reference Inlines
legacyDateRanges ([Reference Inlines]
externalRefs [Reference Inlines] -> [Reference Inlines] -> [Reference Inlines]
forall a. [a] -> [a] -> [a]
++ [Reference Inlines]
inlineRefs)
insertSpace :: Inlines -> Inlines
insertSpace :: Inlines -> Inlines
insertSpace ils :: Inlines
ils =
case Seq Inline -> ViewL Inline
forall a. Seq a -> ViewL a
Seq.viewl (Inlines -> Seq Inline
forall a. Many a -> Seq a
unMany Inlines
ils) of
(Span ("",["csl-left-margin"],[]) xs :: [Inline]
xs) Seq.:< rest :: Seq Inline
rest ->
case Int -> Seq Inline -> Maybe Inline
forall a. Int -> Seq a -> Maybe a
Seq.lookup 0 Seq Inline
rest of
Just (Span ("",["csl-right-inline"],[]) _) ->
Seq Inline -> Inlines
forall a. Seq a -> Many a
Many (Seq Inline -> Inlines) -> Seq Inline -> Inlines
forall a b. (a -> b) -> a -> b
$
Attr -> [Inline] -> Inline
Span ("",["csl-left-margin"],[]) ([Inline]
xs [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ case [Inline] -> Maybe Inline
forall a. [a] -> Maybe a
lastMay [Inline]
xs of
Just Space -> []
_ -> [Inline
Space])
Inline -> Seq Inline -> Seq Inline
forall a. a -> Seq a -> Seq a
Seq.<| Seq Inline
rest
_ -> Inlines
ils
_ -> Inlines
ils
getRefsFromBib :: PandocMonad m
=> Locale -> (Text -> Bool) -> Text -> m [Reference Inlines]
getRefsFromBib :: Locale -> (Text -> Bool) -> Text -> m [Reference Inlines]
getRefsFromBib locale :: Locale
locale idpred :: Text -> Bool
idpred fp :: Text
fp = do
(raw :: ByteString
raw, _) <- Text -> m (ByteString, Maybe Text)
forall (m :: * -> *).
PandocMonad m =>
Text -> m (ByteString, Maybe Text)
fetchItem Text
fp
case String -> Maybe BibFormat
formatFromExtension (Text -> String
T.unpack Text
fp) of
Just f :: BibFormat
f -> Locale
-> BibFormat
-> (Text -> Bool)
-> Maybe Text
-> ByteString
-> m [Reference Inlines]
forall (m :: * -> *).
PandocMonad m =>
Locale
-> BibFormat
-> (Text -> Bool)
-> Maybe Text
-> ByteString
-> m [Reference Inlines]
getRefs Locale
locale BibFormat
f Text -> Bool
idpred (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
fp) ByteString
raw
Nothing -> PandocError -> m [Reference Inlines]
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (PandocError -> m [Reference Inlines])
-> PandocError -> m [Reference Inlines]
forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocAppError (Text -> PandocError) -> Text -> PandocError
forall a b. (a -> b) -> a -> b
$
"Could not determine bibliography format for " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
fp
getRefs :: PandocMonad m
=> Locale
-> BibFormat
-> (Text -> Bool)
-> Maybe Text
-> ByteString
-> m [Reference Inlines]
getRefs :: Locale
-> BibFormat
-> (Text -> Bool)
-> Maybe Text
-> ByteString
-> m [Reference Inlines]
getRefs locale :: Locale
locale format :: BibFormat
format idpred :: Text -> Bool
idpred mbfp :: Maybe Text
mbfp raw :: ByteString
raw = do
let err' :: Text -> m a
err' = PandocError -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (PandocError -> m a) -> (Text -> PandocError) -> Text -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Text -> Text -> PandocError
PandocBibliographyError (Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
forall a. Monoid a => a
mempty Maybe Text
mbfp)
case BibFormat
format of
Format_bibtex ->
(ParseError -> m [Reference Inlines])
-> ([Reference Inlines] -> m [Reference Inlines])
-> Either ParseError [Reference Inlines]
-> m [Reference Inlines]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Text -> m [Reference Inlines]
forall a. Text -> m a
err' (Text -> m [Reference Inlines])
-> (ParseError -> Text) -> ParseError -> m [Reference Inlines]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError -> Text
forall a. Show a => a -> Text
tshow) [Reference Inlines] -> m [Reference Inlines]
forall (m :: * -> *) a. Monad m => a -> m a
return (Either ParseError [Reference Inlines] -> m [Reference Inlines])
-> (ByteString -> Either ParseError [Reference Inlines])
-> ByteString
-> m [Reference Inlines]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Variant
-> Locale
-> (Text -> Bool)
-> Text
-> Either ParseError [Reference Inlines]
forall a.
ToSources a =>
Variant
-> Locale
-> (Text -> Bool)
-> a
-> Either ParseError [Reference Inlines]
readBibtexString Variant
Bibtex Locale
locale Text -> Bool
idpred (Text -> Either ParseError [Reference Inlines])
-> (ByteString -> Text)
-> ByteString
-> Either ParseError [Reference Inlines]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
UTF8.toText (ByteString -> m [Reference Inlines])
-> ByteString -> m [Reference Inlines]
forall a b. (a -> b) -> a -> b
$ ByteString
raw
Format_biblatex ->
(ParseError -> m [Reference Inlines])
-> ([Reference Inlines] -> m [Reference Inlines])
-> Either ParseError [Reference Inlines]
-> m [Reference Inlines]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Text -> m [Reference Inlines]
forall a. Text -> m a
err' (Text -> m [Reference Inlines])
-> (ParseError -> Text) -> ParseError -> m [Reference Inlines]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError -> Text
forall a. Show a => a -> Text
tshow) [Reference Inlines] -> m [Reference Inlines]
forall (m :: * -> *) a. Monad m => a -> m a
return (Either ParseError [Reference Inlines] -> m [Reference Inlines])
-> (ByteString -> Either ParseError [Reference Inlines])
-> ByteString
-> m [Reference Inlines]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Variant
-> Locale
-> (Text -> Bool)
-> Text
-> Either ParseError [Reference Inlines]
forall a.
ToSources a =>
Variant
-> Locale
-> (Text -> Bool)
-> a
-> Either ParseError [Reference Inlines]
readBibtexString Variant
Biblatex Locale
locale Text -> Bool
idpred (Text -> Either ParseError [Reference Inlines])
-> (ByteString -> Text)
-> ByteString
-> Either ParseError [Reference Inlines]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
UTF8.toText (ByteString -> m [Reference Inlines])
-> ByteString -> m [Reference Inlines]
forall a b. (a -> b) -> a -> b
$ ByteString
raw
Format_json ->
(String -> m [Reference Inlines])
-> ([Reference Inlines] -> m [Reference Inlines])
-> Either String [Reference Inlines]
-> m [Reference Inlines]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Text -> m [Reference Inlines]
forall a. Text -> m a
err' (Text -> m [Reference Inlines])
-> (String -> Text) -> String -> m [Reference Inlines]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack)
([Reference Inlines] -> m [Reference Inlines]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Reference Inlines] -> m [Reference Inlines])
-> ([Reference Inlines] -> [Reference Inlines])
-> [Reference Inlines]
-> m [Reference Inlines]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Reference Inlines -> Bool)
-> [Reference Inlines] -> [Reference Inlines]
forall a. (a -> Bool) -> [a] -> [a]
filter (Text -> Bool
idpred (Text -> Bool)
-> (Reference Inlines -> Text) -> Reference Inlines -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ItemId -> Text
unItemId (ItemId -> Text)
-> (Reference Inlines -> ItemId) -> Reference Inlines -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Reference Inlines -> ItemId
forall a. Reference a -> ItemId
referenceId)) (Either String [Reference Inlines] -> m [Reference Inlines])
-> (ByteString -> Either String [Reference Inlines])
-> ByteString
-> m [Reference Inlines]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
ByteString -> Either String [Reference Inlines]
cslJsonToReferences (ByteString -> m [Reference Inlines])
-> ByteString -> m [Reference Inlines]
forall a b. (a -> b) -> a -> b
$ ByteString
raw
Format_yaml -> do
[MetaValue]
rs <- (Text -> Bool)
-> ReaderOptions -> Maybe String -> ByteString -> m [MetaValue]
forall (m :: * -> *).
PandocMonad m =>
(Text -> Bool)
-> ReaderOptions -> Maybe String -> ByteString -> m [MetaValue]
yamlToRefs Text -> Bool
idpred
ReaderOptions
forall a. Default a => a
def{ readerExtensions :: Extensions
readerExtensions = Extensions
pandocExtensions }
(Text -> String
T.unpack (Text -> String) -> Maybe Text -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
mbfp)
ByteString
raw
[Reference Inlines] -> m [Reference Inlines]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Reference Inlines] -> m [Reference Inlines])
-> [Reference Inlines] -> m [Reference Inlines]
forall a b. (a -> b) -> a -> b
$ (MetaValue -> Maybe (Reference Inlines))
-> [MetaValue] -> [Reference Inlines]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe MetaValue -> Maybe (Reference Inlines)
metaValueToReference [MetaValue]
rs
insertResolvedCitations :: Inline -> State [Inlines] Inline
insertResolvedCitations :: Inline -> StateT [Inlines] Identity Inline
insertResolvedCitations (Cite cs :: [Citation]
cs ils :: [Inline]
ils) = do
[Inlines]
resolved <- StateT [Inlines] Identity [Inlines]
forall s (m :: * -> *). MonadState s m => m s
get
case [Inlines]
resolved of
[] -> Inline -> StateT [Inlines] Identity Inline
forall (m :: * -> *) a. Monad m => a -> m a
return ([Citation] -> [Inline] -> Inline
Cite [Citation]
cs [Inline]
ils)
(x :: Inlines
x:xs :: [Inlines]
xs) -> do
[Inlines] -> StateT [Inlines] Identity ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put [Inlines]
xs
Inline -> StateT [Inlines] Identity Inline
forall (m :: * -> *) a. Monad m => a -> m a
return (Inline -> StateT [Inlines] Identity Inline)
-> Inline -> StateT [Inlines] Identity Inline
forall a b. (a -> b) -> a -> b
$ [Citation] -> [Inline] -> Inline
Cite [Citation]
cs (Inlines -> [Inline]
forall a. Many a -> [a]
B.toList Inlines
x)
insertResolvedCitations x :: Inline
x = Inline -> StateT [Inlines] Identity Inline
forall (m :: * -> *) a. Monad m => a -> m a
return Inline
x
getCitations :: Locale
-> M.Map Text ItemId
-> Pandoc
-> [Citeproc.Citation Inlines]
getCitations :: Locale -> Map Text ItemId -> Pandoc -> [Citation Inlines]
getCitations locale :: Locale
locale otherIdsMap :: Map Text ItemId
otherIdsMap = Seq (Citation Inlines) -> [Citation Inlines]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList (Seq (Citation Inlines) -> [Citation Inlines])
-> (Pandoc -> Seq (Citation Inlines))
-> Pandoc
-> [Citation Inlines]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Inline -> Seq (Citation Inlines))
-> Pandoc -> Seq (Citation Inlines)
forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
query Inline -> Seq (Citation Inlines)
getCitation
where
getCitation :: Inline -> Seq (Citation Inlines)
getCitation (Cite cs :: [Citation]
cs _fallback :: [Inline]
_fallback) = Citation Inlines -> Seq (Citation Inlines)
forall a. a -> Seq a
Seq.singleton (Citation Inlines -> Seq (Citation Inlines))
-> Citation Inlines -> Seq (Citation Inlines)
forall a b. (a -> b) -> a -> b
$
$WCitation :: forall a. Maybe Text -> Maybe Int -> [CitationItem a] -> Citation a
Citeproc.Citation { citationId :: Maybe Text
Citeproc.citationId = Maybe Text
forall a. Maybe a
Nothing
, citationNoteNumber :: Maybe Int
Citeproc.citationNoteNumber =
case [Citation]
cs of
[] -> Maybe Int
forall a. Maybe a
Nothing
(Pandoc.Citation{ citationNoteNum :: Citation -> Int
Pandoc.citationNoteNum = Int
n }:
_) | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0 -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
n
| Bool
otherwise -> Maybe Int
forall a. Maybe a
Nothing
, citationItems :: [CitationItem Inlines]
Citeproc.citationItems =
Locale -> Map Text ItemId -> [Citation] -> [CitationItem Inlines]
fromPandocCitations Locale
locale Map Text ItemId
otherIdsMap [Citation]
cs
}
getCitation _ = Seq (Citation Inlines)
forall a. Monoid a => a
mempty
fromPandocCitations :: Locale
-> M.Map Text ItemId
-> [Pandoc.Citation]
-> [CitationItem Inlines]
fromPandocCitations :: Locale -> Map Text ItemId -> [Citation] -> [CitationItem Inlines]
fromPandocCitations locale :: Locale
locale otherIdsMap :: Map Text ItemId
otherIdsMap = (Citation -> [CitationItem Inlines])
-> [Citation] -> [CitationItem Inlines]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Citation -> [CitationItem Inlines]
go
where
locmap :: LocatorMap
locmap = Locale -> LocatorMap
toLocatorMap Locale
locale
go :: Citation -> [CitationItem Inlines]
go c :: Citation
c =
let (mblocinfo :: Maybe LocatorInfo
mblocinfo, suffix :: [Inline]
suffix) = LocatorMap -> [Inline] -> (Maybe LocatorInfo, [Inline])
parseLocator LocatorMap
locmap (Citation -> [Inline]
citationSuffix Citation
c)
cit :: CitationItem Inlines
cit = $WCitationItem :: forall a.
ItemId
-> Maybe Text
-> Maybe Text
-> CitationItemType
-> Maybe a
-> Maybe a
-> CitationItem a
CitationItem
{ citationItemId :: ItemId
citationItemId = ItemId -> Maybe ItemId -> ItemId
forall a. a -> Maybe a -> a
fromMaybe
(Text -> ItemId
ItemId (Text -> ItemId) -> Text -> ItemId
forall a b. (a -> b) -> a -> b
$ Citation -> Text
Pandoc.citationId Citation
c)
(Text -> Map Text ItemId -> Maybe ItemId
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup (Citation -> Text
Pandoc.citationId Citation
c) Map Text ItemId
otherIdsMap)
, citationItemLabel :: Maybe Text
citationItemLabel = LocatorInfo -> Text
locatorLabel (LocatorInfo -> Text) -> Maybe LocatorInfo -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe LocatorInfo
mblocinfo
, citationItemLocator :: Maybe Text
citationItemLocator = LocatorInfo -> Text
locatorLoc (LocatorInfo -> Text) -> Maybe LocatorInfo -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe LocatorInfo
mblocinfo
, citationItemType :: CitationItemType
citationItemType = CitationItemType
NormalCite
, citationItemPrefix :: Maybe Inlines
citationItemPrefix = case Citation -> [Inline]
citationPrefix Citation
c of
[] -> Maybe Inlines
forall a. Maybe a
Nothing
ils :: [Inline]
ils -> Inlines -> Maybe Inlines
forall a. a -> Maybe a
Just (Inlines -> Maybe Inlines) -> Inlines -> Maybe Inlines
forall a b. (a -> b) -> a -> b
$ [Inline] -> Inlines
forall a. [a] -> Many a
B.fromList [Inline]
ils Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<>
Inlines
B.space
, citationItemSuffix :: Maybe Inlines
citationItemSuffix = case [Inline]
suffix of
[] -> Maybe Inlines
forall a. Maybe a
Nothing
ils :: [Inline]
ils -> Inlines -> Maybe Inlines
forall a. a -> Maybe a
Just (Inlines -> Maybe Inlines) -> Inlines -> Maybe Inlines
forall a b. (a -> b) -> a -> b
$ [Inline] -> Inlines
forall a. [a] -> Many a
B.fromList [Inline]
ils
}
in if Citation -> Text
Pandoc.citationId Citation
c Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "*"
then []
else
case Citation -> CitationMode
citationMode Citation
c of
AuthorInText -> [ CitationItem Inlines
cit{ citationItemType :: CitationItemType
citationItemType = CitationItemType
AuthorOnly
, citationItemSuffix :: Maybe Inlines
citationItemSuffix = Maybe Inlines
forall a. Maybe a
Nothing }
, CitationItem Inlines
cit{ citationItemType :: CitationItemType
citationItemType =
CitationItemType
Citeproc.SuppressAuthor
, citationItemPrefix :: Maybe Inlines
citationItemPrefix = Maybe Inlines
forall a. Maybe a
Nothing } ]
NormalCitation -> [ CitationItem Inlines
cit ]
Pandoc.SuppressAuthor
-> [ CitationItem Inlines
cit{ citationItemType :: CitationItemType
citationItemType =
CitationItemType
Citeproc.SuppressAuthor } ]
data BibFormat =
Format_biblatex
| Format_bibtex
| Format_json
| Format_yaml
deriving (Int -> BibFormat -> String -> String
[BibFormat] -> String -> String
BibFormat -> String
(Int -> BibFormat -> String -> String)
-> (BibFormat -> String)
-> ([BibFormat] -> String -> String)
-> Show BibFormat
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
showList :: [BibFormat] -> String -> String
$cshowList :: [BibFormat] -> String -> String
show :: BibFormat -> String
$cshow :: BibFormat -> String
showsPrec :: Int -> BibFormat -> String -> String
$cshowsPrec :: Int -> BibFormat -> String -> String
Show, BibFormat -> BibFormat -> Bool
(BibFormat -> BibFormat -> Bool)
-> (BibFormat -> BibFormat -> Bool) -> Eq BibFormat
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BibFormat -> BibFormat -> Bool
$c/= :: BibFormat -> BibFormat -> Bool
== :: BibFormat -> BibFormat -> Bool
$c== :: BibFormat -> BibFormat -> Bool
Eq, Eq BibFormat
Eq BibFormat =>
(BibFormat -> BibFormat -> Ordering)
-> (BibFormat -> BibFormat -> Bool)
-> (BibFormat -> BibFormat -> Bool)
-> (BibFormat -> BibFormat -> Bool)
-> (BibFormat -> BibFormat -> Bool)
-> (BibFormat -> BibFormat -> BibFormat)
-> (BibFormat -> BibFormat -> BibFormat)
-> Ord BibFormat
BibFormat -> BibFormat -> Bool
BibFormat -> BibFormat -> Ordering
BibFormat -> BibFormat -> BibFormat
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: BibFormat -> BibFormat -> BibFormat
$cmin :: BibFormat -> BibFormat -> BibFormat
max :: BibFormat -> BibFormat -> BibFormat
$cmax :: BibFormat -> BibFormat -> BibFormat
>= :: BibFormat -> BibFormat -> Bool
$c>= :: BibFormat -> BibFormat -> Bool
> :: BibFormat -> BibFormat -> Bool
$c> :: BibFormat -> BibFormat -> Bool
<= :: BibFormat -> BibFormat -> Bool
$c<= :: BibFormat -> BibFormat -> Bool
< :: BibFormat -> BibFormat -> Bool
$c< :: BibFormat -> BibFormat -> Bool
compare :: BibFormat -> BibFormat -> Ordering
$ccompare :: BibFormat -> BibFormat -> Ordering
$cp1Ord :: Eq BibFormat
Ord)
formatFromExtension :: FilePath -> Maybe BibFormat
formatFromExtension :: String -> Maybe BibFormat
formatFromExtension fp :: String
fp = case (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== '.') (String -> String) -> String -> String
forall a b. (a -> b) -> a -> b
$ String -> String
takeExtension String
fp of
"biblatex" -> BibFormat -> Maybe BibFormat
forall a. a -> Maybe a
Just BibFormat
Format_biblatex
"bibtex" -> BibFormat -> Maybe BibFormat
forall a. a -> Maybe a
Just BibFormat
Format_bibtex
"bib" -> BibFormat -> Maybe BibFormat
forall a. a -> Maybe a
Just BibFormat
Format_biblatex
"json" -> BibFormat -> Maybe BibFormat
forall a. a -> Maybe a
Just BibFormat
Format_json
"yaml" -> BibFormat -> Maybe BibFormat
forall a. a -> Maybe a
Just BibFormat
Format_yaml
"yml" -> BibFormat -> Maybe BibFormat
forall a. a -> Maybe a
Just BibFormat
Format_yaml
_ -> Maybe BibFormat
forall a. Maybe a
Nothing
isNote :: Inline -> Bool
isNote :: Inline -> Bool
isNote (Cite _ [Note _]) = Bool
True
isNote (Cite _ [Superscript _]) = Bool
True
isNote _ = Bool
False
isSpacy :: Inline -> Bool
isSpacy :: Inline -> Bool
isSpacy Space = Bool
True
isSpacy SoftBreak = Bool
True
isSpacy _ = Bool
False
movePunctInsideQuotes :: Locale -> [Inline] -> [Inline]
movePunctInsideQuotes :: Locale -> [Inline] -> [Inline]
movePunctInsideQuotes locale :: Locale
locale
| Locale -> Maybe Bool
localePunctuationInQuote Locale
locale Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
= Inlines -> [Inline]
forall a. Many a -> [a]
B.toList (Inlines -> [Inline])
-> ([Inline] -> Inlines) -> [Inline] -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inlines -> Inlines
forall a. CiteprocOutput a => a -> a
movePunctuationInsideQuotes (Inlines -> Inlines)
-> ([Inline] -> Inlines) -> [Inline] -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Inlines
forall a. [a] -> Many a
B.fromList
| Bool
otherwise
= [Inline] -> [Inline]
forall a. a -> a
id
mvPunct :: Bool -> Locale -> [Inline] -> [Inline]
mvPunct :: Bool -> Locale -> [Inline] -> [Inline]
mvPunct moveNotes :: Bool
moveNotes locale :: Locale
locale (x :: Inline
x : xs :: [Inline]
xs)
| Inline -> Bool
isSpacy Inline
x = Inline
x Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: Bool -> Locale -> [Inline] -> [Inline]
mvPunct Bool
moveNotes Locale
locale [Inline]
xs
mvPunct moveNotes :: Bool
moveNotes locale :: Locale
locale (q :: Inline
q : s :: Inline
s : x :: Inline
x : ys :: [Inline]
ys)
| Inline -> Bool
isSpacy Inline
s
, Inline -> Bool
isNote Inline
x
= let spunct :: Text
spunct = (Char -> Bool) -> Text -> Text
T.takeWhile Char -> Bool
isPunctuation (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ [Inline] -> Text
forall a. Walkable Inline a => a -> Text
stringify [Inline]
ys
in if Bool
moveNotes
then if Text -> Bool
T.null Text
spunct
then Inline
q Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: Inline
x Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: Bool -> Locale -> [Inline] -> [Inline]
mvPunct Bool
moveNotes Locale
locale [Inline]
ys
else Locale -> [Inline] -> [Inline]
movePunctInsideQuotes Locale
locale
[Inline
q , Text -> Inline
Str Text
spunct , Inline
x] [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ Bool -> Locale -> [Inline] -> [Inline]
mvPunct Bool
moveNotes Locale
locale
(Inlines -> [Inline]
forall a. Many a -> [a]
B.toList
((Char -> Bool) -> Inlines -> Inlines
forall a. CiteprocOutput a => (Char -> Bool) -> a -> a
dropTextWhile Char -> Bool
isPunctuation ([Inline] -> Inlines
forall a. [a] -> Many a
B.fromList [Inline]
ys)))
else Inline
q Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: Inline
x Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: Bool -> Locale -> [Inline] -> [Inline]
mvPunct Bool
moveNotes Locale
locale [Inline]
ys
mvPunct moveNotes :: Bool
moveNotes locale :: Locale
locale (Cite cs :: [Citation]
cs ils :: [Inline]
ils : ys :: [Inline]
ys)
| Bool -> Bool
not ([Inline] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
ils)
, Inline -> Bool
isNote ([Inline] -> Inline
forall a. [a] -> a
last [Inline]
ils)
, [Inline] -> Bool
startWithPunct [Inline]
ys
, Bool
moveNotes
= let s :: Text
s = [Inline] -> Text
forall a. Walkable Inline a => a -> Text
stringify [Inline]
ys
spunct :: Text
spunct = (Char -> Bool) -> Text -> Text
T.takeWhile Char -> Bool
isPunctuation Text
s
in [Citation] -> [Inline] -> Inline
Cite [Citation]
cs (Locale -> [Inline] -> [Inline]
movePunctInsideQuotes Locale
locale ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall a b. (a -> b) -> a -> b
$
[Inline] -> [Inline]
forall a. [a] -> [a]
init [Inline]
ils
[Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Text -> Inline
Str Text
spunct | Bool -> Bool
not (Bool -> [Inline] -> Bool
endWithPunct Bool
False ([Inline] -> [Inline]
forall a. [a] -> [a]
init [Inline]
ils))]
[Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [[Inline] -> Inline
forall a. [a] -> a
last [Inline]
ils]) Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:
Bool -> Locale -> [Inline] -> [Inline]
mvPunct Bool
moveNotes Locale
locale
(Inlines -> [Inline]
forall a. Many a -> [a]
B.toList ((Char -> Bool) -> Inlines -> Inlines
forall a. CiteprocOutput a => (Char -> Bool) -> a -> a
dropTextWhile Char -> Bool
isPunctuation ([Inline] -> Inlines
forall a. [a] -> Many a
B.fromList [Inline]
ys)))
mvPunct moveNotes :: Bool
moveNotes locale :: Locale
locale (s :: Inline
s : x :: Inline
x : ys :: [Inline]
ys) | Inline -> Bool
isSpacy Inline
s, Inline -> Bool
isNote Inline
x =
Inline
x Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: Bool -> Locale -> [Inline] -> [Inline]
mvPunct Bool
moveNotes Locale
locale [Inline]
ys
mvPunct moveNotes :: Bool
moveNotes locale :: Locale
locale (s :: Inline
s : x :: Inline
x@(Cite _ (Superscript _ : _)) : ys :: [Inline]
ys)
| Inline -> Bool
isSpacy Inline
s = Inline
x Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: Bool -> Locale -> [Inline] -> [Inline]
mvPunct Bool
moveNotes Locale
locale [Inline]
ys
mvPunct moveNotes :: Bool
moveNotes locale :: Locale
locale (Cite cs :: [Citation]
cs ils :: [Inline]
ils : Str "." : ys :: [Inline]
ys)
| "." Text -> Text -> Bool
`T.isSuffixOf` ([Inline] -> Text
forall a. Walkable Inline a => a -> Text
stringify [Inline]
ils)
= [Citation] -> [Inline] -> Inline
Cite [Citation]
cs [Inline]
ils Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: Bool -> Locale -> [Inline] -> [Inline]
mvPunct Bool
moveNotes Locale
locale [Inline]
ys
mvPunct moveNotes :: Bool
moveNotes locale :: Locale
locale (x :: Inline
x:xs :: [Inline]
xs) = Inline
x Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: Bool -> Locale -> [Inline] -> [Inline]
mvPunct Bool
moveNotes Locale
locale [Inline]
xs
mvPunct _ _ [] = []
endWithPunct :: Bool -> [Inline] -> Bool
endWithPunct :: Bool -> [Inline] -> Bool
endWithPunct _ [] = Bool
False
endWithPunct onlyFinal :: Bool
onlyFinal xs :: [Inline]
xs@(_:_) =
case String -> String
forall a. [a] -> [a]
reverse (Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ [Inline] -> Text
forall a. Walkable Inline a => a -> Text
stringify [Inline]
xs) of
[] -> Bool
True
(d :: Char
d:c :: Char
c:_) | Char -> Bool
isPunctuation Char
d
Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
onlyFinal
Bool -> Bool -> Bool
&& Char -> Bool
isEndPunct Char
c -> Bool
True
(c :: Char
c:_) | Char -> Bool
isEndPunct Char
c -> Bool
True
| Bool
otherwise -> Bool
False
where isEndPunct :: Char -> Bool
isEndPunct c :: Char
c = Char
c Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (".,;:!?" :: String)
startWithPunct :: [Inline] -> Bool
startWithPunct :: [Inline] -> Bool
startWithPunct ils :: [Inline]
ils =
case Text -> Maybe (Char, Text)
T.uncons ([Inline] -> Text
forall a. Walkable Inline a => a -> Text
stringify [Inline]
ils) of
Just (c :: Char
c,_) -> Char
c Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (".,;:!?" :: [Char])
Nothing -> Bool
False
truish :: MetaValue -> Bool
truish :: MetaValue -> Bool
truish (MetaBool t :: Bool
t) = Bool
t
truish (MetaString s :: Text
s) = Text -> Bool
isYesValue (Text -> Text
T.toLower Text
s)
truish (MetaInlines ils :: [Inline]
ils) = Text -> Bool
isYesValue (Text -> Text
T.toLower ([Inline] -> Text
forall a. Walkable Inline a => a -> Text
stringify [Inline]
ils))
truish (MetaBlocks [Plain ils :: [Inline]
ils]) = Text -> Bool
isYesValue (Text -> Text
T.toLower ([Inline] -> Text
forall a. Walkable Inline a => a -> Text
stringify [Inline]
ils))
truish _ = Bool
False
isYesValue :: Text -> Bool
isYesValue :: Text -> Bool
isYesValue "t" = Bool
True
isYesValue "true" = Bool
True
isYesValue "yes" = Bool
True
isYesValue _ = Bool
False
insertRefs :: [(Text,Text)] -> [Text] -> Meta -> [Block] -> [Block] -> [Block]
insertRefs :: [(Text, Text)] -> [Text] -> Meta -> [Block] -> [Block] -> [Block]
insertRefs _ _ _ [] bs :: [Block]
bs = [Block]
bs
insertRefs refkvs :: [(Text, Text)]
refkvs refclasses :: [Text]
refclasses meta :: Meta
meta refs :: [Block]
refs bs :: [Block]
bs =
if Meta -> Bool
isRefRemove Meta
meta
then [Block]
bs
else case State Bool [Block] -> Bool -> ([Block], Bool)
forall s a. State s a -> s -> (a, s)
runState ((Block -> StateT Bool Identity Block)
-> [Block] -> State Bool [Block]
forall a b (m :: * -> *).
(Walkable a b, Monad m, Applicative m, Functor m) =>
(a -> m a) -> b -> m b
walkM Block -> StateT Bool Identity Block
go [Block]
bs) Bool
False of
(bs' :: [Block]
bs', True) -> [Block]
bs'
(_, False)
-> case Meta -> Maybe [Inline]
refTitle Meta
meta of
Nothing ->
case [Block] -> [Block]
forall a. [a] -> [a]
reverse [Block]
bs of
Header lev :: Int
lev (id' :: Text
id',classes :: [Text]
classes,kvs :: [(Text, Text)]
kvs) ys :: [Inline]
ys : xs :: [Block]
xs ->
[Block] -> [Block]
forall a. [a] -> [a]
reverse [Block]
xs [Block] -> [Block] -> [Block]
forall a. [a] -> [a] -> [a]
++
[Int -> Attr -> [Inline] -> Block
Header Int
lev (Text
id',[Text] -> [Text]
forall a. (IsString a, Eq a) => [a] -> [a]
addUnNumbered [Text]
classes,[(Text, Text)]
kvs) [Inline]
ys,
Attr -> [Block] -> Block
Div ("refs",[Text]
refclasses,[(Text, Text)]
refkvs) [Block]
refs]
_ -> [Block]
bs [Block] -> [Block] -> [Block]
forall a. [a] -> [a] -> [a]
++ [Block
refDiv]
Just ils :: [Inline]
ils -> [Block]
bs [Block] -> [Block] -> [Block]
forall a. [a] -> [a] -> [a]
++
[Int -> Attr -> [Inline] -> Block
Header 1 ("bibliography", ["unnumbered"], []) [Inline]
ils,
Block
refDiv]
where
refDiv :: Block
refDiv = Attr -> [Block] -> Block
Div ("refs", [Text]
refclasses, [(Text, Text)]
refkvs) [Block]
refs
addUnNumbered :: [a] -> [a]
addUnNumbered cs :: [a]
cs = "unnumbered" a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a
c | a
c <- [a]
cs, a
c a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= "unnumbered"]
go :: Block -> State Bool Block
go :: Block -> StateT Bool Identity Block
go (Div ("refs",cs :: [Text]
cs,kvs :: [(Text, Text)]
kvs) xs :: [Block]
xs) = do
Bool -> StateT Bool Identity ()
forall s (m :: * -> *). MonadState s m => s -> m ()
put Bool
True
let cs' :: [Text]
cs' = [Text] -> [Text]
forall a. Ord a => [a] -> [a]
ordNub ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ [Text]
cs [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text]
refclasses
let kvs' :: [(Text, Text)]
kvs' = [(Text, Text)] -> [(Text, Text)]
forall a. Ord a => [a] -> [a]
ordNub ([(Text, Text)] -> [(Text, Text)])
-> [(Text, Text)] -> [(Text, Text)]
forall a b. (a -> b) -> a -> b
$ [(Text, Text)]
kvs [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall a. [a] -> [a] -> [a]
++ [(Text, Text)]
refkvs
Block -> StateT Bool Identity Block
forall (m :: * -> *) a. Monad m => a -> m a
return (Block -> StateT Bool Identity Block)
-> Block -> StateT Bool Identity Block
forall a b. (a -> b) -> a -> b
$ Attr -> [Block] -> Block
Div ("refs",[Text]
cs',[(Text, Text)]
kvs') ([Block]
xs [Block] -> [Block] -> [Block]
forall a. [a] -> [a] -> [a]
++ [Block]
refs)
go x :: Block
x = Block -> StateT Bool Identity Block
forall (m :: * -> *) a. Monad m => a -> m a
return Block
x
refTitle :: Meta -> Maybe [Inline]
refTitle :: Meta -> Maybe [Inline]
refTitle meta :: Meta
meta =
case Text -> Meta -> Maybe MetaValue
lookupMeta "reference-section-title" Meta
meta of
Just (MetaString s :: Text
s) -> [Inline] -> Maybe [Inline]
forall a. a -> Maybe a
Just [Text -> Inline
Str Text
s]
Just (MetaInlines ils :: [Inline]
ils) -> [Inline] -> Maybe [Inline]
forall a. a -> Maybe a
Just [Inline]
ils
Just (MetaBlocks [Plain ils :: [Inline]
ils]) -> [Inline] -> Maybe [Inline]
forall a. a -> Maybe a
Just [Inline]
ils
Just (MetaBlocks [Para ils :: [Inline]
ils]) -> [Inline] -> Maybe [Inline]
forall a. a -> Maybe a
Just [Inline]
ils
_ -> Maybe [Inline]
forall a. Maybe a
Nothing
isRefRemove :: Meta -> Bool
isRefRemove :: Meta -> Bool
isRefRemove meta :: Meta
meta =
Bool -> (MetaValue -> Bool) -> Maybe MetaValue -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False MetaValue -> Bool
truish (Maybe MetaValue -> Bool) -> Maybe MetaValue -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Meta -> Maybe MetaValue
lookupMeta "suppress-bibliography" Meta
meta
legacyDateRanges :: Reference Inlines -> Reference Inlines
legacyDateRanges :: Reference Inlines -> Reference Inlines
legacyDateRanges ref :: Reference Inlines
ref =
Reference Inlines
ref{ referenceVariables :: Map Variable (Val Inlines)
referenceVariables = (Val Inlines -> Val Inlines)
-> Map Variable (Val Inlines) -> Map Variable (Val Inlines)
forall a b k. (a -> b) -> Map k a -> Map k b
M.map Val Inlines -> Val Inlines
forall a. Val a -> Val a
go (Map Variable (Val Inlines) -> Map Variable (Val Inlines))
-> Map Variable (Val Inlines) -> Map Variable (Val Inlines)
forall a b. (a -> b) -> a -> b
$ Reference Inlines -> Map Variable (Val Inlines)
forall a. Reference a -> Map Variable (Val a)
referenceVariables Reference Inlines
ref }
where
go :: Val a -> Val a
go (DateVal d :: Date
d)
| [DateParts] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Date -> [DateParts]
dateParts Date
d)
, Just lit :: Text
lit <- Date -> Maybe Text
dateLiteral Date
d
= case Text -> Text -> [Text]
T.splitOn "_" Text
lit of
[x :: Text
x,y :: Text
y] -> case Text -> Maybe Date
Citeproc.rawDateEDTF (Text
x Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> "/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
y) of
Just d' :: Date
d' -> Date -> Val a
forall a. Date -> Val a
DateVal Date
d'
Nothing -> Date -> Val a
forall a. Date -> Val a
DateVal Date
d
_ -> Date -> Val a
forall a. Date -> Val a
DateVal Date
d
go x :: Val a
x = Val a
x
extractText :: Val Inlines -> Text
(TextVal x :: Text
x) = Text
x
extractText (FancyVal x :: Inlines
x) = Inlines -> Text
forall a. CiteprocOutput a => a -> Text
toText Inlines
x
extractText (NumVal n :: Int
n) = String -> Text
T.pack (Int -> String
forall a. Show a => a -> String
show Int
n)
extractText _ = Text
forall a. Monoid a => a
mempty
addNote :: Inline -> Inline
addNote :: Inline -> Inline
addNote (Span ("",["csl-note"],[]) ils :: [Inline]
ils) =
[Block] -> Inline
Note [[Inline] -> Block
Para ([Inline] -> Block) -> [Inline] -> Block
forall a b. (a -> b) -> a -> b
$
Inlines -> [Inline]
forall a. Many a -> [a]
B.toList (Inlines -> [Inline])
-> ([Inline] -> Inlines) -> [Inline] -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Lang -> TextCase -> Inlines -> Inlines
forall a. CiteprocOutput a => Maybe Lang -> TextCase -> a -> a
addTextCase Maybe Lang
forall a. Maybe a
Nothing TextCase
CapitalizeFirst (Inlines -> Inlines)
-> ([Inline] -> Inlines) -> [Inline] -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Inlines
forall a. [a] -> Many a
B.fromList ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall a b. (a -> b) -> a -> b
$ [Inline]
ils]
addNote x :: Inline
x = Inline
x
deNote :: Inline -> Inline
deNote :: Inline -> Inline
deNote (Note bs :: [Block]
bs) =
case [Block]
bs of
[Para (cit :: Inline
cit@(Cite (c :: Citation
c:_) _) : ils :: [Inline]
ils)]
| Citation -> CitationMode
citationMode Citation
c CitationMode -> CitationMode -> Bool
forall a. Eq a => a -> a -> Bool
/= CitationMode
AuthorInText ->
[Block] -> Inline
Note [[Inline] -> Block
Para ((Inline -> Inline) -> [Inline] -> [Inline]
forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
removeNotes ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall a b. (a -> b) -> a -> b
$ Inline
cit Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall a b. Walkable a b => (a -> a) -> b -> b
walk [Inline] -> [Inline]
addParens [Inline]
ils)]
_ -> [Block] -> Inline
Note ((Inline -> Inline) -> [Block] -> [Block]
forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
removeNotes ([Block] -> [Block]) -> ([Block] -> [Block]) -> [Block] -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Inline] -> [Inline]) -> [Block] -> [Block]
forall a b. Walkable a b => (a -> a) -> b -> b
walk [Inline] -> [Inline]
addParens ([Block] -> [Block]) -> [Block] -> [Block]
forall a b. (a -> b) -> a -> b
$ [Block]
bs)
where
addParens :: [Inline] -> [Inline]
addParens [] = []
addParens (Cite (c :: Citation
c:cs :: [Citation]
cs) ils :: [Inline]
ils : zs :: [Inline]
zs)
| Citation -> CitationMode
citationMode Citation
c CitationMode -> CitationMode -> Bool
forall a. Eq a => a -> a -> Bool
== CitationMode
AuthorInText
= [Citation] -> [Inline] -> Inline
Cite (Citation
cCitation -> [Citation] -> [Citation]
forall a. a -> [a] -> [a]
:[Citation]
cs) (Bool -> [Inline] -> [Inline]
addCommas ([Inline] -> Bool
needsPeriod [Inline]
zs) [Inline]
ils) Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:
[Inline] -> [Inline]
addParens [Inline]
zs
| Bool
otherwise
= [Citation] -> [Inline] -> Inline
Cite (Citation
cCitation -> [Citation] -> [Citation]
forall a. a -> [a] -> [a]
:[Citation]
cs) ((Inline -> [Inline]) -> [Inline] -> [Inline]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Inline -> [Inline]
noteInParens [Inline]
ils) Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline] -> [Inline]
addParens [Inline]
zs
addParens (x :: Inline
x:xs :: [Inline]
xs) = Inline
x Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline] -> [Inline]
addParens [Inline]
xs
removeNotes :: Inline -> Inline
removeNotes (Span ("",["csl-note"],[]) ils :: [Inline]
ils) = Attr -> [Inline] -> Inline
Span ("",[],[]) [Inline]
ils
removeNotes x :: Inline
x = Inline
x
needsPeriod :: [Inline] -> Bool
needsPeriod [] = Bool
True
needsPeriod (Str t :: Text
t:_) = case Text -> Maybe (Char, Text)
T.uncons Text
t of
Nothing -> Bool
False
Just (c :: Char
c,_) -> Char -> Bool
isUpper Char
c
needsPeriod (Space:zs :: [Inline]
zs) = [Inline] -> Bool
needsPeriod [Inline]
zs
needsPeriod _ = Bool
False
noteInParens :: Inline -> [Inline]
noteInParens (Span ("",["csl-note"],[]) ils :: [Inline]
ils)
= Inline
Space Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: Text -> Inline
Str "(" Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:
[Inline] -> [Inline]
removeFinalPeriod [Inline]
ils [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Text -> Inline
Str ")"]
noteInParens x :: Inline
x = [Inline
x]
addCommas :: Bool -> [Inline] -> [Inline]
addCommas = Bool -> Bool -> [Inline] -> [Inline]
addCommas' Bool
True
addCommas' :: Bool -> Bool -> [Inline] -> [Inline]
addCommas' _ _ [] = []
addCommas' atBeginning :: Bool
atBeginning needsPer :: Bool
needsPer
(Span ("",["csl-note"],[]) ils :: [Inline]
ils : rest :: [Inline]
rest)
| Bool -> Bool
not ([Inline] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
ils)
= (if Bool
atBeginning then [Inline] -> [Inline]
forall a. a -> a
id else ([Text -> Inline
Str "," , Inline
Space] [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++)) ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall a b. (a -> b) -> a -> b
$
(if Bool
needsPer then [Inline]
ils else [Inline] -> [Inline]
removeFinalPeriod [Inline]
ils) [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++
Bool -> Bool -> [Inline] -> [Inline]
addCommas' Bool
False Bool
needsPer [Inline]
rest
addCommas' _ needsPer :: Bool
needsPer (il :: Inline
il : rest :: [Inline]
rest) = Inline
il Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: Bool -> Bool -> [Inline] -> [Inline]
addCommas' Bool
False Bool
needsPer [Inline]
rest
deNote x :: Inline
x = Inline
x
removeFinalPeriod :: [Inline] -> [Inline]
removeFinalPeriod :: [Inline] -> [Inline]
removeFinalPeriod ils :: [Inline]
ils =
case [Inline] -> Maybe Inline
forall a. [a] -> Maybe a
lastMay [Inline]
ils of
Just (Span attr :: Attr
attr ils' :: [Inline]
ils')
-> [Inline] -> [Inline]
forall a. [a] -> [a]
initSafe [Inline]
ils [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Attr -> [Inline] -> Inline
Span Attr
attr ([Inline] -> [Inline]
removeFinalPeriod [Inline]
ils')]
Just (Emph ils' :: [Inline]
ils')
-> [Inline] -> [Inline]
forall a. [a] -> [a]
initSafe [Inline]
ils [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [[Inline] -> Inline
Emph ([Inline] -> [Inline]
removeFinalPeriod [Inline]
ils')]
Just (Strong ils' :: [Inline]
ils')
-> [Inline] -> [Inline]
forall a. [a] -> [a]
initSafe [Inline]
ils [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [[Inline] -> Inline
Strong ([Inline] -> [Inline]
removeFinalPeriod [Inline]
ils')]
Just (SmallCaps ils' :: [Inline]
ils')
-> [Inline] -> [Inline]
forall a. [a] -> [a]
initSafe [Inline]
ils [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [[Inline] -> Inline
SmallCaps ([Inline] -> [Inline]
removeFinalPeriod [Inline]
ils')]
Just (Str t :: Text
t)
| Int -> Text -> Text
T.takeEnd 1 Text
t Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== "." -> [Inline] -> [Inline]
forall a. [a] -> [a]
initSafe [Inline]
ils [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Text -> Inline
Str (Int -> Text -> Text
T.dropEnd 1 Text
t)]
| Text -> Bool
forall a. (Eq a, IsString a) => a -> Bool
isRightQuote (Int -> Text -> Text
T.takeEnd 1 Text
t)
-> [Inline] -> [Inline]
removeFinalPeriod
([Inline] -> [Inline]
forall a. [a] -> [a]
initSafe [Inline]
ils [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Text -> Inline
Str Text
tInit | Bool -> Bool
not (Text -> Bool
T.null Text
tInit)]) [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Text -> Inline
Str Text
tEnd]
where
tEnd :: Text
tEnd = Int -> Text -> Text
T.takeEnd 1 Text
t
tInit :: Text
tInit = Int -> Text -> Text
T.dropEnd 1 Text
t
_ -> [Inline]
ils
where
isRightQuote :: a -> Bool
isRightQuote "\8221" = Bool
True
isRightQuote "\8217" = Bool
True
isRightQuote "\187" = Bool
True
isRightQuote _ = Bool
False
bcp47LangToIETF :: PandocMonad m => Text -> m (Maybe Lang)
bcp47LangToIETF :: Text -> m (Maybe Lang)
bcp47LangToIETF bcplang :: Text
bcplang =
case Text -> Either String Lang
parseLang Text
bcplang of
Left _ -> do
LogMessage -> m ()
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report (LogMessage -> m ()) -> LogMessage -> m ()
forall a b. (a -> b) -> a -> b
$ Text -> LogMessage
InvalidLang Text
bcplang
Maybe Lang -> m (Maybe Lang)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Lang
forall a. Maybe a
Nothing
Right lang :: Lang
lang -> Maybe Lang -> m (Maybe Lang)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Lang -> m (Maybe Lang)) -> Maybe Lang -> m (Maybe Lang)
forall a b. (a -> b) -> a -> b
$ Lang -> Maybe Lang
forall a. a -> Maybe a
Just Lang
lang