-
Notifications
You must be signed in to change notification settings - Fork 8
/
jademagic.py
80 lines (65 loc) · 1.73 KB
/
jademagic.py
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
79
80
# -*- coding: utf-8 -*-
from __future__ import print_function
from IPython import get_ipython
from IPython.display import (
display,
Javascript,
HTML,
)
from IPython.core import magic_arguments
from IPython.core.magic import (
Magics,
magics_class,
cell_magic,
)
import pyjade
__version_info__ = (0, 1, 0)
__version__ = '.'.join(map(str, __version_info__))
@magics_class
class JadeMagics(Magics):
"""
Write and load HTML with Jade in the IPython Notebook.
Example:
%%jade
ul
li: some text!
"""
def __init__(self, shell):
super(JadeMagics, self).__init__(shell)
@cell_magic
@magic_arguments.magic_arguments()
@magic_arguments.argument(
"var_name",
default=None,
nargs="?",
help="""Name of local variable to set to parsed value"""
)
def jade(self, line, cell):
line = line.strip()
args = magic_arguments.parse_argstring(self.jade, line)
display(Javascript(
"""
require(
[
"notebook/js/codecell",
"codemirror/mode/jade/jade"
],
function(cc){
cc.CodeCell.options_default.highlight_modes.magic_jade = {
reg: ["^%%jade"]
}
}
);
"""))
try:
val = pyjade.simple_convert(cell)
except Exception as err:
print(err)
return
if args.var_name is not None:
get_ipython().user_ns[args.var_name] = val
else:
return HTML(val)
def load_ipython_extension(ip):
ip = get_ipython()
ip.register_magics(JadeMagics)