From d7883e172ffca98a1ffcabe6d15135d4efc91626 Mon Sep 17 00:00:00 2001
From: "William J. Bowman" <wjb@williamjbowman.com>
Date: Sun, 23 Aug 2020 09:53:25 -0600
Subject: [PATCH] Fix case sensitivity issues in bibtex

---
 scribble-lib/scriblib/bibtex.rkt              | 37 ++++++++++---------
 .../tests/scriblib/bibtex.number.txt          |  1 +
 scribble-test/tests/scriblib/bibtex.rkt       |  3 +-
 scribble-test/tests/scriblib/example.bib      |  5 +++
 4 files changed, 27 insertions(+), 19 deletions(-)

diff --git a/scribble-lib/scriblib/bibtex.rkt b/scribble-lib/scriblib/bibtex.rkt
index 9eb546ae..ed5bdd31 100644
--- a/scribble-lib/scriblib/bibtex.rkt
+++ b/scribble-lib/scriblib/bibtex.rkt
@@ -49,7 +49,7 @@
     (match (read-until (λ (c) (or (char=? c #\{)
                                   (char=? c #\()))
                        ip)
-      [(app string-downcase "string")
+      [(app string-foldcase "string")
        (slurp-whitespace ip)
        (match (read-char ip)
          [#\{
@@ -70,8 +70,8 @@
              (perror ip 'read-entry "Parsing string, expected }, got ~v; tag is ~v; string is ~v" c tag string)])]
          [c
           (perror ip 'read-entry "Parsing string, expected =, got ~v; tag is ~v" c tag)])]
-      [(or (app string-downcase "comment")
-           (app string-downcase "preamble"))
+      [(or (app string-foldcase "comment")
+           (app string-foldcase "preamble"))
        (read-char ip)
        (let loop ()
          (read-until (λ (c) (or (char=? c #\{) (char=? c #\}))) ip)
@@ -83,7 +83,7 @@
       [typ
        (read-char ip)
        (slurp-whitespace ip)
-       (define label (read-until (λ (c) (char=? c #\,)) ip))
+       (define label (string-foldcase (read-until (λ (c) (char=? c #\,)) ip)))
        (read-char ip)
        (define alist
          (let loop ()
@@ -110,11 +110,11 @@
                 [c
                  (perror ip 'read-entry "Parsing entry tag, expected =, got ~v; label is ~v; atag is ~v" c label atag)])])))
        (hash-set! ENTRY-DB label
-                  (hash-set alist 'type typ))]))
+                  (hash-set alist 'type (string-foldcase typ)))]))
 
   (define (read-tag ip)
     (slurp-whitespace ip)
-    (string-downcase
+    (string-foldcase
      (read-until
       (λ (c) (or (char-whitespace? c)
                  (char=? c #\=)
@@ -197,22 +197,23 @@
       autobib-cite autobib-citet
       ~cite-id citet-id)))
 
+(define ((make-citer bibtex-db citer) f . r)
+  (apply citer
+         (filter-map
+          (λ (key)
+            (and (not (string=? "\n" key))
+                 (generate-bib bibtex-db key)))
+          (append-map (curry regexp-split #px"\\s+")
+                      (cons f r)))))
+
 (define-syntax-rule
   (define-bibtex-cite* bib-pth
     autobib-cite autobib-citet
     ~cite-id citet-id)
   (begin
     (define bibtex-db (path->bibdb bib-pth))
-    (define ((make-citer citer) f . r)
-      (apply citer
-             (filter-map
-              (λ (key)
-                (and (not (string=? "\n" key))
-                     (generate-bib bibtex-db key)))
-              (append-map (curry regexp-split #px"\\s+")
-                          (cons f r)))))
-    (define ~cite-id (make-citer autobib-cite))
-    (define citet-id (make-citer autobib-citet))))
+    (define ~cite-id (make-citer bibtex-db autobib-cite))
+    (define citet-id (make-citer bibtex-db autobib-citet))))
 
 ;; Seems a little redundant to convert latex escapes into unicode only to
 ;; convert them back into latex, but we need to sort authors so we can't
@@ -450,9 +451,9 @@
 
 (define (generate-bib db key)
   (match-define (bibdb raw bibs) db)
-  (hash-ref! bibs key
+  (hash-ref! bibs (string-foldcase key)
              (λ ()
-               (define the-raw (hash-ref raw key (λ () (error 'bibtex "Unknown citation ~e" key))))
+               (define the-raw (hash-ref raw (string-foldcase key) (λ () (error 'bibtex "Unknown citation ~e" key))))
                (define (raw-attr a [def #f])
                  (hash-ref the-raw a def))
                (define (raw-attr* a)
diff --git a/scribble-test/tests/scriblib/bibtex.number.txt b/scribble-test/tests/scriblib/bibtex.number.txt
index de687b03..591b5a72 100644
--- a/scribble-test/tests/scriblib/bibtex.number.txt
+++ b/scribble-test/tests/scriblib/bibtex.number.txt
@@ -6,3 +6,4 @@ Bibliography
    Python. Massachusetts Institute of Technology, 2004.                 
 [3]Sam Tobin-Hochstadt, Vincent St-Amour, Ryan Culpepper, Matthew Flatt,
    and Matthias Felleisen. Languages as Libraries. In Proc. PLDI, 2011. 
+[4]ZA ZAuThOr. StrIngS ArE TerriblE. 2000.                              
diff --git a/scribble-test/tests/scriblib/bibtex.rkt b/scribble-test/tests/scriblib/bibtex.rkt
index b145a2d3..04cc5ee1 100644
--- a/scribble-test/tests/scriblib/bibtex.rkt
+++ b/scribble-test/tests/scriblib/bibtex.rkt
@@ -74,7 +74,8 @@
               (λ (~cite-id citet-id)
                 (citet-id "salib:starkiller")
                 (citet-id "cryptoeprint:2000:067")
-                (citet-id "Tobin-Hochstadt:2011fk")))
+                (citet-id "Tobin-Hochstadt:2011fk")
+                (citet-id "anannoyingkey")))
  (test-render latex-escapes-path (#:style number-style)
               (λ (~cite-id citet-id)
                 (citet-id "Braberman:2008:PPH:1375634.1375655"))))
diff --git a/scribble-test/tests/scriblib/example.bib b/scribble-test/tests/scriblib/example.bib
index 9d5d44b0..9d49f9b8 100644
--- a/scribble-test/tests/scriblib/example.bib
+++ b/scribble-test/tests/scriblib/example.bib
@@ -405,3 +405,8 @@ Book{landru21,
   month = "May",
   year = 2004
 }
+
+@MiSc{AnAnnoyingKeY,
+	Author = {ZA ZAuThOr},
+	Title = {StrIngS ArE TerriblE},
+	Year = {2000}}
\ No newline at end of file