sku-pg
Version:
Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!
118 lines (103 loc) • 4.37 kB
JavaScript
$(document).ready(function () {
var output = $('#edoutput');
var outf = function (text) {
output.text(output.text() + text);
};
var jsoutf = function (text) {
window.js_output.setValue(text);
}
var keymap = {
"Ctrl-Enter" : function (editor) {
Sk.configure({output: outf, read: builtinRead});
Sk.canvas = "mycanvas";
if (editor.getValue().indexOf('turtle') > -1 ) {
$('#mycanvas').show()
}
Sk.pre = "edoutput";
(Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'mycanvas';
try {
Sk.misceval.asyncToPromise(function() {
return Sk.importMainWithBody("<stdin>",false,editor.getValue(),true);
});
} catch(e) {
outf(e.toString() + "\n")
}
},
"Shift-Enter": function (editor) {
Sk.configure({output: outf, read: builtinRead});
Sk.canvas = "mycanvas";
Sk.pre = "edoutput";
if (editor.getValue().indexOf('turtle') > -1 ) {
$('#mycanvas').show()
}
try {
Sk.misceval.asyncToPromise(function() {
return Sk.importMainWithBody("<stdin>",false,editor.getValue(),true);
});
} catch(e) {
outf(e.toString() + "\n")
}
}
}
var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
parserfile: ["parsepython.js"],
autofocus: true,
theme: "solarized dark",
//path: "static/env/codemirror/js/",
styleActiveLine: true,
lineNumbers: true,
textWrapping: false,
indentUnit: 4,
height: "160px",
fontSize: "9pt",
autoMatchParens: true,
extraKeys: keymap,
parserConfig: {'pythonVersion': 2, 'strictErrors': true}
});
var js_output = CodeMirror.fromTextArea(document.getElementById('codeoutput'), {
parserfile: ["parsejavascript.js"],
autofocus: false,
theme: "solarized dark",
//path: "static/env/codemirror/js/",
lineNumbers: true,
textWrapping: false,
indentUnit: 4,
height: "160px",
fontSize: "9pt",
autoMatchParens: true,
extraKeys: keymap,
});
window.code_editor = editor;
window.js_output = js_output;
window.jsoutf = jsoutf;
window.outf = outf;
window.builtinRead = builtinRead;
$("#skulpt_run").click(function (e) { keymap["Ctrl-Enter"](editor)} );
$("#toggledocs").click(function (e) {
$("#quickdocs").toggle();
});
var exampleCode = function (id, text) {
$(id).click(function (e) {
editor.setValue(text);
editor.focus(); // so that F5 works, hmm
});
};
exampleCode('#codeexample1', "print \"Hello, World!\" # natch");
exampleCode('#codeexample2', "for i in range(5):\n print i\n");
exampleCode('#codeexample3', "print [x*x for x in range(20) if x % 2 == 0]");
exampleCode('#codeexample4', "print 45**123");
exampleCode('#codeexample5', "print \"%s:%r:%d:%x\\n%#-+37.34o\" % (\n \"dog\",\n \"cat\",\n 23456,\n 999999999999L,\n 0123456702345670123456701234567L)");
exampleCode('#codeexample6', "def genr(n):\n i = 0\n while i < n:\n yield i\n i += 1\n\nprint list(genr(12))\n");
exampleCode('#codeexample7', "# obscure C3 MRO example from Python docs\nclass O(object): pass\nclass A(O): pass\nclass B(O): pass\nclass C(O): pass\nclass D(O): pass\nclass E(O): pass\nclass K1(A,B,C): pass\nclass K2(D,B,E): pass\nclass K3(D,A): pass\nclass Z(K1,K2,K3): pass\nprint Z.__mro__\n");
exampleCode('#codeexample8', "import document\n\npre = document.getElementById('edoutput')\npre.innerHTML = '''\n<h1> Skulpt can also access DOM! </h1>\n''' \n");
$('#clearoutput').click(function (e) {
$('#edoutput').text('');
$('#mycanvas').hide();
});
function builtinRead(x) {
if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
throw "File not found: '" + x + "'";
return Sk.builtinFiles["files"][x];
}
editor.focus();
});