Skip to content

Commit 2212e84

Browse files
committed
Fix recursion error when nested toctrees contain a self entry
1 parent 52447e1 commit 2212e84

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

sphinx_readme/parser.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,16 +339,18 @@ def _parse_toctree(self, toctree, docname, tocs, is_subtoc=False):
339339
toc['maxdepth'] = toctree.get('maxdepth')
340340

341341
for text, entry in toctree.get('entries', []):
342-
entry = entry if entry != 'self' else docname
343-
title = text if text else self.titles.get(entry)
344-
subtree = tocs[entry].next_node(nodes.bullet_list)
342+
entry_name = entry if entry != 'self' else docname
343+
title = text if text else self.titles.get(entry_name)
344+
subtree = tocs[entry_name].next_node(nodes.bullet_list)
345345

346-
if subtree:
346+
if subtree and entry != 'self':
347347
if is_subtoc or 'maxdepth' in toc:
348348
subtree = self._parse_subtree(subtree, tocs)
349+
else:
350+
subtree = None # Sphinx treats "self" as titlesonly
349351

350352
toc['entries'].append({
351-
'entry': entry,
353+
'entry': entry_name,
352354
'title': title,
353355
'entries': subtree
354356
})
@@ -359,8 +361,9 @@ def _parse_subtree(self, tree: nodes.bullet_list, tocs: Dict[str, nodes.bullet_l
359361

360362
for child in tree.children:
361363
if isinstance(child, addnodes.toctree):
364+
child_doc = Path(child.source).relative_to(self.config.src_dir)
362365
sub_toc = self._parse_toctree(
363-
docname=Path(child.source).stem,
366+
docname=child_doc.as_posix().removesuffix(child_doc.suffix),
364367
is_subtoc=True, tocs=tocs,
365368
toctree=child,
366369
)

0 commit comments

Comments
 (0)