@@ -464,16 +464,13 @@ def _toctree_add_classes(node: Element, depth: int, docname: str) -> None:
464
464
465
465
ET = TypeVar ('ET' , bound = Element )
466
466
467
-
468
- def _toctree_copy (
469
- node : ET , depth : int , maxdepth : int , collapse : bool , tags : Tags
470
- ) -> ET :
467
+ def _toctree_copy_children (
468
+ copy : ET , node : ET , depth : int , maxdepth : int , collapse : bool , tags : Tags
469
+ ) -> None :
471
470
"""Utility: Cut and deep-copy a TOC at a specified depth."""
472
471
keep_bullet_list_sub_nodes = depth <= 1 or (
473
472
(depth <= maxdepth or maxdepth <= 0 ) and (not collapse or 'iscurrent' in node )
474
473
)
475
-
476
- copy = node .copy ()
477
474
for subnode in node .children :
478
475
if isinstance (subnode , addnodes .compact_paragraph | nodes .list_item ):
479
476
# for <p> and <li>, just recurse
@@ -489,18 +486,11 @@ def _toctree_copy(
489
486
# copy sub toctree nodes for later processing
490
487
copy .append (subnode .copy ())
491
488
elif isinstance (subnode , addnodes .only ):
492
- # only keep children if the only node matches the tags
489
+ # Only keep its children if the 'only' node matches the tags. When
490
+ # matching, call recursively the current function without
491
+ # incrementing the depth as 'only' nodes are not toctree nodes.
493
492
if _only_node_keep_children (subnode , tags ):
494
- for child in subnode .children :
495
- copy .append (
496
- _toctree_copy (
497
- child ,
498
- depth ,
499
- maxdepth ,
500
- collapse ,
501
- tags , # type: ignore[type-var]
502
- )
503
- )
493
+ _toctree_copy_children (copy , subnode , depth , maxdepth , collapse , tags )
504
494
elif isinstance (subnode , nodes .reference | nodes .title ):
505
495
# deep copy references and captions
506
496
sub_node_copy = subnode .copy ()
@@ -511,6 +501,15 @@ def _toctree_copy(
511
501
else :
512
502
msg = f'Unexpected node type { subnode .__class__ .__name__ !r} !'
513
503
raise ValueError (msg )
504
+
505
+ return copy
506
+
507
+
508
+ def _toctree_copy (
509
+ node : ET , depth : int , maxdepth : int , collapse : bool , tags : Tags
510
+ ) -> ET :
511
+ copy = node .copy ()
512
+ _toctree_copy_children (copy , node , depth , maxdepth , collapse , tags )
514
513
return copy
515
514
516
515
0 commit comments