""" Filename token sanitiser — ported line-for-line in behaviour from your existing Dokumenten_Beschrifter.html tool, so a name built here and a name built in the browser tool always match. """ import re UMLAUT_MAP = { "ä": "ae", "ö": "oe", "ü": "ue", "ß": "ss", } def sanitise(text: str) -> str: """Lowercase, fold umlauts, spaces -> hyphens, strip anything that isn't a-z/0-9/hyphen, collapse repeats, trim leading/trailing hyphens.""" if not text: return "" result = text.strip().lower() for src, repl in UMLAUT_MAP.items(): result = result.replace(src, repl) result = re.sub(r"\s+", "-", result) result = re.sub(r"[^a-z0-9\-]", "", result) result = re.sub(r"-+", "-", result) return result.strip("-") def build_filename(date: str, topic: str = "", subtopic: str = "", extra: str = "", more: str = "", version: str = "", ext: str = "") -> str: """Assemble the final filename from parts. Empty fields are skipped, so a file only needs as many fields filled in as makes sense for it — same rule as the HTML tool ("fill in as many or as few boxes as needed").""" parts = [sanitise(p) for p in (topic, subtopic, extra, more)] parts = [p for p in parts if p] if version and sanitise(version): parts.append(sanitise(version)) stem = "_".join([date] + parts) if parts else date return f"{stem}{ext}"