"""Cross-reference slugs shared by the export backends.""" from __future__ import annotations import re _LABEL_RE = re.compile(r"[^\w\-]+", re.UNICODE) def sanitize_label(name: str | None, prefix: str = "obj", separator: str = ":") -> str: """ Turn a `%id` caption name into a target usable as an anchor. `separator` differs per backend: LaTeX `\\label` accepts a colon, an XML `NCName` does not. """ if not name: return prefix cleaned = _LABEL_RE.sub("-", name.strip()).strip("-") return f"{prefix}{separator}{cleaned or 'x'}"