|
12 | 12 | # All configuration values have a default; values that are commented out |
13 | 13 | # serve to show the default. |
14 | 14 |
|
15 | | -import m2r |
16 | 15 | import os |
17 | 16 | import re |
18 | | -import semver |
19 | 17 | import sys |
20 | 18 |
|
21 | | -from CPAC import __version__ |
22 | 19 | from dateutil import parser as dparser |
| 20 | +from CPAC import __version__ |
| 21 | +from CPAC.utils.monitoring import custom_logging |
23 | 22 | from github import Github |
24 | 23 | from github.GithubException import RateLimitExceededException, \ |
25 | 24 | UnknownObjectException |
| 25 | +import m2r |
| 26 | +import semver |
26 | 27 | from pybtex.plugin import register_plugin |
27 | 28 |
|
28 | 29 | sys.path.append(os.path.dirname(__file__)) |
@@ -144,11 +145,11 @@ def yaml_to_rst(path): |
144 | 145 | 'sphinx.ext.ifconfig', |
145 | 146 | 'sphinx.ext.intersphinx', |
146 | 147 | 'sphinx.ext.mathjax', |
| 148 | + 'sphinx.ext.napoleon', |
147 | 149 | 'sphinx.ext.viewcode', |
148 | 150 | 'sphinxcontrib.programoutput', |
149 | 151 | 'exec', |
150 | | - 'nbsphinx', |
151 | | - 'numpydoc'] |
| 152 | + 'nbsphinx'] |
152 | 153 |
|
153 | 154 | bibtex_bibfiles = [f'references/{bib}' for bib in os.listdir('references') if |
154 | 155 | bib.endswith('.bib')] |
@@ -571,14 +572,60 @@ def _unireplace(release_note, unireplace): |
571 | 572 | ) if len(gh_tags) >= 5 else "" |
572 | 573 |
|
573 | 574 |
|
574 | | -def setup(app): |
575 | | - from CPAC.utils.monitoring import custom_logging |
| 575 | +def autodoc_process_docstring(app, what, name, obj, options, lines) -> None: |
| 576 | + """Modify docstrings before parsing RST""" |
| 577 | + # pylint: disable=too-many-arguments,unused-argument |
| 578 | + initialize_factory() |
| 579 | + if what == "function": |
| 580 | + format_node_block_docstrings(lines) |
| 581 | + |
576 | 582 |
|
577 | | - # initilaize class to make factory functions available to Sphinx |
578 | | - ml = custom_logging.MockLogger('test', 'test.log', 0, '/tmp') |
| 583 | +def format_node_block_docstrings(lines: list) -> None: |
| 584 | + """Format Node Block docstring dictionaries as Python code blocks |
| 585 | +
|
| 586 | + Parameters |
| 587 | + ---------- |
| 588 | + lines : list |
| 589 | + modified in-place |
| 590 | + """ |
| 591 | + indent = 0 |
| 592 | + insert_at = None |
| 593 | + insert_herald = True |
| 594 | + nevermore = False |
| 595 | + for i, line in enumerate(lines): |
| 596 | + if re.match(r"\s*{['\"]outputs['\"]:", line): |
| 597 | + nevermore = True |
| 598 | + if nevermore and not line.strip(): |
| 599 | + indent = 0 |
| 600 | + if line.lstrip().startswith("Node Block:"): |
| 601 | + indent = 3 |
| 602 | + insert_at = i + 1 |
| 603 | + insert_herald = False |
| 604 | + else: |
| 605 | + if indent == 0 and re.match(r"\s*{['\"]name['\"]:", line): |
| 606 | + insert_at = i |
| 607 | + indent = 3 |
| 608 | + lines[i] = f'{" " * indent}{line}' |
| 609 | + if insert_at is not None: |
| 610 | + lines.insert(insert_at, '') |
| 611 | + lines.insert(insert_at, ".. code-block:: Python") |
| 612 | + if insert_herald: |
| 613 | + lines.insert(insert_at, '') |
| 614 | + lines.insert(insert_at, "Node Block:") |
| 615 | + |
| 616 | + |
| 617 | +def initialize_factory() -> None: |
| 618 | + """Initilaize class to make factory functions available to Sphinx""" |
| 619 | + mocklogger = custom_logging.MockLogger('test', 'test.log', 0, '/tmp') |
579 | 620 | for method in [ |
580 | 621 | method for method in |
581 | | - set(dir(ml)) - set(dir(custom_logging.MockLogger)) if |
| 622 | + set(dir(mocklogger)) - set(dir(custom_logging.MockLogger)) if |
582 | 623 | method not in ['name', 'handlers'] |
583 | 624 | ]: |
584 | | - setattr(custom_logging.MockLogger, method, getattr(ml, method)) |
| 625 | + setattr(custom_logging.MockLogger, method, |
| 626 | + getattr(mocklogger, method)) |
| 627 | + |
| 628 | + |
| 629 | +def setup(app) -> None: |
| 630 | + """Extend Sphinx""" |
| 631 | + app.connect('autodoc-process-docstring', autodoc_process_docstring) |
0 commit comments