1
0
mirror of https://github.com/myhdl/myhdl.git synced 2025-01-24 21:52:56 +08:00
myhdl/doc/source/myhdldoctools.py
2016-05-11 21:02:34 +02:00

36 lines
1.1 KiB
Python

import subprocess
from docutils import nodes
from sphinx.util.compat import Directive
from sphinx.directives.code import LiteralInclude
example_dir = '/../../example/manual/'
class IncludeExample(LiteralInclude):
def run(self):
self.arguments[0] = '{}/{}'.format(example_dir, self.arguments[0])
return super(IncludeExample, self).run()
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]
out = subprocess.check_output(['python3', '-u', prog], cwd=wd,
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)