RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
sphinx-pyexec.py
Go to the documentation of this file.
1try:
2 from StringIO import StringIO
3except ImportError:
4 from io import StringIO
5
6import sys
7from docutils.parsers.rst import Directive
8from docutils import nodes
9from sphinx.util import nested_parse_with_titles
10from docutils.statemachine import StringList
11
12
14 has_content = True
15 required_arguments = 0
16
17 def execute_code(cls, code):
18 codeOut = StringIO()
19 codeErr = StringIO()
20
21 sys.stdout = codeOut
22 sys.stderr = codeErr
23
24 exec(code)
25
26 sys.stdout = sys.__stdout__
27 sys.stderr = sys.__stderr__
28
29 results = list()
30 results.append(codeOut.getvalue())
31 results.append(codeErr.getvalue())
32 results = ''.join(results)
33
34 return results
35
36 def run(self):
37 self.assert_has_content()
38
39 code = '\n'.join(self.content)
40 code_results = self.execute_code(code)
41
42 sl = StringList(code_results.replace("\r", "").split("\n"))
43
44 node = nodes.paragraph()
45 nested_parse_with_titles(self.state, sl, node)
46
47 output = []
48 output.append(node)
49 return output
50
51
52def setup(app):
53 app.add_directive('exec', ExecDirective)