forked from anandology/python-practice-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem.py
65 lines (48 loc) · 1.64 KB
/
problem.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
"""Sphinx extension for adding problem directive.
"""
from sphinx.util.compat import Directive
from docutils import nodes
import sys
class problem(nodes.Element):
pass
class example(nodes.Element):
pass
class ProblemDirective(Directive):
has_content = True
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = True
option_spec = {}
def run(self):
node = self._create_node()
node.document = self.state.document
node.line = self.lineno
inodes, messages = self.state.inline_text(self.arguments[0],
self.lineno)
node.extend(inodes)
self.state.nested_parse(self.content, self.content_offset, node)
return [node] + messages
def _create_node(self):
return problem()
class ExampleDirective(ProblemDirective):
def _create_node(self):
return example()
def process_problem_nodes(app, doctree, docname):
def process(nodecls, label):
index = 1
for node in doctree.traverse(nodecls):
out = [nodes.strong(None, "%s %d: " % (label, index))] + node.children
p = nodes.paragraph()
p.document = node.document
p.line = node.line
p.children = out
node.replace_self(p)
index += 1
process(problem, "Problem")
process(example, "Example")
def setup(app):
app.add_node(problem)
app.add_directive('problem', ProblemDirective)
app.add_node(example)
app.add_directive('example', ExampleDirective)
app.connect('doctree-resolved', process_problem_nodes)