From c98b360d611c37315c9c5330089a1d91dbb2021c Mon Sep 17 00:00:00 2001 From: A Farzat Date: Sat, 7 Mar 2026 09:04:00 +0300 Subject: Fix chapter contents to proper xml Change resource URLs using attribute matching (only img src for now, should add more later). Add respective stylesheets to chapters. --- src/epub.rs | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'src/epub.rs') diff --git a/src/epub.rs b/src/epub.rs index 946c9ee..5383251 100644 --- a/src/epub.rs +++ b/src/epub.rs @@ -1,4 +1,7 @@ -use crate::models::{Chapter, FileEntry}; +use crate::{ + models::{Chapter, EpubResponse, FileEntry}, + xml::build_epub_chapter, +}; use anyhow::{Context, Result}; use relative_path::{RelativePath, RelativePathBuf}; use reqwest::Client; @@ -65,6 +68,7 @@ pub async fn download_all_files( /// Creates the EPUB archive (creates zip and includes all files in it). pub fn create_epub_archive( + epub_data: &EpubResponse, epub_root: &Path, output_epub: &Path, file_entries: &[FileEntry], @@ -106,18 +110,29 @@ pub fn create_epub_archive( let mut buffer = Vec::new(); src_file.read_to_end(&mut buffer)?; if let Some(chapter) = chapters.get(&entry.ourn) { - let stylesheet_entries = chapter + let chapter_dir = entry.full_path.parent().unwrap_or(RelativePath::new("")); + let stylesheet_links = chapter .related_assets .stylesheets .iter() .filter_map(|u| url_to_file.get(u)) - .collect::>(); - let mut html = String::from_utf8(buffer)?; - let chapter_dir = entry.full_path.parent().unwrap_or(RelativePath::new("")); - for (url_path, local_path) in &url_path_to_local { - let rel_path = chapter_dir.relative(local_path); - html = html.replace(url_path, rel_path.as_str()); - } + .map(|e| { + format!( + "\n", + e.media_type, + chapter_dir.relative(&e.full_path) + ) + }) + .collect::(); + let html = String::from_utf8(buffer)?; + let html = build_epub_chapter( + epub_data, + chapter, + chapter_dir, + &html, + &stylesheet_links, + &url_path_to_local, + )?; zip.write_all(html.as_bytes())?; } else { zip.write_all(&buffer)?; -- cgit v1.2.3-70-g09d2