Merge pull request #213 from chiragjangiir/fix/pasted-text-metadata-194

fix: preserve pasted text title in chapter metadata
This commit is contained in:
Deniz Şafak authored and GitHub committed 2026-10-17 08:41:21 -07:00
commit 654ec6d6fd
2 files changed
+272 -2

No files matched your search

+22 -2
View File
@@ -204,7 +204,7 @@ def wizard_upload():
@main_bp.route("/wizard/text", methods=["POST"])
def wizard_text():
text = request.form.get("text", "").strip()
title = request.form.get("title", "").strip() or "Pasted Text"
form_title = request.form.get("title", "").strip() or request.form.get("meta_title", "").strip()
if not text:
if wants_wizard_json():
@@ -221,8 +221,28 @@ def wizard_text():
try:
extraction = extract_from_path(file_path)
# Override title since text extraction might not find one
placeholder = file_path.stem
extracted_title = extraction.metadata.get("title")
if form_title:
title = form_title
elif extracted_title and extracted_title != placeholder:
title = extracted_title
else:
title = "Pasted Text"
extraction.metadata["title"] = title
# Pasted text is stored as a randomly named temp file, and plain-text
# extraction falls back to the file stem whenever it finds no headings.
# That leaves the random id as the chapter title and album name, where
# it gets narrated aloud and written into the m4b tags.
if extraction.metadata.get("album") == placeholder:
extraction.metadata["album"] = title
for chapter in extraction.chapters:
if chapter.title == placeholder:
chapter.title = title
result = build_pending_job_from_extraction(
stored_path=file_path,
+250
View File
@@ -0,0 +1,250 @@
from io import BytesIO
import re
from pathlib import Path
from abogen.webui.app import create_app
UUID_PATTERN = re.compile(r"^[0-9a-f]{32}$")
def _create_test_app(tmp_path: Path):
return create_app(
{
"TESTING": True,
"SECRET_KEY": "test-key",
"OUTPUT_FOLDER": str(tmp_path / "output"),
"UPLOAD_FOLDER": str(tmp_path / "uploads"),
}
)
def test_pasted_text_without_headings_uses_user_supplied_title(tmp_path: Path):
app = _create_test_app(tmp_path)
service = app.extensions["conversion_service"]
with app.test_client() as client:
response = client.post(
"/wizard/text?format=json",
data={
"text": "This is a single block of pasted text with no headings at all.",
"title": "Paste Test",
},
)
assert response.status_code == 200
pending_id = response.get_json()["pending_id"]
pending = service.get_pending_job(pending_id)
assert pending is not None
# Title tag and album tag use user-supplied title
assert pending.metadata_tags.get("title") == "Paste Test"
assert pending.metadata_tags.get("album") == "Paste Test"
# Chapter 1 title uses user-supplied title
assert len(pending.chapters) == 1
assert pending.chapters[0]["title"] == "Paste Test"
# Assert no UUID leak
assert not UUID_PATTERN.match(str(pending.metadata_tags.get("album", "")))
assert not UUID_PATTERN.match(str(pending.chapters[0]["title"]))
def test_pasted_text_without_user_title_defaults_to_pasted_text(tmp_path: Path):
app = _create_test_app(tmp_path)
service = app.extensions["conversion_service"]
with app.test_client() as client:
response = client.post(
"/wizard/text?format=json",
data={
"text": "Pasted text with no title provided in form.",
"title": "",
},
)
assert response.status_code == 200
pending_id = response.get_json()["pending_id"]
pending = service.get_pending_job(pending_id)
assert pending is not None
assert pending.metadata_tags.get("title") == "Pasted Text"
assert pending.metadata_tags.get("album") == "Pasted Text"
assert pending.chapters[0]["title"] == "Pasted Text"
assert not UUID_PATTERN.match(str(pending.metadata_tags.get("album", "")))
assert not UUID_PATTERN.match(str(pending.chapters[0]["title"]))
def test_pasted_text_preserves_extracted_title_when_user_title_omitted(tmp_path: Path):
app = _create_test_app(tmp_path)
service = app.extensions["conversion_service"]
text_with_title = (
"<<METADATA_TITLE:The Secret Chronicle>>\n"
"A fascinating journey begins here without any explicit chapter markers."
)
with app.test_client() as client:
response = client.post(
"/wizard/text?format=json",
data={
"text": text_with_title,
"title": "",
},
)
assert response.status_code == 200
pending_id = response.get_json()["pending_id"]
pending = service.get_pending_job(pending_id)
assert pending is not None
assert pending.metadata_tags.get("title") == "The Secret Chronicle"
assert pending.metadata_tags.get("album") == "The Secret Chronicle"
assert pending.chapters[0]["title"] == "The Secret Chronicle"
assert not UUID_PATTERN.match(str(pending.metadata_tags.get("album", "")))
assert not UUID_PATTERN.match(str(pending.chapters[0]["title"]))
def test_pasted_text_preserves_explicit_album_metadata(tmp_path: Path):
app = _create_test_app(tmp_path)
service = app.extensions["conversion_service"]
text_with_album = (
"<<METADATA_TITLE:Episode Four>>\n"
"<<METADATA_ALBUM:Season One Anthology>>\n"
"Just the script lines for episode four."
)
with app.test_client() as client:
response = client.post(
"/wizard/text?format=json",
data={
"text": text_with_album,
"title": "Episode 4 Override",
},
)
assert response.status_code == 200
pending_id = response.get_json()["pending_id"]
pending = service.get_pending_job(pending_id)
assert pending is not None
# User-supplied title overrides document title
assert pending.metadata_tags.get("title") == "Episode 4 Override"
# Explicit album tag from text is NOT clobbered
assert pending.metadata_tags.get("album") == "Season One Anthology"
# Single unheaded chapter fallback receives the user-supplied title
assert pending.chapters[0]["title"] == "Episode 4 Override"
def test_pasted_text_multi_chapter_preserves_valid_chapter_titles(tmp_path: Path):
app = _create_test_app(tmp_path)
service = app.extensions["conversion_service"]
multi_chapter_text = (
"<<CHAPTER_MARKER:Chapter 1: The Departure>>\n"
"They set out into the misty morning.\n"
"<<CHAPTER_MARKER:Chapter 2: The Crossing>>\n"
"The river was wider than anticipated.\n"
"<<CHAPTER_MARKER:Chapter 3: The Arrival>>\n"
"They finally reached the ancient fortress.\n"
)
with app.test_client() as client:
response = client.post(
"/wizard/text?format=json",
data={
"text": multi_chapter_text,
"title": "The Long Journey",
},
)
assert response.status_code == 200
pending_id = response.get_json()["pending_id"]
pending = service.get_pending_job(pending_id)
assert pending is not None
assert pending.metadata_tags.get("title") == "The Long Journey"
assert pending.metadata_tags.get("album") == "The Long Journey"
# All valid chapter titles must be preserved
assert len(pending.chapters) == 3
assert pending.chapters[0]["title"] == "Chapter 1: The Departure"
assert pending.chapters[1]["title"] == "Chapter 2: The Crossing"
assert pending.chapters[2]["title"] == "Chapter 3: The Arrival"
def test_pasted_text_with_unheaded_preface_and_headings(tmp_path: Path):
app = _create_test_app(tmp_path)
service = app.extensions["conversion_service"]
text_with_preface = (
"Some preface remarks before chapter one begins.\n"
"<<CHAPTER_MARKER:Chapter 1: Dawn>>\n"
"The morning sun broke through the clouds.\n"
)
with app.test_client() as client:
response = client.post(
"/wizard/text?format=json",
data={
"text": text_with_preface,
"title": "Novel with Preface",
},
)
assert response.status_code == 200
pending_id = response.get_json()["pending_id"]
pending = service.get_pending_job(pending_id)
assert pending is not None
assert len(pending.chapters) == 2
# The first unheaded segment fallback receives the novel title instead of UUID
assert pending.chapters[0]["title"] == "Novel with Preface"
# The second segment retains its matched heading
assert pending.chapters[1]["title"] == "Chapter 1: Dawn"
def test_pasted_text_accepts_meta_title_form_field(tmp_path: Path):
app = _create_test_app(tmp_path)
service = app.extensions["conversion_service"]
with app.test_client() as client:
response = client.post(
"/wizard/text?format=json",
data={
"text": "Plain text submission using meta_title key.",
"meta_title": "Title From Meta Field",
},
)
assert response.status_code == 200
pending_id = response.get_json()["pending_id"]
pending = service.get_pending_job(pending_id)
assert pending is not None
assert pending.metadata_tags.get("title") == "Title From Meta Field"
assert pending.metadata_tags.get("album") == "Title From Meta Field"
assert pending.chapters[0]["title"] == "Title From Meta Field"
def test_ordinary_file_upload_unaffected(tmp_path: Path):
app = _create_test_app(tmp_path)
service = app.extensions["conversion_service"]
sample_content = b"Simple uploaded text without headings."
file_data = {
"source_file": (BytesIO(sample_content), "my_uploaded_file.txt"),
}
with app.test_client() as client:
response = client.post(
"/wizard/upload?format=json",
data=file_data,
content_type="multipart/form-data",
)
assert response.status_code == 200
pending_id = response.get_json()["pending_id"]
pending = service.get_pending_job(pending_id)
assert pending is not None
assert pending.original_filename == "my_uploaded_file.txt"