tree.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from __future__ import annotations
  2. from typing import Any, Dict, List, Optional
  3. from .content import DocumentationPage, registry
  4. from .content.overview import tiles
  5. nodes: List[Dict[str, Any]] = []
  6. def build_tree() -> None:
  7. """Build tree by recursively collecting documentation pages and parts."""
  8. nodes.clear()
  9. for module, _ in tiles:
  10. page = registry[module.__name__.rsplit('.', 1)[-1]]
  11. nodes.append({
  12. 'id': page.name,
  13. 'title': _plain(page.title),
  14. 'children': _children(page),
  15. })
  16. def _children(page: DocumentationPage) -> List[Dict[str, Any]]:
  17. return [
  18. {
  19. 'id': part.link if part.link else f'{page.name}#{part.link_target}',
  20. 'title': _plain(part.title),
  21. 'children': _children(registry[part.link]) if part.link else [],
  22. }
  23. for part in page.parts
  24. if part.link or part.link_target
  25. ]
  26. def _plain(string: Optional[str]) -> str:
  27. assert string is not None
  28. return string.replace('*', '')