bundles / papyri latest / papyri / toc / _tree
function
papyri.toc:_tree
source: /papyri/toc.py :25
Signature
def _tree ( current_path : str , unnest : dict[str, list[str]] , counter : dict[str, int] , depth : int = 0 ) → dict[str, Any] Summary
Recursively build a nested toctree dict rooted at current_path.
Extended Summary
Walks the flat toctree adjacency map unnest (node -> list of child references as written in the source) and returns a nested {child_path: {grandchild_path: {...}}} dict describing the tree reachable from current_path.
Path model:
Nodes are addressed with
:-separated keys (e.g.pkg:sub:page); the last segment is the page name and the prefix is its "directory". Child references inunnestare written in source form using/and may be relative (../sibling,sub/page). For each child reference this function:drops empty entries,
skips absolute (
/...) and external (https://) refs,rewrites a trailing
/to/index(Sphinx directory-index convention),resolves
..segments againstcurrent_path's directory via dotdotcount,strips a trailing
.rst,then recurses.
Side effects / bookkeeping:
countermaps every known node to a visit count. It is incremented forcurrent_pathon entry, so callers can detect nodes referenced 0 times (orphans) or >1 times (multi-parent). A reference whose resolved path is not incounteris treated as dangling and skipped with a printed warning.Asserts that the resolved child path is neither
current_pathitself nor already present among siblings — i.e. the input must describe a tree, not a DAG with duplicate edges.
depth is informational only (used by the commented-out debug prints); it does not affect the result.
Aliases
-
papyri.toc._tree