47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
import re
|
|
|
|
from loop_claude.project.checkout import read_checkout_file
|
|
from loop_claude.stop import Stop
|
|
|
|
FENCE = re.compile(r"^ {0,3}(`{3,}|~{3,})", re.M)
|
|
|
|
|
|
def read_premises(root: str) -> str | Stop:
|
|
text = read_checkout_file(root, "README.md", "no premises")
|
|
if isinstance(text, Stop):
|
|
return text
|
|
found = sections(text)
|
|
for name in ("Goals", "Audience"):
|
|
bodies = found.get(name, [])
|
|
if not bodies:
|
|
return Stop(1, f"README.md has no ## {name} heading - no premises.")
|
|
if len(bodies) > 1:
|
|
return Stop(1, f"README.md has ## {name} more than once - no premises.")
|
|
if not bodies[0]:
|
|
return Stop(1, f"README.md has nothing under ## {name} - no premises.")
|
|
return f"## Goals\n{found['Goals'][0]}\n\n## Audience\n{found['Audience'][0]}"
|
|
|
|
|
|
def sections(text: str) -> dict[str, list[str]]:
|
|
blocks: list[tuple[str | None, list[str]]] = []
|
|
opener = ""
|
|
for line in text.splitlines():
|
|
if fence := FENCE.match(line):
|
|
marker = fence.group(1)
|
|
if not opener:
|
|
opener = marker
|
|
elif marker[0] == opener[0] and len(marker) >= len(opener) and not line[fence.end():].strip():
|
|
opener = ""
|
|
fenced = bool(opener or fence)
|
|
if not fenced and line.startswith("## "):
|
|
blocks.append((line.strip()[3:], []))
|
|
elif not fenced and line.startswith("# "):
|
|
blocks.append((None, []))
|
|
elif blocks and line.strip():
|
|
blocks[-1][1].append(line)
|
|
found: dict[str, list[str]] = {}
|
|
for name, body in blocks:
|
|
if name is not None:
|
|
found.setdefault(name, []).append("\n".join(body))
|
|
return found
|