From 4a7f1767b91be7d9b0b273c47a716c0fc83571bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deniz=20=C5=9Eafak?= Date: Mon, 28 Apr 2025 04:59:51 +0300 Subject: [PATCH] Added "Replace single newlines with spaces" and small improvements --- CHANGELOG.md | 3 ++- README.md | 7 ++++++- abogen/constants.py | 2 +- abogen/conversion.py | 4 ++++ abogen/gui.py | 15 +++++++++++++++ abogen/utils.py | 10 +++++++--- demo/README.md | 5 ----- 7 files changed, 35 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce96a61..cc4b24f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,2 @@ -- Fixed "'utf-8' codec can't decode byte" error, mentioned in #3 by @nigelp \ No newline at end of file +- Added "Replace single newlines with spaces" option in the menu. This can be useful for texts that have imaginary line breaks (for example LICENSE files). +- Fixed "'utf-8' codec can't decode byte" error, mentioned in #3 by @nigelp diff --git a/README.md b/README.md index 3c7d103..d272f4c 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ sudo dnf install espeak-ng # Install abogen pip install abogen ``` -> If you get "No matching distribution found" error, try installing it on supported Python (3.10 to 3.12). You can use [pyenv](https://github.com/pyenv/pyenv) to manage multiple Python versions easily in Linux. +> If you get "No matching distribution found" error, try installing it on supported Python (3.10 to 3.12). You can use [pyenv](https://github.com/pyenv/pyenv) to manage multiple Python versions easily in Linux. Watch this [video](https://www.youtube.com/watch?v=MVyb-nI4KyI) by NetworkChuck for a quick guide. Then simply run by typing: @@ -117,6 +117,11 @@ keep-open=yes --audio-device=openal --sub-margin-x=235 --sub-pos=60 +# --- Audio Quality --- +audio-spdif=ac3,dts,eac3,truehd,dts-hd +audio-channels=auto +audio-samplerate=48000 +volume-max=200 ``` ## `Similar Projects` diff --git a/abogen/constants.py b/abogen/constants.py index e657063..e407ae6 100644 --- a/abogen/constants.py +++ b/abogen/constants.py @@ -1,4 +1,4 @@ -from utils import get_version, get_user_config_path +from utils import get_version # Program Information PROGRAM_NAME = "abogen" diff --git a/abogen/conversion.py b/abogen/conversion.py index f14c1e3..de12f65 100644 --- a/abogen/conversion.py +++ b/abogen/conversion.py @@ -170,6 +170,10 @@ class ConversionThread(QThread): self.log_updated.emit(f"- Subtitle mode: {self.subtitle_mode}") self.log_updated.emit(f"- Output format: {self.output_format}") self.log_updated.emit(f"- Save option: {self.save_option}") + if self.replace_single_newlines: + self.log_updated.emit( + f"- Replace single newlines: Yes" + ) # Display save_chapters_separately flag if it's set if hasattr(self, "save_chapters_separately"): diff --git a/abogen/gui.py b/abogen/gui.py index c8e30ed..d6fe50f 100644 --- a/abogen/gui.py +++ b/abogen/gui.py @@ -467,6 +467,7 @@ class abogen(QWidget): self.use_gpu = self.config.get( "use_gpu", True ) # Load GPU setting with default True + self.replace_single_newlines = self.config.get("replace_single_newlines", False) self._pending_close_event = None self.gpu_ok = False # Initialize GPU availability status @@ -1059,6 +1060,8 @@ class abogen(QWidget): self.conversion_thread.file_size_str = file_size_str # Pass max_subtitle_words from config self.conversion_thread.max_subtitle_words = self.max_subtitle_words + # Pass replace_single_newlines setting + self.conversion_thread.replace_single_newlines = self.replace_single_newlines # Pass chapter count for EPUB or PDF files if self.selected_file_type in ["epub", "pdf"] and hasattr( self, "selected_chapters" @@ -1499,6 +1502,13 @@ class abogen(QWidget): """Show a dropdown menu for settings options.""" menu = QMenu(self) + # Add replace single newlines option + newline_action = QAction("Replace single newlines with spaces", self) + newline_action.setCheckable(True) + newline_action.setChecked(self.replace_single_newlines) + newline_action.triggered.connect(self.toggle_replace_single_newlines) + menu.addAction(newline_action) + # Add max words per subtitle option max_words_action = QAction("Configure max words per subtitle", self) max_words_action.triggered.connect(self.set_max_subtitle_words) @@ -1545,6 +1555,11 @@ class abogen(QWidget): menu.exec_(self.settings_btn.mapToGlobal(QPoint(0, self.settings_btn.height()))) + def toggle_replace_single_newlines(self, checked): + self.replace_single_newlines = checked + self.config["replace_single_newlines"] = checked + save_config(self.config) + def reveal_config_in_explorer(self): """Open the configuration file location in file explorer.""" from utils import get_user_config_path diff --git a/abogen/utils.py b/abogen/utils.py index 93f3393..4a6d596 100644 --- a/abogen/utils.py +++ b/abogen/utils.py @@ -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"(?