Skip to content

Commit 9c9d674

Browse files
committed
docs: strip doctest blocks from the API reference + cleaner rendering
Beautification of the generated Sphinx site: - autodoc-process-docstring hook removes the doctest (>>> ...) example blocks from the rendered pages (and any now-dangling 'Example:' lead-in). The examples stay in the source docstrings (still runnable / readable in the code) -- they just no longer clutter the HTML API reference as interactive-session dumps. - add_module_names = False: members render as 'expr()' instead of 'aima.utils.expr()' -- shorter, cleaner headings. - minor napoleon tweaks (no rtype block, ivar style). Build stays at 0 warnings.
1 parent 39f6176 commit 9c9d674

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

docs/conf.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
import os
9+
import re
910
import sys
1011

1112
# make the library importable for autodoc
@@ -56,6 +57,54 @@
5657
templates_path = ["_templates"]
5758
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
5859

60+
# show members as ``expr()`` rather than ``aima.utils.expr()`` -- cleaner headings
61+
add_module_names = False
62+
63+
# napoleon: render Google/NumPy-style sections a bit more compactly
64+
napoleon_use_rtype = False
65+
napoleon_use_ivar = True
66+
67+
68+
def _strip_doctests(app, what, name, obj, options, lines):
69+
"""Drop doctest (``>>> ...``) example blocks from the rendered API docs.
70+
71+
The examples stay in the source docstrings (useful when reading the code or
72+
running them as doctests); they are merely omitted from the HTML so the API
73+
reference reads as clean prose instead of interactive-session transcripts.
74+
"""
75+
cleaned, in_block = [], False
76+
for line in lines:
77+
stripped = line.strip()
78+
if stripped.startswith(">>>"):
79+
if not in_block:
80+
# also drop a now-dangling "Example:"/"Examples:" header + blanks
81+
while cleaned and (not cleaned[-1].strip()
82+
or cleaned[-1].strip().rstrip(":").lower()
83+
in ("example", "examples", "for example")):
84+
cleaned.pop()
85+
# also drop a trailing "... Example:" sentence on the prose line
86+
if cleaned:
87+
cleaned[-1] = re.sub(
88+
r"\.\s+(for example|examples?|e\.g\.)\s*:?\s*$", ".",
89+
cleaned[-1], flags=re.I)
90+
in_block = True
91+
continue
92+
if in_block:
93+
if not stripped: # a blank line ends the doctest block
94+
in_block = False
95+
cleaned.append(line)
96+
# otherwise: a ``...`` continuation or an expected-output line -> drop
97+
continue
98+
cleaned.append(line)
99+
while cleaned and not cleaned[-1].strip():
100+
cleaned.pop()
101+
lines[:] = cleaned
102+
103+
104+
def setup(app):
105+
app.connect("autodoc-process-docstring", _strip_doctests)
106+
107+
59108
# -- HTML output -------------------------------------------------------------
60109

61110
html_theme = "sphinx_rtd_theme"

0 commit comments

Comments
 (0)