Convert Zotero fields in Extra to CSL fields for citeproc-js

This commit is contained in:
Dan Stillman 2020-03-02 01:35:08 -05:00
parent 83cc65eea6
commit da5e8c549e
3 changed files with 46 additions and 2 deletions

View file

@ -408,10 +408,22 @@ Zotero.Cite = {
field = 'archive_location';
break;
// Don't change other lines
default:
// 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;
});
}

View file

@ -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;
}
}

View file

@ -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);
});
});
});