Skip to content

Commit 596ffba

Browse files
committed
Fix toctree sorting
1 parent 42e5cc8 commit 596ffba

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

cuda_python/docs/exts/release_toc.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@
44
from pathlib import Path
55

66
from packaging.version import Version
7+
from sphinx.directives.other import TocTree
78

89

9-
def sort_release_toctree(app, doctree, docname):
10-
"""Sort the entries in a release toctree in by version."""
11-
if docname == "release":
12-
for node in doctree.traverse():
13-
if node.tagname == "toctree":
14-
node["entries"] = [(Version(Path(x[1]).name.removesuffix("-notes")), x[1]) for x in node["entries"]]
15-
node["entries"].sort(key=lambda x: x[0], reverse=True)
16-
break
10+
class TocTreeSorted(TocTree):
11+
"""A toctree directive that sorts entries by version."""
12+
13+
def parse_content(self, toctree):
14+
super().parse_content(toctree)
15+
16+
if not toctree["glob"]:
17+
return
18+
19+
toctree["entries"] = [
20+
(Version(Path(x[1]).name.removesuffix("-notes")), x[1]) for x in toctree.get("entries", [])
21+
]
22+
toctree["entries"].sort(key=lambda x: x[0], reverse=True)
23+
toctree["entries"] = [(str(x[0]), x[1]) for x in toctree["entries"]]
1724

1825

1926
def setup(app):
20-
app.connect("doctree-resolved", sort_release_toctree)
27+
app.add_directive("toctree", TocTreeSorted, override=True)

0 commit comments

Comments
 (0)