Skip to content

Instantly share code, notes, and snippets.

@grapswiz
Created May 28, 2011 07:05
Show Gist options
  • Save grapswiz/996681 to your computer and use it in GitHub Desktop.
Save grapswiz/996681 to your computer and use it in GitHub Desktop.
function ajaxRequest(url, callback) {
var req = new XMLHttpRequest();
req.open('GET', url, true);
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
callback(req.responseText);
}
}
};
req.send(null);
}
function assert(message, expr) {
if(!expr) {
throw new Error(message);
}
assert.count++;
return true;
}
assert.count = 0;
var kamisori = {};
kamisori = {
format: function(s) {
var text = '';
if (s.match(/=== ((\w|\W)+) ===/)) {
text += '<h1>' + RegExp.$1 + '</h1>';
} else if (s.match(/== ((\w|\W)+) ==/)) {
text += '<h2>' + RegExp.$1 + '</h2>';
} else if (s.match(/= ((\w|\W)+) =/)) {
text += '<h3>' + RegExp.$1 + '</h3>';
} else if (s.match(/\* ((\w|\W)+)/)) {
text += '<ul>';
text += '<li>' + RegExp.$1 + '</li>';
text += '</ul>';
} else {
text += s;
}
return text;
},
output: function(path) {
ajaxRequest(path, function(input) {
var text = '';
input = input.split('\n');
for (var i = 0; i < input.length; i++) {
if (input[i] != '') {
text += kamisori.format(input[i]);
}
}
var output = document.getElementById('out');
output.innerHTML = text;
});
}
};
kamisori.output("in.txt");
try {
assert('=== a === surrounded by h1', kamisori.format('=== a ===') == '<h1>a</h1>');
assert('=== あ === surrounded by h1', kamisori.format('=== あ ===') == '<h1>あ</h1>');
assert('=== a a === surrounded by h1', kamisori.format('=== a a ===') == '<h1>a a</h1>');
assert('=== あ あ === surrounded by h1', kamisori.format('=== あ あ ===') == '<h1>あ あ</h1>')
assert('== a == surrounded by h2', kamisori.format('== a ==') == '<h2>a</h2>');
assert('== あ == surrounded by h2', kamisori.format('== あ ==') == '<h2>あ</h2>');
assert('== a a == surrounded by h2', kamisori.format('== a a ==') == '<h2>a a</h2>');
assert('== あ あ == surrounded by h2', kamisori.format('== あ あ ==') == '<h2>あ あ</h2>')
assert('= a = surrounded by h3', kamisori.format('= a =') == '<h3>a</h3>');
assert('= あ = surrounded by h3', kamisori.format('= あ =') == '<h3>あ</h3>');
assert('= a a = surrounded by h3', kamisori.format('= a a =') == '<h3>a a</h3>');
assert('= あ あ = surrounded by h3', kamisori.format('= あ あ =') == '<h3>あ あ</h3>')
assert('* a surrounded by ul and li', kamisori.format('* a') == '<ul><li>a</li></ul>');
assert('* あ surrounded by ul and li', kamisori.format('* あ') == '<ul><li>あ</li></ul>');
assert('* a a surrounded by ul and li', kamisori.format('* a a') == '<ul><li>a a</li></ul>');
assert('* あ あ surrounded by ul and li', kamisori.format('* あ あ') == '<ul><li>あ あ</li></ul>');
console.log(assert.count + ' tests OK');
} catch(e) {
console.log('Test failed: ' + e.message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment