Skip to content

Commit 9e58aad

Browse files
committed
[no ci] docs: replace code fences with RST codeblocks.
If no own docstrings are provided for an overwritten method in a subclass, the docstrings from the parent class are used. As we inherit Keras objects, we sometimes get Keras docstrings, which do not follow the conventions. Most importantly, they use fenced code blocks instead of RST. This change replaces those when building the documentation, making the docstrings somewhat better to read.
1 parent 1c2ba80 commit 9e58aad

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

docsrc/source/conf.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,36 @@
157157
current: GitRef = data["current"]
158158
except LoadError:
159159
print("sphinx_polyversion could not load. Building single version")
160+
161+
162+
def docstring(app, what, name, obj, options, lines):
163+
"""Adapt autodoc docstring.
164+
165+
Inherited Keras docstrings do not follow the numpy docstring format.
166+
Most noticably, they use Markdown code fences. To improve the situation
167+
somewhat, this functions aims to convert fenced code blocks to RST
168+
code blocks.
169+
"""
170+
updated_lines = []
171+
prefix = ""
172+
for line in lines:
173+
if line.count("```") == 1:
174+
if prefix == "":
175+
prefix = " "
176+
updated_lines[-1] = updated_lines[-1] + "::"
177+
else:
178+
prefix = ""
179+
updated_lines.append("\n")
180+
else:
181+
updated_lines.append(prefix + line)
182+
if prefix != "":
183+
raise ValueError(
184+
f"Uneven number of code fences in docstring:\nwhat='{what}', name='{name}'.\n" + "\n".join(lines)
185+
)
186+
# overwrite lines list
187+
lines.clear()
188+
lines += updated_lines
189+
190+
191+
def setup(app):
192+
app.connect("autodoc-process-docstring", docstring)

0 commit comments

Comments
 (0)