aboutsummaryrefslogtreecommitdiff
path: root/src/epub.rs
diff options
context:
space:
mode:
authorA Farzat <a@farzat.xyz>2026-03-07 09:04:00 +0300
committerA Farzat <a@farzat.xyz>2026-03-07 09:07:38 +0300
commitc98b360d611c37315c9c5330089a1d91dbb2021c (patch)
treed190740bb24df7aaafe09e914aa2f1314dd70988 /src/epub.rs
parent7e24d0cd5b671d06383466baf89c340023421d86 (diff)
downloadoreilly-epub-c98b360d611c37315c9c5330089a1d91dbb2021c.tar.gz
oreilly-epub-c98b360d611c37315c9c5330089a1d91dbb2021c.zip
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.
Diffstat (limited to 'src/epub.rs')
-rw-r--r--src/epub.rs33
1 files changed, 24 insertions, 9 deletions
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::<Vec<_>>();
- 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!(
+ "<link rel=\"stylesheet\" type=\"{}\" href=\"{}\" />\n",
+ e.media_type,
+ chapter_dir.relative(&e.full_path)
+ )
+ })
+ .collect::<String>();
+ 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)?;