diff --git a/CHANGELOG.md b/CHANGELOG.md index a767f69..2cde3e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ The package version runs on its own semver; the schema it is generated from is ` ## Unreleased +## 0.154.0 (2026-11-25) + +- **`name_finder` passes over what is no mention**, as the npm SDK's `nameFinder` 4.208.0 does. In any text, a bare web address (`http://` or `https://`, in any ASCII case, to the next white space or `<`) and an autolink (``) are now passed over, beside the addresses and Markdown links they were before. A call can say its text is Markdown, `find(text, markdown=True)`: then nothing is read inside an inline code span (backtick runs matched by length), a fenced code block (closed by its own character, at least as long, or open to the end), an HTML tag or comment, or a reference definition's line either. The option is off by default, so the answers without it change only inside web addresses. An indented code block, a fence inside a quote or a list, and a definition's title on the next line are read as prose. Held by version 3 of the shared cases (84 cases, 16 with the option; 14 timed recipes), with eight planted faults each red; the two packages gave the same answers on 4,000 made texts, read with the option and without. + ## 0.153.0 (2026-11-25) - **`calendar.speaking_calendar(world, elements)`: the calendar the world's dates speak in, by the World's own choice** (the calendar profile's "Which calendar speaks", schema-dist serial 147; the npm SDK's `speakingCalendar`). The World names it in `x_calendar` (`WORLD_CALENDAR_KEY`), the id of the element that carries it, matched as written. A named element among the elements that carries a calendar speaks; anything else named speaks none, never another calendar in its place; with none named (the key absent or None) the only calendar speaks, and several leave none. Pass the whole world's elements. A `world` that is not a mapping is a `TypeError`. Held by the fixture's 12 cases that carry `speaks`. diff --git a/src/onlyworlds/_version.py b/src/onlyworlds/_version.py index e9f59dd..962dd11 100644 --- a/src/onlyworlds/_version.py +++ b/src/onlyworlds/_version.py @@ -1 +1 @@ -__version__ = "0.153.0" +__version__ = "0.154.0" diff --git a/src/onlyworlds/name_finder.py b/src/onlyworlds/name_finder.py index 9e94d6f..e5ff0c9 100644 --- a/src/onlyworlds/name_finder.py +++ b/src/onlyworlds/name_finder.py @@ -7,6 +7,7 @@ from __future__ import annotations +import re import unicodedata from collections.abc import Callable, Iterable, Mapping from dataclasses import dataclass @@ -112,10 +113,189 @@ def _markdown_links(text: str) -> list[tuple[int, int]]: return spans -def _passed_over(text: str) -> list[tuple[int, int]]: - """The stretches no name is read in: every address and every Markdown link, in order, none overlapping.""" +# Unicode's White_Space code points (room's _WHITE_SPACE, one string) end a bare web address and an autolink. +_SCHEME = re.compile(r"https?://", re.ASCII | re.IGNORECASE) +_SCHEME_CHAR = frozenset("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+.-") + + +def _ascii_letter(ch: str) -> bool: + return ("A" <= ch <= "Z") or ("a" <= ch <= "z") + + +def _web_addresses(text: str) -> list[tuple[int, int]]: + """Every bare web address and every autolink, in any text. A bare address starts at ``http://`` or ``https://`` + (the scheme in any ASCII case) and runs to just before the first white space or ``<``. An autolink is ``<``, a + scheme (an ASCII letter, then 1 to 31 of letters, digits, ``+``, ``.`` or ``-``), ``:``, then anything but white + space, a control character, ``<`` or ``>``, up to the closing ``>``.""" + spans: list[tuple[int, int]] = [] + length = len(text) + pos = 0 + while (m := _SCHEME.search(text, pos)) is not None: + end = m.end() + while end < length and text[end] not in _WHITE_SPACE and text[end] != "<": + end += 1 + spans.append((m.start(), end)) + pos = end + opened = text.find("<") + while opened != -1: + i = opened + 1 + if i < length and _ascii_letter(text[i]): + n = 1 + i += 1 + while i < length and n < 32 and text[i] in _SCHEME_CHAR: + i += 1 + n += 1 + if n >= 2 and i < length and text[i] == ":": + i += 1 + while i < length: + c = text[i] + if c in "<>" or c < "\x20" or c == "\x7f" or c in _WHITE_SPACE: + break + i += 1 + if i < length and text[i] == ">": + spans.append((opened, i + 1)) + opened = text.find("<", i + 1) + continue + opened = text.find("<", opened + 1) + return spans + + +def _line_bounds(text: str) -> list[tuple[int, int]]: + """Where each line starts and ends (before its ``\\n``), in order.""" + out: list[tuple[int, int]] = [] + start = 0 + while True: + nl = text.find("\n", start) + if nl == -1: + out.append((start, len(text))) + return out + out.append((start, nl)) + start = nl + 1 + + +def _fence_of(text: str, start: int, end: int) -> tuple[str, int, int] | None: + """A line's fence, after at most three spaces: ``(char, length, after)`` for a run of three or more `` ` `` or + ``~``, or None.""" + i = start + while i < end and i - start < 3 and text[i] == " ": + i += 1 + if i >= end or text[i] not in "`~": + return None + char = text[i] + j = i + while j < end and text[j] == char: + j += 1 + return (char, j - i, j) if j - i >= 3 else None + + +def _markdown_stretches(text: str) -> list[tuple[int, int]]: + """The Markdown stretches no name is read in (``markdown=True``), as the npm SDK's ``markdownStretches`` reads + them. Fenced code blocks first: a line whose fence opens one (a backtick fence holds no backtick in the rest of + its line) runs to a later line holding only a fence of the same character, at least as long, and spaces or tabs + after it, or to the end of the text. Outside them: an inline code span (a run of backticks to the next run of + exactly as many; a run with none is prose), an HTML comment (````), an HTML tag (``<``, + an optional ``/``, an ASCII letter, to the first ``>``), and a reference definition's line (after at most three + spaces, ``[``, a label, and the line's first ``]`` followed by ``:``).""" + spans: list[tuple[int, int]] = [] + lines = _line_bounds(text) + outside: list[tuple[int, int]] = [] + seg_start = 0 + bracket = -2 # the next "]" at or after the place last asked about, searched for once per place passed + k = 0 + while k < len(lines): + ls, le = lines[k] + opener = _fence_of(text, ls, le) + if opener is None or (opener[0] == "`" and text.find("`", opener[2], le) != -1): + i = ls + while i < le and i - ls < 3 and text[i] == " ": + i += 1 + if i < le and text[i] == "[": + if bracket != -1 and bracket < i + 1: + bracket = text.find("]", i + 1) + close = bracket + if close != -1 and close < le and close > i + 1 and text.startswith(":", close + 1): + spans.append((ls, le)) + k += 1 + continue + m = k + 1 + while m < len(lines): + cs, ce = lines[m] + closer = _fence_of(text, cs, ce) + if closer is not None and closer[0] == opener[0] and closer[1] >= opener[1]: + j = closer[2] + while j < ce and text[j] in " \t\r": + j += 1 + if j == ce: + break + m += 1 + block_end = lines[m][1] if m < len(lines) else len(text) + if ls > seg_start: + outside.append((seg_start, ls)) + spans.append((ls, block_end)) + seg_start = block_end + k = m + 1 + if seg_start < len(text): + outside.append((seg_start, len(text))) + for s, e in outside: + # Inline code spans: every maximal backtick run, then each opening run to the next run of its length. + runs: list[tuple[int, int]] = [] + i = text.find("`", s) + while i != -1 and i < e: + j = i + while j < e and text[j] == "`": + j += 1 + runs.append((i, j - i)) + i = text.find("`", j) + by_length: dict[int, list[int]] = {} + for idx, (_, n) in enumerate(runs): + by_length.setdefault(n, []).append(idx) + cursor: dict[int, int] = {} + r = 0 + while r < len(runs): + at, n = runs[r] + positions = by_length[n] + c = cursor.get(n, 0) + while c < len(positions) and positions[c] <= r: + c += 1 + cursor[n] = c + if c < len(positions): + spans.append((at, runs[positions[c]][0] + n)) + r = positions[c] + 1 + else: + r += 1 + # HTML comments and tags. + gt = -2 + comment_end = -2 + opened = text.find("<", s) + while opened != -1 and opened < e: + if text.startswith("", opened + 4) + if comment_end != -1 and comment_end + 3 <= e: + spans.append((opened, comment_end + 3)) + opened = text.find("<", comment_end + 3) + continue + else: + name = opened + 2 if text.startswith("/", opened + 1) else opened + 1 + if name < len(text) and _ascii_letter(text[name]): + if gt != -1 and gt < name: + gt = text.find(">", name) + if gt != -1 and gt < e: + spans.append((opened, gt + 1)) + opened = text.find("<", gt + 1) + continue + opened = text.find("<", opened + 1) + return spans + + +def _passed_over(text: str, markdown: bool) -> list[tuple[int, int]]: + """The stretches no name is read in, in order, none overlapping: every address, every Markdown link, every bare + web address and autolink, and with ``markdown`` the Markdown stretches too.""" spans = _markdown_links(text) spans.extend((a.start, a.end) for a in find_ow_links(text)) + spans.extend(_web_addresses(text)) + if markdown: + spans.extend(_markdown_stretches(text)) spans.sort() out: list[tuple[int, int]] = [] for start, end in spans: @@ -146,14 +326,19 @@ def shared(self) -> tuple[NameShared[T], ...]: one of these, so a tool reads this to say why a name stays plain.""" return self._shared - def __call__(self, text: str) -> list[NameFound[T]]: + def __call__(self, text: str, *, markdown: bool = False) -> list[NameFound[T]]: + """The names found in ``text``. With ``markdown=True`` (since 0.154.0), for a text known to be Markdown, + nothing is read inside an inline code span, a fenced code block, an HTML tag or comment, or a reference + definition's line either.""" if not isinstance(text, str): raise TypeError("name_finder: text must be a string") + if not isinstance(markdown, bool): + raise TypeError(f"name_finder: markdown is True or False, not {type(markdown).__name__}") found: list[NameFound[T]] = [] root = self._root if not root.next: return found - spans = _passed_over(text) + spans = _passed_over(text, markdown) hay = simple_case_fold(text) if self._folded else text length = len(text) ascii_word = _ASCII_WORD @@ -249,8 +434,12 @@ def name_finder[T: Mapping[str, Any]]( - Where several names start at one place, the longest whole one is taken (``Iron Gate`` over ``Iron``). - A name two elements share is never answered, and nothing inside it is: the text stays plain there. One element listed twice, or found by two of its own names, is one element. - - Nothing is read inside an address (what ``find_ow_links`` finds) or a Markdown link ``[label](target)``, and - no name is read across the edge of one. + - Nothing is read inside an address (what ``find_ow_links`` finds), a Markdown link ``[label](target)``, a bare + web address (``https://…`` to the next white space or ``<``) or an autolink (````), and no name is + read across the edge of one. With ``find(text, markdown=True)`` (since 0.154.0), for a text known to be + Markdown, nothing is read inside an inline code span, a fenced code block, an HTML tag or comment, or a + reference definition's line either. An indented code block, a fence inside a quote or a list, and a + definition's title on the next line are read as prose. - Text is compared as written: no Unicode normalisation, and a space in a name matches that one space. ``find.shared`` lists every name two or more elements hold, each a ``NameShared`` (``name``, ``elements``): the diff --git a/tests/fixtures/name-finder.cases.json b/tests/fixtures/name-finder.cases.json index f4ec62f..1071fce 100644 --- a/tests/fixtures/name-finder.cases.json +++ b/tests/fixtures/name-finder.cases.json @@ -1,6 +1,6 @@ { - "about": "Cases for nameFinder (@onlyworlds/sdk) and name_finder (the onlyworlds Python package): the same bytes in both repositories. In a case, an element with a names list is found by those names (the names option), one without by its name; want lists what find(text) answers, in order, each the place of its first character and its length, both in code points, and the id of its element; shared lists what find.shared holds, in order, each a name as it is compared and the ids of the elements that hold it. A timed case is built from its recipe ({i} is the count from 0; a long name is its repeat, times over, and then its then) and must answer count names in under underMs milliseconds. Made by test/fixtures/generate-name-finder-cases.mjs in the SDK repository; every want is written by hand there.", - "version": 2, + "about": "Cases for nameFinder (@onlyworlds/sdk) and name_finder (the onlyworlds Python package): the same bytes in both repositories. In a case, an element with a names list is found by those names (the names option), one without by its name; want lists what find(text) answers, in order, each the place of its first character and its length, both in code points, and the id of its element; shared lists what find.shared holds, in order, each a name as it is compared and the ids of the elements that hold it. A case or a timed recipe with markdown: true is found with that option on the call (since version 3). A timed case is built from its recipe ({i} is the count from 0; a long name is its repeat, times over, and then its then) and must answer count names in under underMs milliseconds. Made by test/fixtures/generate-name-finder-cases.mjs in the SDK repository; every want is written by hand there.", + "version": 3, "underMs": 2000, "cases": [ { @@ -1698,6 +1698,535 @@ } ], "shared": [] + }, + { + "name": "a bare web address is passed over, its scheme in any ASCII case", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": "See https://kira.example/kira and HtTp://Kira.example/Kira, then Kira.", + "want": [ + { + "at": 65, + "length": 4, + "id": "k" + } + ], + "shared": [] + }, + { + "name": "a bare web address ends at white space or <", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": "https://x.example/KiraKira and https://x.example/Kira\u00a0Kira", + "want": [ + { + "at": 25, + "length": 4, + "id": "k" + }, + { + "at": 61, + "length": 4, + "id": "k" + } + ], + "shared": [] + }, + { + "name": "a scheme that is not http or https, or not in ASCII, is prose", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": "ftp://Kira.example and httpx://Kira and http\u017f://Kira", + "want": [ + { + "at": 6, + "length": 4, + "id": "k" + }, + { + "at": 31, + "length": 4, + "id": "k" + }, + { + "at": 48, + "length": 4, + "id": "k" + } + ], + "shared": [] + }, + { + "name": "an autolink is passed over, whatever its scheme", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": " and beside Kira", + "want": [ + { + "at": 64, + "length": 4, + "id": "k" + } + ], + "shared": [] + }, + { + "name": "what is not an autolink is prose", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": " and and and \n[r]: x \"Kira\"", + "want": [ + { + "at": 1, + "length": 4, + "id": "k" + }, + { + "at": 17, + "length": 4, + "id": "k" + }, + { + "at": 29, + "length": 4, + "id": "k" + }, + { + "at": 46, + "length": 4, + "id": "k" + } + ], + "shared": [] + }, + { + "name": "markdown: an inline code span is passed over, its backticks matched by length", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": "Say `Kira` or ``a `Kira` b`` to Kira.", + "want": [ + { + "at": 32, + "length": 4, + "id": "k" + } + ], + "shared": [], + "markdown": true + }, + { + "name": "markdown: backticks with no closing run of their length are prose", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": "a ``Kira` and `Kira", + "want": [ + { + "at": 4, + "length": 4, + "id": "k" + }, + { + "at": 15, + "length": 4, + "id": "k" + } + ], + "shared": [], + "markdown": true + }, + { + "name": "markdown: a fenced code block is passed over, closed or open to the end", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": "Before Kira\n```js\nKira\n```\nKira after\n~~~\nKira and Kira", + "want": [ + { + "at": 7, + "length": 4, + "id": "k" + }, + { + "at": 27, + "length": 4, + "id": "k" + } + ], + "shared": [], + "markdown": true + }, + { + "name": "markdown: a fence closes only with its own character, at least as long", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": "````\nKira\n```\nKira\n~~~~\nKira\n````\nKira", + "want": [ + { + "at": 34, + "length": 4, + "id": "k" + } + ], + "shared": [], + "markdown": true + }, + { + "name": "markdown: a closing fence holds nothing but spaces after it, a carriage return included", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": "```\r\nKira\r\n``` x\r\nKira\r\n``` \r\nKira", + "want": [ + { + "at": 31, + "length": 4, + "id": "k" + } + ], + "shared": [], + "markdown": true + }, + { + "name": "markdown: a backtick line that also holds a backtick is no fence", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": "```a`b Kira\nKira", + "want": [ + { + "at": 7, + "length": 4, + "id": "k" + }, + { + "at": 12, + "length": 4, + "id": "k" + } + ], + "shared": [], + "markdown": true + }, + { + "name": "markdown: a fence indented four spaces is no fence", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": " ```\nKira", + "want": [ + { + "at": 8, + "length": 4, + "id": "k" + } + ], + "shared": [], + "markdown": true + }, + { + "name": "markdown: a run that is no fence opens a code span to the next run of its length, across lines", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": "```a`b Kira\n ```\nKira", + "want": [ + { + "at": 20, + "length": 4, + "id": "k" + } + ], + "shared": [], + "markdown": true + }, + { + "name": "markdown: a fence up to three spaces in is a fence", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": " ~~~\nKira\n ~~~\nKira", + "want": [ + { + "at": 19, + "length": 4, + "id": "k" + } + ], + "shared": [], + "markdown": true + }, + { + "name": "markdown: backticks inside a fence open no span outside it", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": "```\n`\n```\nKira `x", + "want": [ + { + "at": 10, + "length": 4, + "id": "k" + } + ], + "shared": [], + "markdown": true + }, + { + "name": "markdown: an HTML tag and comment are passed over, the text between tags is read", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": "Kira
Kira", + "want": [ + { + "at": 19, + "length": 4, + "id": "k" + }, + { + "at": 51, + "length": 4, + "id": "k" + } + ], + "shared": [], + "markdown": true + }, + { + "name": "markdown: what is not a tag or a comment is prose", + "case": "exact", + "elements": [ + { + "id": "k", + "name": "Kira" + } + ], + "text": "< Kira> and <1 Kira> and