Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
dev:translators:coding [2018/09/27 19:59] – [detectWeb] adamsmithdev:translators:coding [2018/12/23 03:14] – [Export Translators] dstillman
Line 1: Line 1:
 ====== Writing Translator Code ====== ====== Writing Translator Code ======
  
-Below we will describe how the ''detect*'' and ''do*'' functions of Zotero [[dev/translators]] can and should be coded. If you are unfamiliar with JavaScript, make sure to check out a [[https://developer.mozilla.org/en/JavaScript/A_re-introduction_to_JavaScript|JavaScript tutorial]] to get familiar with the syntax. In addition to the information on this page, it can often be very informative to look at existing translators to see how things are done. For an incomplete summary of the main technologiesXPath and regular expressions, used in translators, see [[dev:technologies|the technologies overview]]. +Below we will describe how the ''detect*'' and ''do*'' functions of Zotero [[dev/translators]] can and should be coded. If you are unfamiliar with JavaScript, make sure to check out a [[https://developer.mozilla.org/en/JavaScript/A_re-introduction_to_JavaScript|JavaScript tutorial]] to get familiar with the syntax. In addition to the information on this page, it can often be very informative to look at existing translators to see how things are done. A [[https://www.mediawiki.org/wiki/Citoid/Creating_Zotero_translators|particularly helpful guide]] with up-to-date recommendation on best coding practices is provided by the wikimedia foundationwhose tool Citoid uses Zotero translators
 + 
 +While translators can be written with any text editorthe Zotero add-on [[dev/translators/scaffold|Scaffold]] can make writing them much easier, as it provides the option to test and troubleshoot translators relatively quickly. 
  
-While translators can be written with any text editor, the Firefox add-on [[dev/translators/scaffold|Scaffold]] can make writing them much easier, as it provides the option to test and troubleshoot translators relatively quickly. 
 ====== Web Translators ====== ====== Web Translators ======
  
Line 29: Line 31:
 === Metadata === === Metadata ===
  
-The first step towards saving an item is to create an item object of the desired [[http://gsl-nagoya-u.net/http/pub/csl-fields/index.html|item type]] (examples from "NCBI PubMed.js"):+The first step towards saving an item is to create an item object of the desired [[https://aurimasv.github.io/z2csl/typeMap.xml|item type]] (examples from "NCBI PubMed.js"):
  
 <code javascript>var newItem = new Zotero.Item("journalArticle");</code> <code javascript>var newItem = new Zotero.Item("journalArticle");</code>
  
-Metadata can then be stored in the properties of the object. Of the different fields available for the chosen item type (see the [[http://gsl-nagoya-u.net/http/pub/csl-fields/index.html|Field Index]]), only the title is required. E.g.:+Metadata can then be stored in the properties of the object. Of the different fields available for the chosen item type (see the [[https://aurimasv.github.io/z2csl/typeMap.xml|Field Index]]), only the title is required. E.g.:
  
 <code javascript>var title = article.ArticleTitle.text().toString(); <code javascript>var title = article.ArticleTitle.text().toString();
Line 185: Line 187:
 Export translators use ''Zotero.nextItem()'' and optionally ''Zotero.nextCollection()'' to iterate through the items selected for export, and generally write their output using ''Zotero.write( text )''. A minimal translator might be: Export translators use ''Zotero.nextItem()'' and optionally ''Zotero.nextCollection()'' to iterate through the items selected for export, and generally write their output using ''Zotero.write( text )''. A minimal translator might be:
 <code javascript> <code javascript>
-var item; +function doExport() { 
-while (item = Zotero.nextItem()) { +    var item; 
-     Zotero.write( item.title );+    while (item = Zotero.nextItem()) { 
 +        Zotero.write(item.title); 
 +    }
 } }
 </code> </code>
dev/translators/coding.txt · Last modified: 2023/08/04 01:14 by dstillman