Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
dev:translator_framework [2011/04/07 01:41] – egh | dev:translator_framework [2017/11/12 19:53] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Translator Framework ===== | + | < |
- | The translator framework is a way to build web translators that lets translator authors avoid most of the boilerplate that usually is required | + | in the process |
+ | <a href=" | ||
+ | may be outdated in the meantime. Thanks for your understanding.</ | ||
- | The framework was written and contributed by Erik Hetzner and is licensed under the GPLv3+. It currently resides at http:// | ||
- | To use the framework, simply insert the framework code at the beginning of your translator, after the translator information block (JSON header). If you are using [[dev/scaffold|Scaffold]] to develop your translator, you won't see the information block, and you can just insert the framework at the top of the code box. The latest version of the code is [[http:// | + | See [[dev/translators/Framework]]. |
- | + | ||
- | You'll start writing beneath the line that reads: | + | |
- | ''/ | + | |
- | + | ||
- | ===Example Translator=== | + | |
- | From APN.ru.js (GPLv3+ licensed): | + | |
- | <code javascript> | + | |
- | function detectWeb(doc, | + | |
- | function doWeb(doc, url) { return FW.doWeb(doc, | + | |
- | + | ||
- | /** Articles */ | + | |
- | FW.Scraper({ | + | |
- | itemType | + | |
- | detect | + | |
- | title : FW.Xpath('// | + | |
- | attachments | + | |
- | creators | + | |
- | date : FW.Xpath('// | + | |
- | publicationTitle : " | + | |
- | }); | + | |
- | + | ||
- | /** Search results */ | + | |
- | FW.MultiScraper({ | + | |
- | itemType | + | |
- | detect | + | |
- | titles | + | |
- | urls : FW.Xpath('// | + | |
- | }); | + | |
- | </ | + | |
- | + | ||
- | This is the functional portion of a real, working web translator using the translator framework. It defines two scrapers, in this case one for newspaper articles and one for multiple result pages. | + | |
- | + | ||
- | This is the general model for creating a translator using the framework -- define several scrapers that are triggered by different kinds of page content or URLs. | + | |
- | + | ||
- | ===Scrapers=== | + | |
- | As the example translator above shows, there are two kinds of scrapers in the framework, defined using the functions '' | + | |
- | + | ||
- | Both kinds of scrapers are defined by passing an object with the scraper' | + | |
- | + | ||
- | Each value can be either a string, in which case it is always the same, or a function (FIXME), or a chained series of filters. This last form is most common. In the above example we can see, for instance, the '' | + | |
- | + | ||
- | == FW.Scraper == | + | |
- | * Required keys: '' | + | |
- | * Optional keys: '' | + | |
- | + | ||
- | == FW.MultiScraper == | + | |
- | * Required keys: '' | + | |
- | * Optional keys: '' | + | |
- | + | ||
- | The '' | + | |
- | + | ||
- | The '' | + | |
- | <code javascript> | + | |
- | beforeFilter : function (doc, url) { | + | |
- | var haveBibTeXLinks = FW.Xpath('// | + | |
- | .evaluate(doc); | + | |
- | if(!haveBibTeXLinks) { | + | |
- | url = url.replace (/ | + | |
- | url = url.replace(" | + | |
- | " | + | |
- | } | + | |
- | | + | |
- | } | + | |
- | </ | + | |
- | Here the option is used to guarantee that the multiple item page has links to the BibTeX files that the translator uses. | + | |
- | + | ||
- | == Delegation == | + | |
- | It is possible to have a translator using this framework delegate processing to another translator, by setting the key '' | + | |
- | + | ||
- | <code javascript> | + | |
- | itemTrans : FW.DelegateTranslator({ translatorType : " | + | |
- | translatorId | + | |
- | </ | + | |
- | + | ||
- | ==== Functions ==== | + | |
- | FIXME Functions that can be used with the framework. | + | |
- | === Main functions === | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | === String functions === | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | === Zotero functions === | + | |
- | * '' | + | |
- | * '' | + |