How to remove module prefix (parent) names from sphinx-apidoc generated titles?

43 views
Skip to first unread message

Shawn Hymel

unread,
Aug 17, 2023, 3:02:53 PMAug 17
to sphinx-users
The closest I could find was to go through each .rst file and remove the package/module parents from the title. For example, in the file edgeimpulse_api.api.admin_api.rst, I changed:

edgeimpulse\_api.api.auth\_api module
=====================================

to:

auth\_api module
================

That helped make some of the longer titles much easier to read on the left sidebar and on the page itself (see screenshot, using Furo theme).

Is there any way to change the title like this automatically (either in sphinx-apidoc or sphinx-build) without needing to manually edit every .rst file? My search through docs and forums has not turned up anything. My backup plan is to create a pre-processing script that runs between sphinx-apidoc and sphinx-build to modify the titles in all .rst files (not pretty, so I'm hoping to avoid this option).
Screenshot 2023-08-17 at 11.15.35 AM.png

Shawn Hymel

unread,
Aug 18, 2023, 1:45:14 PMAug 18
to sphinx-users
Here's my quick and dirty script that I added to conf.py to shorten all titles in the .rst source files. If anyone knows of a better/cleaner way to do this, please let me know!

# Recursively crawl through source directory and shorten titles in .rst files
def crawl_source_shorten_titles(path):

# List files in directory
for file_name in os.listdir(path):

# Build path to file
file_path = os.path.join(path, file_name)

# Recursively crawl to next directory level
if os.path.isdir(file_path):
crawl_source_shorten_titles(file_path)

# Modify .rst source file title
else:
_, extension = os.path.splitext(file_path)
if extension == ".rst":

# Read file, modify title, write back to file
with open(file_path, 'r') as file:
lines = file.readlines()
lines[0] = lines[0].split('.')[-1]
lines[1] = ("=" * (len(lines[0]) - 1)) + "\n"
with open(file_path, 'w') as file:
file.writelines(lines)

# Remove parents from titles in all .rst files
if not show_title_parents:
crawl_source_shorten_titles(source_path)
Reply all
Reply to author
Forward
0 new messages