Cosmetic refactor of `_entries_from_toctree`

This commit is contained in:
Adam Turner 2023-01-02 11:40:56 +00:00
parent 085a293357
commit bc262cc8f1

View File

@ -86,7 +86,7 @@ class TocTree:
if isinstance(subnode, (addnodes.compact_paragraph, if isinstance(subnode, (addnodes.compact_paragraph,
nodes.list_item)): nodes.list_item)):
# for <p> and <li>, indicate the depth level and recurse # for <p> and <li>, indicate the depth level and recurse
subnode['classes'].append('toctree-l%d' % (depth - 1)) subnode['classes'].append(f'toctree-l{depth - 1}')
_toctree_add_classes(subnode, depth) _toctree_add_classes(subnode, depth)
elif isinstance(subnode, nodes.bullet_list): elif isinstance(subnode, nodes.bullet_list):
# for <ul>, just recurse # for <ul>, just recurse
@ -111,8 +111,7 @@ class TocTree:
subnode = subnode.parent subnode = subnode.parent
def _entries_from_toctree(toctreenode: addnodes.toctree, parents: list[str], def _entries_from_toctree(toctreenode: addnodes.toctree, parents: list[str],
separate: bool = False, subtree: bool = False subtree: bool = False) -> list[Element]:
) -> list[Element]:
"""Return TOC entries for a toctree node.""" """Return TOC entries for a toctree node."""
refs = [(e[0], e[1]) for e in toctreenode['entries']] refs = [(e[0], e[1]) for e in toctreenode['entries']]
entries: list[Element] = [] entries: list[Element] = []
@ -189,15 +188,15 @@ class TocTree:
logger.warning(message, ref, location=toctreenode) logger.warning(message, ref, location=toctreenode)
else: else:
# children of toc are:
# - list_item + compact_paragraph + (reference and subtoc)
# - only + subtoc
# - toctree
children = cast(Iterable[nodes.Element], toc)
# if titles_only is given, only keep the main title and # if titles_only is given, only keep the main title and
# sub-toctrees # sub-toctrees
if titles_only: if titles_only:
# children of toc are:
# - list_item + compact_paragraph + (reference and subtoc)
# - only + subtoc
# - toctree
children = cast(Iterable[nodes.Element], toc)
# delete everything but the toplevel title(s) # delete everything but the toplevel title(s)
# and toctrees # and toctrees
for toplevel in children: for toplevel in children:
@ -209,22 +208,19 @@ class TocTree:
else: else:
toplevel.pop(1) toplevel.pop(1)
# resolve all sub-toctrees # resolve all sub-toctrees
for subtocnode in list(toc.findall(addnodes.toctree)): for sub_toc_node in list(toc.findall(addnodes.toctree)):
if not (subtocnode.get('hidden', False) and if sub_toc_node.get('hidden', False) and not includehidden:
not includehidden): continue
i = subtocnode.parent.index(subtocnode) + 1 for i, entry in enumerate(
for entry in _entries_from_toctree( _entries_from_toctree(sub_toc_node, [refdoc] + parents,
subtocnode, [refdoc] + parents, subtree=True),
subtree=True): start=sub_toc_node.parent.index(sub_toc_node) + 1
subtocnode.parent.insert(i, entry) ):
i += 1 sub_toc_node.parent.insert(i, entry)
subtocnode.parent.remove(subtocnode) sub_toc_node.parent.remove(sub_toc_node)
if separate:
entries.append(toc) entries.extend(children)
else: if not subtree:
children = cast(Iterable[nodes.Element], toc)
entries.extend(children)
if not subtree and not separate:
ret = nodes.bullet_list() ret = nodes.bullet_list()
ret += entries ret += entries
return [ret] return [ret]
@ -236,10 +232,7 @@ class TocTree:
if not includehidden and toctree.get('includehidden', False): if not includehidden and toctree.get('includehidden', False):
includehidden = True includehidden = True
# NOTE: previously, this was separate=True, but that leads to artificial tocentries = _entries_from_toctree(toctree, [])
# separation when two or more toctree entries form a logical unit, so
# separating mode is no longer used -- it's kept here for history's sake
tocentries = _entries_from_toctree(toctree, [], separate=False)
if not tocentries: if not tocentries:
return None return None