From 665e6d3d9438dabf2634aa6bdd694555122903d3 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Thu, 23 Sep 2021 22:21:05 -0700
Subject: [PATCH] BibTeX parser: fix expansion of special strings in series...

e.g. `newseries` or `library`.  Expansion should not happen
when these strings are protected in braces, or when they're
capitalized.

Closes #7591.
---
 src/Text/Pandoc/Citeproc/BibTeX.hs | 17 ++++++++++-------
 test/command/biblatex-article.md   |  2 +-
 test/command/biblatex-gillies.md   |  2 +-
 test/command/biblatex-shore.md     |  2 +-
 4 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/src/Text/Pandoc/Citeproc/BibTeX.hs b/src/Text/Pandoc/Citeproc/BibTeX.hs
index c178de6e9..13baabccb 100644
--- a/src/Text/Pandoc/Citeproc/BibTeX.hs
+++ b/src/Text/Pandoc/Citeproc/BibTeX.hs
@@ -54,7 +54,6 @@ import           Safe                   (readMay)
 import           Text.Printf            (printf)
 import           Text.DocLayout         (literal, hsep, nest, hang, Doc(..),
                                          braces, ($$), cr)
-
 data Variant = Bibtex | Biblatex
   deriving (Show, Eq, Ord)
 
@@ -527,9 +526,9 @@ itemToReference locale variant item = do
     let fixSeriesTitle [Str xs] | isNumber xs =
           [Str (ordinalize locale xs), Space, Str (resolveKey' lang "jourser")]
         fixSeriesTitle xs = xs
-    seriesTitle' <- (Just . B.fromList . fixSeriesTitle .
-                     B.toList . resolveKey lang <$>
-                        getTitle "series") <|>
+
+    seriesTitle' <- (Just . B.fromList . fixSeriesTitle . B.toList
+                       <$> getTitle "series") <|>
                     return Nothing
     shortTitle' <- (Just <$> (guard (not hasMaintitle || isChapterlike) >>
                               getTitle "shorttitle"))
@@ -984,8 +983,12 @@ getTitle f = do
   ils <- getField f
   utc <- gets untitlecase
   lang <- gets localeLang
+  let ils' =
+        if f == "series"
+           then resolveKey lang $ ils
+           else ils
   let processTitle = if utc then unTitlecase (Just lang) else id
-  return $ processTitle ils
+  return $ processTitle ils'
 
 getShortTitle :: Bool -> Text -> Bib (Maybe Inlines)
 getShortTitle requireColon f = do
@@ -1460,14 +1463,14 @@ bookTrans z =
        _                -> [z]
 
 resolveKey :: Lang -> Inlines -> Inlines
-resolveKey lang ils = Walk.walk go ils
+resolveKey lang (Many ils) = Many $ fmap go ils
   where go (Str s) = Str $ resolveKey' lang s
         go x       = x
 
 resolveKey' :: Lang -> Text -> Text
 resolveKey' lang k =
   case Map.lookup (langLanguage lang) biblatexStringMap >>=
-        Map.lookup (T.toLower k) of
+        Map.lookup k of
     Nothing     -> k
     Just (x, _) -> either (const k) stringify $ parseLaTeX lang x
 
diff --git a/test/command/biblatex-article.md b/test/command/biblatex-article.md
index 705369f78..68d467526 100644
--- a/test/command/biblatex-article.md
+++ b/test/command/biblatex-article.md
@@ -97,7 +97,7 @@ references:
   - family: Author
     given: Ann A.
     suffix: Jr.
-  collection-title: new series
+  collection-title: New series
   container-title: "The Journaltitle: The Journalsubtitle"
   doi: 10.1086/520976
   editor:
diff --git a/test/command/biblatex-gillies.md b/test/command/biblatex-gillies.md
index e1a64af23..a63c1311d 100644
--- a/test/command/biblatex-gillies.md
+++ b/test/command/biblatex-gillies.md
@@ -54,7 +54,7 @@ references:
   author:
   - family: Gillies
     given: Alexander
-  collection-title: new series
+  collection-title: New series
   container-title: Publications of the English Goethe Society
   id: gillies
   issued: 1933
diff --git a/test/command/biblatex-shore.md b/test/command/biblatex-shore.md
index cc9c079e1..efe97ef7a 100644
--- a/test/command/biblatex-shore.md
+++ b/test/command/biblatex-shore.md
@@ -49,7 +49,7 @@ references:
   author:
   - family: Shore
     given: Bradd
-  collection-title: new series
+  collection-title: New series
   container-title: American Anthropologist
   id: shore
   issue: 1