116 lines
5.2 KiB
Python
116 lines
5.2 KiB
Python
"""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, item_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"(?<!\\)\|", r"\\|", tagline) + "**"}
|
|
lines.insert(table.start + len(table.rows), "| " + " | ".join(row.get(column, "") for column in table.columns) + " |")
|
|
text = "\n".join(lines)
|
|
if not detail.strip():
|
|
return id_, text
|
|
entry = f"### {id_}. {tagline}\n\n{detail.strip()}\n"
|
|
section = DETAILS.search(text)
|
|
if section is None:
|
|
return id_, text.rstrip("\n") + f"\n\n## Details\n\n{entry}"
|
|
return id_, text[:section.start(1)] + f"\n{entry}" + text[section.start(1):]
|
|
|
|
|
|
def finished(todo: str, id_: int) -> 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
|
|
at = row_index(table, id_)
|
|
if isinstance(at, Failure):
|
|
return at
|
|
del lines[at]
|
|
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 shown(todo: str, id_: int) -> str | Failure:
|
|
"""The table's header and divider, item id_'s row, then its detail with its heading, sub-headings included; the row alone where the item has no detail."""
|
|
lines = todo.split("\n")
|
|
table = held_table(lines)
|
|
if isinstance(table, Failure):
|
|
return table
|
|
at = row_index(table, id_)
|
|
if isinstance(at, Failure):
|
|
return at
|
|
details = item_details(todo)
|
|
if isinstance(details, Failure):
|
|
return details
|
|
shown_table = "\n".join([*lines[table.header:table.start], lines[at]])
|
|
if id_ not in details:
|
|
return shown_table
|
|
heading = re.search(rf"^### {id_}\. .*$", todo, re.M)
|
|
assert heading is not None # item_details found the detail by this heading
|
|
return f"{shown_table}\n\n{heading.group()}\n\n{details[id_]}"
|
|
|
|
|
|
def row_index(table: Table, id_: int) -> int | Failure:
|
|
"""The index in the file's lines of item id_'s row."""
|
|
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_):
|
|
return at
|
|
return Failure(f"todo.md holds no item {id_}")
|
|
|
|
|
|
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
|