use anyhow::{anyhow, Result}; use std::collections::HashMap; use std::io::Read; use crate::types::{ConversionResult, Converter, StreamInfo}; use crate::utils::html_to_md::{html_to_markdown, normalize_tables_html}; const EXTENSIONS: &[&str] = &[".epub"]; const MIMETYPES: &[&str] = &[ "application/epub", "application/epub+zip", "application/x-epub+zip", ]; pub struct EpubConverter; // ── Converter impl ──────────────────────────────────────────────────────────── impl Converter for EpubConverter { fn name(&self) -> &'static str { "epub" } fn accepts(&self, info: &StreamInfo) -> bool { if let Some(ext) = &info.extension { if EXTENSIONS.contains(&ext.as_str()) { return true; } } if let Some(mime) = &info.mimetype { if MIMETYPES.iter().any(|m| mime.starts_with(m)) { return true; } } false } fn convert(&self, input: &[u8], _info: &StreamInfo) -> Result { let cursor = std::io::Cursor::new(input); let mut archive = zip::ZipArchive::new(cursor)?; // 1. META-INF/container.xml → OPF path let container_xml = read_zip_entry(&mut archive, "META-INF/container.xml") .ok_or_else(|| anyhow!("Invalid EPUB: missing container.xml"))?; let opf_path = parse_container_xml(&container_xml)?; // 2. Parse OPF let opf_xml = read_zip_entry(&mut archive, &opf_path) .ok_or_else(|| anyhow!("Invalid EPUB: missing content.opf"))?; let (metadata, manifest, spine_order) = parse_opf(&opf_xml)?; // 3. Base directory for resolving relative hrefs let base_path = match opf_path.rfind('/') { Some(pos) => opf_path[..pos].to_string(), None => String::new(), }; let mut sections: Vec = Vec::new(); // 4. Metadata block let meta_lines: Vec = [ ("Title", metadata.title.as_deref()), ("Authors", metadata.authors.as_deref()), ("Language", metadata.language.as_deref()), ("Publisher", metadata.publisher.as_deref()), ("Date", metadata.date.as_deref()), ("Description", metadata.description.as_deref()), ] .iter() .filter_map(|(key, value)| value.map(|v| format!("**{}:** {}", key, v))) .collect(); if !meta_lines.is_empty() { sections.push(meta_lines.join("\n")); } // 5. Convert spine chapters for idref in &spine_order { let href = match manifest.get(idref.as_str()) { Some(h) => h, None => continue, }; let file_path = if base_path.is_empty() { href.clone() } else { format!("{}/{}", base_path, href) }; let html = match read_zip_entry(&mut archive, &file_path) { Some(h) => h, None => continue, }; // Strip and blocks (case-insensitive, dotall). fn strip_script_style(html: &str) -> String { use crate::utils::strip_blocks::strip_tag_blocks; let s = strip_tag_blocks(html, ""); strip_tag_blocks(&s, "").into_owned() } // ── Tests ───────────────────────────────────────────────────────────────────── #[cfg(test)] mod tests { use super::*; use std::io::Write; use zip::write::FileOptions; /// Build a minimal in-memory EPUB archive. fn build_epub( container_xml: &str, opf_path: &str, opf_xml: &str, chapters: &[(&str, &str)], ) -> Vec { let buf = Vec::new(); let cursor = std::io::Cursor::new(buf); let mut zip = zip::ZipWriter::new(cursor); let opts: FileOptions<'_, ()> = FileOptions::default().compression_method(zip::CompressionMethod::Stored); zip.start_file("META-INF/container.xml", opts).unwrap(); zip.write_all(container_xml.as_bytes()).unwrap(); zip.start_file(opf_path, opts).unwrap(); zip.write_all(opf_xml.as_bytes()).unwrap(); for (path, content) in chapters { zip.start_file(*path, opts).unwrap(); zip.write_all(content.as_bytes()).unwrap(); } zip.finish().unwrap().into_inner() } fn simple_container(opf_path: &str) -> String { format!( r#" "#, opf_path ) } fn simple_opf(title: &str, authors: &[&str], chapters: &[(&str, &str)]) -> String { let creator_tags: String = authors .iter() .map(|a| format!(" {}\n", a)) .collect(); let item_tags: String = chapters .iter() .enumerate() .map(|(i, (href, _))| { format!( " \n", i, href ) }) .collect(); let itemref_tags: String = chapters .iter() .enumerate() .map(|(i, _)| format!(" \n", i)) .collect(); format!( r#" {} {} en {} {} "#, title, creator_tags, item_tags, itemref_tags ) } fn info_epub() -> StreamInfo { StreamInfo { extension: Some(".epub".to_string()), ..Default::default() } } // ── accepts() ──────────────────────────────────────────────────────────── #[test] fn accepts_epub_extension() { assert!(EpubConverter.accepts(&info_epub())); } #[test] fn accepts_epub_mimetype() { let info = StreamInfo { mimetype: Some("application/epub+zip".to_string()), ..Default::default() }; assert!(EpubConverter.accepts(&info)); } #[test] fn rejects_non_epub() { let info = StreamInfo { extension: Some(".pdf".to_string()), ..Default::default() }; assert!(!EpubConverter.accepts(&info)); } // ── Error cases ─────────────────────────────────────────────────────────── #[test] fn error_missing_container_xml() { let buf = Vec::new(); let cursor = std::io::Cursor::new(buf); let mut zip = zip::ZipWriter::new(cursor); let opts: FileOptions<'_, ()> = FileOptions::default().compression_method(zip::CompressionMethod::Stored); zip.start_file("dummy.txt", opts).unwrap(); zip.write_all(b"nothing").unwrap(); let epub_bytes = zip.finish().unwrap().into_inner(); let err = EpubConverter .convert(&epub_bytes, &info_epub()) .unwrap_err(); assert!( err.to_string().contains("missing container.xml"), "expected 'missing container.xml', got: {}", err ); } #[test] fn error_missing_rootfile_path() { let container = r#" "#; let buf = Vec::new(); let cursor = std::io::Cursor::new(buf); let mut zip = zip::ZipWriter::new(cursor); let opts: FileOptions<'_, ()> = FileOptions::default().compression_method(zip::CompressionMethod::Stored); zip.start_file("META-INF/container.xml", opts).unwrap(); zip.write_all(container.as_bytes()).unwrap(); let epub_bytes = zip.finish().unwrap().into_inner(); let err = EpubConverter .convert(&epub_bytes, &info_epub()) .unwrap_err(); assert!( err.to_string().contains("missing rootfile path"), "got: {}", err ); } #[test] fn error_missing_content_opf() { let container = simple_container("OEBPS/content.opf"); let buf = Vec::new(); let cursor = std::io::Cursor::new(buf); let mut zip = zip::ZipWriter::new(cursor); let opts: FileOptions<'_, ()> = FileOptions::default().compression_method(zip::CompressionMethod::Stored); zip.start_file("META-INF/container.xml", opts).unwrap(); zip.write_all(container.as_bytes()).unwrap(); let epub_bytes = zip.finish().unwrap().into_inner(); let err = EpubConverter .convert(&epub_bytes, &info_epub()) .unwrap_err(); assert!( err.to_string().contains("missing content.opf"), "got: {}", err ); } // ── Metadata ───────────────────────────────────────────────────────────── #[test] fn metadata_title_in_result() { let chapters = [("ch1.xhtml", "

Hello

")]; let container = simple_container("content.opf"); let opf = simple_opf("My Great Book", &["Alice"], &chapters); let epub = build_epub(&container, "content.opf", &opf, &chapters); let result = EpubConverter.convert(&epub, &info_epub()).unwrap(); assert_eq!(result.title.as_deref(), Some("My Great Book")); } #[test] fn metadata_block_contains_title_and_language() { let chapters = [("ch1.xhtml", "

Text

")]; let container = simple_container("content.opf"); let opf = simple_opf("Test Book", &[], &chapters); let epub = build_epub(&container, "content.opf", &opf, &chapters); let result = EpubConverter.convert(&epub, &info_epub()).unwrap(); assert!( result.markdown.contains("**Title:** Test Book"), "missing title in metadata block: {}", result.markdown ); assert!( result.markdown.contains("**Language:** en"), "missing language in metadata block: {}", result.markdown ); } #[test] fn metadata_multiple_authors_joined() { let chapters = [("ch1.xhtml", "

Hello

")]; let container = simple_container("content.opf"); let opf = simple_opf("Multi-Author", &["Alice", "Bob"], &chapters); let epub = build_epub(&container, "content.opf", &opf, &chapters); let result = EpubConverter.convert(&epub, &info_epub()).unwrap(); assert!( result.markdown.contains("**Authors:** Alice, Bob"), "authors not joined: {}", result.markdown ); } // ── Spine order & chapter content ───────────────────────────────────────── #[test] fn chapters_appear_in_spine_order() { let chapters = [ ( "ch1.xhtml", "

Chapter One Content

", ), ( "ch2.xhtml", "

Chapter Two Content

", ), ]; let container = simple_container("content.opf"); let opf = simple_opf("Ordered Book", &[], &chapters); let epub = build_epub(&container, "content.opf", &opf, &chapters); let result = EpubConverter.convert(&epub, &info_epub()).unwrap(); let pos_ch1 = result .markdown .find("Chapter One Content") .expect("ch1 text missing"); let pos_ch2 = result .markdown .find("Chapter Two Content") .expect("ch2 text missing"); assert!(pos_ch1 < pos_ch2, "ch1 should appear before ch2"); } #[test] fn chapter_text_present_in_output() { let chapters = [( "main.xhtml", "

Hello World

", )]; let container = simple_container("content.opf"); let opf = simple_opf("Simple", &[], &chapters); let epub = build_epub(&container, "content.opf", &opf, &chapters); let result = EpubConverter.convert(&epub, &info_epub()).unwrap(); assert!( result.markdown.contains("Hello World"), "chapter text missing: {}", result.markdown ); } #[test] fn script_and_style_stripped() { let chapters = [( "ch.xhtml", "

Visible text

", )]; let container = simple_container("content.opf"); let opf = simple_opf("Strip Test", &[], &chapters); let epub = build_epub(&container, "content.opf", &opf, &chapters); let result = EpubConverter.convert(&epub, &info_epub()).unwrap(); assert!( result.markdown.contains("Visible text"), "visible text missing: {}", result.markdown ); assert!( !result.markdown.contains("alert"), "script not stripped: {}", result.markdown ); assert!( !result.markdown.contains("color:red"), "style not stripped: {}", result.markdown ); } // ── OPF in subdirectory ─────────────────────────────────────────────────── #[test] fn opf_in_subdirectory_resolves_hrefs() { let container = simple_container("OEBPS/content.opf"); // OPF references chapters by relative href (no path prefix) let opf = r#" Sub Book en "#; let buf = Vec::new(); let cursor = std::io::Cursor::new(buf); let mut zip = zip::ZipWriter::new(cursor); let opts: FileOptions<'_, ()> = FileOptions::default().compression_method(zip::CompressionMethod::Stored); zip.start_file("META-INF/container.xml", opts).unwrap(); zip.write_all(container.as_bytes()).unwrap(); zip.start_file("OEBPS/content.opf", opts).unwrap(); zip.write_all(opf.as_bytes()).unwrap(); // Chapter lives at OEBPS/ch1.xhtml (resolved from base "OEBPS") zip.start_file("OEBPS/ch1.xhtml", opts).unwrap(); zip.write_all(b"

Sub-dir chapter

") .unwrap(); let epub_bytes = zip.finish().unwrap().into_inner(); let result = EpubConverter.convert(&epub_bytes, &info_epub()).unwrap(); assert!( result.markdown.contains("Sub-dir chapter"), "subdirectory chapter missing: {}", result.markdown ); } // ── Empty spine ─────────────────────────────────────────────────────────── #[test] fn empty_spine_returns_only_metadata() { let container = simple_container("content.opf"); let opf = r#" Empty Book en "#; let epub = build_epub(&container, "content.opf", opf, &[]); let result = EpubConverter.convert(&epub, &info_epub()).unwrap(); assert_eq!(result.title.as_deref(), Some("Empty Book")); assert!(result.markdown.contains("**Title:** Empty Book")); } }