2016-03-15 17:34:54 -04:00
|
|
|
import subprocess
|
|
|
|
|
|
|
|
from docutils import nodes
|
|
|
|
|
2018-04-01 10:29:07 +02:00
|
|
|
# deprecated
|
|
|
|
# from sphinx.util.compat import Directive
|
|
|
|
from docutils.parsers.rst import Directive
|
2016-03-15 17:34:54 -04:00
|
|
|
from sphinx.directives.code import LiteralInclude
|
|
|
|
|
|
|
|
example_dir = '/../../example/manual/'
|
|
|
|
|
2024-12-21 17:21:11 +01:00
|
|
|
|
2016-03-15 17:34:54 -04:00
|
|
|
class IncludeExample(LiteralInclude):
|
2024-12-21 17:21:11 +01:00
|
|
|
|
2016-03-15 17:34:54 -04:00
|
|
|
def run(self):
|
|
|
|
self.arguments[0] = '{}/{}'.format(example_dir, self.arguments[0])
|
|
|
|
return super(IncludeExample, self).run()
|
|
|
|
|
2024-12-21 17:21:11 +01:00
|
|
|
|
2016-03-15 17:34:54 -04:00
|
|
|
class RunExample(Directive):
|
|
|
|
has_content = False
|
|
|
|
required_arguments = 1
|
|
|
|
final_argument_whitespace = True
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
document = self.state.document
|
|
|
|
env = document.settings.env
|
|
|
|
_ , wd = env.relfn2path(example_dir)
|
|
|
|
prog = self.arguments[0]
|
2024-12-21 17:21:11 +01:00
|
|
|
out = subprocess.check_output(['python', '-u', prog], cwd=wd,
|
2016-03-15 17:34:54 -04:00
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
universal_newlines=True)
|
|
|
|
out = '$ python {}\n{}'.format(prog, out)
|
|
|
|
ret = [nodes.literal_block(out, out)]
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def setup(app):
|
|
|
|
app.add_directive('include-example', IncludeExample)
|
|
|
|
app.add_directive('run-example', RunExample)
|