Skip to content

Commit

Permalink
Slightly simplify toctree code
Browse files Browse the repository at this point in the history
While working on sphinx-doc#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`.
  • Loading branch information
timhoffm committed Aug 1, 2024
1 parent df871ab commit 14e95dd
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions sphinx/directives/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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):
"""
Expand Down

0 comments on commit 14e95dd

Please sign in to comment.