MCPcopy Create free account
hub / github.com/VectifyAI/OpenKB / _ensure_h2_section_before

Function _ensure_h2_section_before

openkb/agent/compiler.py:829–856  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

827
828
829def _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
859def _section_contains_link(lines: list[str], heading: str, link: str) -> bool:

Callers 1

_update_indexFunction · 0.85

Calls 2

_get_section_boundsFunction · 0.85
_ensure_h2_sectionFunction · 0.85

Tested by

no test coverage detected