Chemenu kompiliert Rohnotizen zu einem verlinkten, quellengebundenen Wiki: raw/ -> types/ + tools/ -> kb/ -> reports/. Was mechanisch ist, macht tools/wikitool; was Urteil braucht, macht ein Agent unter Contracts, deren Grenzen in Code durchgesetzt sind statt im Prompt. Dieser Commit ist der Startpunkt der oeffentlichen Historie. Die vorherige Entwicklung fand in einer privaten Instanz statt und ist nicht Teil dieses Repositorys; ihre Erzaehlung steht vollstaendig in CHANGES.md, das mit 44 Eintraegen von 0.1.0 bis 2.1.0 erhalten geblieben ist. Der mitgelieferte Korpus ist ein Testbett und eine Demo: 170 Seiten ueber den Stack selbst - Gates, Lint, Versionierung, Suche, das Wiki-Muster. Er dokumentiert das Werkzeug mit den eigenen Mitteln des Werkzeugs. Lizenz: AGPL-3.0 fuer den Stack (tools/, types/), CC-BY-4.0 fuer die Inhalte. Die Grenze zwischen beiden ist der Dateiplan, den dist export berechnet - siehe NOTICE.
82 lines
3.8 KiB
Python
82 lines
3.8 KiB
Python
"""Mask a page body's code before scanning it for wiki notation.
|
|
|
|
Every text-level scan this tool runs over a page body - `[[wikilinks]]`,
|
|
`[^cite-id]` references, `[^cite-id]: [[...]]` definitions, the quote-limit
|
|
count - asks a question about *prose*. Markdown code is not prose: a page that
|
|
shows the notation instead of using it is documenting the stack, not linking or
|
|
citing. Before this module, those two were the same string to every regex, and
|
|
the checks that read them are hard errors - so a `kb/` page about the citation
|
|
mechanism made `lint --fail-on-error` fail, and the only way out was to write
|
|
about the syntax without writing the syntax.
|
|
|
|
`strip_code_spans()` is the single place that rule lives. Teaching each regex
|
|
its own context logic would be the second copy AGENTS.md invariant 8 forbids,
|
|
and there are six of them.
|
|
|
|
**Offsets are preserved.** Code is replaced by spaces of the same length, never
|
|
removed, so a caller may match against the masked text and slice the original -
|
|
`provenance.split_cite_block()` does exactly that.
|
|
|
|
## What is masked, and what deliberately is not
|
|
|
|
- **Fenced blocks** (``` and ~~~, any fence length, with or without an info
|
|
string), including the fence lines themselves. An unclosed fence runs to the
|
|
end of the body, which is what CommonMark does with it too.
|
|
- **Inline code spans**, matched *within one line*. CommonMark lets a span wrap
|
|
across a newline; this does not, on purpose. A missing closing backtick is a
|
|
common typo, and a line-crossing matcher turns one typo into a silently
|
|
masked paragraph - the failure mode is invisible, because masking too much
|
|
makes findings *disappear*. Line-local matching costs the rare span that is
|
|
wrapped mid-token and nothing else.
|
|
- **Indented code blocks are not masked at all.** In this corpus a four-space
|
|
indent is a nested list continuation far more often than it is code: of the
|
|
three indented `kb/` lines carrying wiki notation, two are bullets whose
|
|
`[[wikilink]]` is a real link. Masking by indentation would delete them from
|
|
the link graph, and CommonMark's own rule for telling the two apart needs the
|
|
list context, not the line. Fence your examples.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
# A fence opener: up to three leading spaces, then three or more backticks or
|
|
# tildes. The run is captured so the closer can be required to be at least as
|
|
# long and of the same character, per CommonMark.
|
|
_FENCE_RE = re.compile(r"^ {0,3}(`{3,}|~{3,})")
|
|
|
|
# An inline code span: a run of backticks, a non-empty body, and a closing run
|
|
# of the same length. The lookarounds are what make "same length" hold - without
|
|
# them ``a`` would match on its first backtick and end one character early.
|
|
_CODE_SPAN_RE = re.compile(r"(?<!`)(`+)(?!`)(.+?)(?<!`)\1(?!`)")
|
|
|
|
|
|
def _blanked(text: str) -> str:
|
|
return " " * len(text)
|
|
|
|
|
|
def _closes(line: str, fence: str) -> bool:
|
|
"""Whether `line` is a closing fence for the opener `fence`."""
|
|
stripped = line.strip()
|
|
return bool(stripped) and set(stripped) == {fence[0]} and len(stripped) >= len(fence)
|
|
|
|
|
|
def strip_code_spans(body: str) -> str:
|
|
"""Return `body` with every fenced block and inline code span replaced by
|
|
spaces of the same length - same total length, same line structure, same
|
|
offsets, no code left for a prose-level regex to match."""
|
|
out: list[str] = []
|
|
fence: str | None = None
|
|
for line in body.split("\n"):
|
|
if fence is not None:
|
|
out.append(_blanked(line))
|
|
if _closes(line, fence):
|
|
fence = None
|
|
continue
|
|
opener = _FENCE_RE.match(line)
|
|
if opener:
|
|
fence = opener.group(1)
|
|
out.append(_blanked(line))
|
|
continue
|
|
out.append(_CODE_SPAN_RE.sub(lambda m: _blanked(m.group(0)), line))
|
|
return "\n".join(out)
|