Use Zotero.Utilities.itemToCSLJSON when sending items to citeproc

This commit is contained in:
Aurimas Vinckevicius 2015-02-08 19:42:27 -06:00
parent 7f52e00341
commit f0bd1e77ff
2 changed files with 59 additions and 141 deletions

View file

@ -522,100 +522,20 @@ Zotero.Cite.System.prototype = {
throw "Zotero.Cite.System.retrieveItem called on non-item "+item; throw "Zotero.Cite.System.retrieveItem called on non-item "+item;
} }
// don't return URL or accessed information for journal articles if a var cslItem = Zotero.Utilities.itemToCSLJSON(zoteroItem);
// pages field exists
var itemType = Zotero.ItemTypes.getName(zoteroItem.itemTypeID); if (!Zotero.Prefs.get("export.citePaperJournalArticleURL")) {
var cslType = CSL_TYPE_MAPPINGS[itemType]; var itemType = Zotero.ItemTypes.getName(zoteroItem.itemTypeID);
if(!cslType) cslType = "article"; // don't return URL or accessed information for journal articles if a
var ignoreURL = ((zoteroItem.getField("accessDate", true, true) || zoteroItem.getField("url", true, true)) && // pages field exists
["journalArticle", "newspaperArticle", "magazineArticle"].indexOf(itemType) !== -1 if (["journalArticle", "newspaperArticle", "magazineArticle"].indexOf(itemType) !== -1
&& zoteroItem.getField("pages") && zoteroItem.getField("pages")
&& !Zotero.Prefs.get("export.citePaperJournalArticleURL")); ) {
delete cslItem.URL;
var cslItem = { delete cslItem.accessed;
'id':zoteroItem.id,
'type':cslType
};
// get all text variables (there must be a better way)
// TODO: does citeproc-js permit short forms?
for(var variable in CSL_TEXT_MAPPINGS) {
var fields = CSL_TEXT_MAPPINGS[variable];
if(variable == "URL" && ignoreURL) continue;
for each(var field in fields) {
var value = zoteroItem.getField(field, false, true).toString();
if(value != "") {
// Strip enclosing quotes
if(value.match(/^".+"$/)) {
value = value.substr(1, value.length-2);
}
cslItem[variable] = value;
break;
}
} }
} }
// separate name variables
var authorID = Zotero.CreatorTypes.getPrimaryIDForType(zoteroItem.itemTypeID);
var creators = zoteroItem.getCreators();
for each(var creator in creators) {
if(creator.creatorTypeID == authorID) {
var creatorType = "author";
} else {
var creatorType = Zotero.CreatorTypes.getName(creator.creatorTypeID);
}
var creatorType = CSL_NAMES_MAPPINGS[creatorType];
if(!creatorType) continue;
var nameObj = {'family':creator.ref.lastName, 'given':creator.ref.firstName};
if(cslItem[creatorType]) {
cslItem[creatorType].push(nameObj);
} else {
cslItem[creatorType] = [nameObj];
}
}
// get date variables
for(var variable in CSL_DATE_MAPPINGS) {
var date = zoteroItem.getField(CSL_DATE_MAPPINGS[variable], false, true);
if(date) {
var dateObj = Zotero.Date.strToDate(date);
// otherwise, use date-parts
var dateParts = [];
if(dateObj.year) {
// add year, month, and day, if they exist
dateParts.push(dateObj.year);
if(dateObj.month !== undefined) {
dateParts.push(dateObj.month+1);
if(dateObj.day) {
dateParts.push(dateObj.day);
}
}
cslItem[variable] = {"date-parts":[dateParts]};
// if no month, use season as month
if(dateObj.part && !dateObj.month) {
cslItem[variable].season = dateObj.part;
}
} else {
// if no year, pass date literally
cslItem[variable] = {"literal":date};
}
}
}
// extract PMID
var extra = zoteroItem.getField("extra", false, true);
if(typeof extra === "string") {
var m = /(?:^|\n)PMID:\s*([0-9]+)/.exec(extra);
if(m) cslItem.PMID = m[1];
m = /(?:^|\n)PMCID:\s*((?:PMC)?[0-9]+)/.exec(extra);
if(m) cslItem.PMCID = m[1];
}
//this._cache[zoteroItem.id] = cslItem;
return cslItem; return cslItem;
}, },

View file

@ -1504,89 +1504,78 @@ Zotero.Utilities = {
/** /**
* Converts an item from toArray() format to citeproc-js JSON * Converts an item from toArray() format to citeproc-js JSON
* @param {Zotero.Item} item * @param {Zotero.Item} zoteroItem
* @return {Object} The CSL item * @return {Object} The CSL item
*/ */
"itemToCSLJSON":function(item) { "itemToCSLJSON":function(zoteroItem) {
if(item instanceof Zotero.Item) { if (zoteroItem instanceof Zotero.Item) {
item = item.toArray(); zoteroItem = zoteroItem.toArray();
} }
var itemType = item.itemType; var cslType = CSL_TYPE_MAPPINGS[zoteroItem.itemType] || "article";
var cslType = CSL_TYPE_MAPPINGS[itemType]; var itemTypeID = Zotero.ItemTypes.getID(zoteroItem.itemType);
if(!cslType) cslType = "article";
var cslItem = { var cslItem = {
'id':item.itemID, 'id':zoteroItem.itemID,
'type':cslType 'type':cslType
}; };
// Map text fields // get all text variables (there must be a better way)
var itemTypeID = Zotero.ItemTypes.getID(itemType);
for(var variable in CSL_TEXT_MAPPINGS) { for(var variable in CSL_TEXT_MAPPINGS) {
var fields = CSL_TEXT_MAPPINGS[variable]; var fields = CSL_TEXT_MAPPINGS[variable];
for(var i=0, n=fields.length; i<n; i++) { for(var i=0, n=fields.length; i<n; i++) {
var field = fields[i], value = undefined; var field = fields[i],
value;
if(field in item) { if(field in zoteroItem) {
value = item[field]; value = zoteroItem[field];
} else { } else {
var fieldID = Zotero.ItemFields.getID(field), var fieldID = Zotero.ItemFields.getID(field),
baseMapping baseMapping;
if(Zotero.ItemFields.isValidForType(fieldID, itemTypeID) if(Zotero.ItemFields.isValidForType(fieldID, itemTypeID)
&& (baseMapping = Zotero.ItemFields.getBaseIDFromTypeAndField(itemTypeID, fieldID))) { && (baseMapping = Zotero.ItemFields.getBaseIDFromTypeAndField(itemTypeID, fieldID))) {
value = item[Zotero.ItemTypes.getName(baseMapping)]; value = zoteroItem[Zotero.ItemTypes.getName(baseMapping)];
} }
} }
if(!value) continue; if (!value) continue;
var valueLength = value.length; if (typeof value == 'string') {
if(valueLength) {
// Strip enclosing quotes // Strip enclosing quotes
if(value[0] === '"' && value[valueLength-1] === '"') { if(value.charAt(0) == '"' && value.indexOf('"', 1) == value.length - 1) {
value = value.substr(1, valueLength-2); value = value.substring(1, value.length-1);
} }
cslItem[variable] = value;
break;
} }
cslItem[variable] = value;
break;
} }
} }
// separate name variables // separate name variables
var authorID = Zotero.CreatorTypes.getPrimaryIDForType(itemTypeID); var author = Zotero.CreatorTypes.getName(Zotero.CreatorTypes.getPrimaryIDForType(itemTypeID));
var authorFieldName = Zotero.CreatorTypes.getName(authorID); var creators = zoteroItem.creators;
var creators = item.creators; for(var i=0; i<creators.length; i++) {
if(creators) { var creator = creators[i];
for(var i=0, n=creators.length; i<n; i++) { var creatorType = creator.creatorType;
var creator = creators[i]; if(creatorType == author) {
creatorType = "author";
if(creator.creatorType == authorFieldName) { }
var creatorType = "author";
} else { creatorType = CSL_NAMES_MAPPINGS[creatorType];
var creatorType = CSL_NAMES_MAPPINGS[creator.creatorType] if(!creatorType) continue;
}
var nameObj = {'family':creator.lastName, 'given':creator.firstName};
if(!creatorType) continue;
if(cslItem[creatorType]) {
if(creator.fieldMode == 1) { cslItem[creatorType].push(nameObj);
var nameObj = {'literal':creator.lastName}; } else {
} else { cslItem[creatorType] = [nameObj];
var nameObj = {'family':creator.lastName, 'given':creator.firstName};
}
if(cslItem[creatorType]) {
cslItem[creatorType].push(nameObj);
} else {
cslItem[creatorType] = [nameObj];
}
} }
} }
// get date variables // get date variables
for(var variable in CSL_DATE_MAPPINGS) { for(var variable in CSL_DATE_MAPPINGS) {
var date = item[CSL_DATE_MAPPINGS[variable]]; var date = zoteroItem[CSL_DATE_MAPPINGS[variable]];
if(date) { if(date) {
var dateObj = Zotero.Date.strToDate(date); var dateObj = Zotero.Date.strToDate(date);
// otherwise, use date-parts // otherwise, use date-parts
@ -1612,8 +1601,17 @@ Zotero.Utilities = {
} }
} }
} }
// extract PMID
var extra = zoteroItem.extra;
if(typeof extra === "string") {
var m = /(?:^|\n)PMID:\s*([0-9]+)/.exec(extra);
if(m) cslItem.PMID = m[1];
m = /(?:^|\n)PMCID:\s*((?:PMC)?[0-9]+)/.exec(extra);
if(m) cslItem.PMCID = m[1];
}
//this._cache[item.id] = cslItem; //this._cache[zoteroItem.id] = cslItem;
return cslItem; return cslItem;
}, },