"""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 ## Details (##) or could open another item's detail (###). HEADING = re.compile(r"^#{2,3} ", re.M) UNRELEASED = "## Unreleased" # The changelog's section for news not yet released: ## Unreleased, or a release heading such as `## 0.6.0 (unreleased)`. UNRELEASED_SECTION = re.compile(r"^## .*unreleased.*\n+", re.M | re.I) 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 the tagline, bold or not.""" 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 ## or ### heading, which would split ## Details; use #### or 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_}\. .*?(?=^### \d+\. |\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 its UNRELEASED_SECTION, adding ## Unreleased above the first release heading where it has none; a new changelog where changelog is empty. Failure where that section holds ### subsections, since the bullet belongs under one of them.""" text = bullet.strip().removeprefix("- ").strip() if not text: return Failure("the changelog bullet is empty") if "\n" in text: return Failure("the changelog bullet holds a line break; keep it on one line") line = f"- {text}" if not changelog.strip(): return f"# Changelog\n\n{UNRELEASED}\n\n{line}\n" if section := UNRELEASED_SECTION.search(changelog): rest = changelog[section.end():] if re.search(r"^### ", re.split(r"^## ", rest, maxsplit=1, flags=re.M)[0], re.M): return Failure(f"CHANGELOG.md's {section.group().strip()} holds ### subsections; add the bullet under the right one by hand, then finish with --no-changelog") 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