Skip to content

Commit

Permalink
Merge 227fbf9 into 34b1090
Browse files Browse the repository at this point in the history
  • Loading branch information
hootnot committed Mar 19, 2016
2 parents 34b1090 + 227fbf9 commit 22d2206
Show file tree
Hide file tree
Showing 8 changed files with 461 additions and 202 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -7,6 +7,7 @@ python:
- '3.5'
install:
- pip install coveralls
- pip install nose nose-parameterized
script:
- coverage run --source=postcodepy setup.py test
after_success:
Expand Down
35 changes: 35 additions & 0 deletions docs/conf.py
Expand Up @@ -358,6 +358,41 @@
#epub_use_index = True
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'

# custom directive (stackoverflow.com: 7250659)
from os.path import basename

try:
from StringIO import StringIO
except ImportError:
from io import StringIO

from sphinx.util.compat import Directive
from docutils import nodes, statemachine

class ExecDirective(Directive):
"""Execute the specified python code and insert the output into the document"""
has_content = True

def run(self):
oldStdout, sys.stdout = sys.stdout, StringIO()

tab_width = self.options.get('tab-width', self.state.document.settings.tab_width)
source = self.state_machine.input_lines.source(self.lineno - self.state_machine.input_offset - 1)

try:
exec('\n'.join(self.content))
text = sys.stdout.getvalue()
lines = statemachine.string2lines(text, tab_width, convert_whitespace=True)
self.state_machine.insert_input(lines, source)
return []
except Exception:
return [nodes.error(None, nodes.paragraph(text = "Unable to execute python code at %s:%d:" % (basename(source), self.lineno)), nodes.paragraph(text = str(sys.exc_info()[1])))]
finally:
sys.stdout = oldStdout

def setup(app):
app.add_directive('exec', ExecDirective)

if not on_rtd: # only import and set the theme if we're building docs locally
import sphinx_rtd_theme
html_theme = 'sphinx_rtd_theme'
Expand Down

0 comments on commit 22d2206

Please sign in to comment.