2 * (C) Copyright 2009 David Kettler
4 * Use, modification, and distribution are subject to the terms specified in the
7 * Construct a webjump (with completer) to visit URLs referenced from
8 * an index page. An xpath expression is used to extract the indexed
9 * URLs. A specialized form is also provided for gitweb summary
13 require("webjump.js");
15 /* Objects with completion data for index webjumps. */
18 define_variable("index_webjumps_directory", null,
19 "A directory (instance of nsILocalFile) for storing the " +
20 "index files corresponding to index webjumps; the index " +
21 "data can be downloaded from the index URL using " +
22 "webjump-get-index. " +
23 "If the index file is available for an index webjump then " +
24 "the webjump will provide completions for the indexed URLs.");
26 define_variable("index_xpath_webjump_tidy_command",
27 "tidy -asxhtml -wrap 0 -numeric --clean yes" +
28 " -modify -quiet --show-warnings no",
29 "A command to run on the downloaded index. The xulrunner " +
30 "parser is quite fussy and specifically requires xhtml (or " +
31 "other xml). Running something like html tidy can avoid " +
34 function index_webjump (key, url, file) {
37 this.file = this.canonicalize_file(file);
38 if (this.require_completions && ! this.file)
39 throw interactive_error("Index file not defined for " + this.key);
41 index_webjump.prototype = {
42 constructor: index_webjump,
45 make_completion: null,
46 require_completions: false,
51 /* Extract full completion list from index file. */
52 extract_completions: function () {
53 /* Parse the index file. */
54 var stream = Cc["@mozilla.org/network/file-input-stream;1"]
55 .createInstance(Ci.nsIFileInputStream);
56 stream.init(this.file, MODE_RDONLY, 0644, false);
57 var parser = Cc["@mozilla.org/xmlextras/domparser;1"]
58 .createInstance(Ci.nsIDOMParser);
59 // todo: catch parser errors
60 var doc = parser.parseFromStream(stream, null,
61 this.file.fileSize, this.mime_type);
63 /* Extract the completion items. */
64 var cmpl = [], node, res;
66 this.xpath_expr, doc, xpath_lookup_namespace,
67 Ci.nsIDOMXPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
68 while ((node = res.iterateNext()))
69 cmpl.push(this.make_completion(node));
71 cmpl.sort(function(a, b) {
72 if (a[1] < b[1]) return -1;
73 if (a[1] > b[1]) return 1;
74 if (a[0] < b[0]) return -1;
75 if (a[0] > b[0]) return 1;
79 this.completions = cmpl;
82 /* The guts of the completer. */
83 internal_completer: function (input, pos, conservative) {
84 if (pos == 0 && conservative)
85 yield co_return(undefined);
87 let require = this.require_completions;
89 /* Update full completion list if necessary. */
90 if (require && ! this.file.exists())
91 throw interactive_error("Index file missing for " + this.key);
92 if (this.file.exists() &&
93 this.file.lastModifiedTime > this.file_time)
95 this.file_time = this.file.lastModifiedTime;
96 this.extract_completions();
98 if (require && !(this.completions && this.completions.length))
99 throw interactive_error("No completions for " + this.key);
100 if (! this.completions)
101 yield co_return(null);
103 /* Match completions against input. */
104 let words = trim_whitespace(input.toLowerCase()).split(/\s+/);
105 let data = this.completions.filter(function (x) {
106 for (var i = 0; i < words.length; ++i) {
107 if (x[0].toLowerCase().indexOf(words[i]) == -1 &&
108 x[1].toLowerCase().indexOf(words[i]) == -1)
116 let c = { count: data.length,
117 get_string: function (i) data[i][0],
118 get_description: function (i) data[i][1],
119 get_input_state: function (i) [data[i][0]],
120 get_match_required: function() require
125 /* A completer suitable for supplying to define_webjump. */
126 make_completer: function () {
130 return function (input, pos, conservative) {
131 return jmp.internal_completer(input, pos, conservative);
135 /* Fetch and save the index for later use with completion.
136 * (buffer is used only to associate with the download) */
137 get_index: function (buffer) {
139 throw interactive_error("Index file not defined for " + this.key);
141 var info = save_uri(load_spec(this.url), this.file,
142 $buffer = buffer, $use_cache = false,
145 // Note: it would be better to run this before the temp file
146 // is renamed; that requires support in save_uri.
147 if (this.tidy_command)
148 info.set_shell_command(this.tidy_command, index_webjumps_directory);
151 /* Try to make a suitable file object when the supplied file is a
153 canonicalize_file: function (file) {
154 if (typeof file == 'string')
155 file = make_file(file);
156 if (! file && index_webjumps_directory) {
157 file = Cc["@mozilla.org/file/local;1"]
158 .createInstance(Ci.nsILocalFile);
159 file.initWithFile(index_webjumps_directory);
160 file.appendRelativePath(this.key + ".index");
167 function index_webjump_xhtml (key, url, file, xpath_expr) {
168 index_webjump.call(this, key, url, file);
169 this.xpath_expr = xpath_expr;
171 index_webjump_xhtml.prototype = {
172 constructor: index_webjump_xhtml,
173 require_completions: true,
174 mime_type: "application/xhtml+xml",
175 tidy_command: index_xpath_webjump_tidy_command,
177 make_completion: function (node) {
178 return [makeURLAbsolute(this.url, node.href), node.text];
181 make_handler: function () {
183 return function (term) {
184 if (!(jmp.completions && jmp.completions.length))
185 throw interactive_error("Completions required for " + this.key);
190 __proto__: index_webjump.prototype
194 function index_webjump_gitweb (key, url, file) {
195 index_webjump.call(this, key, url, file);
197 index_webjump_gitweb.prototype = {
198 constructor: index_webjump_gitweb,
199 mime_type: "text/xml",
200 xpath_expr: '//outline[@type="rss"]',
201 make_completion: function (node) {
202 var name = node.getAttribute("text");
203 return [name.replace(/\.git$/, ""), ""];
205 __proto__: index_webjump.prototype
209 interactive("webjump-get-index",
210 "Fetch and save the index URL corresponding to an index " +
211 "webjump. It will then be available to the completer.",
213 var completions = [];
214 for (let i in index_webjumps)
217 var key = yield I.minibuffer.read(
218 $prompt = "Fetch index for index webjump:",
219 $history = "webjump",
220 $completer = all_word_completer(
221 $completions = completions),
222 $match_required = true);
223 var jmp = index_webjumps[key];
225 jmp.get_index(I.buffer);
229 * Construct a webjump to visit URLs referenced from an index page.
231 * The index page must be able to be parsed as xhtml. The anchor
232 * nodes indexed are those that match the given xpath_expr. Don't
233 * forget to use xhtml: prefixes on the xpath steps.
235 * If an alternative is not specified then it is set to the index page.
237 * A completer is provided that uses the index page. A local file for
238 * the index must be specified either with $index_file or via
239 * index_webjumps_directory. The index must be manually downloaded;
240 * eg. using webjump-get-index. Each time the completer is used it
241 * will check if the file has been updated and reload if necessary.
242 * This kind of webjump is not useful without the completions.
244 define_keywords("$alternative", "$index_file", "$description");
245 function define_xpath_webjump (key, index_url, xpath_expr) {
247 let alternative = arguments.$alternative || index_url;
248 var jmp = new index_webjump_xhtml(key, index_url, arguments.$index_file,
250 index_webjumps[key] = jmp;
251 define_webjump(key, jmp.make_handler(),
252 $completer = jmp.make_completer(),
253 $alternative = alternative,
254 $description = arguments.$description);
258 * Modify the xpath for an index webjump and show the resulting
259 * completions. Useful for figuring out an appropriate xpath. Either
260 * run using mozrepl or eval in the browser with the dump parameter
263 function index_webjump_try_xpath (key, xpath_expr, dump) {
264 jmp = index_webjumps[key];
266 jmp.xpath_expr = xpath_expr;
267 jmp.extract_completions();
269 dumpln(dump_obj(jmp.completions,
270 "Completions for index webjump " + key));
271 return jmp.completions;
276 * Construct a webjump to visit repository summary pages at a gitweb
279 * If a repository name is supplied as $default then the alternative
280 * url is set to that repository at the gitweb site. If an
281 * alternative is not specified by either $default or $alternative
282 * then it is set to the repository list page of the gitweb site.
284 * A completer is provided that uses the list of repositories from the
285 * OPML data on the gitweb server. The completer is setup in the same
286 * way as for define_xpath_webjump, but the webjump will work without
289 define_keywords("$default", "$alternative", "$opml_file", "$description");
290 function define_gitweb_summary_webjump (key, base_url) {
292 let alternative = arguments.$alternative;
293 let gitweb_url = base_url + "/gitweb.cgi";
294 let summary_url = gitweb_url + "?p=%s.git;a=summary";
295 let opml_url = gitweb_url + "?a=opml";
297 if (arguments.$default)
298 alternative = summary_url.replace("%s", arguments.$default);
300 alternative = gitweb_url;
302 var jmp = new index_webjump_gitweb(key, opml_url, arguments.$opml_file);
303 index_webjumps[key] = jmp;
305 define_webjump(key, summary_url,
306 $completer = jmp.make_completer(),
307 $alternative = alternative,
308 $description = arguments.$description);
311 provide("index-webjump");