MediaWiki:Common.js

From Lifeguide Wiki
Revision as of 21:24, 19 January 2023 by DonCruickshank (talk | contribs) (Created page with "var keywords = ["begin", "end", "show", "page", "if", "named", "after", "section", "then", "goto", "feedback", "to", "value", "of", "set", "default", "saveandload", "load", "save", "for", "graph", "else"]; var functions = ["and", "or", "add", "sum", "not", "multiply", "divide", "morethan", "lessthan", "morethanequal", "lessthanequal", "size", "contains", "isempty", "append", "authenticateuser", "cancelemail", "cancelsms", "checkemailvalidity", "checkphonenumbervalidity"...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
var keywords = ["begin",
"end",
"show",
"page",
"if",
"named",
"after",
"section",
"then",
"goto",
"feedback",
"to",
"value",
"of",
"set",
"default",
"saveandload",
"load",
"save",
"for",
"graph",
"else"];

var functions = ["and",
"or",
"add",
"sum",
"not",
"multiply",
"divide",
"morethan",
"lessthan",
"morethanequal",
"lessthanequal",
"size",
"contains",
"isempty",
"append",
"authenticateuser",
"cancelemail",
"cancelsms",
"checkemailvalidity",
"checkphonenumbervalidity",
"checkuserexists",
"sendemail",
"sendtext",
"loadvalue",
"makenewuser",
"getemail",
"setemail",
"countoccurrences",
"saveuniquevalue",
"savevalue",
"stringlength",
"randomnumber",
"timesincelogin",
"printtime",
"hasseen",
"patternmatch",
"resetpassword",
"changepassword",
"checkuserenabled",
"comparetimes",
"currenttime",
"replaceall",
"getuserid",
"urlencode",
"hmacencode"
];

var operators = ["\\+","-","\\*","/","=","&lt;","&gt;","<=",">="];
var realoperators = ["+","-","*","/","=","&lt;","&gt;","<=",">="];

var stringStyle="color:blue";
var commentStyle="color:green";
var functionStyle = "color:#F0F";
var keywordStyle = "color:red;font-weight:bold;";

document.addEventListener("DOMContentLoaded", function(event) {
    document.querySelectorAll("code").forEach(function (codeBlock) {
        prettify(codeBlock);
    });
});

function prettify(codeBlock) {
    var text = codeBlock.innerHTML;
    text = text.replace(/\n/g, "<br/>\n");
    text = text.replace(/#([^\n]*)/g, "<span style='" + commentStyle + "'>#$1</span>");
    text = dealWithComments(text);
    codeBlock.innerHTML = text;

//    $(codeBlock).css('padding', '2px');
//    $(codeBlock).css('background', '#eed');
//    $(codeBlock).css('border', '#aaa dashed 1px');
}

function dealWithComments(text)
{
    if (text.indexOf("#")==0) text = " " + text;
    cs = text.split("#")

    var commentText = "";

    for (var x=0; x<cs.length; x++)
    {
        if (x==0)
        {
            commentText += dealWithStrings(cs[x]);
        }
        else
        {
            comment = cs[x].substring(0, cs[x].indexOf("\n"));
            rest = cs[x].substring(cs[x].indexOf("\n"));
            commentText += "<span style='" + commentStyle + "'>#" +comment + "</span>" + dealWithStrings(rest);
        }
    }
    return commentText
}

function dealWithStrings(text)
{
    if (text.indexOf("\"")==0) text = " " + text;
    ts = text.replace(/\\"/g, "\\'").split("\"")

    var newText = "";

    for (var i=0; i<ts.length; i++)
    {
        if (i%2==0)
        {
            var t = prettifyOperators(ts[i]);
            t = convert(t, functions, functionStyle);
            t = convert(t, keywords, keywordStyle);
            newText = newText+t;
        }
        else
        {
            newText = newText+ "<span style='" + stringStyle + "'>\"" + ts[i] + "\"</span>";
        }
    }

    newText = newText.replace(/\\'/g, "\\\"")
    return newText
}

function prettifyOperators(s)
{
    var ret = s;
    for (var i=0; i<operators.length; i++)
    {
        var newText = "";
        var op = operators[i]
        ret = ret.replace(new RegExp("([ 0-9]|^)" + op + "([ 0-9]|$)", "g"), "$1<span style='"+functionStyle+"'>" + realoperators[i] + "</span>$2");
    }
    return ret;
}

function convert(s, vocab, style)
{
    var ret = s;
    for (var i=0; i<vocab.length; i++)
    {
        var newText = "";
        var word = vocab[i]
        ret = ret.replace(new RegExp("([ \t\\(\\)\\n\\r,]|^)"+ word + "([ \t\\)\\(\\n\\r,]|$)", "gi"), "$1<span style='"+style+"'>" + word + "</span>$2");
    }
    return ret;
}