Skip to content

Commit 1be7f54

Browse files
committed
Allows sorting of sections by their title and a custom key function
1 parent eca0cd3 commit 1be7f54

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

autodocsumm/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,12 @@ def get_grouped_documenters(self, all_members=False):
270270
if not use_sections or section in use_sections:
271271
documenters.setdefault(section, []).append(e)
272272
self.options.update(options_save)
273-
return documenters
273+
274+
if callable(self.env.config.autodocsumm_section_sorter):
275+
return {k: documenters[k] for k in sorted(documenters, key=self.env.config.autodocsumm_section_sorter)}
276+
else:
277+
return documenters
278+
274279

275280
def add_autosummary(self, relative_ref_paths=False):
276281
"""Add the autosammary table of this documenter.
@@ -656,6 +661,7 @@ def setup(app):
656661
app.add_config_value('autodata_content', 'class', True)
657662
app.add_config_value('document_data', True, True)
658663
app.add_config_value('not_document_data', [], True)
664+
app.add_config_value('autodocsumm_section_sorter', None, True)
659665
return {'version': sphinx.__display_version__, 'parallel_read_safe': True}
660666

661667

docs/conf_settings.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,12 @@ Configuration values and events
135135
To exclude the string representation of specific data objects. You may
136136
provide a list of fully qualified object names (e.g. in the form of
137137
``'zipfile.ZipFile'``) or ``True`` or ``False``
138+
139+
140+
.. confval:: autodocsumm_section_sorter
141+
142+
This can be set with a callable that is passed to :func:`sorted` as key
143+
argument to sort the the summary sections by their name. Example usage for
144+
an alphanumerical order::
145+
146+
autodocsumm_section_sorter = lambda s: s

tests/test-root/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
autodata_content = 'both'
2626

27+
autodocsumm_section_sorter = lambda a: a
28+
2729

2830
def member_filter(app, what, name, obj, skip, options):
2931
import dummy

tests/test_autodocsumm.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def get_html(app, fname):
4141
return f.read()
4242

4343

44+
def get_soup(app, fname):
45+
return bs4.BeautifulSoup(get_html(app, fname), 'html.parser')
46+
47+
4448
def in_autosummary(what, html) -> bool:
4549
soup = bs4.BeautifulSoup(html, "html.parser")
4650
autosummaries = soup("table")
@@ -377,6 +381,11 @@ def test_module_submodule(self, app):
377381
assert re.findall(r'<td>.*href="#dummy_submodule\.submodule2'
378382
r'\.SubmoduleClass2\.func2".*</td>', html)
379383

384+
def test_sorted_sections(self, app):
385+
soup = get_soup(app, 'test_autoclasssumm_some_sections.html')
386+
sections = soup.select("p strong")
387+
assert [s.string[:-1] for s in sections] == ["Attributes", "DummySection"]
388+
380389

381390
class TestAutoDocSummDirective:
382391
"""Test case for the :class:`autodocsumm.AutoDocSummDirective`."""

0 commit comments

Comments
 (0)