-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial.py
98 lines (72 loc) · 2.72 KB
/
tutorial.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from docutils import nodes
from docutils.parsers.rst import Directive
from docutils.parsers.rst.directives.admonitions import BaseAdmonition
from sphinx.util.compat import make_admonition
from sphinx.locale import _
def setup(app):
app.add_stylesheet('tutorial.css')
app.add_javascript('jquery.fitvids.js')
app.add_javascript('tutorial.js')
app.add_node(exercise,
html=(visit_exercise, depart_exercise))
app.add_node(exercisehint,
html=(visit_hint, depart_hint))
app.add_node(youtube,
html=(visit_youtube, depart_youtube))
app.add_directive('exercise', ExerciseDirective)
app.add_directive('exercisehint', ExerciseHintDirective)
app.add_directive('youtube', YouTubeDirective)
return {'version': '0.1'} # identifies the version of our extension
## exercise directive
class exercise(nodes.Admonition, nodes.Element):
pass
def visit_exercise(self, node):
self.body.append(self.starttag(
node, 'div', CLASS=('admonition exercise')))
node.insert(0, nodes.title('exercise', 'Exercise'))
self.set_first_last(node)
def depart_exercise(self, node):
self.depart_admonition(node)
class ExerciseDirective(BaseAdmonition):
node_class = exercise
## exercisehint directive
class exercisehint(nodes.Body, nodes.TextElement):
pass
def visit_hint(self, node):
self.body.append(self.starttag(node, 'div', CLASS="exercise-hint") + \
'<button>Hint</button><div class="hidden hint-body">')
def depart_hint(self, node):
self.body.append('</div></div>')
class ExerciseHintDirective(Directive):
has_content = True
final_argument_whitespace = True
option_spec = {}
def run(self):
self.assert_has_content()
text = '\n'.join(self.content)
eh_node = exercisehint(text)
self.add_name(eh_node)
self.state.nested_parse(self.content, self.content_offset, eh_node)
return [eh_node]
## youtube directive
# Inspired by this extension:
# https://github.com/shomah4a/sphinxcontrib.youtube/blob/master/sphinxcontrib/youtube/youtube.py
class youtube(nodes.General, nodes.TextElement):
pass
def visit_youtube(self, node):
url = 'https://www.youtube.com/embed/' + node.video_id
self.body.append('<iframe src="%s" frameborder="0" allowfullscreen>' % url)
def depart_youtube(self, node):
self.body.append('</iframe>')
class YouTubeDirective(Directive):
has_content = False
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = False
option_spec = {}
def run(self):
if not self.arguments:
return []
youtube_node = youtube()
youtube_node.video_id = self.arguments[0]
return [youtube_node]