| 1 |
"""Include single scripts with doc string, code, and image |
| 2 |
|
| 3 |
Use case |
| 4 |
-------- |
| 5 |
There is an "examples" directory in the root of a repository, |
| 6 |
e.g. 'include_doc_code_img_path = "../examples"' in conf.py |
| 7 |
(default). An example is a file ("an_example.py") that consists |
| 8 |
of a doc string at the beginning of the file, the example code, |
| 9 |
and, optionally, an image file (png, jpg) ("an_example.png"). |
| 10 |
|
| 11 |
|
| 12 |
Configuration |
| 13 |
------------- |
| 14 |
In conf.py, set the parameter |
| 15 |
|
| 16 |
fancy_include_path = "../examples" |
| 17 |
|
| 18 |
to wherever the included files reside. |
| 19 |
|
| 20 |
|
| 21 |
Usage |
| 22 |
----- |
| 23 |
The directive |
| 24 |
|
| 25 |
.. fancy_include:: an_example.py |
| 26 |
|
| 27 |
will display the doc string formatted with the first line as a |
| 28 |
heading, a code block with line numbers, and the image file. |
| 29 |
""" |
| 30 |
import pathlib |
| 31 |
|
| 32 |
from docutils.statemachine import ViewList |
| 33 |
from docutils.parsers.rst import Directive |
| 34 |
from sphinx.util.nodes import nested_parse_with_titles |
| 35 |
from docutils import nodes |
| 36 |
|
| 37 |
|
| 38 |
class IncludeDirective(Directive): |
| 39 |
required_arguments = 1 |
| 40 |
optional_arguments = 0 |
| 41 |
|
| 42 |
def run(self): |
| 43 |
path = self.state.document.settings.env.config.fancy_include_path |
| 44 |
path = pathlib.Path(path) |
| 45 |
full_path = path / self.arguments[0] |
| 46 |
|
| 47 |
text = full_path.read_text() |
| 48 |
|
| 49 |
# add reference |
| 50 |
name = full_path.stem |
| 51 |
rst = [".. _example_{}:".format(name), |
| 52 |
"", |
| 53 |
] |
| 54 |
|
| 55 |
# add docstring |
| 56 |
source = text.split('"""') |
| 57 |
doc = source[1].split("\n") |
| 58 |
doc.insert(1, "~" * len(doc[0])) # make title heading |
| 59 |
|
| 60 |
code = source[2].split("\n") |
| 61 |
|
| 62 |
for line in doc: |
| 63 |
rst.append(line) |
| 64 |
|
| 65 |
# image |
| 66 |
for ext in [".png", ".jpg"]: |
| 67 |
image_path = full_path.with_suffix(ext) |
| 68 |
if image_path.exists(): |
| 69 |
break |
| 70 |
else: |
| 71 |
image_path = "" |
| 72 |
if image_path: |
| 73 |
rst.append(".. figure:: {}".format(image_path.as_posix())) |
| 74 |
rst.append("") |
| 75 |
|
| 76 |
# download file |
| 77 |
rst.append(":download:`{} <{}>`".format( |
| 78 |
full_path.name, full_path.as_posix())) |
| 79 |
|
| 80 |
# code |
| 81 |
rst.append("") |
| 82 |
rst.append(".. code-block:: python") |
| 83 |
rst.append(" :linenos:") |
| 84 |
rst.append("") |
| 85 |
for line in code: |
| 86 |
rst.append(" {}".format(line)) |
| 87 |
rst.append("") |
| 88 |
|
| 89 |
vl = ViewList(rst, "fakefile.rst") |
| 90 |
# Create a node. |
| 91 |
node = nodes.section() |
| 92 |
node.document = self.state.document |
| 93 |
# Parse the rst. |
| 94 |
nested_parse_with_titles(self.state, vl, node) |
| 95 |
return node.children |
| 96 |
|
| 97 |
|
| 98 |
def setup(app): |
| 99 |
app.add_config_value('fancy_include_path', "../examples", 'html') |
| 100 |
|
| 101 |
app.add_directive('fancy_include', IncludeDirective) |
| 102 |
|
| 103 |
return {'version': '0.1'} # identifies the version of our extension |