23 lines
512 B
Python
23 lines
512 B
Python
"""XML escape helpers for FODT output."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from xml.sax.saxutils import escape as _xml_escape
|
|
|
|
|
|
def escape_text(s: str) -> str:
|
|
if not s:
|
|
return ""
|
|
return _xml_escape(s, {"\"": """, "'": "'"})
|
|
|
|
|
|
_LABEL_RE = re.compile(r"[^\w\-]+", re.UNICODE)
|
|
|
|
|
|
def xml_id(name: str | None, prefix: str = "obj") -> str:
|
|
if not name:
|
|
return prefix
|
|
cleaned = _LABEL_RE.sub("-", name.strip()).strip("-")
|
|
return f"{prefix}-{cleaned or 'x'}"
|