]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/modules/help.jsm
Import r6976 from upstream hg supporting Firefox up to 25.*
[dactyl.git] / common / modules / help.jsm
index dadadfcd6993d2e78dd960a290fd91ca9762f2d4..f477411ed4b9acae873f5792a36b995f6cc14b04 100644 (file)
@@ -1,17 +1,17 @@
-// Copyright (c) 2008-2011 by Kris Maglione <maglione.k@gmail.com>
+// Copyright (c) 2008-2013 Kris Maglione <maglione.k@gmail.com>
 //
 // This work is licensed for reuse under an MIT license. Details are
 // given in the LICENSE.txt file included with this file.
-/* use strict */
+"use strict";
 
-Components.utils.import("resource://dactyl/bootstrap.jsm");
 defineModule("help", {
     exports: ["help"],
     require: ["cache", "dom", "protocol", "services", "util"]
-}, this);
+});
 
-this.lazyRequire("completion", ["completion"]);
-this.lazyRequire("overlay", ["overlay"]);
+lazyRequire("completion", ["completion"]);
+lazyRequire("overlay", ["overlay"]);
+lazyRequire("template", ["template"]);
 
 var HelpBuilder = Class("HelpBuilder", {
     init: function init() {
@@ -27,7 +27,7 @@ var HelpBuilder = Class("HelpBuilder", {
 
             // Scrape the list of help files from all.xml
             this.tags["all"] = this.tags["all.xml"] = "all";
-            let files = this.findHelpFile("all").map(function (doc)
+            let files = this.findHelpFile("all").map(doc =>
                     [f.value for (f in DOM.XPath("//dactyl:include/@href", doc))]);
 
             // Scrape the tags from the rest of the help files.
@@ -90,8 +90,8 @@ var Help = Module("Help", {
             }
 
         update(services["dactyl:"].providers, {
-            "help": Loop(function (uri, path) help.files[path]),
-            "help-overlay": Loop(function (uri, path) help.overlays[path]),
+            "help": Loop((uri, path) => help.files[path]),
+            "help-overlay": Loop((uri, path) => help.overlays[path]),
             "help-tag": Loop(function (uri, path) {
                 let tag = decodeURIComponent(path);
                 if (tag in help.files)
@@ -108,7 +108,7 @@ var Help = Module("Help", {
                                     { mimeType: "text/plain;charset=UTF-8" })
                            .responseText;
 
-            let re = util.regexp(UTF8(<![CDATA[
+            let re = util.regexp(UTF8(literal(/*
                   ^ (?P<comment> \s* # .*\n)
 
                 | ^ (?P<space> \s*)
@@ -124,92 +124,83 @@ var Help = Module("Help", {
                   )
 
                 | (?: ^ [^\S\n]* \n) +
-            ]]>), "gmxy");
+            */)), "gmxy");
 
             let betas = util.regexp(/\[((?:b|rc)\d)\]/, "gx");
 
             let beta = array(betas.iterate(NEWS))
-                        .map(function (m) m[1]).uniq().slice(-1)[0];
-
+                        .map(m => m[1]).uniq().slice(-1)[0];
 
-            default xml namespace = NS;
             function rec(text, level, li) {
-                XML.ignoreWhitespace = XML.prettyPrinting = false;
-
-                let res = <></>;
+                let res = [];
                 let list, space, i = 0;
 
-
                 for (let match in re.iterate(text)) {
                     if (match.comment)
                         continue;
                     else if (match.char) {
                         if (!list)
-                            res += list = <ul/>;
-                        let li = <li/>;
-                        li.* += rec(match.content.replace(RegExp("^" + match.space, "gm"), ""), level + 1, li);
-                        list.* += li;
+                            res.push(list = ["ul", {}]);
+                        let li = ["li", {}];
+                        li.push(rec(match.content
+                                         .replace(RegExp("^" + match.space, "gm"), ""),
+                                    level + 1,
+                                    li));
+                        list.push(li);
                     }
                     else if (match.par) {
                         let [, par, tags] = /([^]*?)\s*((?:\[[^\]]+\])*)\n*$/.exec(match.par);
                         let t = tags;
-                        tags = array(betas.iterate(tags)).map(function (m) m[1]);
+                        tags = array(betas.iterate(tags)).map(m => m[1]);
 
-                        let group = !tags.length                       ? "" :
-                                    !tags.some(function (t) t == beta) ? "HelpNewsOld" : "HelpNewsNew";
+                        let group = !tags.length               ? "" :
+                                    !tags.some(t => t == beta) ? "HelpNewsOld" : "HelpNewsNew";
                         if (i === 0 && li) {
-                            li.@highlight = group;
+                            li[1]["dactyl:highlight"] = group;
                             group = "";
                         }
 
                         list = null;
                         if (level == 0 && /^.*:\n$/.test(match.par)) {
                             let text = par.slice(0, -1);
-                            res += <h2 tag={"news-" + text}>{template.linkifyHelp(text, true)}</h2>;
+                            res.push(["h2", { tag: "news-" + text },
+                                          template.linkifyHelp(text, true)]);
                         }
                         else {
                             let [, a, b] = /^(IMPORTANT:?)?([^]*)/.exec(par);
-                            res += <p highlight={group + " HelpNews"}>{
-                                !tags.length ? "" :
-                                <hl key="HelpNewsTag">{tags.join(" ")}</hl>
-                            }{
-                                a ? <hl key="HelpWarning">{a}</hl> : ""
-                            }{
-                                template.linkifyHelp(b, true)
-                            }</p>;
+
+                            res.push(["p", { "dactyl:highlight": group + " HelpNews" },
+                                !tags.length ? "" : ["hl", { key: "HelpNewsTag" }, tags.join(" ")],
+                                a ? ["hl", { key: "HelpWarning" }, a] : "",
+                                template.linkifyHelp(b, true)]);
                         }
                     }
                     i++;
                 }
-                for each (let attr in res..@highlight) {
-                    attr.parent().@NS::highlight = attr;
-                    delete attr.parent().@highlight;
-                }
+
                 return res;
             }
 
-            XML.ignoreWhitespace = XML.prettyPrinting = false;
             let body = rec(NEWS, 0);
-            for each (let li in body..li) {
-                let list = li..li.(@NS::highlight == "HelpNewsOld");
-                if (list.length() && list.length() == li..li.(@NS::highlight != "").length()) {
-                    for each (let li in list)
-                        li.@NS::highlight = "";
-                    li.@NS::highlight = "HelpNewsOld";
-                }
-            }
 
+            // E4X-FIXME
+            // for each (let li in body..li) {
+            //     let list = li..li.(@NS::highlight == "HelpNewsOld");
+            //     if (list.length() && list.length() == li..li.(@NS::highlight != "").length()) {
+            //         for each (let li in list)
+            //             li.@NS::highlight = "";
+            //         li.@NS::highlight = "HelpNewsOld";
+            //     }
+            // }
 
             return '<?xml version="1.0"?>\n' +
                    '<?xml-stylesheet type="text/xsl" href="dactyl://content/help.xsl"?>\n' +
-                   '<!DOCTYPE document SYSTEM "resource://dactyl-content/dactyl.dtd">\n' +
-                   <document xmlns={NS} xmlns:dactyl={NS}
-                       name="versions" title={config.appName + " Versions"}>
-                       <h1 tag="versions news NEWS">{config.appName} Versions</h1>
-                       <toc start="2"/>
-
-                       {body}
-                   </document>.toXMLString()
+                   DOM.toXML(["document", { xmlns: "dactyl", name: "versions",
+                                  title: config.appName + " Versions" },
+                       ["h1", { tag: "versions news NEWS" }, config.appName + " Versions"],
+                       ["toc", { start: "2" }],
+
+                       body]);
         });
     },
 
@@ -234,7 +225,7 @@ var Help = Module("Help", {
         init: function init() {
             dactyl.commands["dactyl.help"] = function (event) {
                 let elem = event.originalTarget;
-                help.help(elem.getAttribute("tag") || elem.textContent);
+                modules.help.help(elem.getAttribute("tag") || elem.textContent);
             };
         },
 
@@ -304,7 +295,7 @@ var Help = Module("Help", {
                 var addURIEntry  = function addURIEntry(file, uri) addDataEntry(file, util.httpGet(uri).responseText);
             }
             else {
-                var zip = services.ZipWriter(FILE, File.MODE_CREATE | File.MODE_WRONLY | File.MODE_TRUNCATE);
+                var zip = services.ZipWriter(FILE.file, File.MODE_CREATE | File.MODE_WRONLY | File.MODE_TRUNCATE);
 
                 addURIEntry = function addURIEntry(file, uri)
                     zip.addEntryChannel(PATH + file, TIME, 9,
@@ -316,15 +307,15 @@ var Help = Module("Help", {
             let empty = Set("area base basefont br col frame hr img input isindex link meta param"
                                 .split(" "));
             function fix(node) {
-                switch(node.nodeType) {
+                switch (node.nodeType) {
                 case Ci.nsIDOMNode.ELEMENT_NODE:
                     if (isinstance(node, [Ci.nsIDOMHTMLBaseElement]))
                         return;
 
-                    data.push("<"); data.push(node.localName);
+                    data.push("<"node.localName);
                     if (node instanceof Ci.nsIDOMHTMLHtmlElement)
-                        data.push(" xmlns=" + XHTML.uri.quote(),
-                                  " xmlns:dactyl=" + NS.uri.quote());
+                        data.push(" xmlns=" + XHTML.quote(),
+                                  " xmlns:dactyl=" + NS.quote());
 
                     for (let { name, value } in array.iterValues(node.attributes)) {
                         if (name == "dactyl:highlight") {
@@ -352,22 +343,20 @@ var Help = Module("Help", {
                             value = value.replace(/.*\//, "");
                         }
 
-                        data.push(" ", name, '="',
-                                  <>{value}</>.toXMLString().replace(/"/g, "&quot;"),
-                                  '"');
+                        data.push(" ", name, '="', DOM.escapeHTML(value), '"');
                     }
                     if (node.localName in empty)
                         data.push(" />");
                     else {
                         data.push(">");
                         if (node instanceof Ci.nsIDOMHTMLHeadElement)
-                            data.push(<link rel="stylesheet" type="text/css" href="help.css"/>.toXMLString());
+                            data.push('<link rel="stylesheet" type="text/css" href="help.css"/>');
                         Array.map(node.childNodes, fix);
                         data.push("</", node.localName, ">");
                     }
                     break;
                 case Ci.nsIDOMNode.TEXT_NODE:
-                    data.push(<>{node.textContent}</>.toXMLString());
+                    data.push(DOM.escapeHTML(node.textContent, true));
                 }
             }
 
@@ -378,8 +367,8 @@ var Help = Module("Help", {
             for (let [file, ] in Iterator(help.files)) {
                 let url = "dactyl://help/" + file;
                 dactyl.open(url);
-                util.waitFor(function () content.location.href == url && buffer.loaded
-                                && content.document.documentElement instanceof Ci.nsIDOMHTMLHtmlElement,
+                util.waitFor(() => (content.location.href == url && buffer.loaded &&
+                                    content.document.documentElement instanceof Ci.nsIDOMHTMLHtmlElement),
                              15000);
                 events.waitForPageLoad();
                 var data = [
@@ -392,9 +381,9 @@ var Help = Module("Help", {
             }
 
             let data = [h for (h in highlight) if (Set.has(styles, h.class) || /^Help/.test(h.class))]
-                .map(function (h) h.selector
-                                   .replace(/^\[.*?=(.*?)\]/, ".hl-$1")
-                                   .replace(/html\|/g, "") + "\t" + "{" + h.cssText + "}")
+                .map(h => h.selector
+                           .replace(/^\[.*?=(.*?)\]/, ".hl-$1")
+                           .replace(/html\|/g, "") + "\t" + "{" + h.cssText + "}")
                 .join("\n");
             addDataEntry("help.css", data.replace(/chrome:[^ ")]+\//g, ""));
 
@@ -414,7 +403,7 @@ var Help = Module("Help", {
     })
 }, {
 }, {
-    commands: function init_commands(dactyl, modules, window) {
+    commands: function initCommands(dactyl, modules, window) {
         const { commands, completion, help } = modules;
 
         [
@@ -441,7 +430,7 @@ var Help = Module("Help", {
                 });
         });
     },
-    completion: function init_completion(dactyl, modules, window) {
+    completion: function initCompletion(dactyl, modules, window) {
         const { completion } = modules;
 
         completion.help = function completion_help(context, consolidated) {
@@ -453,23 +442,19 @@ var Help = Module("Help", {
                 context.keys = { text: 0, description: function () "all" };
         };
     },
-    mappings: function init_mappings(dactyl, modules, window) {
-        const { help, mappings, modes } = modules;
-
-        mappings.add([modes.MAIN], ["<open-help>", "<F1>"],
-            "Open the introductory help page",
-            function () { help.help(); });
-
-        mappings.add([modes.MAIN], ["<open-single-help>", "<A-F1>"],
-            "Open the single, consolidated help page",
-            function () { modules.ex.helpall(); });
-    },
-    javascript: function init_javascript(dactyl, modules, window) {
+    javascript: function initJavascript(dactyl, modules, window) {
         modules.JavaScript.setCompleter([modules.help.exportHelp],
-            [function (context, args) overlay.activeModules.completion.file(context)]);
+            [(context, args) => overlay.activeModules.completion.file(context)]);
+    },
+    options: function initOptions(dactyl, modules, window) {
+        const { options } = modules;
+
+        options.add(["helpfile", "hf"],
+            "Name of the main help file",
+            "string", "intro");
     }
 });
 
 endModule();
 
-// vim: set fdm=marker sw=4 ts=4 et ft=javascript:
+// vim: set fdm=marker sw=4 sts=4 ts=8 et ft=javascript: