// Scholar for Firefox Utilities ///////////////////////////////////////////////////////////////// // // Scholar.Utilities // ///////////////////////////////////////////////////////////////// Scholar.Utilities = function () {} Scholar.Utilities.prototype.debug = function(msg) { Scholar.debug(msg, 4); } /* * Converts a JavaScript date object to an SQL-style date */ Scholar.Utilities.prototype.dateToSQL = function(jsDate) { var date = ""; var year = jsDate.getFullYear().toString(); var month = (jsDate.getMonth()+1).toString(); var day = jsDate.getDate().toString(); for(var i = year.length; i<4; i++) { date += "0"; } date += year+"-"; if(month.length == 1) { date += "0"; } date += month+"-"; if(day.length == 1) { date += "0"; } date += day; return date; } /* * Cleans extraneous punctuation off an author name */ Scholar.Utilities.prototype.cleanAuthor = function(author, type, useComma) { author = author.replace(/^[\s\.\,\/\[\]\:]+/, ''); author = author.replace(/[\s\,\/\[\]\:\.]+$/, ''); author = author.replace(/ +/, ' '); if(useComma) { // Add period for initials if(author.substr(author.length-2, 1) == " ") { author += "."; } var splitNames = author.split(', '); if(splitNames.length > 1) { var lastName = splitNames[0]; var firstName = splitNames[1]; } else { var lastName = author; } } else { var spaceIndex = author.lastIndexOf(" "); var lastName = author.substring(spaceIndex+1); var firstName = author.substring(0, spaceIndex); } // TODO: take type into account return {firstName:firstName, lastName:lastName, creatorType:type}; } /* * Cleans whitespace off a string and replaces multiple spaces with one */ Scholar.Utilities.prototype.cleanString = function(s) { s = s.replace(/[ \xA0\r\n]+/g, " "); s = s.replace(/^\s+/, ""); return s.replace(/\s+$/, ""); } /* * Cleans any non-word non-parenthesis characters off the ends of a string */ Scholar.Utilities.prototype.superCleanString = function(x) { var x = x.replace(/^[^\w(]+/, ""); return x.replace(/[^\w)]+$/, ""); } /* * Eliminates HTML tags, replacing
s with /ns */ Scholar.Utilities.prototype.cleanTags = function(x) { x = x.replace(/]*>/gi, "\n"); return x.replace(/<[^>]+>/g, ""); } /* * Test if a string is an integer */ Scholar.Utilities.prototype.isInt = function(x) { if(parseInt(x) == x) { return true; } return false; } /* * Get current scholar version */ Scholar.Utilities.prototype.getVersion = function() { return Scholar.version; } /* * Get a page range, given a user-entered set of pages */ Scholar.Utilities.prototype._pageRangeRegexp = /^\s*([0-9]+)-([0-9]+)\s*$/; Scholar.Utilities.prototype.getPageRange = function(pages) { var pageNumbers; var m = this._pageRangeRegexp.exec(pages); if(m) { // A page range pageNumbers = [m[1], m[2]]; } else { // Assume start and end are the same pageNumbers = [pages, pages]; } return pageNumbers; } /* * provide inArray function */ Scholar.Utilities.prototype.inArray = Scholar.inArray; /* * pads a number or other string with a given string on the left */ Scholar.Utilities.prototype.lpad = function(string, pad, length) { while(string.length < length) { string = pad + string; } return string; } /* * returns true if an item type exists, false if it does not */ Scholar.Utilities.prototype.itemTypeExists = function(type) { if(Scholar.ItemTypes.getID(type)) { return true; } else { return false; } } /* * END SCHOLAR FOR FIREFOX EXTENSIONS */ ///////////////////////////////////////////////////////////////// // // Scholar.Utilities.Ingester // ///////////////////////////////////////////////////////////////// // Scholar.Utilities.Ingester extends Scholar.Utilities, offering additional // classes relating to data extraction specifically from HTML documents. Scholar.Utilities.Ingester = function(translate, proxiedURL) { this.translate = translate; } Scholar.Utilities.Ingester.prototype = new Scholar.Utilities(); // Takes an XPath query and returns the results Scholar.Utilities.Ingester.prototype.gatherElementsOnXPath = function(doc, parentNode, xpath, nsResolver) { var elmts = []; var iterator = doc.evaluate(xpath, parentNode, nsResolver, Components.interfaces.nsIDOMXPathResult.ANY_TYPE,null); var elmt = iterator.iterateNext(); var i = 0; while (elmt) { elmts[i++] = elmt; elmt = iterator.iterateNext(); } return elmts; } /* * Gets a given node as a string containing all child nodes */ Scholar.Utilities.Ingester.prototype.getNodeString = function(doc, contextNode, xpath, nsResolver) { var elmts = this.gatherElementsOnXPath(doc, contextNode, xpath, nsResolver); var returnVar = ""; for(var i=0; i