PMFlow/backend/app/utils/title.py

23 lines
643 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Title normalization helpers."""
from __future__ import annotations
import re
_SPLIT_PATTERN = re.compile(r"[,,。;;::!!??、|/]+")
_TRAIL_PATTERN = re.compile(r"[,,。;;::!!??、\-_ ]+$")
def compact_title(value: str, max_len: int = 24) -> str:
text = re.sub(r"\s+", " ", str(value or "")).strip()
if not text:
return ""
parts = [item.strip() for item in _SPLIT_PATTERN.split(text) if item.strip()]
title = parts[0] if parts else text
if len(title) > max_len:
title = title[:max_len].strip()
title = _TRAIL_PATTERN.sub("", title)
return title or text[:max_len].strip()