-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglowscriptmagic.py
More file actions
78 lines (68 loc) · 5.42 KB
/
glowscriptmagic.py
File metadata and controls
78 lines (68 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from IPython.display import display, Javascript, HTML
import json
def glowscript(line, cell):
lst = line.lower().split()
if (len(lst) == 0) or (lst[-1] in ['vpython','rapydscript','coffeescript','javascript']) :
display(HTML("""<div id="glowscript" class="glowscript"></div>"""))
lang = lst[-1] if len(lst) > 0 else ''
display(Javascript("""
'use strict';
if (window.location.protocol === 'https:') {
require(['https://cdnjs.cloudflare.com/ajax/libs/acorn/3.2.0/acorn.min.js','https://dl.dropboxusercontemnt.com/u/5095342/glowscript/lib/jquery/2.1/jquery-ui.custom.min.js','https://dl.dropboxusercontent.com/u/5095342/glowscript/package/compiler.2.1.min.js','https://dl.dropboxusercontent.com/u/5095342/glowscript/package/symbols.2.1.min.js','https://dl.dropboxusercontent.com/u/5095342/glowscript/package/RSrun.2.1.min.js','https://dl.dropboxusercontent.com/u/5095342/glowscript/package/RScompiler.2.1.min.js','https://dl.dropboxusercontent.com/u/5095342/glowscript/package/glow.2.1.min.js'], function(acorn) {
window.acorn = acorn;
var cell_content = """+json.dumps(cell)+""";
var embedScript = window.glowscript_compile(cell_content, {lang:'"""+lang+"""'});
embedScript = "require(['https://dl.dropboxusercontent.com/u/5095342/glowscript/lib/jquery/2.1/jquery-ui.custom.min.js','https://dl.dropboxusercontent.com/u/5095342/glowscript/package/glow.2.1.min.js'], function() {" + embedScript + ";$(function(){ window.__context = { glowscript_container: $('#glowscript').removeAttr('id') }; main() });})";
embedScript = embedScript.replace("</", "<\\/"); // escape anything that could be a close script tag... hopefully this sequence only occurs in strings!
eval(embedScript);
//console.log(embedScript);
})
} else if (window.location.host === 'nbviewer.ipython.org') {
require(['http://cdnjs.cloudflare.com/ajax/libs/acorn/3.2.0/acorn.min.js','http://www.glowscript.org/lib/jquery/2.1/jquery.min.js','http://www.glowscript.org/lib/jquery/2.1/jquery-ui.custom.min.js','http://www.glowscript.org/package/compiler.2.1.min.js','http://www.glowscript.org/package/symbols.2.1.min.js','http://www.glowscript.org/package/RSrun.2.1.min.js','http://www.glowscript.org/package/RScompiler.2.1.min.js','http://www.glowscript.org/package/glow.2.1.min.js'], function(acorn) {
window.acorn = acorn;
var cell_content = """+json.dumps(cell)+""";
var embedScript = window.glowscript_compile(cell_content, {lang:'"""+lang+"""'});
embedScript = "require(['http://www.glowscript.org/lib/jquery/2.1/jquery-ui.custom.min.js','http://www.glowscript.org/package/glow.2.1.min.js'], function() {" + embedScript + ";$(function(){ window.__context = { glowscript_container: $('#glowscript').removeAttr('id') }; main() });})";
embedScript = embedScript.replace("</", "<\\/"); // escape anything that could be a close script tag... hopefully this sequence only occurs in strings!
eval(embedScript);
//console.log(embedScript);
})
} else {
require(['http://cdnjs.cloudflare.com/ajax/libs/acorn/3.2.0/acorn.min.js','http://www.glowscript.org/lib/jquery/2.1/jquery-ui.custom.min.js','http://www.glowscript.org/package/compiler.2.1.min.js','http://www.glowscript.org/package/symbols.2.1.min.js','http://www.glowscript.org/package/RSrun.2.1.min.js','http://www.glowscript.org/package/RScompiler.2.1.min.js','http://www.glowscript.org/package/glow.2.1.min.js'], function(acorn) {
window.acorn = acorn;
var cell_content = """+json.dumps(cell)+""";
var embedScript = window.glowscript_compile(cell_content, {lang:'"""+lang+"""'});
embedScript = "require(['http://www.glowscript.org/lib/jquery/2.1/jquery-ui.custom.min.js','http://www.glowscript.org/package/glow.2.1.min.js'], function() {" + embedScript + ";$(function(){ window.__context = { glowscript_container: $('#glowscript').removeAttr('id') }; main() });})";
embedScript = embedScript.replace("</", "<\\/"); // escape anything that could be a close script tag... hopefully this sequence only occurs in strings!
eval(embedScript);
//console.log(embedScript);
})
}
""" ))
def GlowScript(line, cell):
glowscript(line, cell)
def vpython(line, cell):
glowscript("vpython", cell)
def VPython(line, cell):
glowscript("vpython", cell)
def rapydscript(line, cell):
glowscript("rapydscript", cell)
def RapydScript(line, cell):
glowscript("rapydscript", cell)
def coffeescript(line, cell):
glowscript("coffeescript", cell)
def CoffeeScript(line, cell):
glowscript("coffeescript", cell)
def load_ipython_extension(ipython):
"""This function is called when the extension is loaded.
It accepts an IPython InteractiveShell instance.
We can register the magic with the `register_magic_function`
method of the shell instance."""
ipython.register_magic_function(glowscript, 'cell')
ipython.register_magic_function(vpython, 'cell')
ipython.register_magic_function(rapydscript, 'cell')
ipython.register_magic_function(coffeescript, 'cell')
ipython.register_magic_function(GlowScript, 'cell')
ipython.register_magic_function(VPython, 'cell')
ipython.register_magic_function(RapydScript, 'cell')
ipython.register_magic_function(CoffeeScript, 'cell')