Remove ``source_file`` from the inline ``sources:`` list in YAML frontmatter. Returns ``(rewritten_text, sources_now_empty)``. ``sources_now_empty`` is True when ``source_file`` was the only remaining item in the list (callers can use this to decide whether to delete the page entirely).
(text: str, source_file: str)
| 1138 | |
| 1139 | |
| 1140 | def _remove_source_from_frontmatter(text: str, source_file: str) -> tuple[str, bool]: |
| 1141 | """Remove ``source_file`` from the inline ``sources:`` list in YAML frontmatter. |
| 1142 | |
| 1143 | Returns ``(rewritten_text, sources_now_empty)``. ``sources_now_empty`` is |
| 1144 | True when ``source_file`` was the only remaining item in the list (callers |
| 1145 | can use this to decide whether to delete the page entirely). |
| 1146 | |
| 1147 | If the frontmatter is missing, malformed, has no ``sources:`` line, or |
| 1148 | the source is not present in the list, returns ``(text, False)``. |
| 1149 | """ |
| 1150 | if not text.startswith("---"): |
| 1151 | return text, False |
| 1152 | |
| 1153 | parts = frontmatter.split(text) |
| 1154 | if parts is None: |
| 1155 | return text, False |
| 1156 | |
| 1157 | fm_block, body = parts |
| 1158 | # See _prepend_source_to_frontmatter: the closing delimiter may be "\n---\n" |
| 1159 | # or a bare "\n---" (no trailing newline); strip whichever is present so the |
| 1160 | # existing frontmatter lines (and the sources: line we need) are preserved. |
| 1161 | closing = "\n---\n" if fm_block.endswith("\n---\n") else "\n---" |
| 1162 | fm_prefix = fm_block[: -len(closing)] |
| 1163 | fm_lines = fm_prefix.split("\n") |
| 1164 | |
| 1165 | for i, line in enumerate(fm_lines): |
| 1166 | if not line.lstrip().startswith("sources:"): |
| 1167 | continue |
| 1168 | items = _parse_yaml_list_value(line) |
| 1169 | if items is None: |
| 1170 | return text, False |
| 1171 | if source_file not in items: |
| 1172 | return text, False |
| 1173 | items.remove(source_file) |
| 1174 | fm_lines[i] = _yaml_list_line("sources", items) |
| 1175 | return "\n".join(fm_lines) + closing + body, len(items) == 0 |
| 1176 | |
| 1177 | return text, False |
| 1178 | |
| 1179 | |
| 1180 | def _add_related_link( |
no outgoing calls