fix(sanitize): normalise filenames to NFC and keep combining marks

Remove only control characters in unicode mode. A mark after a zero width joiner or a digit was still dropped (#161). In ASCII mode a decomposed title now transliterates like the composed one.

Names change for titles with combining marks, compatibility ideographs or decomposed spellings, so an existing library can get a second folder.
This commit is contained in:
imSp4rky
2026-10-02 13:22:04 -06:00
parent 7b3794333f
commit 34eb94b55d
3 changed files with 59 additions and 46 deletions
+7 -24
View File
@@ -118,25 +118,6 @@ def import_module_by_path(path: Path) -> ModuleType:
return module
def _strip_hidden_characters(text: str, keep_letter_marks: bool) -> str:
"""Drop control characters (Cc) and hidden nonspacing marks (Mn).
Several scripts write vowels and tone marks as nonspacing marks on a base letter, e.g.
Thai "เป็นต่อ" or Devanagari "हिंदी"; dropping those changes the word ("เปนตอ"). With
``keep_letter_marks`` a mark that sits on a letter (or on another such mark) is kept, while
one with nothing to attach to, or on a symbol (a variation selector after an emoji), is not.
"""
kept: list[str] = []
for c in text:
category = unicodedata.category(c)
if category == "Cc":
continue
if category == "Mn" and not (keep_letter_marks and kept and unicodedata.category(kept[-1])[0] in "LM"):
continue
kept.append(c)
return "".join(kept)
def sanitize_filename(filename: str, spacer: str = ".", unicode: Optional[bool] = None) -> str:
"""
Sanitise a string to be filename safe.
@@ -147,14 +128,15 @@ def sanitize_filename(filename: str, spacer: str = ".", unicode: Optional[bool]
Set `unicode_filenames: true` in config to preserve the characters of the
original language (for example Korean, Japanese, or Chinese) instead of
transliterating them to ASCII equivalents. Pass ``unicode`` to decide that
for one call instead of the config value.
for one call instead of the config value. The name goes to NFC first, so
composed and decomposed spellings of a title give the same name.
"""
keep_unicode = config.unicode_filenames if unicode is None else unicode
if not keep_unicode:
filename = unicodedata.normalize("NFC", filename)
if not (config.unicode_filenames if unicode is None else unicode):
filename = unidecode(filename)
filename = re.sub(r"\[\(+", "[", filename)
filename = re.sub(r"\)+\]", "]", filename)
filename = _strip_hidden_characters(filename, keep_letter_marks=keep_unicode)
filename = "".join(c for c in filename if unicodedata.category(c) != "Cc")
filename = filename.replace("/", " & ").replace(";", " & ") # e.g. multi-episode filenames
if spacer == ".":
filename = re.sub(r" - ", spacer, filename) # title separators to spacer (avoids .-. pattern)
@@ -163,7 +145,8 @@ def sanitize_filename(filename: str, spacer: str = ".", unicode: Optional[bool]
filename = re.sub(rf"[{spacer}]{{2,}}", spacer, filename) # remove extra neighbouring (spacer)s
filename = filename.strip(" .") # strip leading and trailing spaces and dots for OS path safety
return filename
# A removed character can leave a letter next to a mark it did not have before.
return unicodedata.normalize("NFC", filename)
def is_close_match(language: Union[str, Language], languages: Sequence[Union[str, Language, None]]) -> bool: