Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (`<scheme:…>`) 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`.
Expand Down
2 changes: 1 addition & 1 deletion src/onlyworlds/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.153.0"
__version__ = "0.154.0"
201 changes: 195 additions & 6 deletions src/onlyworlds/name_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from __future__ import annotations

import re
import unicodedata
from collections.abc import Callable, Iterable, Mapping
from dataclasses import dataclass
Expand Down Expand Up @@ -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 (``<!--`` to the first ``-->``), 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):
if comment_end != -1 and comment_end < opened + 4:
comment_end = text.find("-->", 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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 (``<scheme:…>``), 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
Expand Down
Loading
Loading