Added a better logic for detecting chapters from the epub

This commit is contained in:
Deniz Şafak
2025-07-15 09:05:18 +03:00
parent a35bea9053
commit b9c3fd7c6c
2 changed files with 9 additions and 11 deletions
+1
View File
@@ -1,6 +1,7 @@
# v1.1.2 (pre-release) # v1.1.2 (pre-release)
- Now you can play the audio files while they are processing. - Now you can play the audio files while they are processing.
- While generating audio files, now it directly writes to audio file instead of RAM, which significantly reduces memory usage and prevents memory outage issues. - While generating audio files, now it directly writes to audio file instead of RAM, which significantly reduces memory usage and prevents memory outage issues.
- Added a better logic for detecting chapters from the epub, mentioned by @jefro108 in #33
- Potential fix for #37 and #38, where the program was becoming slow while processing large files. - Potential fix for #37 and #38, where the program was becoming slow while processing large files.
- Fixed `Open folder` and `Open file` buttons in the queue manager GUI. - Fixed `Open folder` and `Open file` buttons in the queue manager GUI.
- Improvements in code structure. - Improvements in code structure.
+9 -12
View File
@@ -22,7 +22,7 @@ from PyQt5.QtWidgets import (
QLabel, QLabel,
) )
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from utils import clean_text, calculate_text_length from abogen.utils import clean_text, calculate_text_length
import os import os
import logging # Add logging import logging # Add logging
import urllib.parse import urllib.parse
@@ -240,20 +240,17 @@ class HandlerDialog(QDialog):
tag.decompose() tag.decompose()
text = clean_text(soup.get_text()).strip() text = clean_text(soup.get_text()).strip()
if text: if text:
# Use doc_href as the identifier
self.content_texts[doc_href] = text self.content_texts[doc_href] = text
self.content_lengths[doc_href] = len(text) self.content_lengths[doc_href] = len(text)
# Create a synthetic TOC entry
title = f"Chapter {i+1}: {doc_href}"
# Try to get a better title from <h1> or <title>
h1 = soup.find("h1")
if h1 and h1.get_text(strip=True):
title = h1.get_text(strip=True)
else:
title_tag = soup.find("title")
if title_tag and title_tag.get_text(strip=True):
title = title_tag.get_text(strip=True)
title = None
if soup.title and soup.title.string:
title = soup.title.string.strip()
elif (h1 := soup.find("h1")) and h1.get_text(strip=True):
title = h1.get_text(strip=True)
if not title:
title = f"Untitled Chapter {i+1}"
synthetic_toc.append( synthetic_toc.append(
(epub.Link(doc_href, title, doc_href), []) (epub.Link(doc_href, title, doc_href), [])
) # Wrap in tuple and empty list for compatibility ) # Wrap in tuple and empty list for compatibility