aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorA Farzat <a@farzat.xyz>2026-03-05 19:08:44 +0300
committerA Farzat <a@farzat.xyz>2026-03-06 08:40:21 +0300
commitc04ea0dc4b0f2c5ddf4c15b796f0439905116a34 (patch)
treeadff789d577228dd045016458f106d70f32572c0 /src
parent7ab28d6362b5b2b9b9d1a71a0420e6eeb53d76cd (diff)
downloadoreilly-epub-c04ea0dc4b0f2c5ddf4c15b796f0439905116a34.tar.gz
oreilly-epub-c04ea0dc4b0f2c5ddf4c15b796f0439905116a34.zip
Gather stylesheet file entries for each chapter
This prepares them to be added to their xhtml content.
Diffstat (limited to 'src')
-rw-r--r--src/epub.rs13
-rw-r--r--src/models.rs2
2 files changed, 13 insertions, 2 deletions
diff --git a/src/epub.rs b/src/epub.rs
index 406135b..946c9ee 100644
--- a/src/epub.rs
+++ b/src/epub.rs
@@ -91,6 +91,11 @@ pub fn create_epub_archive(
.iter()
.map(|e| (e.url.path(), &e.full_path))
.collect::<HashMap<_, _>>();
+ // Prepare url to local path mapping to insert related assets based on their URL.
+ let url_to_file = file_entries
+ .iter()
+ .map(|e| (&e.url, e))
+ .collect::<HashMap<_, _>>();
// Add the rest of the files according to file_entries.
let options: FileOptions<()> =
@@ -100,7 +105,13 @@ pub fn create_epub_archive(
let mut src_file = std::fs::File::open(entry.full_path.to_path(epub_root))?;
let mut buffer = Vec::new();
src_file.read_to_end(&mut buffer)?;
- if chapters.contains_key(&entry.ourn) {
+ if let Some(chapter) = chapters.get(&entry.ourn) {
+ let stylesheet_entries = 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 {
diff --git a/src/models.rs b/src/models.rs
index 77b3730..704ad84 100644
--- a/src/models.rs
+++ b/src/models.rs
@@ -30,7 +30,7 @@ pub struct Chapter {
/// Sub-model of Chapter - related_assets.
#[derive(Debug, Deserialize)]
-struct ChapRelAssets {
+pub struct ChapRelAssets {
pub stylesheets: Vec<Url>,
}