Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
dev:technologies [2017/11/12 19:53] – external edit 127.0.0.1 | dev:technologies [2017/12/07 13:42] – bwiernik | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | < | + | Three commonly-used technologies |
- | in the process of updating the documentation for | + | |
- | <a href=" | + | |
- | may be outdated in the meantime. Thanks | + | |
+ | ==== CSS Selectors ==== | ||
+ | A comprehensive and accessible resource is available from W3Schools: | ||
+ | https:// | ||
- | A very brief introduction to some commonly used technologies used in [[dev:translators|translator development]]. | + | The Mozilla Development Network (MDN) also provides a CSS Selector resource:\\ |
+ | https:// | ||
- | ====== XPath ====== | + | ==== XPath ==== |
- | XPath provides a way to refer to specific parts of HTML or XML documents. It's usually the best way to extract data from webpages when writing a translator. | + | |
- | An XPath expression | + | A tutorial |
+ | https:// | ||
- | * ''/'' | + | ==== Regex ==== |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * Plus much more. See the [[http:// | + | |
- | The best introduction to XPath for use in translators | + | There are many high-quality regex tutorials available online. A comprehensive guide is provided all in one page at:\\ |
+ | https://github.com/zeeshanu/learn-regex/ | ||
- | === Examples === | + | Regex 101 also provides |
- | <code html> | + | http://regex101.com/ |
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | < | + | |
- | <tr class=" | + | |
- | < | + | |
- | < | + | |
- | </ | + | |
- | </ | + | |
- | </ | + | |
- | </ | + | |
- | </ | + | |
- | For the sample document above, these expressions would refer to... | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | + | ||
- | === In Translators === | + | |
- | When writing Zotero translators, | + | |
- | <code javascript> | + | |
- | var xpath = '// | + | |
- | var tableRows = doc.evaluate(xpath, | + | |
- | </ | + | |
- | Then iterate through the rows using '' | + | |
- | <code javascript> | + | |
- | while (var row = tableRows.iterateNext()) { | + | |
- | // do something with each row | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Starting with Zotero 2.1.9, there are two new utility functions | + | |
- | + | ||
- | <code javascript> | + | |
- | var xpath = '// | + | |
- | var tableRows = Zotero.Utilities.xpath(doc, | + | |
- | for (var row in tableRows) { | + | |
- | // do something with each row | + | |
- | } | + | |
- | </ | + | |
- | ==== Using Firebug to design XPath expressions ==== | + | |
- | To make it easier to design XPath expressions, | + | |
- | + | ||
- | **Note:** The Firebug screenshots here are quite outdated. The functionality, | + | |
- | + | ||
- | First, install Firebug and restart Firefox. You'll see a bug icon at the bottom right hand side of your browser window: | + | |
- | + | ||
- | {{firebug2.png|}} | + | |
- | + | ||
- | {{firebug1.png|}} | + | |
- | + | ||
- | Click the icon to open the Firebug pane, which will show lots of details about the current page: | + | |
- | + | ||
- | {{firebug3.png}} | + | |
- | + | ||
- | Next, click the " | + | |
- | + | ||
- | {{firebug4.png}} | + | |
- | + | ||
- | Firebug will highlight the part of the web page's code that corresponds to the location you clicked on. Right-click on that piece of code in the Firebug pane and select "Copy XPath" | + | |
- | + | ||
- | Then we can paste the XPath expression into the editor (i.e., the code pane of [[dev: | + | |
- | + | ||
- | < | + | |
- | /html/ | + | |
- | </ | + | |
- | + | ||
- | XPath like this, however, is very fragile. It's better to latch onto some identifiers and make an expression relative to them: | + | |
- | < | + | |
- | // | + | |
- | </ | + | |
- | + | ||
- | ====== Regular Expressions ====== | + | |
- | Regular expressions are a way of matching and extracting pieces of text from a larger body of text. In translator development, | + | |
- | + | ||
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * ''?'' | + | |
- | + | ||
- | For a complete description of how regular expressions are used in JavaScript, with examples of their use, see Mozilla' | + | |
- | + | ||
- | There are many regular expressions guides on the web, and entire books on the topic. While you can and should consult these guides, keep in mind that the details of how they work will depend a little on the environment they' | + |