Merge pull request #253 from simonster/abbreviations
Support journal abbreviations
This commit is contained in:
commit
fdf1c35114
6 changed files with 156 additions and 2 deletions
|
@ -564,8 +564,9 @@ To add a new preference:
|
|||
helpTopic="cite">
|
||||
|
||||
<preferences id="zotero-preferences-cite">
|
||||
<preference id="pref-cite-citePaperJournalArticleURL" name="extensions.zotero.export.citePaperJournalArticleURL" type="bool"/>
|
||||
<preference id="pref-cite-useClassicAddCitationDialog" name="extensions.zotero.integration.useClassicAddCitationDialog" type="bool"/>
|
||||
<preference id="pref-cite-citePaperJournalArticleURL" name="extensions.zotero.export.citePaperJournalArticleURL" type="bool"/>
|
||||
<preference id="pref-cite-automaticTitleAbbreviation" name="extensions.zotero.cite.automaticTitleAbbreviation" type="bool"/>
|
||||
</preferences>
|
||||
|
||||
<tabbox>
|
||||
|
@ -611,6 +612,8 @@ To add a new preference:
|
|||
<label id="export-citePaperJournalArticleURL" width="45em">
|
||||
&zotero.preferences.export.citePaperJournalArticleURL.description;
|
||||
</label>
|
||||
|
||||
<checkbox label="&zotero.preferences.cite.styles.automaticTitleAbbreviation;" preference="pref-cite-automaticTitleAbbreviation"/>
|
||||
</groupbox>
|
||||
</tabpanel>
|
||||
</tabpanels>
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
"use strict";
|
||||
|
||||
/**
|
||||
* Utility functions for dealing with citations
|
||||
* @namespace
|
||||
|
@ -471,3 +473,145 @@ Zotero.Cite.System = {
|
|||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Functions for creating and manipulating field abbreviations
|
||||
* @namespace
|
||||
*/
|
||||
Zotero.Cite.Abbreviations = new function() {
|
||||
var abbreviations,
|
||||
abbreviationCategories;
|
||||
|
||||
/**
|
||||
* Initialize abbreviations database.
|
||||
*/
|
||||
function init() {
|
||||
if(!abbreviations) Zotero.Cite.Abbreviations.loadAbbreviations();
|
||||
}
|
||||
|
||||
this.loadAbbreviations = function() {
|
||||
var file = Zotero.getZoteroDirectory();
|
||||
file.append("abbreviations.json");
|
||||
|
||||
var json, origin;
|
||||
if(file.exists()) {
|
||||
json = Zotero.File.getContents(file);
|
||||
origin = file.path;
|
||||
} else {
|
||||
json = Zotero.File.getContentsFromURL("resource://zotero/schema/abbreviations.json");
|
||||
origin = "resource://zotero/schema/abbreviations.json";
|
||||
}
|
||||
|
||||
try {
|
||||
abbreviations = JSON.parse(json);
|
||||
} catch(e) {
|
||||
throw new Zotero.Exception.Alert("styles.abbreviations.parseError", origin,
|
||||
"styles.abbreviations.title", e);
|
||||
}
|
||||
|
||||
if(!abbreviations.info || !abbreviations.info.name || !abbreviations.info.URI) {
|
||||
throw new Zotero.Exception.Alert("styles.abbreviations.missingInfo", origin,
|
||||
"styles.abbreviations.title");
|
||||
}
|
||||
|
||||
abbreviationCategories = {};
|
||||
for(var jurisdiction in abbreviations) {
|
||||
for(var category in abbreviations[jurisdiction]) {
|
||||
abbreviationCategories[category] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes a key
|
||||
*/
|
||||
function normalizeKey(key) {
|
||||
// Strip periods, normalize spacing, and convert to lowercase
|
||||
return key.toString().toLowerCase().
|
||||
replace(/(?:\b|^)(?:and|et|y|und|l[ae]|the|[ld]')(?:\b|$)|[\x21-\x2C.\/\x3A-\x40\x5B-\x60\\\x7B-\x7E]/g, "").
|
||||
replace(/\s+/g, " ").trim();
|
||||
}
|
||||
|
||||
function lookupKey(key) {
|
||||
return key.toLowerCase().replace(/\s*\./g, "." );
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace getAbbreviation on citeproc-js with our own handler.
|
||||
*/
|
||||
Zotero.CiteProc.CSL.getAbbreviation = function getAbbreviation(listname, obj, jurisdiction, category, key) {
|
||||
if(!Zotero.Prefs.get("cite.automaticTitleAbbreviation")) return;
|
||||
|
||||
init();
|
||||
|
||||
// Short circuit if we know we don't handle this kind of abbreviation
|
||||
if(!abbreviationCategories[category] && !abbreviationCategories[category+"-word"]) return;
|
||||
|
||||
var normalizedKey = normalizeKey(key),
|
||||
lcNormalizedKey = lookupKey(normalizedKey),
|
||||
abbreviation;
|
||||
if(!normalizedKey) return;
|
||||
|
||||
var jurisdictions = ["default"];
|
||||
if(jurisdiction !== "default" && abbreviations[jurisdiction]) {
|
||||
jurisdictions.unshift(jurisdiction);
|
||||
}
|
||||
|
||||
// Look for full abbreviation
|
||||
var jur, cat;
|
||||
for(var i=0; i<jurisdictions.length && !abbreviation; i++) {
|
||||
if((jur = abbreviations[jurisdictions[i]]) && (cat = jur[category])) {
|
||||
abbreviation = cat[lcNormalizedKey];
|
||||
}
|
||||
}
|
||||
|
||||
if(!abbreviation) {
|
||||
// Abbreviate words individually
|
||||
var words = normalizedKey.split(/([ \-])/);
|
||||
|
||||
if(words.length > 1) {
|
||||
for(var j=0; j<words.length; j+=2) {
|
||||
var word = words[j],
|
||||
lcWord = lookupKey(word),
|
||||
newWord = undefined;
|
||||
|
||||
for(var i=0; i<jurisdictions.length && newWord === undefined; i++) {
|
||||
if(!(jur = abbreviations[jurisdictions[i]])) continue;
|
||||
if(!(cat = jur[category+"-word"])) continue;
|
||||
|
||||
// Complete match
|
||||
if(cat.hasOwnProperty(lcWord)) {
|
||||
newWord = cat[lcWord];
|
||||
} else {
|
||||
// Partial match
|
||||
for(var k=1; k<=word.length && newWord === undefined; k++) {
|
||||
newWord = cat[lcWord.substr(0, k)+"-"];
|
||||
if(newWord && word.length - newWord.length < 1) {
|
||||
newWord = undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to full word
|
||||
if(newWord === undefined ) newWord = word;
|
||||
|
||||
words[j] = newWord.substr(0, 1).toUpperCase() + newWord.substr(1);
|
||||
}
|
||||
}
|
||||
abbreviation = words.join("").replace(/\s+/g, " ").trim();
|
||||
}
|
||||
|
||||
if(!abbreviation || abbreviation === key) {
|
||||
Zotero.debug("No abbreviation found for "+key);
|
||||
return;
|
||||
}
|
||||
Zotero.debug("Abbreviated "+key+" as "+abbreviation);
|
||||
|
||||
// Add to jurisdiction object
|
||||
if(!obj[jurisdiction]) {
|
||||
obj[jurisdiction] = new Zotero.CiteProc.CSL.AbbreviationSegments();
|
||||
}
|
||||
obj[jurisdiction][category][key] = abbreviation;
|
||||
}
|
||||
};
|
|
@ -118,6 +118,7 @@
|
|||
<!ENTITY zotero.preferences.cite.styles.styleManager.title "Title">
|
||||
<!ENTITY zotero.preferences.cite.styles.styleManager.updated "Updated">
|
||||
<!ENTITY zotero.preferences.cite.styles.styleManager.csl "CSL">
|
||||
<!ENTITY zotero.preferences.cite.styles.automaticTitleAbbreviation "Automatically abbreviate journal titles">
|
||||
<!ENTITY zotero.preferences.export.getAdditionalStyles "Get additional styles...">
|
||||
|
||||
<!ENTITY zotero.preferences.prefpane.keys "Shortcuts">
|
||||
|
|
|
@ -669,6 +669,10 @@ styles.installSourceError = %1$S references an invalid or non-existent CSL fil
|
|||
styles.deleteStyle = Are you sure you want to delete the style "%1$S"?
|
||||
styles.deleteStyles = Are you sure you want to delete the selected styles?
|
||||
|
||||
styles.abbreviations.title = Load Abbreviations
|
||||
styles.abbreviations.parseError = The abbreviations file "%1$S" is not valid JSON.
|
||||
styles.abbreviations.missingInfo = The abbreviations file "%1$S" does not specify a complete info block.
|
||||
|
||||
sync.cancel = Cancel Sync
|
||||
sync.openSyncPreferences = Open Sync Preferences
|
||||
sync.resetGroupAndSync = Reset Group and Sync
|
||||
|
|
|
@ -93,8 +93,9 @@ pref("extensions.zotero.export.translatorSettings", 'true,false');
|
|||
pref("extensions.zotero.export.lastStyle", 'http://www.zotero.org/styles/chicago-note-bibliography');
|
||||
pref("extensions.zotero.export.bibliographySettings", 'save-as-rtf');
|
||||
pref("extensions.zotero.export.bibliographyLocale", '');
|
||||
pref("extensions.zotero.export.citePaperJournalArticleURL", false);
|
||||
pref("extensions.zotero.export.displayCharsetOption", false);
|
||||
pref("extensions.zotero.export.citePaperJournalArticleURL", false);
|
||||
pref("extensions.zotero.cite.automaticTitleAbbreviation", false);
|
||||
pref("extensions.zotero.import.charset", "auto");
|
||||
pref("extensions.zotero.import.createNewCollection.fromFileOpenHandler", true);
|
||||
pref("extensions.zotero.rtfScan.lastInputFile", "");
|
||||
|
|
1
resource/schema/abbreviations.json
Normal file
1
resource/schema/abbreviations.json
Normal file
File diff suppressed because one or more lines are too long
Loading…
Add table
Reference in a new issue