Added "Replace single newlines with spaces" and small improvements

This commit is contained in:
Deniz Şafak
2025-04-28 04:59:51 +03:00
parent a6bdc9c086
commit 4a7f1767b9
7 changed files with 35 additions and 11 deletions
+7 -3
View File
@@ -69,13 +69,17 @@ def get_user_config_path():
_sleep_procs = {"Darwin": None, "Linux": None} # Store sleep prevention processes
def clean_text(text):
def clean_text(text, *args, **kwargs):
# Load replace_single_newlines from config
cfg = load_config()
replace_single_newlines = cfg.get("replace_single_newlines", False)
# Trim spaces and tabs at the start and end of each line, preserving blank lines
text = "\n".join(line.strip() for line in text.splitlines())
# Standardize paragraph breaks (multiple newlines become exactly two) and trim overall whitespace
text = re.sub(r"\n{3,}", "\n\n", text).strip()
# Replace single newlines with spaces, but preserve double newlines
# text = re.sub(r"(?<!\n)\n(?!\n)", " ", text)
# Optionally replace single newlines with spaces, but preserve double newlines
if replace_single_newlines:
text = re.sub(r"(?<!\n)\n(?!\n)", " ", text)
# Collapse multiple spaces and tabs into a single space
text = re.sub(r"[ \t]+", " ", text)
return text