ebook-rs 0.16.3

Pure Rust multi-format eBook engine (EPUB 2/3, MOBI, AZW3, KFX, FB2, LIT, CBZ, PDF, ODT, DOCX, RTF, TXT, MD) featuring Mozilla UniFFI, Readium CFI/LCP, SpeechSynthesis TTS sync, CJK vertical/RTL reflow, EPUB3 optimizer, AI RAG BM25 chunking, zero-copy search, Zstd caching, Python/WASM bindings, and native MCP server.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use crate::archive::EpubArchive;
use crate::book::Book;
use crate::section::Section;
use ahash::{AHashMap, AHashSet};
use serde::{Deserialize, Serialize};

/// Configuration options for the EPUB 3 optimizer and minifier engine.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EpubOptimizerOptions {
    /// Minify HTML/XHTML structures (strip non-essential whitespace, HTML comments).
    pub minify_html: bool,
    /// Minify CSS style sheets (collapse whitespace, strip CSS comments, simplify rules).
    pub minify_css: bool,
    /// Purge unreferenced CSS rules across all section documents.
    pub purge_unused_css: bool,
    /// Deduplicate identical fonts and binary images using SHA-256 fingerprinting.
    pub deduplicate_assets: bool,
}

impl Default for EpubOptimizerOptions {
    fn default() -> Self {
        Self {
            minify_html: true,
            minify_css: true,
            purge_unused_css: true,
            deduplicate_assets: true,
        }
    }
}

/// Statistics and report returned after running EPUB optimization.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct OptimizationReport {
    pub original_size_bytes: usize,
    pub optimized_size_bytes: usize,
    pub saved_bytes: usize,
    pub deduplicated_assets_count: usize,
    pub purged_css_rules_count: usize,
}

/// EPUB 3 Lossless Optimizer and Minifier engine.
pub struct EpubOptimizer;

impl EpubOptimizer {
    /// Optimize a loaded `Book` instance in-place with the specified options.
    pub fn optimize(book: &mut Book, options: &EpubOptimizerOptions) -> OptimizationReport {
        let mut report = OptimizationReport::default();

        // 1. Gather all referenced classes, IDs, and element tag names for CSS Purging
        let mut used_classes = AHashSet::new();
        let mut used_ids = AHashSet::new();
        let mut used_tags = AHashSet::new();

        for section in &book.sections {
            extract_dom_identifiers(
                &section.raw_html,
                &mut used_classes,
                &mut used_ids,
                &mut used_tags,
            );
            extract_dom_identifiers(
                &section.processed_html,
                &mut used_classes,
                &mut used_ids,
                &mut used_tags,
            );
        }

        // 2. HTML Minification
        if options.minify_html {
            for section in &mut book.sections {
                section.raw_html = Self::minify_html(&section.raw_html);
                section.processed_html = Self::minify_html(&section.processed_html);
            }
        }

        // 3. Asset & Font Deduplication
        if options.deduplicate_assets {
            let dedup_count = Self::deduplicate_assets(&mut book.archive, &mut book.sections);
            report.deduplicated_assets_count = dedup_count;
        }

        // 4. CSS Minification & Purging
        if options.minify_css || options.purge_unused_css {
            let css_files: Vec<String> = book
                .archive
                .list_files()
                .into_iter()
                .filter(|p| p.to_lowercase().ends_with(".css"))
                .collect();

            for css_path in css_files {
                if let Ok(css_content) = book.archive.read_string(&css_path) {
                    let mut optimized_css = css_content;
                    if options.purge_unused_css {
                        let (purged, count) =
                            Self::purge_css(&optimized_css, &used_classes, &used_ids, &used_tags);
                        optimized_css = purged;
                        report.purged_css_rules_count += count;
                    }
                    if options.minify_css {
                        optimized_css = Self::minify_css(&optimized_css);
                    }
                    book.archive.insert(&css_path, optimized_css.into_bytes());
                }
            }
        }

        book.invalidate_render_cache();
        report
    }

    /// Minify HTML content by removing HTML comments and collapsing redundant whitespace.
    pub fn minify_html(html: &str) -> String {
        let mut out = String::with_capacity(html.len());
        let mut in_tag = false;
        let mut in_quote: Option<char> = None;
        let mut in_pre = false;
        let bytes = html.as_bytes();
        let mut i = 0;

        while i < bytes.len() {
            if !in_tag && html[i..].starts_with("<!--") {
                if let Some(end_idx) = html[i + 4..].find("-->") {
                    i += 4 + end_idx + 3;
                } else {
                    break;
                }
                continue;
            }

            if !in_tag && bytes[i] == b'<' {
                in_tag = true;
                in_quote = None;
                let rest = &html[i..];
                if rest.starts_with("<pre")
                    || rest.starts_with("<code")
                    || rest.starts_with("<script")
                    || rest.starts_with("<style")
                    || rest.starts_with("<textarea")
                {
                    in_pre = true;
                } else if rest.starts_with("</pre>")
                    || rest.starts_with("</code>")
                    || rest.starts_with("</script>")
                    || rest.starts_with("</style>")
                    || rest.starts_with("</textarea>")
                {
                    in_pre = false;
                }
            } else if in_tag {
                if let Some(q) = in_quote {
                    if html.as_bytes()[i] == q as u8 {
                        in_quote = None;
                    }
                } else if bytes[i] == b'"' || bytes[i] == b'\'' {
                    in_quote = Some(bytes[i] as char);
                } else if bytes[i] == b'>' {
                    in_tag = false;
                    in_quote = None;
                }
            }

            let ch = html[i..].chars().next().unwrap_or(' ');
            let ch_len = ch.len_utf8();

            if !in_pre && in_quote.is_none() && ch.is_whitespace() {
                if !out.ends_with(' ')
                    && !out.ends_with('>')
                    && !out.ends_with('<')
                    && !out.is_empty()
                {
                    out.push(' ');
                }
                i += ch_len;
                continue;
            }

            if in_tag && in_quote.is_none() && (ch == '>' || ch == '/') && out.ends_with(' ') {
                out.pop();
            }

            out.push(ch);
            i += ch_len;
        }

        out.trim().to_string()
    }

    /// Minify CSS style sheets by removing CSS comments and collapsing whitespace.
    pub fn minify_css(css: &str) -> String {
        let mut out = String::with_capacity(css.len());
        let bytes = css.as_bytes();
        let mut i = 0;

        while i < bytes.len() {
            if css[i..].starts_with("/*") {
                if let Some(end_idx) = css[i + 2..].find("*/") {
                    i += 2 + end_idx + 2;
                } else {
                    break;
                }
                continue;
            }

            let ch = css[i..].chars().next().unwrap_or(' ');
            let ch_len = ch.len_utf8();

            if ch.is_whitespace() {
                if !out.ends_with(' ')
                    && !out.ends_with('{')
                    && !out.ends_with('}')
                    && !out.ends_with(':')
                    && !out.ends_with(';')
                    && !out.ends_with(',')
                    && !out.is_empty()
                {
                    out.push(' ');
                }
                i += ch_len;
                continue;
            }

            if (ch == '{' || ch == '}' || ch == ':' || ch == ';' || ch == ',') && out.ends_with(' ')
            {
                out.pop();
            }

            out.push(ch);
            i += ch_len;
        }

        out.trim().to_string()
    }

    /// Purge unreferenced CSS rules based on observed classes, IDs, and HTML tags.
    pub fn purge_css(
        css: &str,
        used_classes: &AHashSet<String>,
        used_ids: &AHashSet<String>,
        used_tags: &AHashSet<String>,
    ) -> (String, usize) {
        let mut purged = String::with_capacity(css.len());
        let mut purged_count = 0;

        let mut i = 0;
        let bytes = css.as_bytes();
        let len = bytes.len();

        while i < len {
            // Skip leading whitespace
            while i < len && bytes[i].is_ascii_whitespace() {
                i += 1;
            }
            if i >= len {
                break;
            }

            // Find matching statement/block with proper brace nesting
            let start = i;
            let mut brace_depth = 0;
            let mut found_open = false;

            while i < len {
                if bytes[i] == b'{' {
                    brace_depth += 1;
                    found_open = true;
                } else if bytes[i] == b'}' {
                    if brace_depth > 0 {
                        brace_depth -= 1;
                    }
                    if found_open && brace_depth == 0 {
                        i += 1;
                        break;
                    }
                } else if bytes[i] == b';' && !found_open {
                    i += 1;
                    break;
                }
                i += 1;
            }

            let block = &css[start..i];
            let trimmed = block.trim();
            if trimmed.is_empty() {
                continue;
            }

            if let Some(open_brace) = trimmed.find('{') {
                let selector_part = trimmed[..open_brace].trim();

                // At-rules (@media, @supports, @font-face, @keyframes, @import, @charset) are preserved intact
                if selector_part.starts_with('@') {
                    purged.push_str(trimmed);
                    purged.push('\n');
                    continue;
                }

                let body_part = if trimmed.ends_with('}') {
                    &trimmed[open_brace + 1..trimmed.len() - 1]
                } else {
                    &trimmed[open_brace + 1..]
                };

                // Check selector components
                let mut is_used = false;
                for selector in selector_part.split(',') {
                    let clean_sel = selector.trim();
                    if clean_sel == "*"
                        || clean_sel == ":root"
                        || clean_sel == "html"
                        || clean_sel == "body"
                    {
                        is_used = true;
                        break;
                    }

                    // Check if class selector (.my-class)
                    if let Some(dot_idx) = clean_sel.find('.') {
                        let class_name: String = clean_sel[dot_idx + 1..]
                            .chars()
                            .take_while(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_')
                            .collect();
                        if used_classes.contains(&class_name) {
                            is_used = true;
                            break;
                        }
                    }

                    // Check if ID selector (#my-id)
                    if let Some(hash_idx) = clean_sel.find('#') {
                        let id_name: String = clean_sel[hash_idx + 1..]
                            .chars()
                            .take_while(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_')
                            .collect();
                        if used_ids.contains(&id_name) {
                            is_used = true;
                            break;
                        }
                    }

                    // Check tag selector
                    let tag_name: String = clean_sel
                        .chars()
                        .take_while(|c| c.is_ascii_alphabetic())
                        .collect();
                    if !tag_name.is_empty() && used_tags.contains(&tag_name) {
                        is_used = true;
                        break;
                    }
                }

                if is_used {
                    purged.push_str(selector_part);
                    purged.push('{');
                    purged.push_str(body_part.trim());
                    purged.push_str("}\n");
                } else {
                    purged_count += 1;
                }
            } else {
                purged.push_str(trimmed);
                purged.push('\n');
            }
        }

        (purged, purged_count)
    }

    /// Deduplicate identical images and fonts across the EPUB archive.
    pub fn deduplicate_assets(archive: &mut EpubArchive, sections: &mut [Section]) -> usize {
        let mut hash_to_canonical_path: AHashMap<String, String> = AHashMap::new();
        let mut path_redirects: AHashMap<String, String> = AHashMap::new();
        let mut dedup_count = 0;

        let all_files = archive.list_files();
        for path in all_files {
            let lower = path.to_lowercase();
            if lower.ends_with(".png")
                || lower.ends_with(".jpg")
                || lower.ends_with(".jpeg")
                || lower.ends_with(".webp")
                || lower.ends_with(".gif")
                || lower.ends_with(".svg")
                || lower.ends_with(".ttf")
                || lower.ends_with(".otf")
                || lower.ends_with(".woff")
                || lower.ends_with(".woff2")
            {
                if let Ok(bytes) = archive.read_bytes(&path) {
                    let hash = sha1_smol::Sha1::from(&bytes).digest().to_string();
                    if let Some(canonical) = hash_to_canonical_path.get(&hash) {
                        path_redirects.insert(path.clone(), canonical.clone());
                        dedup_count += 1;
                    } else {
                        hash_to_canonical_path.insert(hash, path.clone());
                    }
                }
            }
        }

        // Remap section HTML src / href references and remove duplicate files from archive
        if !path_redirects.is_empty() {
            for section in sections {
                for (old_path, new_path) in &path_redirects {
                    let old_filename = old_path.split('/').next_back().unwrap_or(old_path);
                    let new_filename = new_path.split('/').next_back().unwrap_or(new_path);
                    section.raw_html = section.raw_html.replace(old_filename, new_filename);
                    section.processed_html =
                        section.processed_html.replace(old_filename, new_filename);
                }
            }
            for old_path in path_redirects.keys() {
                archive.remove(old_path);
            }
        }

        dedup_count
    }
}

/// Helper to extract class names, ID attributes, and tag names from HTML.
fn extract_dom_identifiers(
    html: &str,
    classes: &mut AHashSet<String>,
    ids: &mut AHashSet<String>,
    tags: &mut AHashSet<String>,
) {
    let mut i = 0;
    let bytes = html.as_bytes();

    while i < bytes.len() {
        if let Some(open_tag) = memchr::memchr(b'<', &bytes[i..]) {
            let abs_open = i + open_tag;
            let rest = &html[abs_open + 1..];

            // Extract tag name
            let tag: String = rest
                .chars()
                .take_while(|c| c.is_ascii_alphanumeric() || *c == '-')
                .collect();
            if !tag.is_empty() && !tag.starts_with('/') && !tag.starts_with('!') {
                tags.insert(tag.to_lowercase());
            }

            if let Some(close_tag) = rest.find('>') {
                let tag_body = &rest[..close_tag];

                // Extract class="..."
                if let Some(class_pos) = tag_body.find("class=") {
                    let after = &tag_body[class_pos + 6..].trim_start();
                    let quote = after.chars().next().unwrap_or('"');
                    if (quote == '"' || quote == '\'') && after.len() > 1 {
                        if let Some(end_q) = after[1..].find(quote) {
                            let class_str = &after[1..=end_q];
                            for cls in class_str.split_whitespace() {
                                classes.insert(cls.to_string());
                            }
                        }
                    }
                }

                // Extract id="..."
                if let Some(id_pos) = tag_body.find("id=") {
                    let after = &tag_body[id_pos + 3..].trim_start();
                    let quote = after.chars().next().unwrap_or('"');
                    if (quote == '"' || quote == '\'') && after.len() > 1 {
                        if let Some(end_q) = after[1..].find(quote) {
                            let id_str = &after[1..=end_q];
                            ids.insert(id_str.trim().to_string());
                        }
                    }
                }

                i = abs_open + 1 + close_tag + 1;
            } else {
                break;
            }
        } else {
            break;
        }
    }
}