Skip to content

Commit

Permalink
Merge pull request #158 from jck/docs-update
Browse files Browse the repository at this point in the history
docs: custom directive for including/running examples
  • Loading branch information
jandecaluwe committed Mar 20, 2016
2 parents 1a856f1 + 038310d commit ff844ae
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
4 changes: 3 additions & 1 deletion doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath('../..'))
sys.path.append(os.path.abspath('.'))
import myhdl

# -- General configuration -----------------------------------------------------
Expand All @@ -28,7 +29,8 @@
# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = ['sphinx.ext.intersphinx',
'sphinx.ext.doctest']
'sphinx.ext.doctest',
'myhdldoctools']

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
Expand Down
27 changes: 4 additions & 23 deletions doc/source/manual/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,13 @@ A basic MyHDL simulation
We will introduce MyHDL with a classic ``Hello World`` style example. All
example code can be found in the distribution directory under
:file:`example/manual/`. Here are the contents of a MyHDL simulation script
called :file:`hello1.py`::
called :file:`hello1.py`:

from myhdl import Signal, delay, always, now, Simulation

def HelloWorld():
.. include-example:: hello1.py

interval = delay(10)

@always(interval)
def sayHello():
print "%s Hello World!" % now()

return sayHello
When we run this simulation, we get the following output:


inst = HelloWorld()
sim = Simulation(inst)
sim.run(30)

When we run this simulation, we get the following output::

% python hello1.py
10 Hello World!
20 Hello World!
30 Hello World!
_SuspendSimulation: Simulated 30 timesteps
.. run-example:: hello1.py

The first line of the script imports a number of objects from the ``myhdl``
package. In Python we can only use identifiers that are literally defined in the
Expand Down
35 changes: 35 additions & 0 deletions doc/source/myhdldoctools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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(['python', '-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)
2 changes: 1 addition & 1 deletion example/manual/hello1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def HelloWorld():

@always(interval)
def sayHello():
print "%s Hello World!" % now()
print("%s Hello World!" % now())

return sayHello

Expand Down

0 comments on commit ff844ae

Please sign in to comment.