From da5e8c549ea5becd445acb8d42c8b002d45f7913 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Mon, 2 Mar 2020 01:35:08 -0500 Subject: [PATCH] Convert Zotero fields in Extra to CSL fields for citeproc-js --- chrome/content/zotero/xpcom/cite.js | 16 ++++++++++++++-- chrome/content/zotero/xpcom/schema.js | 14 ++++++++++++++ test/tests/citeTest.js | 18 ++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/chrome/content/zotero/xpcom/cite.js b/chrome/content/zotero/xpcom/cite.js index 9c63a5d802..00e6540fdc 100644 --- a/chrome/content/zotero/xpcom/cite.js +++ b/chrome/content/zotero/xpcom/cite.js @@ -408,9 +408,21 @@ Zotero.Cite = { field = 'archive_location'; break; - // Don't change other lines default: - field = originalField; + // See if this is a Zotero field written out (e.g., "Publication Title"), and if so + // convert to its associated CSL field + var zoteroField = originalField.replace(/ ([A-Z])/, '$1'); + // If second character is lowercase (so not an acronym), lowercase first letter too + if (zoteroField[1] == zoteroField[1].toLowerCase()) { + zoteroField = zoteroField[0].toLowerCase() + zoteroField.substr(1); + } + if (Zotero.Schema.CSL_FIELD_MAPPINGS_REVERSE[zoteroField]) { + field = Zotero.Schema.CSL_FIELD_MAPPINGS_REVERSE[zoteroField]; + } + // Don't change other lines + else { + field = originalField; + } } return field + value; }); diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index f00acbc776..53244017c1 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -573,6 +573,8 @@ Zotero.Schema = new function(){ ); Zotero.Schema.globalSchemaLocale = data.locales[locale]; Zotero.Schema.globalSchemaMeta = data.meta; + + // CSL mappings Zotero.Schema.CSL_TYPE_MAPPINGS = {}; Zotero.Schema.CSL_TYPE_MAPPINGS_REVERSE = {}; for (let cslType in data.csl.types) { @@ -584,6 +586,18 @@ Zotero.Schema = new function(){ Zotero.Schema.CSL_TEXT_MAPPINGS = data.csl.fields.text; Zotero.Schema.CSL_DATE_MAPPINGS = data.csl.fields.date; Zotero.Schema.CSL_NAME_MAPPINGS = data.csl.names; + + // Map Zotero fields to CSL fields + Zotero.Schema.CSL_FIELD_MAPPINGS_REVERSE = {}; + for (let cslField in data.csl.fields.text) { + for (let zoteroField of data.csl.fields.text[cslField]) { + Zotero.Schema.CSL_FIELD_MAPPINGS_REVERSE[zoteroField] = cslField; + } + } + for (let cslField in data.csl.fields.date) { + let zoteroField = data.csl.fields.date[cslField]; + Zotero.Schema.CSL_FIELD_MAPPINGS_REVERSE[zoteroField] = cslField; + } } diff --git a/test/tests/citeTest.js b/test/tests/citeTest.js index 6c4fa76051..a5a5e3579f 100644 --- a/test/tests/citeTest.js +++ b/test/tests/citeTest.js @@ -23,5 +23,23 @@ describe("Zotero.Cite", function () { + 'This is just some text.'; assert.equal(Zotero.Cite.extraToCSL(str1), str2); }); + + it("should convert Zotero field names to CSL fields", function () { + var str1 = 'publicationTitle: My Publication'; + var str2 = 'container-title: My Publication'; + assert.equal(Zotero.Cite.extraToCSL(str1), str2); + }); + + it("should convert capitalized and spaced Zotero field names to CSL fields", function () { + var str1 = 'Publication Title: My Publication\nDate: 1989'; + var str2 = 'container-title: My Publication\nissued: 1989'; + assert.equal(Zotero.Cite.extraToCSL(str1), str2); + }); + + it("should convert lowercase 'doi' to uppercase", function () { + var str1 = 'doi: 10.0/abc'; + var str2 = 'DOI: 10.0/abc'; + assert.equal(Zotero.Cite.extraToCSL(str1), str2); + }); }); });