"""The text edits behind todo-item: filing an item in todo.md, removing a finished one, and adding a bullet to CHANGELOG.md. Each returns new text; ranking.rank then orders the table and details and writes the derived cells.""" import re from release_plan.todo_table import cells, DETAILS, items_table, Table from failure import Failure from release_plan.ranking import stated # A line that would end the item's detail, or open another one, inside ## Details. HEADING = re.compile(r"^#{1,3} ", re.M) UNRELEASED = "## Unreleased" def added(todo: str, named: dict[str, str], detail: str) -> tuple[int, str] | Failure: """todo with an item filed under Next ID, and that ID; named holds its cells by column name, its Item cell a plain tagline.""" next_id = stated(todo, "Next ID") if isinstance(next_id, Failure): return next_id if not next_id.isdigit(): return Failure(f"Next ID {next_id!r} is not a whole number") tagline = named["Item"].strip().removeprefix("**").removesuffix("**") if "\n" in tagline: return Failure("the tagline holds a line break; keep it on one line") if HEADING.search(detail): return Failure("the detail holds a markdown heading, which would split ## Details; drop the #s") id_ = int(next_id) lines = re.sub(r"^`Next ID = [0-9]+`$", f"`Next ID = {id_ + 1}`", todo, count=1, flags=re.M).split("\n") table = held_table(lines) if isinstance(table, Failure): return table row = {**named, "ID": str(id_), "Item": "**" + re.sub(r"(? str | Failure: """todo without item id_'s row and its detail.""" lines = todo.split("\n") table = held_table(lines) if isinstance(table, Failure): return table for at, row in enumerate(table.rows, table.start): found = cells(row, len(table.columns)) if isinstance(found, Failure): return found if found[0] == str(id_): del lines[at] break else: return Failure(f"todo.md holds no item {id_}") text = "\n".join(lines) section = DETAILS.search(text) if section is None: return text body = re.sub(rf"^### {id_}\. .*?(?=^### |\Z)", "", section.group(1), count=1, flags=re.M | re.S) return text[:section.start(1)] + body + text[section.end(1):] def with_bullet(changelog: str, bullet: str) -> str | Failure: """changelog with bullet first under ## Unreleased, which goes above the first release where missing; a new changelog where changelog is empty.""" line = "- " + bullet.strip().removeprefix("- ") if "\n" in line: return Failure("the changelog bullet holds a line break; keep it on one line") if not changelog.strip(): return f"# Changelog\n\n{UNRELEASED}\n\n{line}\n" if section := re.search(rf"^{UNRELEASED}\n+", changelog, re.M): rest = changelog[section.end():] return changelog[:section.end()] + line + "\n" + ("" if rest.startswith("- ") or not rest else "\n") + rest if release := re.search(r"^## ", changelog, re.M): return changelog[:release.start()] + f"{UNRELEASED}\n\n{line}\n\n" + changelog[release.start():] return changelog.rstrip("\n") + f"\n\n{UNRELEASED}\n\n{line}\n" def held_table(lines: list[str]) -> Table | Failure: table = items_table(lines) return Failure("todo.md holds no items table; rank_todo.py --help shows one") if table is None else table