git.iko.soy/typeable/pandoc
on master help sign in
T.P.Citeproc: refactor and export `getReferences`.

See #7016.

commit15e33b33b4df67f989062a356e009ffc596bbc32 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 10 Jan 2021, 10:15 (6 years ago) parentfe13782
1 file changed+51−28
src/Text/Pandoc/Citeproc.hs+51−28
src/Text/Pandoc/Citeproc.hs+51−28
@@ -5,7 +5,9 @@
55{-# LANGUAGE FlexibleInstances #-}
66{-# LANGUAGE OverloadedStrings #-}
77module Text.Pandoc.Citeproc
8− ( processCitations )
8+ ( processCitations,
9+ getReferences
10+ )
911where
1012
1113import Citeproc
@@ -46,7 +48,7 @@ import Safe (lastMay, initSafe)
4648-- import Debug.Trace as Trace (trace, traceShowId)
4749
4850
49−processCitations :: PandocMonad m => Pandoc -> m Pandoc
51+processCitations :: PandocMonad m => Pandoc -> m Pandoc
5052processCitations (Pandoc meta bs) = do
5153 let cslfile = (lookupMeta "csl" meta <|> lookupMeta "citation-style" meta)
5254 >>= metaValueToText
@@ -96,31 +98,9 @@ processCitations (Pandoc meta bs) = do
9698 ((lookupMeta "lang" meta <|> lookupMeta "locale" meta) >>=
9799 metaValueToText)
98100 let locale = Citeproc.mergeLocales mblang style
99− let getCiteId (Cite cs _) = Set.fromList $ map B.citationId cs
100− getCiteId _ = mempty
101− let metanocites = lookupMeta "nocite" meta
102− let meta' = deleteMeta "nocite" meta
103− let nocites = maybe mempty (query getCiteId) metanocites
104− let citeIds = query getCiteId (Pandoc meta bs)
105− let idpred = if "*" `Set.member` nocites
106− then const True
107− else (`Set.member` citeIds)
108− let inlineRefs = case lookupMeta "references" meta of
109− Just (MetaList rs) -> mapMaybe metaValueToReference rs
110− _ -> []
111− externalRefs <- case lookupMeta "bibliography" meta of
112− Just (MetaList xs) ->
113− mconcat <$>
114− mapM (getRefsFromBib locale idpred)
115− (mapMaybe metaValueToText xs)
116− Just x ->
117− case metaValueToText x of
118− Just fp -> getRefsFromBib locale idpred fp
119− Nothing -> return []
120− Nothing -> return []
121− let refs = map (linkifyVariables . legacyDateRanges)
122− (externalRefs ++ inlineRefs)
123− -- note that inlineRefs can override externalRefs
101+
102+ refs <- getReferences (Just locale) (Pandoc meta bs)
103+
124104 let otherIdsMap = foldr (\ref m ->
125105 case T.words . extractText <$>
126106 M.lookup "other-ids"
@@ -130,8 +110,10 @@ processCitations (Pandoc meta bs) = do
130110 (\id' ->
131111 M.insert id' (referenceId ref)) m ids)
132112 M.empty refs
133− -- TODO: issue warning if no refs defined
113+ let meta' = deleteMeta "nocite" meta
134114 let citations = getCitations locale otherIdsMap $ Pandoc meta' bs
115+
116+
135117 let linkCites = maybe False truish $ lookupMeta "link-citations" meta
136118 let opts = defaultCiteprocOptions{ linkCitations = linkCites }
137119 let result = Citeproc.citeproc opts style (localeLanguage locale)
@@ -161,6 +143,7 @@ processCitations (Pandoc meta bs) = do
161143 B.toList . movePunctuationInsideQuotes . B.fromList
162144 _ -> id
163145
146+ let metanocites = lookupMeta "nocite" meta
164147 let Pandoc meta'' bs' =
165148 maybe id (setMeta "nocite") metanocites .
166149 walk (map capitalizeNoteCitation .
@@ -172,6 +155,46 @@ processCitations (Pandoc meta bs) = do
172155 $ insertRefs refkvs classes meta''
173156 (walk fixLinks $ B.toList bibs) bs'
174157
158+-- | Get references defined inline in the metadata and via an external
159+-- bibliography. Only references that are actually cited in the document
160+-- (either with a genuine citation or with `nocite`) are returned.
161+-- URL variables are converted to links.
162+getReferences :: PandocMonad m
163+ => Maybe Locale -> Pandoc -> m [Reference Inlines]
164+getReferences mblocale (Pandoc meta bs) = do
165+ let lang = maybe (Lang "en" (Just "US")) (parseLang . stringify) $
166+ lookupMeta "lang" meta
167+ let locale = case mblocale of
168+ Just l -> l
169+ Nothing -> either mempty id $ getLocale lang
170+
171+ let getCiteId (Cite cs _) = Set.fromList $ map B.citationId cs
172+ getCiteId _ = mempty
173+ let metanocites = lookupMeta "nocite" meta
174+ let nocites = maybe mempty (query getCiteId) metanocites
175+ let citeIds = query getCiteId (Pandoc meta bs)
176+ let idpred = if "*" `Set.member` nocites
177+ then const True
178+ else (`Set.member` citeIds)
179+ let inlineRefs = case lookupMeta "references" meta of
180+ Just (MetaList rs) -> mapMaybe metaValueToReference rs
181+ _ -> []
182+ externalRefs <- case lookupMeta "bibliography" meta of
183+ Just (MetaList xs) ->
184+ mconcat <$>
185+ mapM (getRefsFromBib locale idpred)
186+ (mapMaybe metaValueToText xs)
187+ Just x ->
188+ case metaValueToText x of
189+ Just fp -> getRefsFromBib locale idpred fp
190+ Nothing -> return []
191+ Nothing -> return []
192+ return $ map (linkifyVariables . legacyDateRanges)
193+ (externalRefs ++ inlineRefs)
194+ -- note that inlineRefs can override externalRefs
195+
196+
197+
175198-- If we have a span.csl-left-margin followed by span.csl-right-inline,
176199-- we insert a space. This ensures that they will be separated by a space,
177200-- even in formats that don't have special handling for the display spans.