Wednesday, July 29, 2015

The below is a simple demonstration of some awesome javascript code.
It will compare two sets of text, generating the differences in a nice highlighted way, The output looks like:

...I will give you a complete account a full explanation of the system...



<style>
div { border: 1px solid #000; }
del { color: #f00; text-decoration: line-through; }
ins { color: #070; text-decoration: underline; }
</style>
Old: <textarea id="older" rows="10" cols="40">The below content is the translation of the "Lorem Ipsum" text.

But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?</textarea>
New: <textarea id="newer" rows="10" cols="40">The below content is the translation of the "Lorem Ipsum" text, run through google translate from English to Spanish to English.

But I must explain to you how was all this mistaken idea of ??denouncing pleasure and praising pain and I will give a full explanation of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor is there anyone who loves or pursues or wishes to obtain pain of itself, because it is pain, but because occasionally circumstances in which fatigue and pain you can purchase a great pleasure occur. To take a trivial example, which of us undertakes laborious physical exercise, except to obtain some advantage from it? But who has the right to criticize a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?</textarea>
<br/>
<button onclick="compare();">Compare</button>
<div style="white-space: pre-wrap;" id="differences" rows="10" cols="40" />
<script>
String.prototype.replaceAll = function (find, replace) { return this.replace(new RegExp(find, 'g'), replace); }
function compare() {
  document.getElementById('differences').innerHTML = diffString(document.getElementById('older').value, document.getElementById('newer').value);
}

function splitString(s) {
  return s.split(/\s+/);
}

function diffString(o, n) {
    var escapeEntities = function(s) { return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;"); }
    o = escapeEntities(o);
    n = escapeEntities(n);
    o = o.replace(/\s+$/, '');
    n = n.replace(/\s+$/, '');
    var out = diff(o == '' ? [] : splitString(o), n == '' ? [] : splitString(n));
    var str = '';
    var oSpace = o.match(/\s+/g);
    if (oSpace == null) {
        oSpace = ['\n'];
    } else {
        oSpace.push('\n');
    }
    var nSpace = n.match(/\s+/g);
    if (nSpace == null) {
        nSpace = ['\n'];
    } else {
        nSpace.push('\n');
    }
    if (out.n.length == 0) {
        for (var i = 0; i < out.o.length; i++) {
            str += '<del>' + out.o[i] + oSpace[i] + '</del>';
        }
    } else {
        if (out.n[0].text == null) {
            for (n = 0; n < out.o.length && out.o[n].text == null; n++) {
                str += '<del>' + out.o[n] + oSpace[n] + '</del>';
            }
        }
        for (var i = 0; i < out.n.length; i++) {
            if (out.n[i].text == null) {
                str += '<ins>' + out.n[i] + nSpace[i] + '</ins>';
            } else {
                var pre = '';
                for (n = out.n[i].row + 1; n < out.o.length && out.o[n].text == null; n++) {
                    pre += '<del>' + out.o[n] + oSpace[n] + '</del>';
                }
                str += ' ' + out.n[i].text + nSpace[i] + pre;
            }
        }
    }
    return str;
}

function diff(o, n) {
    var ns = new Object();
    var os = new Object();
    for (var i = 0; i < n.length; i++) {
        if (ns[n[i]] == null)
            ns[n[i]] = { rows: new Array(), o: null };
            ns[n[i]].rows.push(i);
    }
    for (var i = 0; i < o.length; i++) {
        if (os[o[i]] == null)
            os[o[i]] = { rows: new Array(), n: null };
            os[o[i]].rows.push(i);
    }
    for (var i in ns) {
      if (ns[i].rows.length == 1 && typeof (os[i]) != "undefined" && os[i].rows.length == 1) {
          n[ns[i].rows[0]] = { text: n[ns[i].rows[0]], row: os[i].rows[0] };
          o[os[i].rows[0]] = { text: o[os[i].rows[0]], row: ns[i].rows[0] };
      }
    }
    for (var i = 0; i < n.length - 1; i++) {
        if (n[i].text != null && n[i + 1].text == null && n[i].row + 1 < o.length && o[n[i].row + 1].text == null && n[i + 1] == o[n[i].row + 1]) {
            n[i + 1] = { text: n[i + 1], row: n[i].row + 1 };
            o[n[i].row + 1] = { text: o[n[i].row + 1], row: i + 1 };
        }
    }
    for (var i = n.length - 1; i > 0; i--) {
        if (n[i].text != null && n[i - 1].text == null && n[i].row > 0 && o[n[i].row - 1].text == null && n[i - 1] == o[n[i].row - 1]) {
            n[i - 1] = { text: n[i - 1], row: n[i].row - 1 };
            o[n[i].row - 1] = { text: o[n[i].row - 1], row: i - 1 };
        }
    }
    return { o: o, n: n };
}

</script>

Thursday, December 18, 2014

Coder's blog, earthdate 20111223.7

If I can formulate a set of symbols to visually convey all the elements of programming, it can be turned into a puzzle game. Levels/solutions can be saved as code files, in the preferred unicde format. If the game becomes popular, it becomes a forum for discussing the future of coding wth full symbols.