Difference between revisions of "MediaWiki:ExportDocSnippets.js"

From HSYCO
Jump to navigation Jump to search
 
(20 intermediate revisions by the same user not shown)
Line 1: Line 1:
// copy/paste and call print()
 
 
var myWindow;
 
var myWindow;
  
Line 5: Line 4:
 
oReq.addEventListener("load", reqListener);
 
oReq.addEventListener("load", reqListener);
 
var l = -1;
 
var l = -1;
var baseUrl = "http://wiki.hsyco.com/3.6.0/index.php/"
+
var baseUrl = "https://wiki.hsyco.com/3.7/index.php/"
var urls = [
+
var urls = [{
{url: "Event_Keywords", sub:"Events", opt:{event:true}, arr:"events"},
+
        url: "Event_Keywords",
{url: "Action_Keywords", sub:"Actions", opt:{action:true}, arr:"actions"},
+
        sub: "Events",
{url: "JavaScript_Callback_Functions_API", sub:"Javascript Callback Functions", opt:{jsEvent:true}, arr:"events"},
+
        opt: {
{url: "JavaScript_Command_and_Utility_Functions_API", sub:"Javascript Command and Utility Functions", opt:{jsAction:true}, arr:"js"},
+
            event: true
{url: "Index.js", sub:"Javascript", opt:{js:true}, arr:"indexjs"},
+
        },
 +
        arr: "events"
 +
    },
 +
    {
 +
        url: "Action_Keywords",
 +
        sub: "Actions",
 +
        opt: {
 +
            action: true
 +
        },
 +
        arr: "actions"
 +
    },
 +
    {
 +
        url: "JavaScript_Callback_Functions_API",
 +
        sub: "Javascript Callback Functions",
 +
        opt: {
 +
            jsEvent: true
 +
        },
 +
        arr: "events"
 +
    },
 +
    {
 +
        url: "JavaScript_Command_and_Utility_Functions_API",
 +
        sub: "Javascript Command and Utility Functions",
 +
        opt: {
 +
            jsAction: true
 +
        },
 +
        arr: "js"
 +
    },
 +
    {
 +
        url: "Index.js",
 +
        sub: "Javascript",
 +
        opt: {
 +
            js: true
 +
        },
 +
        arr: "indexjs"
 +
    },
 
];
 
];
  
function reqListener () {
+
function reqListener() {
console.log("loaded "+urls[l].url);
+
    console.log("loaded " + urls[l].url);
var div = document.createElement('div');
+
    var div = document.createElement('div');
div.innerHTML = this.responseText;
+
    div.innerHTML = this.responseText;
urls[l].res = parse(div, urls[l].sub, urls[l].opt);
+
    urls[l].res = parse(div, urls[l].sub, urls[l].opt);
next();
+
    next();
 
}
 
}
  
 +
// CALL THIS
 
function next() {
 
function next() {
if (!myWindow)
+
    if (!myWindow) {
myWindow = window.open();
+
        initWin();
l++;
+
        myWindow.document.write("Please wait...");
if (urls[l]) {
+
    }
oReq.open("GET", baseUrl+urls[l].url);
+
    l++;
oReq.send();
+
    if (urls[l]) {
} else {
+
        oReq.open("GET", baseUrl + urls[l].url);
var head = "\
+
        oReq.send();
 +
    } else {
 +
        var head = "\
 
ace.define('ace/snippets/%what',['require','exports','module'], function(require, exports, module) {\n\
 
ace.define('ace/snippets/%what',['require','exports','module'], function(require, exports, module) {\n\
 
var s = {\n\
 
var s = {\n\
Line 40: Line 76:
 
exports.snippetText = s;\n\n\n";
 
exports.snippetText = s;\n\n\n";
  
var tail = "exports.scope = '%what';\n});";
+
        var tail = "exports.scope = '%what';\n});";
  
str = head.replace("%what","events");
+
        str = head.replace("%what", "events");
for (var i = 0; i < urls.length-1; i++) { // skip index.js
+
        for (var i = 0; i < urls.length - 1; i++) { // skip index.js
str += "var a = s."+urls[i].arr+";\n";
+
            str += "var a = s." + urls[i].arr + ";\n";
str += urls[i].res;
+
            str += urls[i].res;
str += "\n\n\n";
+
            str += "\n\n\n";
}
+
        }
str += tail.replace("%what","events");;
+
        str += tail.replace("%what", "events");;
openWin(str);
+
        openWin("events.js", str);
  
  
str = head.replace("%what","javascript");
+
        str = head.replace("%what", "javascript");
str += "var a = s.js;\n";
+
        str += "var a = s.js;\n";
str += urls[4].res;
+
        str += urls[4].res;
str += "\n\n\n// js\n\n";
+
        str += "\n\n\n// js\n\n";
var s;
+
        var s;
for (var i=0; i<jsSnippets.length; i++) {
+
        for (var i = 0; i < jsSnippets.length; i++) {
s = jsSnippets[i];
+
            s = jsSnippets[i];
str += "a.push({\n";
+
            str += "a.push({\n";
str += "\ttitle:\""+s.title+"\",\n";
+
            str += "\ttitle:\"" + s.title.split("(")[0] + "\",\n";
str += "\ttabTrigger:\""+s.tabTrigger+"\",\n";
+
            str += "\ttabTrigger:\"" + s.tabTrigger + "\",\n";
str += "\tcontent:\""+s.content+"\"\n";
+
            str += "\tcontent:\"" + s.content.replace(/\t/g, "\\t").replace(/\n/g, "\\n") + "\"\n";
str += "});\n";
+
            str += "});\n";
}
+
        }
str += "\n\n\n";
+
        str += "\n\n\n";
str += tail.replace("%what","events");;
+
        str += tail.replace("%what", "javascript");;
openWin(str);
+
        openWin("javascript.js", str);
  
  
  
console.log("done");
+
        console.log("done");
}
+
    }
 
}
 
}
  
Line 84: Line 120:
 
         event.initEvent('click', true, true);
 
         event.initEvent('click', true, true);
 
         pom.dispatchEvent(event);
 
         pom.dispatchEvent(event);
     }
+
     } else {
    else {
 
 
         pom.click();
 
         pom.click();
 
     }
 
     }
 
}
 
}
  
// CALL THIS
 
 
function print(sub, opt) {
 
function print(sub, opt) {
var url = "";
+
    var url = "";
for (var i=0; i<urls.length; i++) {
+
    for (var i = 0; i < urls.length; i++) {
if (window.location.href.indexOf(urls[i].url)!=-1) {
+
        if (window.location.href.indexOf(urls[i].url) != -1) {
sub = urls[i].sub;
+
            sub = urls[i].sub;
opt = urls[i].opt;
+
            opt = urls[i].opt;
url = urls[i].url;
+
            url = urls[i].url;
break;
+
            break;
}
+
        }
}
+
    }
  
var str = parse(document,sub,opt,baseUrl+url);
+
    var str = parse(document, sub, opt, baseUrl + url);
openWin(str);
+
    openWin("", str);
 
}
 
}
  
function openWin(str) {
+
function initWin() {
if (!myWindow)
+
    myWindow = window.open("about:blank", "Editor Snippets");
myWindow = window.open();
+
 
myWindow.document.write("<textarea id='txt' style='width:80%; height:80%'>"+str+"</textarea>");
+
    myWindow.document.write("\
myWindow.document.getElementById('txt').select();
+
<script>\n\
 +
function addDownload(n,f) {\n\
 +
var data = document.getElementById('txt'+n).value;\n\
 +
var link = document.createElement('a');\n\
 +
link.setAttribute('href','data:text/text;charset=utf-8,'+encodeURI(data));\n\
 +
link.setAttribute('download', f);\n\
 +
link.innerHTML = 'download file';\n\
 +
document.getElementById('p'+n).appendChild(link);\n\
 +
}\n\
 +
</script>");
 +
}
 +
 
 +
var _no = 0; // number of outputs
 +
function openWin(title, str) {
 +
    if (!myWindow)
 +
        initWin();
 +
 
 +
    _no++;
 +
 
 +
    myWindow.document.write("<hr>");
 +
    myWindow.document.write("<h1>" + title + "</h1>");
 +
    myWindow.document.write("<p id='p" + _no + "'>");
 +
    myWindow.document.write("</p>");
 +
    myWindow.document.write("<textarea id='txt" + _no + "' style='width:800px; height:400px'>" + str + "</textarea>");
 +
    myWindow.document.write("<script>addDownload(" + _no + ",'" + title + "')</script>");
 +
    myWindow.document.write("<p></p>");
 +
    myWindow.document.getElementById('txt' + _no).select();
 
}
 
}
  
 
function parse(e, asub, opt, url) {
 
function parse(e, asub, opt, url) {
function rr(s) { // remove return and trim
+
    function rr(s) { // remove return and trim
s = s.trim();
+
        s = s.trim();
while (s[s.length-1] == "\n")
+
        while (s[s.length - 1] == "\n")
s = s.substr(0,s.length-1);
+
            s = s.substr(0, s.length - 1);
return s;
+
        return s;
}
+
    }
function stripDescr(d) {
+
 
d = d.replace(/\n+/g, "");
+
    function stripDescr(d) {
d = d.replace(/(^(<br>)+|(<br>)+$)/g, ""); // initial, end br
+
        d = d.replace(/\n+/g, "");
d = d.replace(/\n+/g, "<br>");
+
        d = d.replace(/(^(<br>)+|(<br>)+$)/g, ""); // initial, end br
d = d.replace(/(<br>)+/g, "<br>");
+
        d = d.replace(/\n+/g, "<br>");
d = d.replace(/\<(\/?(ul|li|string|br))\>/g, "+___$1___-"); // keep these
+
        d = d.replace(/(<br>)+/g, "<br>");
d = d.replace(/<(?:.|\n)*?>/gm, ''); // strip html
+
        d = d.replace(/\<(\/?(ul|li|string|br))\>/g, "+___$1___-"); // keep these
d = d.replace(/\+___/g, "<"); // restore
+
        d = d.replace(/<(?:.|\n)*?>/gm, ''); // strip html
d = d.replace(/___-/g, ">");
+
        d = d.replace(/\+___/g, "<"); // restore
d = d.replace(/(<br>)?<\/li>(<br>)?/g, "</li>"); // remove breaklines inside/outside li
+
        d = d.replace(/___-/g, ">");
d = d.trim();
+
        d = d.replace(/(<br>)?<\/li>(<br>)?/g, "</li>"); // remove breaklines inside/outside li
return d;
+
        d = d.trim();
}
+
        return d;
var a = e.getElementsByClassName("mw-headline");
+
    }
if (!opt) opt = {};
+
    var a = e.getElementsByClassName("mw-headline");
 +
    if (!opt) opt = {};
  
var nl = "\\n\\"+"\n";
+
    var nl = "\\n\\" + "\n";
  
var snippets = [];
+
    var snippets = [];
var variations = [];
+
    var variations = [];
var pE, nE;
+
    var pE, nE;
var str = "";
+
    var str = "";
var title, subtitle, descr, snippet, content;
+
    var title, subtitle, descr, snippet, content;
var bsub; // combined with asub to create subtitle
+
    var bsub; // combined with asub to create subtitle
for (var i=0; i<a.length; i++) {
+
    for (var i = 0; i < a.length; i++) {
pE = a[i].parentElement;
+
        pE = a[i].parentElement;
nE = pE ? pE.nextElementSibling : null;
+
        nE = pE ? pE.nextElementSibling : null;
  
title = rr(a[i].innerHTML);
+
        title = rr(a[i].innerHTML);
subtitle = asub + (bsub? ", "+bsub : "");
+
        subtitle = asub + (bsub ? ", " + bsub : "");
  
snippet = "";
+
        snippet = "";
if (pE.nodeName == "H3") {
+
        if (pE.nodeName == "H3") {
if (nE && (nE.nodeName == "PRE" || nE.className.indexOf("mw-code")!=-1)) {
+
            // if (nE && (nE.nodeName == "PRE" || nE.className.indexOf("mw-code") != -1)) { // old ver
snippet = nE.innerHTML;
+
            if (nE && (nE.nodeName == "PRE" || (nE.childNodes[0] && nE.childNodes[0].nodeName == "PRE")) && nE.innerHTML.indexOf("\t") == -1) {
snippet = snippet.replace(/<(?:.|\n)*?>/gm, ''); // strip html
+
                snippet = nE.innerHTML;
content = snippet;
+
                snippet = snippet.replace(/<(?:.|\n)*?>/gm, ''); // strip html
title = snippet;
+
                content = snippet;
nE = nE.nextElementSibling;
+
                title = snippet;
}
+
                nE = nE.nextElementSibling;
descr = "";
+
            }
var grabAll = opt.event || opt.action; // grab all html
+
            descr = "";
while (nE) {
+
            var grabAll = true; //opt.event || opt.action; // grab all html
if (!grabAll && nE.nodeName != "P" && nE.nodeName != "UL")
+
            while (nE) {
break;
+
                if (!grabAll && nE.nodeName != "P" && nE.nodeName != "UL")
if (nE.className == "mw-headline" || nE.getElementsByClassName("mw-headline").length)
+
                    break;
break;
+
                if (nE.className == "mw-headline" || nE.getElementsByClassName("mw-headline").length)
if (nE.getAttribute("data-skipeditordoc"))
+
                    break;
break;
+
                if (nE.getAttribute("data-skipeditordoc") != null) // like CameraCommandEvent on JavaScript_Callback_Functions_API
 +
                    break;
  
var d = rr(grabAll ? nE.outerHTML : nE.innerHTML);
+
                var d = rr(grabAll ? nE.outerHTML : nE.innerHTML);
  
//d = d.replace(/<a href="/g, "<a target='_blank' href=\"http://wiki.hsyco.com/3.6.0");
+
                if (!grabAll) {
if (!grabAll) {
+
                    d = stripDescr(d);
d = stripDescr(d);
+
                    if (nE.nodeName == "UL")
if (nE.nodeName == "UL")
+
                        d = "<ul>" + d + "</ul>";
d = "<ul>"+d+"</ul>";
 
  
if (d.indexOf("Examples:")!=-1)
+
                    if (d.indexOf("Examples:") != -1)
break;
+
                        break;
  
if (d.indexOf("Parameters:")!=-1 ||
+
                    if (d.indexOf("Parameters:") != -1 ||
d.indexOf("Returns:")!=-1) {
+
                        d.indexOf("Returns:") != -1) {
descr += "<br>";
+
                        descr += "<br>";
}
+
                    }
} else {
+
                    if (d)
// bulb image
+
                        descr += (descr ? "<br>" : "") + d;
d = d.replace(/<img .+src=".+Bulbgraph.png".+(width="[^"]+").+(height="[^"]+")[^>]+>/g,
+
                } else {
"<img src=\"/manager/pic/bulb.png\" $1 $2>");
+
                    // bulb image
// remove \n
+
                    d = d.replace(/<img .+src=".+Bulbgraph.png".+(width="[^"]+").+(height="[^"]+")[^>]+>/g,
d = d.replace(/\n/g,"");
+
                        "<img src=\"/manager/pic/bulb.png\" $1 $2>");
// variations?
+
                    // replace \n\t
if (nE.nodeName == "TABLE") {
+
                    d = d.replace(/\n/g, "\\n").replace(/\t/g, "\\t");
var f = nE.getElementsByTagName("TD")[0];
+
                    // variations?
if (f && f.innerHTML &&
+
                    if (nE.nodeName == "TABLE") {
(f.innerHTML.indexOf("Action")!=-1 ||f.innerHTML.indexOf("Event")!=-1)) {
+
                        var f = nE.getElementsByTagName("TD")[0];
var rows = nE.getElementsByTagName("TR");
+
                        if (f && f.innerHTML &&
var cols;
+
                            (f.innerHTML.indexOf("Action") != -1 || f.innerHTML.indexOf("Event") != -1)) {
var txt, atxt;
+
                            var rows = nE.getElementsByTagName("TR");
for (var r = 1; r < rows.length; r++) {
+
                            var cols;
cols = rows[r].getElementsByTagName("TD");
+
                            var txt, atxt;
txt = cols[0] ? cols[0].innerHTML : "";
+
                            for (var r = 1; r < rows.length; r++) {
if (txt && txt != title) {
+
                                cols = rows[r].getElementsByTagName("TD");
atxt = txt.split(/\s*\<br[^\>]*\>\s*/);
+
                                txt = cols[0] ? cols[0].innerHTML : "";
for (var v = 0; v < atxt.length; v++) {
+
                                if (txt && txt != title) {
variations.push({src:atxt[v].trim(), descr:cols[1] ? cols[1].innerHTML : ""});
+
                                    atxt = txt.split(/\s*\<br[^\>]*\>\s*/);
}
+
                                    for (var v = 0; v < atxt.length; v++) {
}
+
                                        variations.push({
}
+
                                            src: atxt[v].trim(),
}
+
                                            descr: cols[1] ? cols[1].innerHTML : ""
}
+
                                        });
}
+
                                    }
 +
                                }
 +
                            }
 +
                        }
 +
                    }
 +
                    if (d)
 +
                        descr += d;
 +
                }
  
if (d)
+
                nE = nE.nextElementSibling;
descr += (descr ? "<br>" : "") + d;
+
            }
nE = nE.nextElementSibling;
+
            // check variations
}
+
            var s, c;
// check variations
+
            for (var v = 0; v < variations.length; v++) {
var s,c;
+
                s = variations[v].src;
for (var v = 0; v < variations.length; v++) {
+
                s = s.replace("&lt;", "<");
s = variations[v].src;
+
                s = s.replace("&gt;", ">");
// replace lowercase words, separated by space or start/end line
+
                // replace variables (lowercase words), separated by space or start/end line
c = s.replace(/(^|\s+)("?[a-z0-9]+"?)($|\s+)/g, "$1\${0:$2}$3");
+
                c = s.replace(/(^|\s*|:)("?)([a-z0-9:_\.]+)("?)($|\n|\s*|:)/g, "$1$2\${0:$3}$4$5");
var count = 1;
+
                var count = 1;
c = c.replace(/\$\{0/g, function (match, capture) { return '${' + count++;});
+
                c = c.replace(/\$\{0/g, function (match, capture) {
 +
                    return '${' + count++;
 +
                });
  
// strip html, new lines
+
                // strip html, new lines
s = s.replace(/<(?:.|\n)*?>/gm, '').replace(/\n/g,"");
+
                s = s.replace(/<(?:.|\n)*?>/gm, '').replace(/\n/g, "");
c = c.replace(/<(?:.|\n)*?>/gm, '').replace(/\n/g,"");
+
                c = c.replace(/<(?:.|\n)*?>/gm, '').replace(/\n/g, "");
variations[v].snippet = s;
+
                variations[v].snippet = s;
variations[v].content = c; // strip html;
+
                variations[v].content = c; // strip html;
}
+
            }
  
if (!snippet) {
+
            if (!snippet) {
snippet = title;
+
                snippet = title;
content = snippet;
+
                content = snippet;
}
+
            }
  
title = rr(title);
+
            title = rr(title);
snippet = rr(snippet);
+
            snippet = rr(snippet);
content = rr(content);
+
            content = rr(content);
  
if (opt.jsEvent || (opt.js && bsub == "Events")) {
+
            if (opt.jsEvent || (opt.js && bsub == "Events")) {
snippet = "function " + snippet;
+
                if (title.indexOf("function") != 0)
if (content.indexOf("function") != 0)
+
                    title = "function " + title;
content = "function " + content;
+
                if (snippet.indexOf("function") != 0)
content += (opt.js? " {" : " : {") + "\\n";
+
                    snippet = "function " + snippet;
content += "\\t${1:// body...} " + "\\n";
+
                if (content.indexOf("function") != 0)
content += "}";
+
                    content = "function " + content;
} else if (opt.jsAction || (opt.js && bsub == "Functions")) {
+
                content += (opt.js ? " {" : " : {") + "\\n";
content = content.replace(/([\(,]+\s?)([^\)^\s^,]+)/g, "$1\${0:$2}");
+
                content += "\\t${1:// body...} " + "\\n";
var count = 1;
+
                content += "}";
content = content.replace(/\$\{0/g, function (match, capture) { return '${' + count++;});
+
            } else if (opt.jsAction || (opt.js && bsub == "Functions")) {
} else if (opt.js && (bsub == "Examples" || bsub == "User Object")) {
+
                content = content.replace(/([\(,]+\s?)([^\)^\s^,]+)/g, "$1\${0:$2}");
continue;
+
                var count = 1;
} else {
+
                content = content.replace(/\$\{0/g, function (match, capture) {
content += " ";
+
                    return '${' + count++;
}
+
                });
 +
            } else if (opt.js && (bsub == "Examples" || bsub == "User Object")) {
 +
                continue;
 +
            } else {
 +
                content += " ";
 +
            }
  
if (title && descr) {
+
            if (title && descr) {
descr = descr.replace(/["“”]/g,'\\"');
+
                descr = descr.replace(/["“”]/g, '\\"');
/*
+
                if (variations.length) {
var _url = url+"#"+title;
+
                    for (var v = 0; v < variations.length; v++) {
title = "<br><br><a target='_blank' href='"+_url+"'>"+title+"</a>";
+
                        snippets.push({
*/
+
                            title: variations[v].snippet,
if (variations.length) {
+
                            subtitle: subtitle,
for (var v = 0; v < variations.length; v++) {
+
                            descr: v ? "_again_" : descr, //+"<br><br>"+variations[v].descr+_url,
snippets.push({
+
                            snippet: variations[v].snippet,
title:variations[v].snippet,
+
                            content: variations[v].content
subtitle:subtitle,
+
                        });
descr:v?"_again_":descr, //+"<br><br>"+variations[v].descr+_url,
+
                    }
snippet:variations[v].snippet,
+
                    variations = [];
content:variations[v].content
+
                } else {
});
+
                    snippets.push({
}
+
                        title: title,
variations = [];
+
                        subtitle: subtitle,
} else {
+
                        descr: descr,
snippets.push({
+
                        snippet: snippet,
title:title,
+
                        content: content
subtitle:subtitle,
+
                    });
descr:descr,
+
                }
snippet:snippet,
+
            }
content:content
+
        } else if (pE.nodeName == "H2") {
});
+
            bsub = title;
}
+
        }
}
+
    }
} else if (pE.nodeName == "H2"){
 
bsub = title;
 
}
 
}
 
  
if (false && !opt.noSort) {
+
    if (false && !opt.noSort) {
snippets.sort(function(a,b){
+
        snippets.sort(function (a, b) {
if (a.snippet > b.snippet) {
+
            if (a.snippet > b.snippet) {
return 1;
+
                return 1;
}
+
            }
if (a.snippet < b.snippet) {
+
            if (a.snippet < b.snippet) {
return -1;
+
                return -1;
}
+
            }
// a must be equal to b
+
            // a must be equal to b
return 0;
+
            return 0;
});
+
        });
}
+
    }
  
var s;
+
    var s;
for (var i=0; i<snippets.length; i++) {
+
    for (var i = 0; i < snippets.length; i++) {
s = snippets[i];
+
        s = snippets[i];
str += "a.push({\n";
+
        str += "a.push({\n";
str += "\ttitle:\""+s.title.replace(/["“”]/g,'\\"')+"\",\n";
+
        str += "\ttitle:\"" + s.title.replace(/["“”]/g, '\\"') + "\",\n";
str += "\tsubTitle:\""+s.subtitle.replace(/["“”]/g,'\\"')+"\",\n";
+
        str += "\tsubTitle:\"" + s.subtitle.replace(/["“”]/g, '\\"') + "\",\n";
  
if (s.descr == "_again_")
+
        if (s.descr == "_again_")
str += "\tdocHTML:a.last().docHTML,\n";
+
            str += "\tdocHTML:a.last().docHTML,\n";
else
+
        else
str += "\tdocHTML:\""+s.descr+"\",\n";
+
            str += "\tdocHTML:\"" + s.descr + "\",\n";
  
str += "\ttabTrigger:\""+s.snippet.replace(/["“”]/g,'\\"')+"\",\n";
+
        //str += "\ttabTrigger:\""+s.snippet.replace(/["“”]/g,'\\"')+"\",\n";
str += "\tcontent:\""+s.content.replace(/["“”]/g,'\\"')+"\"\n";
+
        str += "\tcontent:\"" + s.content.replace(/["“”]/g, '\\"') + "\"\n";
str += "});\n";
+
        str += "});\n";
}
+
    }
  
return str;
+
    return str;
 
}
 
}
  
Line 330: Line 401:
 
var a = jsSnippets;
 
var a = jsSnippets;
 
a.push({
 
a.push({
title:"fun",
+
    title: "fun",
tabTrigger:"fun",
+
    tabTrigger: "fun",
content:"function ${1?:function_name}(${2:argument}) {\n\t${3:// body...}\n}"
+
    content: "function ${1?:function_name}(${2:argument}) {\n\t${3:// body...}\n}"
 
});
 
});
 
a.push({
 
a.push({
title:"if",
+
    title: "if",
tabTrigger:"if",
+
    tabTrigger: "if",
content:"if (${1:true}) {\n\t${0}\n}"
+
    content: "if (${1:true}) {\n\t${0}\n}"
 
});
 
});
 
a.push({
 
a.push({
title:"ife",
+
    title: "ife",
tabTrigger:"ife",
+
    tabTrigger: "ife",
content:"if (${1:true}) {\n\t${2}\n} else {\n\t${0}\n}"
+
    content: "if (${1:true}) {\n\t${2}\n} else {\n\t${0}\n}"
 
});
 
});
 
a.push({
 
a.push({
title:"ter",
+
    title: "ter",
tabTrigger:"ter",
+
    tabTrigger: "ter",
content:"${1:/* condition */} ? ${2:a} : ${3:b}"
+
    content: "${1:/* condition */} ? ${2:a} : ${3:b}"
 
});
 
});
 
a.push({
 
a.push({
title:"switch",
+
    title: "switch",
tabTrigger:"switch",
+
    tabTrigger: "switch",
content:"switch (${1:expression}) {\n\tcase '${3:case}':\n\t\t${4:// code}\n\t\tbreak;\n\t${5}\n\tdefault:\n\t\t${2:// code}\n}"
+
    content: "switch (${1:expression}) {\n\tcase '${3:case}':\n\t\t${4:// code}\n\t\tbreak;\n\t${5}\n\tdefault:\n\t\t${2:// code}\n}"
 
});
 
});
 
a.push({
 
a.push({
title:"case",
+
    title: "case",
tabTrigger:"case",
+
    tabTrigger: "case",
content:"case '${1:case}':\n\t${2:// code}\n\tbreak;\n${3}"
+
    content: "case '${1:case}':\n\t${2:// code}\n\tbreak;\n${3}"
 
});
 
});
 
a.push({
 
a.push({
title:"wh",
+
    title: "wh",
tabTrigger:"wh",
+
    tabTrigger: "wh",
content:"while (${1:/* condition */}) {\n\t${0:/* code */}\n}"
+
    content: "while (${1:/* condition */}) {\n\t${0:/* code */}\n}"
 
});
 
});
 
a.push({
 
a.push({
title:"try",
+
    title: "try",
tabTrigger:"try",
+
    tabTrigger: "try",
content:"try {\n\t${0:/* code */}\n} catch (e) {}"
+
    content: "try {\n\t${0:/* code */}\n} catch (e) {}"
 
});
 
});
 
a.push({
 
a.push({
title:"do",
+
    title: "do",
tabTrigger:"do",
+
    tabTrigger: "do",
content:"do {\n\t${2:/* code */}\n} while (${1:/* condition */});"
+
    content: "do {\n\t${2:/* code */}\n} while (${1:/* condition */});"
 
});
 
});
 
a.push({
 
a.push({
title:"setTimeout",
+
    title: "setTimeout",
tabTrigger:"setTimeout",
+
    tabTrigger: "setTimeout",
content:"setTimeout(function() {${3:$TM_SELECTED_TEXT}}, ${1:10});"
+
    content: "setTimeout(function() {${3:$TM_SELECTED_TEXT}}, ${1:10});"
 
});
 
});
 
a.push({
 
a.push({
title:"gett",
+
    title: "gett",
tabTrigger:"gett",
+
    tabTrigger: "gett",
content:"getElementsBy${1:TagName}('${2}')${3}"
+
    content: "getElementsBy${1:TagName}('${2}')${3}"
 
});
 
});
 
a.push({
 
a.push({
title:"get",
+
    title: "get",
tabTrigger:"get",
+
    tabTrigger: "get",
content:"getElementBy${1:Id}('${2}')${3}"
+
    content: "getElementBy${1:Id}('${2}')${3}"
 
});
 
});
 
a.push({
 
a.push({
title:"cl",
+
    title: "cl",
tabTrigger:"cl",
+
    tabTrigger: "cl",
content:"console.log(${1});"
+
    content: "console.log(${1});"
 
});
 
});
 
a.push({
 
a.push({
title:"ret",
+
    title: "ret",
tabTrigger:"ret",
+
    tabTrigger: "ret",
content:"return ${1:result}"
+
    content: "return ${1:result}"
 
});
 
});
 
a.push({
 
a.push({
title:"fori",
+
    title: "fori",
tabTrigger:"fori",
+
    tabTrigger: "fori",
content:"for (var ${1:prop} in ${2:Things}) {\n\t${0:$2[$1]}\n}"
+
    content: "for (var ${1:prop} in ${2:Things}) {\n\t${0:$2[$1]}\n}"
 
});
 
});
 
a.push({
 
a.push({
title:"jsonp",
+
    title: "jsonp",
tabTrigger:"jsonp",
+
    tabTrigger: "jsonp",
content:"JSON.parse(${1:jstr});"
+
    content: "JSON.parse(${1:jstr});"
 
});
 
});
 
a.push({
 
a.push({
title:"jsons",
+
    title: "jsons",
tabTrigger:"jsons",
+
    tabTrigger: "jsons",
content:"JSON.stringify(${1:object});"
+
    content: "JSON.stringify(${1:object});"
 
});
 
});
 
a.push({
 
a.push({
title:"for-",
+
    title: "for-",
tabTrigger:"for-",
+
    tabTrigger: "for-",
content:"for (var ${1:i} = ${2:Things}.length; ${1:i}--; ) {\n\t${0:${2:Things}[${1:i}];}\n}"
+
    content: "for (var ${1:i} = ${2:Things}.length; ${1:i}--; ) {\n\t${0:${2:Things}[${1:i}];}\n}"
 
});
 
});
 
a.push({
 
a.push({
title:"for",
+
    title: "for",
tabTrigger:"for",
+
    tabTrigger: "for",
content:"for (var ${1:i} = 0; $1 < ${2:Things}.length; $1++) {\n\t${3:$2[$1]}$0\n}"
+
    content: "for (var ${1:i} = 0; $1 < ${2:Things}.length; $1++) {\n\t${3:$2[$1]}$0\n}"
 
});
 
});
 
a.push({
 
a.push({
title:"forr",
+
    title: "forr",
tabTrigger:"forr",
+
    tabTrigger: "forr",
content:"for (var ${1:i} = ${2:Things}.length - 1; $1 >= 0; $1--) {\n\t${3:$2[$1]}$0\n}"
+
    content: "for (var ${1:i} = ${2:Things}.length - 1; $1 >= 0; $1--) {\n\t${3:$2[$1]}$0\n}"
 
});
 
});

Latest revision as of 13:11, 19 October 2020

var myWindow;

var oReq = new XMLHttpRequest();
oReq.addEventListener("load", reqListener);
var l = -1;
var baseUrl = "https://wiki.hsyco.com/3.7/index.php/"
var urls = [{
        url: "Event_Keywords",
        sub: "Events",
        opt: {
            event: true
        },
        arr: "events"
    },
    {
        url: "Action_Keywords",
        sub: "Actions",
        opt: {
            action: true
        },
        arr: "actions"
    },
    {
        url: "JavaScript_Callback_Functions_API",
        sub: "Javascript Callback Functions",
        opt: {
            jsEvent: true
        },
        arr: "events"
    },
    {
        url: "JavaScript_Command_and_Utility_Functions_API",
        sub: "Javascript Command and Utility Functions",
        opt: {
            jsAction: true
        },
        arr: "js"
    },
    {
        url: "Index.js",
        sub: "Javascript",
        opt: {
            js: true
        },
        arr: "indexjs"
    },
];

function reqListener() {
    console.log("loaded " + urls[l].url);
    var div = document.createElement('div');
    div.innerHTML = this.responseText;
    urls[l].res = parse(div, urls[l].sub, urls[l].opt);
    next();
}

// CALL THIS
function next() {
    if (!myWindow) {
        initWin();
        myWindow.document.write("Please wait...");
    }
    l++;
    if (urls[l]) {
        oReq.open("GET", baseUrl + urls[l].url);
        oReq.send();
    } else {
        var head = "\
ace.define('ace/snippets/%what',['require','exports','module'], function(require, exports, module) {\n\
var s = {\n\
\t// scopes\n\
\tevents:[],\n\
\tactions:[],\n\
\tjs:[]\n\
};\n\n\
exports.snippetText = s;\n\n\n";

        var tail = "exports.scope = '%what';\n});";

        str = head.replace("%what", "events");
        for (var i = 0; i < urls.length - 1; i++) { // skip index.js
            str += "var a = s." + urls[i].arr + ";\n";
            str += urls[i].res;
            str += "\n\n\n";
        }
        str += tail.replace("%what", "events");;
        openWin("events.js", str);


        str = head.replace("%what", "javascript");
        str += "var a = s.js;\n";
        str += urls[4].res;
        str += "\n\n\n// js\n\n";
        var s;
        for (var i = 0; i < jsSnippets.length; i++) {
            s = jsSnippets[i];
            str += "a.push({\n";
            str += "\ttitle:\"" + s.title.split("(")[0] + "\",\n";
            str += "\ttabTrigger:\"" + s.tabTrigger + "\",\n";
            str += "\tcontent:\"" + s.content.replace(/\t/g, "\\t").replace(/\n/g, "\\n") + "\"\n";
            str += "});\n";
        }
        str += "\n\n\n";
        str += tail.replace("%what", "javascript");;
        openWin("javascript.js", str);



        console.log("done");
    }
}

function download(filename, text) {
    var pom = document.createElement('a');
    pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
    pom.setAttribute('download', filename);

    if (document.createEvent) {
        var event = document.createEvent('MouseEvents');
        event.initEvent('click', true, true);
        pom.dispatchEvent(event);
    } else {
        pom.click();
    }
}

function print(sub, opt) {
    var url = "";
    for (var i = 0; i < urls.length; i++) {
        if (window.location.href.indexOf(urls[i].url) != -1) {
            sub = urls[i].sub;
            opt = urls[i].opt;
            url = urls[i].url;
            break;
        }
    }

    var str = parse(document, sub, opt, baseUrl + url);
    openWin("", str);
}

function initWin() {
    myWindow = window.open("about:blank", "Editor Snippets");

    myWindow.document.write("\
	<script>\n\
		function addDownload(n,f) {\n\
			var data = document.getElementById('txt'+n).value;\n\
			var link = document.createElement('a');\n\
			link.setAttribute('href','data:text/text;charset=utf-8,'+encodeURI(data));\n\
			link.setAttribute('download', f);\n\
			link.innerHTML = 'download file';\n\
			document.getElementById('p'+n).appendChild(link);\n\
		}\n\
	</script>");
}

var _no = 0; // number of outputs
function openWin(title, str) {
    if (!myWindow)
        initWin();

    _no++;

    myWindow.document.write("<hr>");
    myWindow.document.write("<h1>" + title + "</h1>");
    myWindow.document.write("<p id='p" + _no + "'>");
    myWindow.document.write("</p>");
    myWindow.document.write("<textarea id='txt" + _no + "' style='width:800px; height:400px'>" + str + "</textarea>");
    myWindow.document.write("<script>addDownload(" + _no + ",'" + title + "')</script>");
    myWindow.document.write("<p></p>");
    myWindow.document.getElementById('txt' + _no).select();
}

function parse(e, asub, opt, url) {
    function rr(s) { // remove return and trim
        s = s.trim();
        while (s[s.length - 1] == "\n")
            s = s.substr(0, s.length - 1);
        return s;
    }

    function stripDescr(d) {
        d = d.replace(/\n+/g, "");
        d = d.replace(/(^(<br>)+|(<br>)+$)/g, ""); // initial, end br
        d = d.replace(/\n+/g, "<br>");
        d = d.replace(/(<br>)+/g, "<br>");
        d = d.replace(/\<(\/?(ul|li|string|br))\>/g, "+___$1___-"); // keep these
        d = d.replace(/<(?:.|\n)*?>/gm, ''); // strip html
        d = d.replace(/\+___/g, "<"); // restore
        d = d.replace(/___-/g, ">");
        d = d.replace(/(<br>)?<\/li>(<br>)?/g, "</li>"); // remove breaklines inside/outside li
        d = d.trim();
        return d;
    }
    var a = e.getElementsByClassName("mw-headline");
    if (!opt) opt = {};

    var nl = "\\n\\" + "\n";

    var snippets = [];
    var variations = [];
    var pE, nE;
    var str = "";
    var title, subtitle, descr, snippet, content;
    var bsub; // combined with asub to create subtitle
    for (var i = 0; i < a.length; i++) {
        pE = a[i].parentElement;
        nE = pE ? pE.nextElementSibling : null;

        title = rr(a[i].innerHTML);
        subtitle = asub + (bsub ? ", " + bsub : "");

        snippet = "";
        if (pE.nodeName == "H3") {
            // if (nE && (nE.nodeName == "PRE" || nE.className.indexOf("mw-code") != -1)) { // old ver
            if (nE && (nE.nodeName == "PRE" || (nE.childNodes[0] && nE.childNodes[0].nodeName == "PRE")) && nE.innerHTML.indexOf("\t") == -1) {
                snippet = nE.innerHTML;
                snippet = snippet.replace(/<(?:.|\n)*?>/gm, ''); // strip html
                content = snippet;
                title = snippet;
                nE = nE.nextElementSibling;
            }
            descr = "";
            var grabAll = true; //opt.event || opt.action; // grab all html
            while (nE) {
                if (!grabAll && nE.nodeName != "P" && nE.nodeName != "UL")
                    break;
                if (nE.className == "mw-headline" || nE.getElementsByClassName("mw-headline").length)
                    break;
                if (nE.getAttribute("data-skipeditordoc") != null) // like CameraCommandEvent on JavaScript_Callback_Functions_API
                    break;

                var d = rr(grabAll ? nE.outerHTML : nE.innerHTML);

                if (!grabAll) {
                    d = stripDescr(d);
                    if (nE.nodeName == "UL")
                        d = "<ul>" + d + "</ul>";

                    if (d.indexOf("Examples:") != -1)
                        break;

                    if (d.indexOf("Parameters:") != -1 ||
                        d.indexOf("Returns:") != -1) {
                        descr += "<br>";
                    }
                    if (d)
                        descr += (descr ? "<br>" : "") + d;
                } else {
                    // bulb image
                    d = d.replace(/<img .+src=".+Bulbgraph.png".+(width="[^"]+").+(height="[^"]+")[^>]+>/g,
                        "<img src=\"/manager/pic/bulb.png\" $1 $2>");
                    // replace \n\t
                    d = d.replace(/\n/g, "\\n").replace(/\t/g, "\\t");
                    // variations?
                    if (nE.nodeName == "TABLE") {
                        var f = nE.getElementsByTagName("TD")[0];
                        if (f && f.innerHTML &&
                            (f.innerHTML.indexOf("Action") != -1 || f.innerHTML.indexOf("Event") != -1)) {
                            var rows = nE.getElementsByTagName("TR");
                            var cols;
                            var txt, atxt;
                            for (var r = 1; r < rows.length; r++) {
                                cols = rows[r].getElementsByTagName("TD");
                                txt = cols[0] ? cols[0].innerHTML : "";
                                if (txt && txt != title) {
                                    atxt = txt.split(/\s*\<br[^\>]*\>\s*/);
                                    for (var v = 0; v < atxt.length; v++) {
                                        variations.push({
                                            src: atxt[v].trim(),
                                            descr: cols[1] ? cols[1].innerHTML : ""
                                        });
                                    }
                                }
                            }
                        }
                    }
                    if (d)
                        descr += d;
                }

                nE = nE.nextElementSibling;
            }
            // check variations
            var s, c;
            for (var v = 0; v < variations.length; v++) {
                s = variations[v].src;
                s = s.replace("&lt;", "<");
                s = s.replace("&gt;", ">");
                // replace variables (lowercase words), separated by space or start/end line
                c = s.replace(/(^|\s*|:)("?)([a-z0-9:_\.]+)("?)($|\n|\s*|:)/g, "$1$2\${0:$3}$4$5");
                var count = 1;
                c = c.replace(/\$\{0/g, function (match, capture) {
                    return '${' + count++;
                });

                // strip html, new lines
                s = s.replace(/<(?:.|\n)*?>/gm, '').replace(/\n/g, "");
                c = c.replace(/<(?:.|\n)*?>/gm, '').replace(/\n/g, "");
                variations[v].snippet = s;
                variations[v].content = c; // strip html;
            }

            if (!snippet) {
                snippet = title;
                content = snippet;
            }

            title = rr(title);
            snippet = rr(snippet);
            content = rr(content);

            if (opt.jsEvent || (opt.js && bsub == "Events")) {
                if (title.indexOf("function") != 0)
                    title = "function " + title;
                if (snippet.indexOf("function") != 0)
                    snippet = "function " + snippet;
                if (content.indexOf("function") != 0)
                    content = "function " + content;
                content += (opt.js ? " {" : " : {") + "\\n";
                content += "\\t${1:// body...} " + "\\n";
                content += "}";
            } else if (opt.jsAction || (opt.js && bsub == "Functions")) {
                content = content.replace(/([\(,]+\s?)([^\)^\s^,]+)/g, "$1\${0:$2}");
                var count = 1;
                content = content.replace(/\$\{0/g, function (match, capture) {
                    return '${' + count++;
                });
            } else if (opt.js && (bsub == "Examples" || bsub == "User Object")) {
                continue;
            } else {
                content += " ";
            }

            if (title && descr) {
                descr = descr.replace(/["“”]/g, '\\"');
                if (variations.length) {
                    for (var v = 0; v < variations.length; v++) {
                        snippets.push({
                            title: variations[v].snippet,
                            subtitle: subtitle,
                            descr: v ? "_again_" : descr, //+"<br><br>"+variations[v].descr+_url,
                            snippet: variations[v].snippet,
                            content: variations[v].content
                        });
                    }
                    variations = [];
                } else {
                    snippets.push({
                        title: title,
                        subtitle: subtitle,
                        descr: descr,
                        snippet: snippet,
                        content: content
                    });
                }
            }
        } else if (pE.nodeName == "H2") {
            bsub = title;
        }
    }

    if (false && !opt.noSort) {
        snippets.sort(function (a, b) {
            if (a.snippet > b.snippet) {
                return 1;
            }
            if (a.snippet < b.snippet) {
                return -1;
            }
            // a must be equal to b
            return 0;
        });
    }

    var s;
    for (var i = 0; i < snippets.length; i++) {
        s = snippets[i];
        str += "a.push({\n";
        str += "\ttitle:\"" + s.title.replace(/["“”]/g, '\\"') + "\",\n";
        str += "\tsubTitle:\"" + s.subtitle.replace(/["“”]/g, '\\"') + "\",\n";

        if (s.descr == "_again_")
            str += "\tdocHTML:a.last().docHTML,\n";
        else
            str += "\tdocHTML:\"" + s.descr + "\",\n";

        //str += "\ttabTrigger:\""+s.snippet.replace(/["“”]/g,'\\"')+"\",\n";
        str += "\tcontent:\"" + s.content.replace(/["“”]/g, '\\"') + "\"\n";
        str += "});\n";
    }

    return str;
}

//////////////////////////////////////////////////////////////////////
// DEFAULT JS
//////////////////////////////////////////////////////////////////////
var jsSnippets = [];
var a = jsSnippets;
a.push({
    title: "fun",
    tabTrigger: "fun",
    content: "function ${1?:function_name}(${2:argument}) {\n\t${3:// body...}\n}"
});
a.push({
    title: "if",
    tabTrigger: "if",
    content: "if (${1:true}) {\n\t${0}\n}"
});
a.push({
    title: "ife",
    tabTrigger: "ife",
    content: "if (${1:true}) {\n\t${2}\n} else {\n\t${0}\n}"
});
a.push({
    title: "ter",
    tabTrigger: "ter",
    content: "${1:/* condition */} ? ${2:a} : ${3:b}"
});
a.push({
    title: "switch",
    tabTrigger: "switch",
    content: "switch (${1:expression}) {\n\tcase '${3:case}':\n\t\t${4:// code}\n\t\tbreak;\n\t${5}\n\tdefault:\n\t\t${2:// code}\n}"
});
a.push({
    title: "case",
    tabTrigger: "case",
    content: "case '${1:case}':\n\t${2:// code}\n\tbreak;\n${3}"
});
a.push({
    title: "wh",
    tabTrigger: "wh",
    content: "while (${1:/* condition */}) {\n\t${0:/* code */}\n}"
});
a.push({
    title: "try",
    tabTrigger: "try",
    content: "try {\n\t${0:/* code */}\n} catch (e) {}"
});
a.push({
    title: "do",
    tabTrigger: "do",
    content: "do {\n\t${2:/* code */}\n} while (${1:/* condition */});"
});
a.push({
    title: "setTimeout",
    tabTrigger: "setTimeout",
    content: "setTimeout(function() {${3:$TM_SELECTED_TEXT}}, ${1:10});"
});
a.push({
    title: "gett",
    tabTrigger: "gett",
    content: "getElementsBy${1:TagName}('${2}')${3}"
});
a.push({
    title: "get",
    tabTrigger: "get",
    content: "getElementBy${1:Id}('${2}')${3}"
});
a.push({
    title: "cl",
    tabTrigger: "cl",
    content: "console.log(${1});"
});
a.push({
    title: "ret",
    tabTrigger: "ret",
    content: "return ${1:result}"
});
a.push({
    title: "fori",
    tabTrigger: "fori",
    content: "for (var ${1:prop} in ${2:Things}) {\n\t${0:$2[$1]}\n}"
});
a.push({
    title: "jsonp",
    tabTrigger: "jsonp",
    content: "JSON.parse(${1:jstr});"
});
a.push({
    title: "jsons",
    tabTrigger: "jsons",
    content: "JSON.stringify(${1:object});"
});
a.push({
    title: "for-",
    tabTrigger: "for-",
    content: "for (var ${1:i} = ${2:Things}.length; ${1:i}--; ) {\n\t${0:${2:Things}[${1:i}];}\n}"
});
a.push({
    title: "for",
    tabTrigger: "for",
    content: "for (var ${1:i} = 0; $1 < ${2:Things}.length; $1++) {\n\t${3:$2[$1]}$0\n}"
});
a.push({
    title: "forr",
    tabTrigger: "forr",
    content: "for (var ${1:i} = ${2:Things}.length - 1; $1 >= 0; $1--) {\n\t${3:$2[$1]}$0\n}"
});