p = 'backend/app/services/autolink.py'
s = open(p).read()

s = s.replace('''def apply(text: str, matchers: list[tuple[re.Pattern, str]], skip: str | None = None,
          limit: int = 6) -> tuple[str, int]:
    """The section with its first mentions linked, and how many were added.

    `skip` is the slug of the article being written, so it does not link to
    itself. `limit` caps a section: six cross-references is a generous section
    and past that the prose is a list of links with words between them.
    """''',
'''def apply(text: str, matchers: list[tuple[re.Pattern, str]],
          skip: str | Iterable[str] | None = None,
          limit: int = 3) -> tuple[str, int]:
    """The section with its first mentions linked, and how many were added.

    `skip` is the slugs this article must not link — its own, and anything its
    title already is. `limit` caps a section. Three, not six: the editorial
    rule is "one useful mention, low density", and a light pass is the right
    one because a person tunes it afterwards. A section wearing six links is
    a list of links with words between them.
    """''')

s = s.replace('''    seen: set[str] = set(re.findall(r"\\[\\[[^\\]|]+\\|([a-z0-9-]+)\\]\\]", text))
    if skip:
        seen.add(skip)''',
'''    seen: set[str] = set(re.findall(r"\\[\\[[^\\]|]+\\|([a-z0-9-]+)\\]\\]", text))
    if isinstance(skip, str):
        seen.add(skip)
    elif skip:
        seen.update(skip)''')

s = s.replace('''"""Turn a disease named in prose into a link to the article about it.''',
'''"""Turn a disease named in prose into a link to the article about it.

The editorial rule, in the user's words: link only where the link takes the
reader somewhere useful. An article about Adolescent Depression does not link
the word "depression" — the reader is already there — but it does link
insomnia and substance use disorder, because those lead somewhere. And one
useful mention rather than every mention: a person tunes these afterwards, so
a light pass leaves them something to tune rather than something to undo.''')

s = s.replace('''import re

#: Titles that are ordinary English''',
'''import re
from typing import Iterable

#: Titles that are ordinary English''')

s += '''

def self_referential(title: str, category: str | None,
                     targets: list[tuple[str, str]]) -> set[str]:
    """The slugs an article must not link, because it already is them.

    An article called "Adolescent Depression" linking the word "depression" is
    offering the reader a door to the room they are standing in. So a target
    whose title is a run of words inside this article's title is dropped, and
    so is one that merely names the category the article is filed under — the
    reader got here through it.

    Only a whole-word run counts. "Anemia" is inside "Iron Deficiency Anemia"
    and must not be linked from it; "Anemia" is not inside "Anemic Hypoxia" in
    the sense that matters, and a substring test would say it was.
    """
    own = set(re.findall(r"[a-z0-9']+", (title or "").lower()))
    filed = " ".join(re.findall(r"[a-z0-9']+", (category or "").lower()))
    drop = set()
    for target_title, slug in targets:
        words = re.findall(r"[a-z0-9']+", (target_title or "").lower())
        if not words:
            continue
        if set(words) <= own:
            drop.add(slug)
        elif filed and " ".join(words) == filed:
            drop.add(slug)
    return drop
'''
open(p, 'w').write(s)
print('written')
