Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagecell
Path: blob/master/contrib/sphinx/sagecellext.py
447 views
1
"""
2
Sphinx extension to insert sagecell in sphinx docs.
3
4
Add the following lines to your layout.html file (e.g., in source/_templates)
5
6
######### BEGIN layout.html ###############
7
{% extends "!layout.html" %}
8
9
{%- block extrahead %}
10
<script type="text/javascript" src="http://aleph.sagemath.org/static/jquery.min.js"></script>
11
<script type="text/javascript" src="http://aleph.sagemath.org/static/embedded_sagecell.js"></script>
12
<style type="text/css">.sagecell_output th, .sagecell_output td {border: none;}</style>
13
{% endblock %}
14
15
############ END ########################
16
17
Add the directory of this file to the path in conf.py
18
19
import sys, os
20
sys.path.append(os.path.abspath('path/to/file'))
21
22
Add sagecellext to the list of extensions in conf.py.
23
24
extensions = ['sphinx.ext.mathjax', 'sphinx.ext.graphviz', 'sagecellext']
25
26
27
USAGE:
28
.. sagecell::
29
30
1+1
31
print("hello world")
32
33
34
"""
35
from docutils import nodes, utils
36
from docutils.nodes import Body, Element
37
from docutils.parsers.rst import directives
38
39
from sphinx.util.nodes import set_source_info
40
from sphinx.util.compat import Directive
41
42
class sagecell(Body, Element):
43
pass
44
45
46
class Sagecell(Directive):
47
48
has_content = True
49
required_arguments = 0
50
optional_arguments = 0
51
final_argument_whitespace = False
52
53
54
def run(self):
55
node = sagecell()
56
node['code'] = u'\n'.join(self.content)
57
return [node]
58
59
def html_sagecell(self, node):
60
"""
61
Convert block to the script here.
62
"""
63
from uuid import uuid4
64
65
template= """
66
<div id="sagecell-%(random)s"><script type="text/x-sage">%(code)s</script></div>
67
<script type="text/javascript">
68
$(function () {
69
sagecell.makeSagecell({inputLocation: '#sagecell-%(random)s'});
70
});
71
</script>
72
"""
73
self.body.append(template%{'random': uuid4(), 'code': node['code']})
74
raise nodes.SkipNode
75
76
77
def setup(app):
78
app.add_node(sagecell,
79
html=(html_sagecell, None))
80
81
app.add_directive('sagecell', Sagecell)
82
83