Ensure H2 ``heading`` exists, inserting it just before ``before``. If ``heading`` is already present, no-op. If ``before`` is absent, fall back to :func:`_ensure_h2_section` (append at end). This keeps the canonical index order (e.g. ``## Entities`` ahead of ``## Explorations``) whe
(
lines: list[str],
heading: str,
before: str,
)
| 827 | |
| 828 | |
| 829 | def _ensure_h2_section_before( |
| 830 | lines: list[str], |
| 831 | heading: str, |
| 832 | before: str, |
| 833 | ) -> None: |
| 834 | """Ensure H2 ``heading`` exists, inserting it just before ``before``. |
| 835 | |
| 836 | If ``heading`` is already present, no-op. If ``before`` is absent, fall |
| 837 | back to :func:`_ensure_h2_section` (append at end). This keeps the |
| 838 | canonical index order (e.g. ``## Entities`` ahead of ``## Explorations``) |
| 839 | when recovering an older index.md that predates the section. |
| 840 | """ |
| 841 | if _get_section_bounds(lines, heading) is not None: |
| 842 | return |
| 843 | before_bounds = _get_section_bounds(lines, before) |
| 844 | if before_bounds is None: |
| 845 | _ensure_h2_section(lines, heading) |
| 846 | return |
| 847 | # ``start`` is the line after the ``before`` heading; insert the new |
| 848 | # section (heading + blank line) right before that heading line. |
| 849 | insert_at = before_bounds[0] - 1 |
| 850 | logger.warning( |
| 851 | "Wiki index is missing %r section; inserting it before %r. " |
| 852 | "Check whether the file was hand-edited away from the canonical layout.", |
| 853 | heading, |
| 854 | before, |
| 855 | ) |
| 856 | lines[insert_at:insert_at] = [heading, ""] |
| 857 | |
| 858 | |
| 859 | def _section_contains_link(lines: list[str], heading: str, link: str) -> bool: |
no test coverage detected