We’re in the process of updating the documentation for Zotero 5.0. Some documentation may be outdated in the meantime. Thanks for your understanding.
Writing Zotero translators can be made much easier by using the right tools. Here are some suggested downloads:
To do: include details on all the useful (but hidden) functions in Zotero.Utilities
When writing translator code, you can make use of a number of functions in Zotero.Utilities. Below each function is described, and an example of its use is given.
Function description
https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L40
Zotero.Utilities.prototype.cleanAuthor = function(author, type, useComma)
@param {String} author Creator string
@param {String} type Creator type string (e.g., “author” or “editor”)
@param {Boolean} useComma Whether the creator string is in inverted (Last, First) format
@return {Object} firstName, lastName, and creatorType
Sometimes it is difficult to extract clean author names from webpages. cleanAuthor
removes white-space and punctuation (.,/[]:) that precedes or follows the author name, and performs some additional clean-up as well (e.g. removal of double spaces). If the author name is inverted (last name first, separated from the first name by a comma or comma-space), set useComma
to true and cleanAuthor
will correctly isolate the last and first name (see example).
Example code
var name = " :Doe, John"; Zotero.debug(Zotero.Utilities.cleanAuthor(name, "author",true));
Example code debug output
'firstName' => "John" 'lastName' => "Doe" 'creatorType' => "author"
Function description
https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L85
Zotero.Utilities.prototype.trim = function(s)
@type String
Removes leading and trailing whitespace from a string
Function description
https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L98
Zotero.Utilities.prototype.trimInternal = function(s)
@type String
Cleans whitespace off a string and replaces multiple spaces with one
Deprecated function, use trimInternal instead.
Function description
https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L123
Zotero.Utilities.prototype.superCleanString = function(x)
@type String
Cleans any non-word non-parenthesis characters off the ends of a string
Function description
https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L136
Zotero.Utilities.prototype.cleanTags = function(x)
@type String
Eliminates HTML tags, replacing each instance of <br> with a newline
Function description
https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L153
Zotero.Utilities.prototype.htmlSpecialChars = function(str)
@type String
Escapes several predefined characters:
and
Function description
https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L189
Zotero.Utilities.prototype.unescapeHTML = function(str)
@type String
Converts all HTML entities in a string into Unicode characters.
Function description
https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L206
Zotero.Utilities.prototype.parseMarkup = function(str)
@return {Array} An array of objects with the following form:
{
type: 'text'|'link',
text: “text content”,
[ attributes: { key1: val [ , key2: val, …] }
}</pre>
Parses a text string for HTML/XUL markup and returns an array of parts. Currently only finds HTML links (<a> tags)
Function description
https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L247
Zotero.Utilities.prototype.isInt = function(x)
@deprecated Use isNaN(parseInt(x))
@type Boolean
Tests if a string is an integer
Function description
https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L260
Zotero.Utilities.prototype.getPageRange = function(pages)
@param {String} Page range to parse
@return {Integer[]} Start and end pages
Parses a page range
Function description
https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L283
Zotero.Utilities.prototype.lpad = function(string, pad, length)
@param {String} string String to pad
@param {String} pad String to use as padding
@length {Integer} length Length of new padded string
@type String
Pads a number or other string with a given string on the left
Function description
https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L342
Zotero.Utilities.prototype.capitalizeTitle = function(string, force)
@param {String} string
@param {Boolean} force Forces title case conversion, even if the capitalizeTitles pref is off
@type String
Cleans a title, converting it to title case and replacing ” :“ with ”:“
Function description
https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L297
Zotero.Utilities.prototype.itemTypeExists = function(type)
@param {String} type Item type
@type Boolean
Tests if an item type exists (: what is the use case for this?)
Function description
https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L311
Zotero.Utilities.prototype.getCreatorsForType = function(type)
@param {String} type Item type
@return {String[]} Creator types
Find valid creator types for a given item type (: what is the use case for this?)
Function description
https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L327
Zotero.Utilities.prototype.getLocalizedCreatorType = function(type)
@param {String} type Creator type
@param {String} Localized creator type
@type Boolean
Gets a creator type name, localized to the current locale (: what is the use case for this?)
Zotero.Utilities.processAsync https://www.zotero.org/trac/browser/extension/branches/1.0/chrome/content/zotero/xpcom/utilities.js#L361
To Do:
processDocuments, HTTP.doGet, HTTP.doPost, getItemArray
Especially for screen scraper translators, knowing which item types (book
, journalArticle
, etc) and item fields (title
, url
, etc) exist in Zotero can be very helpful. Fortunately, the possible item properties can be found in the following source code file (types are listed as “itemTypes” entries, fields as “itemFields”):
http://aurimasv.github.io/z2csl/typeMap.xml
Note that the different item types make use of different combinations of item fields (e.g. the book
item type has the field ISBN
, while the journalArticle
item type lacks this field).
To do: describe how translators can call other translators (annotate existing RIS-translator with a bunch of comments?)
To do: pick some examples of the different types of translators: