Debian package: Support xulrunner 9+10 in debian/conkeror.bin, drop support for unver...
[conkeror.git] / modules / index-webjump.js
blob83485ada44031fe5571e5d67dec7e8596f32fa14
1 /**
2  * (C) Copyright 2009 David Kettler
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6  *
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
10  * pages.
11 **/
13 in_module(null);
15 require("webjump.js");
17 /* Objects with completion data for index webjumps. */
18 index_webjumps = {};
20 define_variable("index_webjumps_directory", null,
21                 "A directory (instance of nsILocalFile) for storing the " +
22                 "index files corresponding to index webjumps; the index " +
23                 "data can be downloaded from the index URL using " +
24                 "webjump-get-index.  " +
25                 "If the index file is available for an index webjump then " +
26                 "the webjump will provide completions for the indexed URLs.");
28 define_variable("index_xpath_webjump_tidy_command",
29                 "tidy -asxhtml -wrap 0  -numeric --clean yes" +
30                 " -modify -quiet --show-warnings no",
31                 "A command to run on the downloaded index.  The xulrunner " +
32                 "parser is quite fussy and specifically requires xhtml (or " +
33                 "other xml).  Running something like html tidy can avoid " +
34                 "parser problems.");
36 function index_webjump(key, url, file) {
37     this.key = key;
38     this.url = url;
39     this.file = this.canonicalize_file(file);
41     if (this.require_completions && !this.file)
42         throw interactive_error("Index file not defined for " + this.key);
44 index_webjump.prototype = {
45     constructor : index_webjump,
47     mime_type : null,
48     xpath_expr : null,
49     make_completion : null,
50     require_completions : false,
51     completions : null,
52     file_time : 0,
53     tidy_command : null,
55     /* Extract full completion list from index file. */
56     extract_completions : function () {
57         /* Parse the index file. */
58         var stream = Cc["@mozilla.org/network/file-input-stream;1"]
59             .createInstance(Ci.nsIFileInputStream);
60         stream.init(this.file, MODE_RDONLY, 0644, false);
61         var parser = Cc["@mozilla.org/xmlextras/domparser;1"]
62             .createInstance(Ci.nsIDOMParser);
63         // todo: catch parser errors
64         var doc = parser.parseFromStream(stream, null,
65                                          this.file.fileSize, this.mime_type);
67         /* Extract the completion items. */
68         var cmpl = [], node, res;
69         res = doc.evaluate(
70             this.xpath_expr, doc, xpath_lookup_namespace,
71             Ci.nsIDOMXPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
72         while ((node = res.iterateNext()))
73             cmpl.push(this.make_completion(node));
75         cmpl.sort(function(a, b) {
76             if (a[1] < b[1])  return -1;
77             if (a[1] > b[1])  return 1;
78             if (a[0] < b[0])  return -1;
79             if (a[0] > b[0])  return 1;
80             return 0;
81         });
83         this.completions = cmpl;
84     },
86     /* The guts of the completer. */
87     internal_completer : function (input, pos, conservative) {
88         if (pos == 0 && conservative)
89             yield co_return(undefined);
91         let require = this.require_completions;
93         /* Update full completion list if necessary. */
94         if (require && !this.file.exists())
95             throw interactive_error("Index file missing for " + this.key);
96         if (this.file.exists() &&
97             this.file.lastModifiedTime > this.file_time) {
98             this.file_time = this.file.lastModifiedTime;
99             this.extract_completions();
100         }
101         if (require && !(this.completions && this.completions.length))
102             throw interactive_error("No completions for " + this.key);
103         if (!this.completions)
104             yield co_return(null);
106         /* Match completions against input. */
107         let words = trim_whitespace(input.toLowerCase()).split(/\s+/);
108         let data = this.completions.filter(function (x) {
109             for (var i = 0; i < words.length; ++i)
110                 if (x[0].toLowerCase().indexOf(words[i]) == -1 &&
111                     x[1].toLowerCase().indexOf(words[i]) == -1)
112                     return false;
113             return true;
114         });
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
121                 };
122         yield co_return(c);
123     },
125     /* A completer suitable for supplying to define_webjump. */
126     make_completer : function() {
127         if (!this.file)
128             return null;
129         let jmp = this;
130         return function (input, pos, conservative) {
131             return jmp.internal_completer(input, pos, conservative);
132         };
133     },
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) {
138         if (!this.file)
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,
143                             $temp_file = true);
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);
149     },
151     /* Try to make a suitable file object when the supplied file is a
152      * string or null. */
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");
161         }
162         return file;
163     }
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,
174     require_completions : true,
175     mime_type : "application/xhtml+xml",
176     tidy_command : index_xpath_webjump_tidy_command,
178     make_completion : function (node) {
179         return [makeURLAbsolute(this.url, node.href), node.text];
180     },
182     make_handler : function () {
183         let jmp = this;
184         return function (term) {
185             if (!(jmp.completions && jmp.completions.length))
186                 throw interactive_error("Completions required for " + this.key);
187             return term;
188         };
189     },
191     __proto__ : index_webjump.prototype
195 function index_webjump_gitweb(key, url, file) {
196     index_webjump.call(this, key, url, file);
198 index_webjump_gitweb.prototype = {
199     constructor : index_webjump_gitweb,
201     mime_type : "text/xml",
202     xpath_expr : '//outline[@type="rss"]',
204     make_completion : function (node) {
205         var name = node.getAttribute("text");
206         return [name.replace(/\.git$/, ""), ""];
207     },
209     __proto__ : index_webjump.prototype
213 interactive("webjump-get-index",
214             "Fetch and save the index URL corresponding to an index " +
215             "webjump.  It will then be available to the completer.",
216             function (I) {
217                 var completions = [];
218                 for (let i in index_webjumps)
219                     completions.push(i);
220                 completions.sort();
222                 var key = yield I.minibuffer.read(
223                     $prompt = "Fetch index for index webjump:",
224                     $history = "webjump",
225                     $completer =
226                         all_word_completer($completions = completions),
227                     $match_required = true);
229                 var jmp = index_webjumps[key];
230                 if (jmp)
231                     jmp.get_index(I.buffer);
232             });
235  * Construct a webjump to visit URLs referenced from an index page.
237  * The index page must be able to be parsed as xhtml.  The anchor
238  * nodes indexed are those that match the given xpath_expr.  Don't
239  * forget to use xhtml: prefixes on the xpath steps.
241  * If an alternative is not specified then it is set to the index page.
243  * A completer is provided that uses the index page.  A local file for
244  * the index must be specified either with $index_file or via
245  * index_webjumps_directory.  The index must be manually downloaded;
246  * eg. using webjump-get-index.  Each time the completer is used it
247  * will check if the file has been updated and reload if necessary.
248  * This kind of webjump is not useful without the completions.
249  */
250 define_keywords("$alternative", "$index_file", "$description");
251 function define_xpath_webjump(key, index_url, xpath_expr) {
252     keywords(arguments);
253     let alternative = arguments.$alternative || index_url;
255     var jmp = new index_webjump_xhtml(key, index_url, arguments.$index_file,
256                                       xpath_expr);
257     index_webjumps[key] = jmp;
259     define_webjump(key, jmp.make_handler(),
260                    $completer = jmp.make_completer(),
261                    $alternative = alternative,
262                    $description = arguments.$description);
266  * Modify the xpath for an index webjump and show the resulting
267  * completions.  Useful for figuring out an appropriate xpath.  Either
268  * run using mozrepl or eval in the browser with the dump parameter
269  * set.
270  */
271 function index_webjump_try_xpath(key, xpath_expr, dump) {
272     jmp = index_webjumps[key];
273     if (xpath_expr)
274         jmp.xpath_expr = xpath_expr;
275     jmp.extract_completions();
276     if (dump)
277         dumpln(dump_obj(jmp.completions,
278                         "Completions for index webjump " + key));
279     return jmp.completions;
284  * Construct a webjump to visit repository summary pages at a gitweb
285  * server.
287  * If a repository name is supplied as $default then the alternative
288  * url is set to that repository at the gitweb site.  If an
289  * alternative is not specified by either $default or $alternative
290  * then it is set to the repository list page of the gitweb site.
292  * A completer is provided that uses the list of repositories from the
293  * OPML data on the gitweb server.  The completer is setup in the same
294  * way as for define_xpath_webjump, but the webjump will work without
295  * the completions.
296  */
297 define_keywords("$default", "$alternative", "$opml_file", "$description");
298 function define_gitweb_summary_webjump(key, base_url) {
299     keywords(arguments);
300     let alternative = arguments.$alternative;
301     let gitweb_url = base_url + "/gitweb.cgi";
302     let summary_url = gitweb_url + "?p=%s.git;a=summary";
303     let opml_url = gitweb_url + "?a=opml";
305     if (arguments.$default)
306         alternative = summary_url.replace("%s", arguments.$default);
307     if (!alternative)
308         alternative = gitweb_url;
310     var jmp = new index_webjump_gitweb(key, opml_url, arguments.$opml_file);
311     index_webjumps[key] = jmp;
313     define_webjump(key, summary_url,
314                    $completer = jmp.make_completer(),
315                    $alternative = alternative,
316                    $description = arguments.$description);
319 provide("index-webjump");