Build the YAML nav fragment, hierarchically.
(nav: dict, parent_map: dict)
| 595 | |
| 596 | |
| 597 | def build_nav_yaml(nav: dict, parent_map: dict) -> str: |
| 598 | """Build the YAML nav fragment, hierarchically.""" |
| 599 | lines = [' - API Reference:'] |
| 600 | lines.append(' - Overview: api/index.md') |
| 601 | |
| 602 | tree = {} |
| 603 | |
| 604 | def add_to_tree(path_parts, items, current_node, full_path): |
| 605 | if len(path_parts) == 1: |
| 606 | folder_name = path_parts[0] |
| 607 | if folder_name not in current_node: |
| 608 | current_node[folder_name] = {} |
| 609 | if '__items__' not in current_node[folder_name]: |
| 610 | current_node[folder_name]['__items__'] = [] |
| 611 | current_node[folder_name]['__items__'].extend(items) |
| 612 | current_node[folder_name]['__path__'] = full_path |
| 613 | else: |
| 614 | folder_name = path_parts[0] |
| 615 | if folder_name not in current_node: |
| 616 | current_node[folder_name] = {} |
| 617 | |
| 618 | # The full path to this node so far |
| 619 | current_path = full_path.split(folder_name)[0] + folder_name |
| 620 | current_node[folder_name]['__path__'] = current_path |
| 621 | |
| 622 | add_to_tree(path_parts[1:], items, current_node[folder_name], full_path) |
| 623 | |
| 624 | # Enforce ordering |
| 625 | sections_to_emit = [] |
| 626 | for sec in NAV_SECTION_ORDER: |
| 627 | if sec in nav: sections_to_emit.append(sec) |
| 628 | for sec in sorted(nav.keys()): |
| 629 | if sec not in NAV_SECTION_ORDER: sections_to_emit.append(sec) |
| 630 | |
| 631 | for sec in sections_to_emit: |
| 632 | items = sorted(nav[sec], key=lambda x: (_get_depth(x[0], parent_map), x[0])) |
| 633 | add_to_tree(sec.split('/'), items, tree, sec) |
| 634 | |
| 635 | def write_tree(node, depth): |
| 636 | indent = ' ' * depth |
| 637 | |
| 638 | # Then write subfolders and items |
| 639 | for k, v in node.items(): |
| 640 | if k in ('__items__', '__path__'): |
| 641 | continue |
| 642 | |
| 643 | # Get the display title for this folder from _SECTION_META if it exists, otherwise use the folder name |
| 644 | node_path = v.get('__path__', k) |
| 645 | title, _ = _SECTION_META.get(node_path, (k, '')) |
| 646 | # If the title contains the parent path (e.g. "Components — Sensors"), strip the parent part off for nested display |
| 647 | if ' — ' in title: |
| 648 | title = title.split(' — ')[-1] |
| 649 | |
| 650 | lines.append(f'{indent}- {title}:') |
| 651 | |
| 652 | # Write items directly inside this folder |
| 653 | if '__items__' in v: |
| 654 | for name, path in v['__items__']: |
no test coverage detected