PageId (UUIDv7) and PageTitle validate on construction; frontmatter is split losslessly, read as text-only YAML without anchors or duplicate keys, and written canonically. All page errors live in domain/errors. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
76 lines
2.6 KiB
Python
76 lines
2.6 KiB
Python
"""Page identity: id (UUIDv7) and title."""
|
|
|
|
import re
|
|
import unicodedata
|
|
from dataclasses import dataclass
|
|
|
|
from chemenu.domain.errors import InvalidPageIdError, InvalidTitleError
|
|
|
|
_ID_RE = re.compile(r"[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}")
|
|
_FORBIDDEN_CHARS = frozenset('<>:"/\\|?*[]#^')
|
|
_LINE_BREAKS = frozenset("\u2028\u2029")
|
|
_MAX_TITLE_BYTES = 252
|
|
# Invisible format characters (Cf) are forbidden, except the joiners that some
|
|
# scripts and emoji sequences need and the tag characters of subdivision flags.
|
|
_ALLOWED_FORMAT = frozenset("\u200c\u200d")
|
|
_TAGS = range(0xE0020, 0xE0080)
|
|
_DEVICE_NAMES = frozenset(
|
|
{"CON", "PRN", "AUX", "NUL", "CONIN$", "CONOUT$"}
|
|
| {f"{name}{digit}" for name in ("COM", "LPT") for digit in "123456789¹²³"}
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class PageId:
|
|
"""A lowercase UUIDv7 with hyphens."""
|
|
|
|
value: str
|
|
|
|
def __post_init__(self) -> None:
|
|
if not _ID_RE.fullmatch(self.value):
|
|
raise InvalidPageIdError(f"invalid page id: {self.value!r}")
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class PageTitle:
|
|
"""A page title that is safe as a file name and as a single title line."""
|
|
|
|
value: str
|
|
|
|
def __post_init__(self) -> None:
|
|
reason = _title_problem(self.value)
|
|
if reason:
|
|
raise InvalidTitleError(f"invalid title {self.value!r}: {reason}")
|
|
|
|
|
|
def _invisible(char: str) -> bool:
|
|
return (
|
|
unicodedata.category(char) == "Cf"
|
|
and char not in _ALLOWED_FORMAT
|
|
and ord(char) not in _TAGS
|
|
)
|
|
|
|
|
|
def _title_problem(title: str) -> str | None:
|
|
if not title:
|
|
return "empty"
|
|
if any(0xD800 <= ord(c) <= 0xDFFF for c in title):
|
|
return "contains surrogates"
|
|
if any(c in _FORBIDDEN_CHARS for c in title) or "%%" in title:
|
|
return "contains a forbidden character"
|
|
if any(unicodedata.category(c) == "Cc" or c in _LINE_BREAKS for c in title):
|
|
return "contains a control character or line break"
|
|
if any(_invisible(c) for c in title):
|
|
return "contains an invisible character"
|
|
if title.casefold().endswith(".md"):
|
|
return "ends with .md, which Obsidian reads as the file extension"
|
|
if title != title.strip() or title.startswith(".") or title.endswith("."):
|
|
return "starts or ends with whitespace or a dot"
|
|
if title.split(".", 1)[0].rstrip(" ").upper() in _DEVICE_NAMES:
|
|
return "is a reserved device name"
|
|
if not unicodedata.is_normalized("NFC", title):
|
|
return "is not NFC"
|
|
if len(title.encode("utf-8")) > _MAX_TITLE_BYTES:
|
|
return f"longer than {_MAX_TITLE_BYTES} bytes"
|
|
return None
|