I have a package with submodules and I wanted to still display the submodule name in navigation, as there are multiple duplicate function names across submodules.
For
foo.bar.baz()
I need to display
bar.baz()
This makes
For this, I created a custom Jinja filter and then modified autosummary's core templates to use it. The filter is injected through a monkey-patch in Sphinx's conf.py.
An example conf.py:
# Monkey-patch autosummary template context
from sphinx.ext.autosummary.generate import AutosummaryRenderer
def smart_fullname(fullname):
parts = fullname.split(".")
return ".".join(parts[1:])
def fixed_init(self, app, template_dir=None):
AutosummaryRenderer.__old_init__(self, app, template_dir)
self.env.filters["smart_fullname"] = smart_fullname
AutosummaryRenderer.__old_init__ = AutosummaryRenderer.__init__
AutosummaryRenderer.__init__ = fixed_init
Then here is the example from _templates/autosummary/module.rst
:
{{ fullname | smart_fullname | escape | underline}}
Documentation for `{{ fullname }}` module.
.. automodule:: {{ fullname }}
See the full documentation for further examples.