File size: 10,200 Bytes
d736789 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
const warnings = (function () {
const fieldCrossRefMap = {
doi: function (result) { return result.DOI },
journal: function (result) { return result['container-title'][0] },
pages: function (result) { return result.page },
title: function (result) { return result.title[0] }
}
return {
expectedFields: {
inproceedings: ['author', 'booktitle', 'pages', 'publisher', 'title', 'year', 'doi'],
article: ['author', 'journal', 'number', 'pages', 'title', 'volume', 'year', 'doi'],
techreport: ['author', 'institution', 'title', 'year'],
incollection: ['author', 'booktitle', 'pages', 'publisher', 'title', 'year'],
book: ['author', 'publisher', 'title', 'year'],
inbook: ['author', 'booktitle', 'pages', 'publisher', 'title', 'year'],
proceedings: ['editor', 'publisher', 'title', 'year'],
phdthesis: ['author', 'school', 'title', 'year'],
mastersthesis: ['author', 'school', 'title', 'year'],
bachelorsthesis: ['author', 'school', 'title', 'year'],
electronic: ['author', 'title', 'url', 'year'],
misc: ['author', 'howpublished', 'title', 'year']
},
computeAllWarnings: function (entries) {
var that = this;
var warnings = {};
$.each(entries, function (id, entry) {
warnings[id] = that.computeWarnings(entry);
});
return warnings;
},
computeWarnings: function (entry) {
var warningsList = [];
if (editable) {
warningsList = warningsList.concat(warnings.computeMissingFieldWarning(entry));
warningsList = warningsList.concat(warnings.computeTitleCapitalizationWarning(entry));
warningsList = warningsList.concat(warnings.computeProtectedIdentifierCapitalizationWarning(entry));
$.each(entry, function (field, value) {
computeFirstNameUnknown(warningsList, field, value);
computeWholeFieldCapitalizationProtected(warningsList, field, value);
});
}
return warningsList;
},
computeMissingFieldWarning: function (entry) {
function condenseTitle(title) {
return title.toLowerCase().replace(/\W+/g, '');
}
var warningsList = [];
if (entry) {
var expected = warnings.expectedFields[entry['type']];
if (expected) {
$.each(expected, function (i, field) {
if (!entry[field] || entry[field].trim() === '') {
warningsList.push({
type: `missing or empty field '${field}'`,
fix: {
description: 'try to load from CrossRef', 'function': function (onFix) {
page.notify('Loading data from CrossRef...');
const searchString = (entry.doi ? `/${entry.doi}` : `?query=${entry.title.replace(/\W+/g, '+')}`);
fetch(`https://api.crossref.org/works${searchString}`).then(response => {
return response.json()
}).then(data => {
if (!data.message) {
throw 'No return message';
}
const result = (entry.doi ? data.message : data.message.items[0]);
if (!entry.doi && condenseTitle(entry.title) != condenseTitle(result.title[0])) {
throw `Titles do not match: "${entry.title}" and "${result.title[0]}"`;
}
page.notify(`Paper found on CrossRef titled '${result.title}'`);
let value = (fieldCrossRefMap[field] ? fieldCrossRefMap[field](result) : result[field]);
if (!value) {
page.notify(`However, field '${field}' not available in the CrossRef record.`, 'error')
} else {
if (field === 'pages') {
value = value.replace('-', '--');
}
entry[field] = value;
page.notify(`Updated field '${field}' with value '${value}'.`)
onFix(entry);
}
}).catch(error => {
console.error(error);
page.notify(`Could not find a publication with this ${(entry.doi ? 'DOI' : 'title')} on CrossRef.`, 'error');
});
}
}
});
}
});
}
}
return warningsList;
},
computeTitleCapitalizationWarning: function (entry) {
if (!entry) {
return [];
}
var warningsList = [];
$.each(['booktitle', 'journal'], function (i, field) {
if (entry[field]) {
var value = entry[field];
var capitalizationCorrect = true;
var correctedValue = '';
$.each(value.split(' '), function (i, word) {
var capitalizedWord = word;
if (!word.match(/\d.*/)) {
if ((word.length >= 5 || i == 0) && word.toLowerCase() == word) {
capitalizationCorrect = false;
capitalizedWord = word.charAt(0).toUpperCase() + word.substring(1);
}
}
correctedValue += (correctedValue ? ' ' : '') + capitalizedWord;
});
if (!capitalizationCorrect) {
warningsList.push({
type: 'capitalization in field "' + field + '"',
fix: {
description: 'capitalize longer words', 'function': function (onFix) {
entry[field] = correctedValue;
onFix(entry);
}
}
});
}
}
});
return warningsList;
},
computeProtectedIdentifierCapitalizationWarning: function (entry) {
if (!entry) {
return [];
}
var warningsList = [];
$.each(['title'], function (i, field) {
if (entry[field]) {
var value = entry[field];
var capitalizationCorrect = true;
var correctedValue = value;
if (value.indexOf('{') < 0) {
$.each(value.split(/[\s,:\-()\/]+/), function (i, word) {
if (!word.startsWith('{')) {
var subword = word.substring(1);
if (subword.length > 0 && subword.toLowerCase() != subword) {
capitalizationCorrect = false;
var re = new RegExp('\{?' + word + '\}?', 'g');
correctedValue = correctedValue.replace(re, '{' + word + '}');
}
}
});
if (!capitalizationCorrect) {
warningsList.push({
type: 'non-protected capitalization of identifier in field "' + field + '"',
fix: {
description: 'protect identifiers', 'function': function (onFix) {
entry[field] = correctedValue;
onFix(entry);
}
}
});
}
}
}
});
return warningsList;
}
};
function computeFirstNameUnknown(warningsList, field, value) {
if (field === 'author' || field === 'editor') {
var firstNameKnown = true;
$.each(value.split(' and '), function (i, name) {
var parsedName = name.trim().split(',');
if (parsedName.length > 1) {
var firstName = parsedName[1].trim().split(' ')[0].trim();
if (firstName.indexOf('.') > 0 || firstName.length < 2) {
firstNameKnown = false;
}
} else {
firstNameKnown = false;
}
});
if (!firstNameKnown) {
warningsList.push('unknown or abbreviated first name in field "' + field + '"');
}
}
}
function computeWholeFieldCapitalizationProtected(warningsList, field, value) {
if (value.indexOf('{') === 0 && value.lastIndexOf('}') === value.length - 1 && value.length > 10 && (value.split("{").length - 1 === 1)) {
warningsList.push('whole field "' + field + '" with protected capitalization');
}
}
})(); |