MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / _replace_or_append_section

Function _replace_or_append_section

graphify/install.py:906–960  ·  view source on GitHub ↗

Idempotently update or append a graphify-owned section in shared files. If no line is exactly ``marker`` (the heading, at column 0), append ``new_section`` to the end (with a blank-line separator if there's existing content). If a real ``marker`` heading exists, replace the existin

(
    content: str, marker: str, new_section: str, boundary_prefix: str = "## "
)

Source from the content-addressed store, hash-verified

904 """Resolve a CLI platform alias to its real _PLATFORM_CONFIG key."""
905 return _PLATFORM_ALIASES.get(platform_name, platform_name)
906def _replace_or_append_section(
907 content: str, marker: str, new_section: str, boundary_prefix: str = "## "
908) -> str:
909 """Idempotently update or append a graphify-owned section in shared files.
910
911 If no line is exactly ``marker`` (the heading, at column 0), append
912 ``new_section`` to the end (with a blank-line separator if there's existing
913 content).
914
915 If a real ``marker`` heading exists, replace the existing section in place.
916 The section runs from that heading to the line before the next
917 ``boundary_prefix`` heading (default the next H2), or to EOF if none
918 follows. This lets older installs receive the updated copy without users
919 having to uninstall and reinstall (issue #580).
920
921 ``boundary_prefix`` must match whatever level ``marker`` itself is (``"# "``
922 for an H1 marker, the default ``"## "`` for an H2 one) — mirrors
923 ``_remove_marker_section``'s own ``boundary_prefix`` so an insert and its
924 matching removal agree on where a section ends (#3668).
925
926 The heading is matched only when a line *is* exactly ``marker`` (after
927 stripping surrounding whitespace), never as a substring. Matching ``##
928 graphify`` inside a bullet or an inline reference used to anchor the replace
929 on that mention and delete every line from there to the next heading,
930 silently destroying hand-curated content (#1688). When several exact
931 headings exist, the last one is used, since graphify's section is appended.
932 """
933 lines = content.split("\n")
934 starts = [i for i, line in enumerate(lines) if line.strip() == marker]
935 if not starts:
936 if content.strip():
937 return content.rstrip() + "\n\n" + new_section.lstrip()
938 return new_section.lstrip()
939
940 start = starts[-1]
941 end = len(lines)
942 for j in range(start + 1, len(lines)):
943 if lines[j].startswith(boundary_prefix):
944 end = j
945 break
946
947 head = "\n".join(lines[:start]).rstrip()
948 tail = "\n".join(lines[end:]).lstrip()
949 section = new_section.strip()
950
951 parts: list[str] = []
952 if head:
953 parts.append(head)
954 parts.append(section)
955 if tail:
956 parts.append(tail)
957 out = "\n\n".join(parts)
958 if not out.endswith("\n"):
959 out += "\n"
960 return out
961
962
963def _remove_marker_section(content: str, marker: str, boundary_prefix: str = "## ") -> "str | None":

Calls 1

joinMethod · 0.45