From 14e95ddee81e6488678cc07ae5f419d0ab37e572 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Thu, 1 Aug 2024 12:00:25 +0200 Subject: [PATCH] Slightly simplify toctree code While working on #12720, I noticed that `parse_content` creates and returns an empty node list. It does not do anything with it. This PR moves the node out of `parse_content`, so that `parse_content` only populates the toctree entries. The complete node creation logic is now united in `TocTree.run`. --- sphinx/directives/other.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/sphinx/directives/other.py b/sphinx/directives/other.py index f74f919cc3e..b9e2586adeb 100644 --- a/sphinx/directives/other.py +++ b/sphinx/directives/other.py @@ -78,17 +78,19 @@ def run(self) -> list[Node]: subnode['numbered'] = self.options.get('numbered', 0) subnode['titlesonly'] = 'titlesonly' in self.options self.set_source_info(subnode) + self.parse_content(subnode) + wrappernode = nodes.compound( classes=['toctree-wrapper', *self.options.get('class', ())], ) wrappernode.append(subnode) self.add_name(wrappernode) + return [wrappernode] - ret = self.parse_content(subnode) - ret.append(wrappernode) - return ret - - def parse_content(self, toctree: addnodes.toctree) -> list[Node]: + def parse_content(self, toctree: addnodes.toctree) -> None: + """ + Populate ``toctree['entries']`` and ``toctree['includefiles']`` from content. + """ generated_docnames = frozenset(StandardDomain._virtual_doc_names) suffixes = self.config.source_suffix current_docname = self.env.docname @@ -99,7 +101,6 @@ def parse_content(self, toctree: addnodes.toctree) -> list[Node]: all_docnames.remove(current_docname) # remove current document frozen_all_docnames = frozenset(all_docnames) - ret: list[Node] = [] excluded = Matcher(self.config.exclude_patterns) for entry in self.content: if not entry: @@ -170,8 +171,6 @@ def parse_content(self, toctree: addnodes.toctree) -> list[Node]: toctree['entries'] = list(reversed(toctree['entries'])) toctree['includefiles'] = list(reversed(toctree['includefiles'])) - return ret - class Author(SphinxDirective): """