Skip to content

Commit c4c6603

Browse files
committed
Merge pull request github#546 from moreati/rst-doctest
Added doctest blocks to reStructuredText (with tests)
2 parents bdb490b + c5e6585 commit c4c6603

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

lib/github/commands/rest2html

+17-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ except:
4545
import codecs
4646

4747
from docutils import nodes
48-
from docutils.parsers.rst import roles
48+
from docutils.parsers.rst import directives, roles
49+
from docutils.parsers.rst.directives.body import CodeBlock
4950
from docutils.core import publish_parts
5051
from docutils.writers.html4css1 import Writer, HTMLTranslator
5152

@@ -63,6 +64,18 @@ SETTINGS = {
6364
'field_name_limit': 50,
6465
}
6566

67+
68+
class DoctestDirective(CodeBlock):
69+
"""Render Sphinx 'doctest:: [group]' blocks as 'code:: python'
70+
"""
71+
72+
def run(self):
73+
"""Discard any doctest group argument, render contents as python code
74+
"""
75+
self.arguments = ['python']
76+
return super(DoctestDirective, self).run()
77+
78+
6679
class GitHubHTMLTranslator(HTMLTranslator):
6780
# removes the <div class="document"> tag wrapped around docs
6881
# see also: http://bit.ly/1exfq2h (warning! sourceforge link.)
@@ -153,6 +166,9 @@ def main():
153166

154167
roles.register_canonical_role('kbd', kbd)
155168

169+
# Render source code in Sphinx doctest blocks
170+
directives.register_directive('doctest', DoctestDirective)
171+
156172
parts = publish_parts(text, writer=writer, settings_overrides=SETTINGS)
157173
if 'html_body' in parts:
158174
html = parts['html_body']

test/markups/README.rst

+13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ The UTF-8 quote character in this table used to cause python to go boom. Now doc
3131
Tabular Data, 5
3232
Made up ratings, 11
3333

34+
.. code::
35+
36+
A block of code
37+
38+
.. code:: python
39+
40+
python.code('hooray')
41+
42+
.. doctest:: ignored
43+
44+
>>> some_function()
45+
'result'
46+
3447
============== ==========================================================
3548
Travis http://travis-ci.org/tony/pullv
3649
Docs http://pullv.rtfd.org

test/markups/README.rst.html

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ <h2><a href="#id1">Header 2</a></h2>
1616
<li>Somé UTF-8°</li>
1717
</ol>
1818
<p>The UTF-8 quote character in this table used to cause python to go boom. Now docutils just silently ignores it.</p>
19+
<pre>
20+
A block of code
21+
</pre>
22+
<pre lang="python">
23+
python.code('hooray')
24+
</pre>
25+
<pre lang="python">
26+
&gt;&gt;&gt; some_function()
27+
'result'
28+
</pre>
1929
<table>
2030

2131

0 commit comments

Comments
 (0)