﻿String.prototype.LTrim = new Function("return this.replace(/^\\s+/,'')");

function RenderCodeBlocks() {

    jQuery("pre:has(code)").each(function() {
        jQuery(this).replaceWith(RenderCode(jQuery(this).text()));
    });
}

function RenderCode(code) {
    lines = code.split(/\n/);
    outputArray = new Array();

    for (var i = 0; i < lines.length; i++) {
        line = lines[i];

        oddEven = i % 2 == 0 ? "odd" : "even";
        numTabs = line.length - line.LTrim().length;

        outputArray.push(new Array(oddEven, line, numTabs));
    }

    while (outputArray[0][1] == "") {
        outputArray.shift();
    }

    while (outputArray[outputArray.length - 1][1] == "") {
        outputArray.pop();
    }

    output = '<ol class="codelist">';
    for (var i = 0; i < outputArray.length; i++) {
        output = output + '<li class="' + outputArray[i][0] + ' ' + "tab" + outputArray[i][2] + '">' + outputArray[i][1] + "</li>";

    }
    output = output + "</ol>";

    return output;
}
