Skip to content

Commit

Permalink
Use zotero/translate and zotero/utilities for shared code
Browse files Browse the repository at this point in the history
  • Loading branch information
adomasven committed Jul 28, 2021
1 parent c929055 commit 7814efc
Show file tree
Hide file tree
Showing 19 changed files with 337 additions and 527 deletions.
8 changes: 8 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@
path = note-editor
url = https://github.com/zotero/note-editor.git
branch = master
[submodule "chrome/content/zotero/xpcom/utilities"]
path = chrome/content/zotero/xpcom/utilities
url = https://github.com/zotero/utilities.git
shallow = true
[submodule "chrome/content/zotero/xpcom/translate"]
path = chrome/content/zotero/xpcom/translate
url = https://github.com/zotero/translate.git
shallow = true
2 changes: 1 addition & 1 deletion chrome/content/zotero/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var Zotero_Lookup = new function () {
* @returns {Promise<boolean>}
*/
this.addItemsFromIdentifier = async function (textBox, childItem, toggleProgress) {
var identifiers = Zotero.Utilities.Internal.extractIdentifiers(textBox.value);
var identifiers = Zotero.Utilities.extractIdentifiers(textBox.value);
if (!identifiers.length) {
Zotero.alert(
window,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ function haveTranslators(translators, type) {

var promises = [];
for(var i in translators) {
promises.push(translators[i].getCode());
promises.push(Zotero.Translators.getCodeForTranslator(translators[i]));
}

return Promise.all(promises).then(function(codes) {
Expand Down
110 changes: 2 additions & 108 deletions chrome/content/zotero/xpcom/cite.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,114 +318,8 @@ Zotero.Cite = {
},

extraToCSL: function (extra) {
return extra.replace(/^([A-Za-z \-]+)(:\s*.+)/gm, function (_, field, value) {
var originalField = field;
var field = field.toLowerCase().replace(/ /g, '-');
// Fields from https://aurimasv.github.io/z2csl/typeMap.xml
switch (field) {
// Standard fields
case 'abstract':
case 'accessed':
case 'annote':
case 'archive':
case 'archive-place':
case 'author':
case 'authority':
case 'call-number':
case 'chapter-number':
case 'citation-label':
case 'citation-number':
case 'collection-editor':
case 'collection-number':
case 'collection-title':
case 'composer':
case 'container':
case 'container-author':
case 'container-title':
case 'container-title-short':
case 'dimensions':
case 'director':
case 'edition':
case 'editor':
case 'editorial-director':
case 'event':
case 'event-date':
case 'event-place':
case 'first-reference-note-number':
case 'genre':
case 'illustrator':
case 'interviewer':
case 'issue':
case 'issued':
case 'jurisdiction':
case 'keyword':
case 'language':
case 'locator':
case 'medium':
case 'note':
case 'number':
case 'number-of-pages':
case 'number-of-volumes':
case 'original-author':
case 'original-date':
case 'original-publisher':
case 'original-publisher-place':
case 'original-title':
case 'page':
case 'page-first':
case 'publisher':
case 'publisher-place':
case 'recipient':
case 'references':
case 'reviewed-author':
case 'reviewed-title':
case 'scale':
case 'section':
case 'source':
case 'status':
case 'submitted':
case 'title':
case 'title-short':
case 'translator':
case 'type':
case 'version':
case 'volume':
case 'year-suffix':
break;

// Uppercase fields
case 'doi':
case 'isbn':
case 'issn':
case 'pmcid':
case 'pmid':
case 'url':
field = field.toUpperCase();
break;

// Weirdo
case 'archive-location':
field = 'archive_location';
break;

default:
// See if this is a Zotero field written out (e.g., "Publication Title"), and if so
// convert to its associated CSL field
var zoteroField = originalField.replace(/ ([A-Z])/, '$1');
// If second character is lowercase (so not an acronym), lowercase first letter too
if (zoteroField[1] && zoteroField[1] == zoteroField[1].toLowerCase()) {
zoteroField = zoteroField[0].toLowerCase() + zoteroField.substr(1);
}
if (Zotero.Schema.CSL_FIELD_MAPPINGS_REVERSE[zoteroField]) {
field = Zotero.Schema.CSL_FIELD_MAPPINGS_REVERSE[zoteroField];
}
// Don't change other lines
else {
field = originalField;
}
}
return field + value;
});
Zotero.debug(`Zotero.Cite.extraToCSL() is deprecated -- use Zotero.Utilities.Item.extraToCSL() instead`);
return Zotero.Utilities.Item.extraToCSL(extra);
}
};

Expand Down
2 changes: 1 addition & 1 deletion chrome/content/zotero/xpcom/connector/server_connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,7 @@ Zotero.Server.Connector.GetTranslatorCode.prototype = {
*/
init: function(postData, sendResponseCallback) {
var translator = Zotero.Translators.get(postData.translatorID);
translator.getCode().then(function(code) {
Zotero.Translators.getCodeForTranslator(translator).then(function(code) {
sendResponseCallback(200, "application/javascript", code);
});
}
Expand Down
2 changes: 1 addition & 1 deletion chrome/content/zotero/xpcom/data/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -2172,7 +2172,7 @@ Zotero.Item.prototype.setNote = function(text) {

this._hasNote = text !== '';
this._noteText = text;
this._noteTitle = Zotero.Notes.noteToTitle(text);
this._noteTitle = Zotero.Utilities.Item.noteToTitle(text);
if (this.isNote()) {
this._displayTitle = this._noteTitle;
}
Expand Down
29 changes: 2 additions & 27 deletions chrome/content/zotero/xpcom/data/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,9 @@ Zotero.Notes = new function() {
this._editorInstances = [];
this._downloadInProgressPromise = null;

/**
* Return first line (or first MAX_LENGTH characters) of note content
**/
this.noteToTitle = function(text) {
var origText = text;
text = text.trim();
text = text.replace(/<br\s*\/?>/g, ' ');
text = Zotero.Utilities.unescapeHTML(text);

// If first line is just an opening HTML tag, remove it
//
// Example:
//
// <blockquote>
// <p>Foo</p>
// </blockquote>
if (/^<[^>\n]+[^\/]>\n/.test(origText)) {
text = text.trim();
}

var max = this.MAX_TITLE_LENGTH;

var t = text.substring(0, max);
var ln = t.indexOf("\n");
if (ln>-1 && ln<max) {
t = t.substring(0, ln);
}
return t;
Zotero.debug(`Zotero.Note.noteToTitle() is deprecated -- use Zotero.Utilities.Item.noteToTitle() instead`);
return Zotero.Utilities.Item.noteToTitle(text);
};

this.registerEditorInstance = function(instance) {
Expand Down
2 changes: 1 addition & 1 deletion chrome/content/zotero/xpcom/quickCopy.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ Zotero.QuickCopy = new function() {
return;
}
translator.cacheCode = true;
await translator.getCode();
await Zotero.Translators.getCodeForTranslator(translator);
}
};

Expand Down
1 change: 1 addition & 0 deletions chrome/content/zotero/xpcom/translate
Submodule translate added at 4eeca1
24 changes: 14 additions & 10 deletions chrome/content/zotero/xpcom/translation/translate_item.js
Original file line number Diff line number Diff line change
Expand Up @@ -1010,17 +1010,17 @@ Zotero.Translate.ItemGetter = function() {
};

Zotero.Translate.ItemGetter.prototype = {
"setItems":function(items) {
setItems: function (items) {
this._itemsLeft = items;
this._itemsLeft.sort(function(a, b) { return a.id - b.id; });
this._itemsLeft.sort((a, b) => a.id - b.id);
this.numItems = this._itemsLeft.length;
},

"setCollection": function (collection, getChildCollections) {
setCollection: function (collection, getChildCollections) {
// get items in this collection
var items = new Set(collection.getChildItems());

if(getChildCollections) {
if (getChildCollections) {
// Get child collections
this._collectionsLeft = Zotero.Collections.getByParent(collection.id);

Expand All @@ -1033,12 +1033,16 @@ Zotero.Translate.ItemGetter.prototype = {
}

this._itemsLeft = Array.from(items.values());
this._itemsLeft.sort(function(a, b) { return a.id - b.id; });
this._itemsLeft.sort((a, b) => a.id - b.id);
this.numItems = this._itemsLeft.length;
},

"setAll": Zotero.Promise.coroutine(function* (libraryID, getChildCollections) {
this._itemsLeft = (yield Zotero.Items.getAll(libraryID, true))
/**
* NOTE: This function should use the Zotero.Promise.method wrapper which adds a
* isResolved property to the returned promise for noWait translation.
*/
setAll: Zotero.Promise.method(async function (libraryID, getChildCollections) {
this._itemsLeft = (await Zotero.Items.getAll(libraryID, true))
.filter((item) => {
// Don't export annotations
switch (item.itemType) {
Expand All @@ -1048,11 +1052,11 @@ Zotero.Translate.ItemGetter.prototype = {
return true;
});

if(getChildCollections) {
if (getChildCollections) {
this._collectionsLeft = Zotero.Collections.getByLibrary(libraryID);
}
this._itemsLeft.sort(function(a, b) { return a.id - b.id; });

this._itemsLeft.sort((a, b) => a.id - b.id);
this.numItems = this._itemsLeft.length;
}),

Expand Down
29 changes: 19 additions & 10 deletions chrome/content/zotero/xpcom/translation/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,31 +326,40 @@ Zotero.Translators = new function() {
*
* @param {String} id The ID of the translator
*/
this.get = function(id) {
this.get = function (id) {
if (!_initialized) {
throw new Zotero.Exception.UnloadedDataException("Translators not yet loaded", 'translators');
}
return _translators[id] ? _translators[id] : false
return _translators[id] ? _translators[id] : false;
}

this.getCodeForTranslator = Zotero.Promise.method(function (translator) {
if (translator.code) return translator.code;
return Zotero.File.getContentsAsync(translator.path).then(function(code) {
if (translator.cacheCode) {
// See Translator.init() for cache rules
translator.code = code;
}
return code;
});
});

/**
* Gets all translators for a specific type of translation
*
* @param {String} type The type of translators to get (import, export, web, or search)
*/
this.getAllForType = function(type) {
return this.init().then(function () {
return _cache[type].slice();
});
this.getAllForType = async function (type) {
await this.init();
return _cache[type].slice();
}

/**
* Gets all translators for a specific type of translation
*/
this.getAll = function() {
return this.init().then(function () {
return Object.keys(_translators).map(id => _translators[id]);
});
this.getAll = async function () {
await this.init();
return Object.keys(_translators).map(id => _translators[id]);
}

/**
Expand Down
1 change: 1 addition & 0 deletions chrome/content/zotero/xpcom/utilities
Submodule utilities added at b89bab
Loading

0 comments on commit 7814efc

Please sign in to comment.