Merge pull request #820 from aurimasv/csl-json
Fix issues with computerProgram citations
This commit is contained in:
commit
1ecc53b099
4 changed files with 111 additions and 14 deletions
|
@ -330,6 +330,8 @@ Zotero.Date = new function(){
|
||||||
}
|
}
|
||||||
|
|
||||||
if(date.month) date.month--; // subtract one for JS style
|
if(date.month) date.month--; // subtract one for JS style
|
||||||
|
else delete date.month;
|
||||||
|
|
||||||
Zotero.debug("DATE: retrieved with algorithms: "+JSON.stringify(date));
|
Zotero.debug("DATE: retrieved with algorithms: "+JSON.stringify(date));
|
||||||
|
|
||||||
parts.push(
|
parts.push(
|
||||||
|
@ -369,7 +371,7 @@ Zotero.Date = new function(){
|
||||||
}
|
}
|
||||||
|
|
||||||
// MONTH
|
// MONTH
|
||||||
if(!date.month) {
|
if(date.month === undefined) {
|
||||||
// compile month regular expression
|
// compile month regular expression
|
||||||
var months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul',
|
var months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul',
|
||||||
'aug', 'sep', 'oct', 'nov', 'dec'];
|
'aug', 'sep', 'oct', 'nov', 'dec'];
|
||||||
|
|
|
@ -99,6 +99,7 @@ const CSL_DATE_MAPPINGS = {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Mappings for types
|
* Mappings for types
|
||||||
|
* Also see itemFromCSLJSON
|
||||||
*/
|
*/
|
||||||
const CSL_TYPE_MAPPINGS = {
|
const CSL_TYPE_MAPPINGS = {
|
||||||
'book':"book",
|
'book':"book",
|
||||||
|
@ -1611,7 +1612,7 @@ Zotero.Utilities = {
|
||||||
cslItem[variable] = {"date-parts":[dateParts]};
|
cslItem[variable] = {"date-parts":[dateParts]};
|
||||||
|
|
||||||
// if no month, use season as month
|
// if no month, use season as month
|
||||||
if(dateObj.part && !dateObj.month) {
|
if(dateObj.part && dateObj.month === undefined) {
|
||||||
cslItem[variable].season = dateObj.part;
|
cslItem[variable].season = dateObj.part;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -1645,14 +1646,44 @@ Zotero.Utilities = {
|
||||||
* @param {Object} cslItem
|
* @param {Object} cslItem
|
||||||
*/
|
*/
|
||||||
"itemFromCSLJSON":function(item, cslItem) {
|
"itemFromCSLJSON":function(item, cslItem) {
|
||||||
var isZoteroItem = item instanceof Zotero.Item, zoteroType;
|
var isZoteroItem = item instanceof Zotero.Item,
|
||||||
|
zoteroType;
|
||||||
|
|
||||||
|
// Some special cases to help us map item types correctly
|
||||||
|
// This ensures that we don't lose data on import. The fields
|
||||||
|
// we check are incompatible with the alternative item types
|
||||||
|
if (cslItem.type == 'book') {
|
||||||
|
zoteroType = 'book';
|
||||||
|
if (cslItem.version) {
|
||||||
|
zoteroType = 'computerProgram';
|
||||||
|
}
|
||||||
|
} else if (cslItem.type == 'bill') {
|
||||||
|
zoteroType = 'bill';
|
||||||
|
if (cslItem.publisher || cslItem['number-of-volumes']) {
|
||||||
|
zoteroType = 'hearing';
|
||||||
|
}
|
||||||
|
} else if (cslItem.type == 'song') {
|
||||||
|
zoteroType = 'audioRecording';
|
||||||
|
if (cslItem.number) {
|
||||||
|
zoteroType = 'podcast';
|
||||||
|
}
|
||||||
|
} else if (cslItem.type == 'motion_picture') {
|
||||||
|
zoteroType = 'film';
|
||||||
|
if (cslItem['collection-title'] || cslItem['publisher-place']
|
||||||
|
|| cslItem['event-place'] || cslItem.volume
|
||||||
|
|| cslItem['number-of-volumes'] || cslItem.ISBN
|
||||||
|
) {
|
||||||
|
zoteroType = 'videoRecording';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
for(var type in CSL_TYPE_MAPPINGS) {
|
for(var type in CSL_TYPE_MAPPINGS) {
|
||||||
if(CSL_TYPE_MAPPINGS[type] == cslItem.type) {
|
if(CSL_TYPE_MAPPINGS[type] == cslItem.type) {
|
||||||
zoteroType = type;
|
zoteroType = type;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(!zoteroType) zoteroType = "document";
|
if(!zoteroType) zoteroType = "document";
|
||||||
|
|
||||||
var itemTypeID = Zotero.ItemTypes.getID(zoteroType);
|
var itemTypeID = Zotero.ItemTypes.getID(zoteroType);
|
||||||
|
@ -1667,9 +1698,14 @@ Zotero.Utilities = {
|
||||||
for(var variable in CSL_TEXT_MAPPINGS) {
|
for(var variable in CSL_TEXT_MAPPINGS) {
|
||||||
if(variable in cslItem) {
|
if(variable in cslItem) {
|
||||||
var textMappings = CSL_TEXT_MAPPINGS[variable];
|
var textMappings = CSL_TEXT_MAPPINGS[variable];
|
||||||
for(var i in textMappings) {
|
for(var i=0; i<textMappings.length; i++) {
|
||||||
var field = textMappings[i],
|
var field = textMappings[i];
|
||||||
fieldID = Zotero.ItemFields.getID(field);
|
|
||||||
|
// Until 5.0, use version instead of versionNumber
|
||||||
|
if (field == 'versionNumber') field = 'version';
|
||||||
|
|
||||||
|
var fieldID = Zotero.ItemFields.getID(field);
|
||||||
|
|
||||||
if(Zotero.ItemFields.isBaseField(fieldID)) {
|
if(Zotero.ItemFields.isBaseField(fieldID)) {
|
||||||
var newFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(itemTypeID, fieldID);
|
var newFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(itemTypeID, fieldID);
|
||||||
if(newFieldID) fieldID = newFieldID;
|
if(newFieldID) fieldID = newFieldID;
|
||||||
|
@ -1677,10 +1713,12 @@ Zotero.Utilities = {
|
||||||
|
|
||||||
if(Zotero.ItemFields.isValidForType(fieldID, itemTypeID)) {
|
if(Zotero.ItemFields.isValidForType(fieldID, itemTypeID)) {
|
||||||
if(isZoteroItem) {
|
if(isZoteroItem) {
|
||||||
item.setField(fieldID, cslItem[variable], true);
|
item.setField(fieldID, cslItem[variable]);
|
||||||
} else {
|
} else {
|
||||||
item[field] = cslItem[variable];
|
item[field] = cslItem[variable];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1347,8 +1347,7 @@
|
||||||
1,
|
1,
|
||||||
2
|
2
|
||||||
]
|
]
|
||||||
],
|
]
|
||||||
"season": "2000-01-02"
|
|
||||||
},
|
},
|
||||||
"title": "Title",
|
"title": "Title",
|
||||||
"type": "patent"
|
"type": "patent"
|
||||||
|
|
|
@ -221,7 +221,7 @@ describe("Zotero.Utilities", function() {
|
||||||
|
|
||||||
attachment.save();
|
attachment.save();
|
||||||
|
|
||||||
cslJSONAttachment = Zotero.Utilities.itemToCSLJSON(attachment);
|
let cslJSONAttachment = Zotero.Utilities.itemToCSLJSON(attachment);
|
||||||
assert.equal(cslJSONAttachment.type, 'article', 'attachment is exported as "article"');
|
assert.equal(cslJSONAttachment.type, 'article', 'attachment is exported as "article"');
|
||||||
assert.equal(cslJSONAttachment.title, 'Empty', 'attachment title is correct');
|
assert.equal(cslJSONAttachment.title, 'Empty', 'attachment title is correct');
|
||||||
assert.deepEqual(cslJSONAttachment.accessed, {"date-parts":[["2001",2,3]]}, 'attachment access date is mapped correctly');
|
assert.deepEqual(cslJSONAttachment.accessed, {"date-parts":[["2001",2,3]]}, 'attachment access date is mapped correctly');
|
||||||
|
@ -357,4 +357,62 @@ describe("Zotero.Utilities", function() {
|
||||||
assert.deepEqual(cslCreators[5], creators[5].expect, 'protected last name prevents parsing');
|
assert.deepEqual(cslCreators[5], creators[5].expect, 'protected last name prevents parsing');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe("itemFromCSLJSON", function() {
|
||||||
|
it("should stably perform itemToCSLJSON -> itemFromCSLJSON -> itemToCSLJSON", function() {
|
||||||
|
let data = loadSampleData('citeProcJSExport');
|
||||||
|
|
||||||
|
Zotero.DB.beginTransaction();
|
||||||
|
|
||||||
|
for (let i in data) {
|
||||||
|
let item = data[i];
|
||||||
|
|
||||||
|
let zItem = new Zotero.Item();
|
||||||
|
Zotero.Utilities.itemFromCSLJSON(zItem, item);
|
||||||
|
zItem = Zotero.Items.get(zItem.save());
|
||||||
|
|
||||||
|
let newItem = Zotero.Utilities.itemToCSLJSON(zItem);
|
||||||
|
|
||||||
|
delete newItem.id;
|
||||||
|
delete item.id;
|
||||||
|
|
||||||
|
assert.deepEqual(newItem, item, i + ' export -> import -> export is stable');
|
||||||
|
}
|
||||||
|
|
||||||
|
Zotero.DB.commitTransaction();
|
||||||
|
|
||||||
|
});
|
||||||
|
it("should import exported standalone note", function() {
|
||||||
|
let note = new Zotero.Item('note');
|
||||||
|
note.setNote('Some note longer than 50 characters, which will become the title.');
|
||||||
|
note = Zotero.Items.get(note.save());
|
||||||
|
|
||||||
|
let jsonNote = Zotero.Utilities.itemToCSLJSON(note);
|
||||||
|
|
||||||
|
let zItem = new Zotero.Item();
|
||||||
|
Zotero.Utilities.itemFromCSLJSON(zItem, jsonNote);
|
||||||
|
zItem = Zotero.Items.get(zItem.save());
|
||||||
|
|
||||||
|
assert.equal(zItem.getField('title'), jsonNote.title, 'title imported correctly');
|
||||||
|
});
|
||||||
|
it("should import exported standalone attachment", function() {
|
||||||
|
let file = getTestDataDirectory();
|
||||||
|
file.append("empty.pdf");
|
||||||
|
|
||||||
|
let attachment = Zotero.Items.get(Zotero.Attachments.importFromFile(file));
|
||||||
|
attachment.setField('title', 'Empty');
|
||||||
|
attachment.setField('accessDate', '2001-02-03 12:13:14');
|
||||||
|
attachment.setField('url', 'http://example.com');
|
||||||
|
attachment.setNote('Note');
|
||||||
|
|
||||||
|
attachment.save();
|
||||||
|
|
||||||
|
let jsonAttachment = Zotero.Utilities.itemToCSLJSON(attachment);
|
||||||
|
|
||||||
|
let zItem = new Zotero.Item();
|
||||||
|
Zotero.Utilities.itemFromCSLJSON(zItem, jsonAttachment);
|
||||||
|
zItem = Zotero.Items.get(zItem.save());
|
||||||
|
|
||||||
|
assert.equal(zItem.getField('title'), jsonAttachment.title, 'title imported correctly');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue