15 lines
583 B
Python
15 lines
583 B
Python
import re
|
|
|
|
|
|
def safe_path_component(name) -> str:
|
|
"""Sanitize an LLM-derived identifier for use as a filesystem path component.
|
|
|
|
Identifiers come from model output over user-supplied story text, so they may
|
|
contain separators or traversal sequences; keep word characters (including
|
|
CJK), dashes, dots and spaces, replace everything else, and strip leading
|
|
dots so the result can never escape or hide within the working directory.
|
|
"""
|
|
cleaned = re.sub(r"[^\w\-. ]", "_", str(name))
|
|
cleaned = cleaned.strip().lstrip(".")
|
|
return cleaned or "unnamed"
|