commit abce94458b3635276015f59e34247b749180722d
parent aa005041a8d06933ab772f1b87b7c84b72a05912
Author: Simon Kornblith <simon@simonster.com>
Date: Fri, 24 Oct 2014 16:10:08 -0400
Merge pull request #535 from aurimasv/lookup-doi+isbn
[RecognizePDF] Don't skip ISBN search if a DOI search fails
Diffstat:
2 files changed, 55 insertions(+), 25 deletions(-)
diff --git a/chrome/content/zotero/recognizePDF.js b/chrome/content/zotero/recognizePDF.js
@@ -95,30 +95,34 @@ var Zotero_RecognizePDF = new function() {
// Look up DOI
Zotero.debug("RecognizePDF: Found DOI: "+doi);
- var translate = new Zotero.Translate.Search();
- translate.setTranslator("11645bd1-0420-45c1-badb-53fb41eeb753");
- translate.setSearch({"itemType":"journalArticle", "DOI":doi});
- promise = _promiseTranslate(translate, libraryID);
+ var translateDOI = new Zotero.Translate.Search();
+ translateDOI.setTranslator("11645bd1-0420-45c1-badb-53fb41eeb753");
+ translateDOI.setSearch({"itemType":"journalArticle", "DOI":doi});
+ promise = _promiseTranslate(translateDOI, libraryID);
} else {
- // Look for ISBNs if no DOI
- var isbns = _findISBNs(allText);
- if(isbns.length) {
- Zotero.debug("RecognizePDF: Found ISBNs: " + isbns);
-
- var translate = new Zotero.Translate.Search();
- translate.setTranslator("c73a4a8c-3ef1-4ec8-8229-7531ee384cc4");
- translate.setSearch({"itemType":"book", "ISBN":isbns[0]});
- promise = _promiseTranslate(translate, libraryID);
- } else {
- promise = Q.reject("No ISBN or DOI found");
- }
+ promise = Q.reject("No DOI found in text");
}
- // If no DOI or ISBN, query Google Scholar
- return promise.fail(function(error) {
- Zotero.debug("RecognizePDF: "+error);
- return me.GSFullTextSearch.findItem(lines, libraryID, stopCheckCallback);
- });
+ return promise
+ // Look for ISBNs if no DOI
+ .fail(function(error) {
+ Zotero.debug("RecognizePDF: " + error);
+ var isbns = _findISBNs(allText);
+ if (isbns.length) {
+ Zotero.debug("RecognizePDF: Found ISBNs: " + isbns);
+
+ var translate = new Zotero.Translate.Search();
+ translate.setSearch({"itemType":"book", "ISBN":isbns[0]});
+ return _promiseTranslate(translate, libraryID);
+ } else {
+ return Q.reject("No ISBN found in text.");
+ }
+ })
+ // If no DOI or ISBN, query Google Scholar
+ .fail(function(error) {
+ Zotero.debug("RecognizePDF: " + error);
+ return me.GSFullTextSearch.findItem(lines, libraryID, stopCheckCallback);
+ });
});
}
@@ -196,7 +200,10 @@ var Zotero_RecognizePDF = new function() {
if(success && translate.newItems.length) {
deferred.resolve(translate.newItems[0]);
} else {
- deferred.reject("Translation with Google Scholar failed");
+ deferred.reject(translate.translator && translate.translator.length
+ ? "Translation with " + translate.translator.map(t => t.label) + " failed"
+ : "Could not find a translator for given search item"
+ );
}
});
translate.translate(libraryID, false);
diff --git a/chrome/content/zotero/xpcom/translation/translate.js b/chrome/content/zotero/xpcom/translation/translate.js
@@ -1112,13 +1112,23 @@ Zotero.Translate.Base.prototype = {
* @param {Boolean} [saveAttachments=true] Exclude attachments (e.g., snapshots) on import
*/
"translate":function(libraryID, saveAttachments) { // initialize properties specific to each translation
- this._currentState = "translate";
-
if(!this.translator || !this.translator.length) {
- this.complete(false, new Error("No translator specified"));
+ var args = arguments;
+ Zotero.debug("Translate: translate called without specifying a translator. Running detection first.");
+ this.setHandler('translators', function(me, translators) {
+ if(!translators.length) {
+ me.complete(false, "Could not find an appropriate translator");
+ } else {
+ me.setTranslator(translators);
+ Zotero.Translate.Base.prototype.translate.apply(me, args);
+ }
+ });
+ this.getTranslators();
return;
}
+ this._currentState = "translate";
+
this._libraryID = libraryID;
this._saveAttachments = saveAttachments === undefined || saveAttachments;
this._savingAttachments = [];
@@ -2159,6 +2169,19 @@ Zotero.Translate.Export.prototype._prepareTranslation = function() {
}
/**
+ * Overload Zotero.Translate.Base#translate to make sure that
+ * Zotero.Translate.Export#translate is not called without setting a
+ * translator first. Doesn't make sense to run detection for export.
+ */
+Zotero.Translate.Export.prototype.translate = function() {
+ if(!this.translator || !this.translator.length) {
+ this.complete(false, new Error("Export translation initiated without setting a translator"));
+ } else {
+ Zotero.Translate.Base.prototype.translate.apply(this, arguments);
+ }
+};
+
+/**
* Return the progress of the import operation, or null if progress cannot be determined
*/
Zotero.Translate.Export.prototype.getProgress = function() {