Add beginning of support for <script type="text/paperscript">, inspired by Coffee Script.

This commit is contained in:
Jürg Lehni 2011-03-03 18:31:56 +00:00
parent 08970abd6d
commit 2c78a0a08b
4 changed files with 88 additions and 32 deletions

54
src/util/PaperScript.js Normal file
View file

@ -0,0 +1,54 @@
var PaperScript = new function() {
function compile(code) {
return code;
}
function run(code) {
with (paper) {
return eval(compile(code));
}
}
//#ifdef BROWSER
// Code borrowed from Coffee Script:
function load(url) {
var xhr = new (window.ActiveXObject || XMLHttpRequest)('Microsoft.XMLHTTP');
xhr.open('GET', url, true);
if ('overrideMimeType' in xhr) {
xhr.overrideMimeType('text/plain');
}
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
return run(xhr.responseText);
}
};
return xhr.send(null);
}
function runScripts() {
var scripts = document.getElementsByTagName('script');
for (var i = 0, l = scripts.length; i < l; i++) {
var script = scripts[i];
if (script.type === 'text/paperscript') {
if (script.src) {
load(script.src);
} else {
run(script.innerHTML);
}
}
}
return null;
}
if (window.addEventListener) {
addEventListener('DOMContentLoaded', runScripts, false);
} else {
attachEvent('onload', runScripts);
}
//#endif // BROWSER
return {
compile: compile,
run: run
};
};