-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathsagecell.py
53 lines (37 loc) · 1.33 KB
/
sagecell.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
"""
MoinMoin - Sage Cell Parser
@copyright: 2012 Jason Grout <[email protected]>
@license: Modified BSD
Usage::
{{{#!sagecell
1+1
}}}
Installation
Put this file in ``data/plugin/parser/``.
You must also something like these lines in your wikiconfig::
html_head = '<script type="text/javascript" src="https://sagecell.sagemath.org/static/embedded_sagecell.js"></script>'
html_head += '<style type="text/css">.sagecell_output th, .sagecell_output td {border: none;}</style>'
"""
from MoinMoin.parser._ParserBase import ParserBase
from uuid import uuid4
Dependencies = ['user']
template="""
<div id="sagecell-%(random)s"><script type="text/x-sage">%(code)s</script></div>
<script type="text/javascript">
sagecell.makeSagecell({inputLocation: '#sagecell-%(random)s'});
</script>
"""
class Parser(ParserBase):
parsername = "sagecell"
Dependencies = []
def __init__(self, code, request, **kw):
self.code = self.sanitize(code)
self.request = request
def sanitize(self, code):
"""
Sanitize the code, for example, escape any instances of </script>
"""
sanitized=code.replace("</script>", "<\/script>")
return sanitized
def format(self, formatter):
self.request.write(formatter.rawHTML(template%{'random': uuid4(), 'code': self.code}))