Skip to content

Commit 887eb3b

Browse files
committed
Apply a Sphinx transform to make the core module docs look better
1 parent 9582cc5 commit 887eb3b

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

conf.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717
#
1818
# SPDX-License-Identifier: MIT
1919

20-
import json
2120
import logging
2221
import os
2322
import subprocess
2423
import sys
2524
import urllib.parse
2625

2726
import recommonmark
27+
from sphinx.transforms import SphinxTransform
28+
from docutils import nodes
29+
from sphinx import addnodes
2830

2931
# If extensions (or modules to document with autodoc) are in another directory,
3032
# add these directories to sys.path here. If the directory is relative to the
@@ -84,6 +86,7 @@
8486
autoapi_add_toctree_entry = False
8587
autoapi_options = ['members', 'undoc-members', 'private-members', 'show-inheritance', 'special-members', 'show-module-summary']
8688
autoapi_template_dir = 'docs/autoapi/templates'
89+
autoapi_python_class_content = "both"
8790
autoapi_python_use_implicit_namespaces = True
8891
autoapi_root = "shared-bindings"
8992

@@ -423,7 +426,38 @@ def generate_redirects(app):
423426
with open(redirected_filename, 'w') as f:
424427
f.write(TEMPLATE % urllib.parse.quote(to_path, '#/'))
425428

429+
430+
class CoreModuleTransform(SphinxTransform):
431+
default_priority = 870
432+
433+
def _convert_first_paragraph_into_title(self):
434+
title = self.document.next_node(nodes.title)
435+
paragraph = self.document.next_node(nodes.paragraph)
436+
if not title or not paragraph:
437+
return
438+
if isinstance(paragraph[0], nodes.paragraph):
439+
paragraph = paragraph[0]
440+
if all(isinstance(child, nodes.Text) for child in paragraph.children):
441+
for child in paragraph.children:
442+
title.append(nodes.Text(" \u2013 "))
443+
title.append(child)
444+
paragraph.parent.remove(paragraph)
445+
446+
def _enable_linking_to_nonclass_targets(self):
447+
for desc in self.document.traverse(addnodes.desc):
448+
for xref in desc.traverse(addnodes.pending_xref):
449+
if xref.attributes.get("reftype") == "class":
450+
xref.attributes.pop("refspecific", None)
451+
452+
def apply(self, **kwargs):
453+
docname = self.env.docname
454+
if docname.startswith(autoapi_root) and docname.endswith("/index"):
455+
self._convert_first_paragraph_into_title()
456+
self._enable_linking_to_nonclass_targets()
457+
458+
426459
def setup(app):
427460
app.add_css_file("customstyle.css")
428461
app.add_config_value('redirects_file', 'redirects', 'env')
429462
app.connect('builder-inited', generate_redirects)
463+
app.add_transform(CoreModuleTransform)

docs/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
sphinx<3
1+
sphinx<4
22
recommonmark==0.6.0
33
sphinxcontrib-svg2pdfconverter==0.1.0
44
astroid

shared-bindings/help.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
THE SOFTWARE.
2424
25-
:func:`help` - Built-in method to provide helpful information
25+
:func:`help` -- Built-in method to provide helpful information
2626
==============================================================
2727

2828
.. function:: help(object=None)

0 commit comments

Comments
 (0)