From 82638ad53bb47acde835083390f8211ac21cda6c Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Fri, 13 Aug 2021 19:19:43 -0700
Subject: [PATCH] Convert Quoted in bib entries to special Spans...

before passing them off to citeproc.
This ensures that we get proper localization and flipflopping
if, e.g., quotes are used in titles.

Closes jgm/citeproc#87.
---
 pandoc.cabal                |  2 +-
 src/Text/Pandoc/Citeproc.hs |  4 +++-
 stack.yaml                  |  1 +
 test/command/citeproc-87.md | 42 +++++++++++++++++++++++++++++++++++++
 4 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/pandoc.cabal b/pandoc.cabal
index bc8ea2588..f4a173f59 100644
--- a/pandoc.cabal
+++ b/pandoc.cabal
@@ -492,7 +492,7 @@ library
                  syb                   >= 0.1      && < 0.8,
                  tagsoup               >= 0.14.6   && < 0.15,
                  temporary             >= 1.1      && < 1.4,
-                 texmath               >= 0.12.3   && < 0.12.4,
+                 texmath               >= 0.12.3.1 && < 0.12.4,
                  text                  >= 1.1.1.0  && < 1.3,
                  text-conversions      >= 0.3      && < 0.4,
                  time                  >= 1.5      && < 1.12,
diff --git a/src/Text/Pandoc/Citeproc.hs b/src/Text/Pandoc/Citeproc.hs
index 0f007f336..39760776d 100644
--- a/src/Text/Pandoc/Citeproc.hs
+++ b/src/Text/Pandoc/Citeproc.hs
@@ -208,7 +208,9 @@ getReferences mblocale (Pandoc meta bs) = do
                         Just fp -> getRefsFromBib locale idpred fp
                         Nothing -> return []
                     Nothing -> return []
-  return $ map (linkifyVariables . legacyDateRanges)
+  let addQuoteSpan (Quoted _ xs) = Span ("",["csl-quoted"],[]) xs
+      addQuoteSpan x = x
+  return $ map (linkifyVariables . legacyDateRanges . walk addQuoteSpan)
                (externalRefs ++ inlineRefs)
             -- note that inlineRefs can override externalRefs
 
diff --git a/stack.yaml b/stack.yaml
index ef92855c1..ce58cf732 100644
--- a/stack.yaml
+++ b/stack.yaml
@@ -12,6 +12,7 @@ extra-deps:
 - skylighting-core-0.11
 - skylighting-0.11
 - doctemplates-0.10
+- texmath-0.12.3.1
 # - citeproc-0.4.1
 - git: https://github.com/jgm/citeproc.git
   commit: 8d03cb722fac3ea8f17f432ac20abe35361df551
diff --git a/test/command/citeproc-87.md b/test/command/citeproc-87.md
index 42cf7a6ee..4faa6bfba 100644
--- a/test/command/citeproc-87.md
+++ b/test/command/citeproc-87.md
@@ -14,3 +14,45 @@ references:
 ^D
 Foo (Aristotele, s.d., 50: «Disse: "bar"»). «Disse: "baz"»
 ```
+
+For en, the localized quotes match what Quoted would produce,
+so the Quoted is passed to citeproc as a Span ("",["csl-quoted"],[])
+and flipflopping and localization occur.
+```
+% pandoc -C -t plain -Mlang=en
+---
+references:
+- id: a
+  author:
+    - literal: Aristotele
+  title: Metafisica et "Physica"
+  type: article-journal
+...
+
+Foo [@a 50].
+^D
+Foo (Aristotele, n.d., 50).
+
+Aristotele. n.d. “Metafisica Et ‘Physica’.”
+```
+
+For cs, there is no such match, so the Quoted is left alone
+and has the same effect it would have elsewhere in a pandoc document.
+```
+% pandoc -C -t plain -Mlang=it
+---
+references:
+- id: a
+  author:
+    - literal: Aristotele
+  title: Metafisica et "Physica"
+  type: article-journal
+...
+
+Foo [@a 50].
+^D
+Foo (Aristotele, s.d., 50).
+
+Aristotele. s.d. «Metafisica et “Physica”».
+```
+