Commit e567ddec authored by Maxime Chaillet's avatar Maxime Chaillet

Refactor the code to handle authorship format in the citations. Class Author created.

parent 252c4086
......@@ -8,6 +8,7 @@
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<!-- Using grunt-include-source, the proper js files created by us (not minified) are included below -->
<script src="src/author.js"></script>
<script src="src/beamlineUrl.js"></script>
<script src="src/doicontroller.js"></script>
<script src="src/doiview.js"></script>
......
This diff is collapsed.
function Author() {
}
/**
* Set the first name as recived from Json
*/
Author.prototype.setFirstName = function(firstName) {
this.firstName = firstName;
};
/**
* Set the family name as received from Json
*/
Author.prototype.setFamilyName = function(familyName) {
this.familyName = familyName;
};
/**
* Set the single name which contains the first name and the family name
*/
Author.prototype.setSingleName = function(singleName) {
this.singleName = singleName;
};
/**
* Return the raw family name.
* @return {string} the family name if it was found. Null if not.
*/
Author.prototype.getRawFamilyName = function(){
if (this.familyName){
return this.familyName;
} else {
if (this.singleName){
// Check whether the family name can be extracted from the singleChain.
// 1 - play first Card : there are only 2 words in the chain. => second word is the familyName
var findWordsRegExp = new RegExp(/(\S+)/g);
var wordTable = this.singleName.match(findWordsRegExp);
if (wordTable.length === 2){
// there are 2 words
this.familyName = wordTable[1];
return this.familyName;
}
// 2 - play second card : Assumes all family names are FULLY UPPSERCASE in the single string
var familyRegExp = new RegExp(/\s([^a-z]+$)/);
var result = familyRegExp.exec(this.singleName); // null if not found
if (result){
this.familyName = result[1];
}
return this.familyName; //returns null if it not found
}
}
};
/**
* Return the raw first name.
* @return {string} the first name if it was found. Null if not.
*/
Author.prototype.getRawFirstName = function(){
if (this.firstName){
return this.firstName;
} else {
if (this.singleName){
// Check whether the family name can be extracted from the singleChain.
// 1 - play first Card : there are only 2 words in the chain. => first word is the firstName
var findWordsRegExp = new RegExp(/(\S+)/g);
var wordTable = this.singleName.match(findWordsRegExp);
if (wordTable.length === 2){
// there are 2 words
this.firstName = wordTable[0];
return this.firstName;
}
// 2 - play second card : Assumes all first name are lowercase except first letter which can optionally be uppercase
var firstNameRegExp = new RegExp(/^(.[^A-Z]+)+\b/);
var result = firstNameRegExp.exec(this.singleName); // null if not found
if (result){
this.firstName = result[1];
}
return result; //returns null if it not found
}
}
};
/**
* Returns the family name as title name. Only the first letter of each words is
* uppercase.
*/
Author.prototype.getTitleCaseFamilyName = function() {
if (this.familyName) {
allLowerCase = this.familyName.toLowerCase();
var findFirstLetter = new RegExp(/^./);
return allLowerCase.replace(findFirstLetter, function(foundCharacter){return foundCharacter.toUpperCase();});
} else {
console.log ("family name is undefined");
return null;
}
};
/**
* Returns the initials of the first name.
*/
Author.prototype.getFirstNameInitials = function(){
var firstNameInitialsRegExp = new RegExp(/(\S+)/g);
if (this.firstName) {
return this.firstName.replace(firstNameInitialsRegExp, function(foundString){return foundString.charAt(0) + ".";});
}
else {
console.log ("first name is undefined");
return null;
}
};
......@@ -182,44 +182,33 @@ function extractFieldsFromDoi(data) {
data.investigationId = regExp.exec(data.doi)[1];
}
// Create the citation for the current work. Citation proposes a recommendation to the reader
// on how to cite this work.
//@data: the data object, it is a reference
function createCitation(data) {
// Authors sent in json can be in several format. The following call find first name and last name when possible.
// It writes a message in the console in case it fails identifyng them in which case the string containing first anme and last name will
// It writes a message in the console in case it fails identifying them in which case the string containing first name and last name will
// be displayed as it came from json.
var authorList = _.transform(data.creator, function(result, obj) {
var authorList = _.transform(data.creator, function(result, obj) {
var author = null;
if (obj.givenName) {
result.push(obj.familyName + ' ' + obj.givenName);
} else {
// Here first name and last name are in a single string creator.name
// 1 - detect the words in UPPERCASE as FAMILY name
// 2 - detect the words in lowercase except first letter which can optionally be uppercase.
// 3 - flip them
var familyRegExp = new RegExp(/\s([^a-z]+$)/);
var firstNameRegExp = new RegExp(/^(.[^A-Z]+)+\b/);
var familyName = familyRegExp.exec(obj.name);
var firstName = firstNameRegExp.exec(obj.name);
author = new Author();
author.setFamilyName(obj.familyName);
author.setFirstName(obj.givenName);
if (!familyName || !firstName ){
// Sometime all words are uppercase or lowercase or uppercase for the first letter only. In this case
// we can not distinguish between family name and firstname if there are more than 2 words.
// => string unchanged
var findWordsRegExp = new RegExp(/(\S+)/g);
var wordTable = obj.name.match(findWordsRegExp);
if (wordTable.length === 2){
result.push(wordTable[1] + " " + wordTable[0]);
} else {
// there are not 2 words so it is not possible to distingues first name and family name
result.push(author.getTitleCaseFamilyName() + ' ' + author.getFirstNameInitials());
} else {
// Here first name and last name are in a single string field creator.name
author = new Author();
author.setSingleName(obj.name);
if (!author.getRawFamilyName() || !author.getRawFirstName() ){
result.push(obj.name);
}
} else {
result.push(familyName[1] + " " + firstName[0].trim());
// FirstName and family name are not in a single string
result.push(author.getTitleCaseFamilyName() + " " + author.getFirstNameInitials());
}
}
},[]);
......@@ -227,7 +216,7 @@ function createCitation(data) {
var citation = authorString +
'. (' +
' (' +
new Date(data.date_registered).getFullYear() +
'). ' +
data.title +
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment