/** Runnable examples functionality Copyright: 2012 by Digital Mars License: http://boost.org/LICENSE_1_0.txt, Boost License 1.0 Authors: Andrei Alexandrescu, Damian Ziemba */ /** Script workflow: 1. Scan current document DOM tree for
 elements with class=d_code.
    
...
blocks are generated by DDOC example sections. 2. Iterate each pre element and apply our custom form, replacing default
 block
3. Get text from original 
 block, strip any spaces and newlines from it and compute md5sum
4. Look up mainPage map with md5sum as a key and see if there are any elements associated with this key
5. If yes: Add to our custom form default Standard input and/or Standard Arguments, stdin being 0 key, stdout 1 key.
6. If no: Just skip to point 7
7. Continue to next 
 element and repeat begging from point 2 if there are still nodes left

How to add new example or update existing:

If example doesn't require any standard input neither standard argugments by default you are done.
Otherwise, copy example text without example separator ie:

------
[start here]
import std.stdio;

void main(string[] args) {
    writeln("Hello world. ", args);
    writeln("What's your name?");
    writeln("Hello ", readln());
}
[end here]
------

Go to http://dpaste.dzfl.pl/md5sum, paste your example to Source box and click "Compute".
Copy generated md5sum. Open run-main-website.js file and add following

mainPage["yourMd5Sum"] = ["standard input is has 0 position", "standard args has 1 position"];

Save, reload website and see if standard input and/or standard arguments are displayed in your example form.

TL;DR
All examples are replaced with custom form by default. You need to do additional work only if you wan't
your example to have deafault standard input or default standard arguments.

*/

/**
Taken from http://www.webtoolkit.info/javascript-md5.html
*/
var MD5 = function (string) {

    function RotateLeft(lValue, iShiftBits) {
        return (lValue<>>(32-iShiftBits));
    }

    function AddUnsigned(lX,lY) {
        var lX4,lY4,lX8,lY8,lResult;
        lX8 = (lX & 0x80000000);
        lY8 = (lY & 0x80000000);
        lX4 = (lX & 0x40000000);
        lY4 = (lY & 0x40000000);
        lResult = (lX & 0x3FFFFFFF)+(lY & 0x3FFFFFFF);
        if (lX4 & lY4) {
            return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
        }
        if (lX4 | lY4) {
            if (lResult & 0x40000000) {
                return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
            } else {
                return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
            }
        } else {
            return (lResult ^ lX8 ^ lY8);
        }
    }

    function F(x,y,z) { return (x & y) | ((~x) & z); }
    function G(x,y,z) { return (x & z) | (y & (~z)); }
    function H(x,y,z) { return (x ^ y ^ z); }
    function I(x,y,z) { return (y ^ (x | (~z))); }

    function FF(a,b,c,d,x,s,ac) {
        a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac));
        return AddUnsigned(RotateLeft(a, s), b);
    };

    function GG(a,b,c,d,x,s,ac) {
        a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac));
        return AddUnsigned(RotateLeft(a, s), b);
    };

    function HH(a,b,c,d,x,s,ac) {
        a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac));
        return AddUnsigned(RotateLeft(a, s), b);
    };

    function II(a,b,c,d,x,s,ac) {
        a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac));
        return AddUnsigned(RotateLeft(a, s), b);
    };

    function ConvertToWordArray(string) {
        var lWordCount;
        var lMessageLength = string.length;
        var lNumberOfWords_temp1=lMessageLength + 8;
        var lNumberOfWords_temp2=(lNumberOfWords_temp1-(lNumberOfWords_temp1 % 64))/64;
        var lNumberOfWords = (lNumberOfWords_temp2+1)*16;
        var lWordArray=Array(lNumberOfWords-1);
        var lBytePosition = 0;
        var lByteCount = 0;
        while ( lByteCount < lMessageLength ) {
            lWordCount = (lByteCount-(lByteCount % 4))/4;
            lBytePosition = (lByteCount % 4)*8;
            lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount)<>>29;
        return lWordArray;
    };

    function WordToHex(lValue) {
        var WordToHexValue="",WordToHexValue_temp="",lByte,lCount;
        for (lCount = 0;lCount<=3;lCount++) {
            lByte = (lValue>>>(lCount*8)) & 255;
            WordToHexValue_temp = "0" + lByte.toString(16);
            WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length-2,2);
        }
        return WordToHexValue;
    };

    function Utf8Encode(string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    };

    var x=Array();
    var k,AA,BB,CC,DD,a,b,c,d;
    var S11=7, S12=12, S13=17, S14=22;
    var S21=5, S22=9 , S23=14, S24=20;
    var S31=4, S32=11, S33=16, S34=23;
    var S41=6, S42=10, S43=15, S44=21;

    string = Utf8Encode(string);

    x = ConvertToWordArray(string);

    a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476;

    for (k=0;k");
}

function safeVar(data, path)
{
    var p = path.split(".");
    var res = null;

    try
    {
        res = data[p[0]][p[1]];
        if (typeof res == "object")
            res = "";
    }
    catch (e)
    {
        return "";
    }

    return res;
}

function parseOutput(data, o, oTitle)
{
    if (typeof data.compilation == "undefined")
    {
        o.text("Temporarily unavailable");
        return;
    }

    var output = "";
    var cout = safeVar(data, "compilation.stdout");
    var stdout = safeVar(data, "runtime.stdout");
    var stderr = safeVar(data, "runtime.stderr");
    var ctime = parseInt(safeVar(data, "compilation.time"));
    var rtime = parseInt(safeVar(data, "runtime.time"));
    var cstatus = parseInt(safeVar(data, "compilation.status"));
    var rstatus = parseInt(safeVar(data, "runtime.status"));
    var cerr = safeVar(data, "compilation.err");
    var rerr = safeVar(data, "runtime.err");
    var defaultOutput = data.defaultOutput || '-- No output --';

    if (cstatus != 0)
    {
        oTitle.text("Compilation output ("+cstatus+": "+cerr+")");
        if ($.browser.msie)
            o.html(nl2br(cout));
        else
            o.text(cout);

        return;
    }
    else
    {
        oTitle.text("Application output");// (compile "+ctime+"ms, run "+rtime+"ms)");
        if ( cout != "")
            output = 'Compilation output: \n' + cout + "\n";

        output += (stdout == "" && stderr == "" ? defaultOutput : stdout);

        if (stderr != "")
            output += stderr;

        if (rstatus != 0)
            oTitle.text("Application output ("+rstatus+": "+rerr+")");
    }

    if ($.browser.msie)
        o.html(nl2br(cout));
    else
        o.text(output);
}

$(document).ready(function()
{
    setUpExamples();

    var currentPage = $(location).attr('pathname');

    if ($('body')[0].id != "Home")
        return;

    $('pre[class~=d_code]').each(function(index)
    {
        var stripedText = $(this).text().replace(/\s/gm,'');
        var md5sum = MD5(stripedText);

        var stdin = "";
        var args = "";
        var currentExample = $(this);
        var orig = currentExample.html();

        if (typeof mainPage !== 'undefined' && md5sum in mainPage)
        {
            var elements = mainPage[md5sum];

            if (elements == null)
                return; // this example is not runnable online

            if (elements[0] != null)
                stdin = elements[0];

            if (elements[1] != null)
                args = elements[1];
        }

        currentExample.replaceWith(
            '
'+orig+'
' + '
' + '' + '
Standard input
' + '
' + '
Command line arguments
' + '
' + '
Application output
' + '' + '' + '' + '' + '
' ); }); $('textarea[class=d_code]').each(function(index) { var parent = $(this).parent(); var outputDiv = parent.children("div.d_code_output"); setupTextarea(this, {parent: parent, outputDiv: outputDiv, stdin: true, args: true}); }); }); function setupTextarea(el, opts) { opts = opts || {}; // set default opts opts = jQuery.extend({}, { stdin: false, args: false, transformOutput: function(out) { return out } }, opts); if (!!opts.parent) var parent = opts.parent; else console.error("parent node node not found"); if (!!opts.outputDiv) var outputDiv = opts.outputDiv; else console.error("outputDiv node not found"); var thisObj = $(el); parent.css("display", "block"); var orgSrc = parent.parent().children("div.d_code").children("pre.d_code"); var prepareForMain = function() { var src = $.browser.msie && $.browser.version < 9.0 ? orgSrc[0].innerText : orgSrc.text(); var arr = src.split("\n"); var str = ""; for ( i = 0; i < arr.length; i++) { str += arr[i]+"\n"; } if ($.browser.msie && $.browser.version < 9.0) str = str.substr(0, str.length - 1); else str = str.substr(0, str.length - 2); return str; }; var editor = CodeMirror.fromTextArea(thisObj[0], { lineNumbers: true, tabSize: 4, indentUnit: 4, indentWithTabs: true, mode: "text/x-d", lineWrapping: true, theme: "eclipse", readOnly: false, matchBrackets: true }); editor.setValue(prepareForMain()); var height = function(diff) { var par = code != null ? code : parent.parent().children("div.d_code"); return (parseInt(par.css('height')) - diff) + 'px'; }; var runBtn = parent.children("input.runButton"); var editBtn = parent.children("input.editButton"); var resetBtn = parent.children("input.resetButton"); var code = $(editor.getWrapperElement()); code.css('display', 'none'); var plainSourceCode = parent.parent().children("div.d_code"); var output = outputDiv.children("textarea.d_code_output"); var outputTitle = outputDiv.children("span.d_code_title"); if (opts.args) { var argsBtn = parent.children("input.argsButton"); var argsDiv = parent.children("div.d_code_args"); var args = argsDiv.children("textarea.d_code_args"); var orgArgs = args.val(); } if (opts.stdin) { var inputBtn = parent.children("input.inputButton"); var stdinDiv = parent.children("div.d_code_stdin"); var stdin = stdinDiv.children("textarea.d_code_stdin"); var orgStdin = stdin.val(); } var hideAllWindows = function(args) { args = args || {}; if (opts.stdin) { stdinDiv.css('display', 'none'); } if (opts.args) { argsDiv.css('display', 'none'); } outputDiv.css('display', 'none'); if (!args.keepPlainSourceCode) { plainSourceCode.css('display', 'none'); } if (!args.keepCode) { code.css('display', 'none'); } }; if (opts.args) { argsBtn.click(function(){ resetBtn.css('display', 'inline-block'); args.css('height', height(31)); hideAllWindows(); argsDiv.css('display', 'block'); args.focus(); }); } if (opts.stdin) { inputBtn.click(function(){ resetBtn.css('display', 'inline-block'); stdin.css('height', height(31)); hideAllWindows(); stdinDiv.css('display', 'block'); stdin.focus(); }); } editBtn.click(function(){ resetBtn.css('display', 'inline-block'); hideAllWindows(); code.css('display', 'block'); editor.refresh(); editor.focus(); }); resetBtn.click(function(){ resetBtn.css('display', 'none'); editor.setValue(prepareForMain()); if (opts.args) { args.val(orgArgs); } if (opts.stdin) { stdin.val(orgStdin); } hideAllWindows(); plainSourceCode.css('display', 'block'); }); runBtn.click(function(){ resetBtn.css('display', 'inline-block'); $(this).attr("disabled", true); var args = {}; // check what boxes are currently open if (opts.keepCode) { args.keepCode = code.is(":visible"); args.keepPlainSourceCode = plainSourceCode.is(":visible"); } hideAllWindows(args); output.css('height', opts.outputHeight || height(31)); outputDiv.css('display', 'block'); outputTitle.text("Application output"); output.html("Running..."); output.focus(); var data = { 'code' : opts.transformOutput(editor.getValue()), } if (opts.stdin) { data.stdin = stdin.val(); } if (opts.args) { data.args = args.val(); } $.ajax({ type: 'POST', url: "https://dpaste.dzfl.pl/request/", dataType: "json", data: data, success: function(data) { data.defaultOutput = opts.defaultOutput; parseOutput(data, output, outputTitle); runBtn.attr("disabled", false); }, error: function(jqXHR, textStatus, errorThrown ) { output.html("Temporarily unavailable"); if (typeof console != "undefined") { console.log(textStatus + ": " + errorThrown); } runBtn.attr("disabled", false); } }); }); }; function setUpExamples() { /* Sets up expandable example boxes. * max-height and CSS transitions are used to animate the closing and opening for smooth animations even on less powerful devices */ $('.example-box').each(function() { var $box = $(this); var boxId = $box.attr('id'); // A little juggling here because the content needs to be a block element and the control needs to be an inline // element in the previous paragraph. var $control = $('#' + boxId + '-control'); $control.attr('aria-controls', boxId); var $showLabel = $('Show example '); var $hideLabel = $('Hide example '); function toggle() { if ($box.attr('aria-hidden') === 'true') { $box.attr('aria-hidden', false); $control.attr('aria-expanded', true); $control.empty().append($hideLabel); $box.css('max-height', $box[0].scrollHeight); } else { $box.attr('aria-hidden', true); $control.attr('aria-expanded', false); $control.empty().append($showLabel); $box.css('max-height', 0); } return false; } $control.on('click', toggle); toggle(); }); // NB: href needed for browsers to include the controls in the (keyboard) tab order $('.example-control').attr('href', '#'); }