p = 'backend/app/services/citations.py'
s = open(p).read()
old = s[s.index('def cite(entry: dict, topic'):]
new = '''def cite(entry: dict) -> str | None:
    """One reference line, in the standard form. None if it is not a source."""
    return source_line(entry)


def tidy(refs: list[dict] | None, limit: int = 10) -> list[dict]:
    """A reference list a reader can use, out of what the corpus recorded.

    Junk goes, the same book named two ways becomes one entry, and each keeps
    the pages it was found on so a passage can still be traced — the pages are
    simply not what the reader is shown.
    """
    out: dict[str, dict] = {}
    for entry in refs or []:
        if not isinstance(entry, dict):
            continue
        text = cite(entry)
        if not text:
            continue
        row = out.setdefault(text, {"text": text, "title": entry.get("title"),
                                    "author": entry.get("author"), "pages": []})
        pages = entry.get("pages")
        if not isinstance(pages, list):
            pages = [entry.get("page")] if entry.get("page") is not None else []
        for page in pages:
            if isinstance(page, int) and page not in row["pages"]:
                row["pages"].append(page)
    for row in out.values():
        row["pages"] = sorted(row["pages"])[:6]
    # Alphabetical, which is how a reference list of books is read — the order
    # the retriever happened to return them in means nothing to anybody.
    return sorted(out.values(), key=lambda row: row["text"])[:limit]
'''
s = s.replace(old, new)
# topic_of is no longer used by anything here; the block goes with it.
start = s.index('#: A chunk usually opens by naming what it is about')
end = s.index('def cite(entry: dict) -> str | None:')
s = s[:start] + s[end:]
open(p, 'w').write(s)
print('written')
