ebook-rs 0.5.1

Pure Rust EPUB parser and reader engine with CFI location mapping, full-text search, font de-obfuscation, and WASM/HTTP reader support.
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
484
use crate::archive::{EpubArchive, resolve_relative_path};
use crate::layout::AssetDeliveryStrategy;
use base64::Engine;
use serde::{Deserialize, Serialize};

/// Loaded and processed EPUB section ready for reading.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Section {
    pub index: usize,
    pub idref: String,
    pub href: String,
    pub full_path: String,
    pub raw_html: String,
    pub processed_html: String,
    pub plain_text: String,
    pub plain_text_lower: String, // P4: Pre-computed lowercased text for zero-alloc search
    pub char_count: usize,
    pub viewport_width: Option<f64>,  // FXL viewport target width
    pub viewport_height: Option<f64>, // FXL viewport target height
}

impl Section {
    /// Create and process a section from archive content using default Base64 inlining.
    pub fn new(
        index: usize,
        idref: String,
        href: String,
        full_path: String,
        archive: &EpubArchive,
    ) -> Result<Self, String> {
        Self::new_with_strategy(
            index,
            idref,
            href,
            full_path,
            archive,
            &AssetDeliveryStrategy::InlinedBase64,
        )
    }

    /// Create and process a section using a specific asset delivery strategy (Base64 vs Resource Stream).
    pub fn new_with_strategy(
        index: usize,
        idref: String,
        href: String,
        full_path: String,
        archive: &EpubArchive,
        strategy: &AssetDeliveryStrategy,
    ) -> Result<Self, String> {
        let raw_html = archive.read_string(&full_path)?;
        let plain_text = extract_plain_text(&raw_html);
        let plain_text_lower = plain_text.to_lowercase();
        let char_count = plain_text.chars().count();
        let processed_html =
            process_section_resources_with_strategy(&raw_html, &full_path, archive, strategy);
        let (viewport_width, viewport_height) = parse_viewport_meta(&raw_html);

        Ok(Self {
            index,
            idref,
            href,
            full_path,
            raw_html,
            processed_html,
            plain_text,
            plain_text_lower,
            char_count,
            viewport_width,
            viewport_height,
        })
    }

    /// Strip embedded <script> tags, inline event attributes (on*="..."), and javascript: URIs.
    pub fn strip_script_content(&mut self) {
        self.processed_html = sanitize_html_scripts(&self.processed_html);
    }

    /// Extract footnotes and endnotes for popup previewing.
    pub fn extract_footnotes(&self) -> Vec<crate::footnote::Footnote> {
        crate::footnote::parse_footnotes_from_html(&self.raw_html)
    }

    /// Calculate structural NLP Reading Analytics (word count, WPM reading time, difficulty score, keywords).
    pub fn analytics(&self) -> crate::analytics::ReadingAnalytics {
        crate::analytics::ReadingAnalytics::analyze_text(&self.plain_text)
    }

    /// Calculate virtual reflow page breaks for this section using default or custom paginator bounds.
    pub fn paginate(
        &self,
        paginator: Option<&crate::paginator::ReflowPaginator>,
    ) -> crate::paginator::SectionPageMap {
        let default_paginator = crate::paginator::ReflowPaginator::default();
        let active_paginator = paginator.unwrap_or(&default_paginator);
        active_paginator.paginate_section(self)
    }
}

/// Extract clean plain text from HTML content by stripping tags, styles, and scripts.
/// B2 Fix: Multibyte UTF-8 aware extraction.
pub fn extract_plain_text(html: &str) -> String {
    let mut in_tag = false;
    let mut text = String::with_capacity(html.len());
    let mut skipping_tag: Option<&'static str> = None;

    let lower = html.to_lowercase();
    let lower_bytes = lower.as_bytes();
    let html_bytes = html.as_bytes();
    let len = html_bytes.len();

    let mut i = 0;
    while i < len {
        if !in_tag && html_bytes[i] == b'<' {
            in_tag = true;
            let slice = &lower_bytes[i..];
            if skipping_tag.is_none() {
                if slice.starts_with(b"<style") {
                    skipping_tag = Some("style");
                } else if slice.starts_with(b"<script") {
                    skipping_tag = Some("script");
                }
            } else if let Some(tag) = skipping_tag {
                if (tag == "style" && slice.starts_with(b"</style"))
                    || (tag == "script" && slice.starts_with(b"</script"))
                    || slice.starts_with(b"<p")
                    || slice.starts_with(b"<div")
                    || slice.starts_with(b"<body")
                    || slice.starts_with(b"<h")
                    || slice.starts_with(b"<section")
                {
                    skipping_tag = None;
                }
            }
            text.push(' ');
            i += 1;
            continue;
        }

        if in_tag {
            if html_bytes[i] == b'>' {
                in_tag = false;
            }
            i += 1;
            continue;
        }

        if skipping_tag.is_none() {
            if let Some(ch) = html[i..].chars().next() {
                text.push(ch);
                i += ch.len_utf8();
            } else {
                i += 1;
            }
        } else {
            i += 1;
        }
    }

    // Collapse multiple whitespace spaces into single space
    let mut result = String::with_capacity(text.len());
    let mut prev_space = false;
    for ch in text.chars() {
        if ch.is_whitespace() {
            if !prev_space {
                result.push(' ');
                prev_space = true;
            }
        } else {
            result.push(ch);
            prev_space = false;
        }
    }

    result.trim().to_string()
}

/// Process XHTML/HTML resources: Inline images, styles, and fonts as base64 Data URIs.
pub fn process_section_resources(html: &str, section_path: &str, archive: &EpubArchive) -> String {
    process_section_resources_with_strategy(
        html,
        section_path,
        archive,
        &AssetDeliveryStrategy::InlinedBase64,
    )
}

/// Process XHTML/HTML resources according to specified delivery strategy (Base64 vs ResourceStream).
pub fn process_section_resources_with_strategy(
    html: &str,
    section_path: &str,
    archive: &EpubArchive,
    strategy: &AssetDeliveryStrategy,
) -> String {
    let section_dir = if let Some(idx) = section_path.rfind('/') {
        &section_path[..idx]
    } else {
        ""
    };

    let mut output = html.to_string();

    // Replace image src attributes
    let img_src_regex = regex_find_attr(html, "src");
    for (orig_attr, src_val) in img_src_regex {
        if src_val.starts_with("data:")
            || src_val.starts_with("http://")
            || src_val.starts_with("https://")
        {
            continue;
        }
        let res_path = resolve_relative_path(section_dir, &src_val);

        match strategy {
            AssetDeliveryStrategy::InlinedBase64 => {
                if let Ok(bytes) = archive.read_bytes(&res_path) {
                    let mime = EpubArchive::get_mime_type(&res_path);
                    let b64 = base64::engine::general_purpose::STANDARD.encode(&bytes);
                    let data_uri = format!("data:{};base64,{}", mime, b64);
                    output = output.replace(&orig_attr, &format!("src=\"{}\"", data_uri));
                }
            }
            AssetDeliveryStrategy::ResourceStream => {
                let stream_url = format!("resource/{}", res_path);
                output = output.replace(&orig_attr, &format!("src=\"{}\"", stream_url));
            }
        }
    }

    // Replace ONLY <link href="*.css"> stylesheet links
    let css_href_regex = regex_find_link_css(html);
    for (orig_attr, href_val) in css_href_regex {
        let res_path = resolve_relative_path(section_dir, &href_val);
        match strategy {
            AssetDeliveryStrategy::InlinedBase64 => {
                if let Ok(css_text) = archive.read_string(&res_path) {
                    let processed_css = process_css_resources(&css_text, &res_path, archive);
                    let b64 =
                        base64::engine::general_purpose::STANDARD.encode(processed_css.as_bytes());
                    let data_uri = format!("data:text/css;base64,{}", b64);
                    output = output.replace(&orig_attr, &format!("href=\"{}\"", data_uri));
                }
            }
            AssetDeliveryStrategy::ResourceStream => {
                let stream_url = format!("resource/{}", res_path);
                output = output.replace(&orig_attr, &format!("href=\"{}\"", stream_url));
            }
        }
    }

    output
}

/// Helper to find ONLY `<link ... href="...">` tags for CSS inlining (E5 Fix).
fn regex_find_link_css(html: &str) -> Vec<(String, String)> {
    let mut list = Vec::new();
    let lower = html.to_lowercase();
    let mut search_idx = 0;

    while let Some(link_idx) = lower[search_idx..].find("<link") {
        let abs_link = search_idx + link_idx;
        if let Some(close_idx) = html[abs_link..].find('>') {
            let tag_str = &html[abs_link..=abs_link + close_idx];
            if let Some((orig_href, val)) = extract_attr(tag_str, "href") {
                let val_lower = val.to_lowercase();
                if val_lower.contains(".css")
                    || tag_str.to_lowercase().contains("rel=\"stylesheet\"")
                {
                    list.push((orig_href, val));
                }
            }
            search_idx = abs_link + close_idx + 1;
        } else {
            break;
        }
    }

    list
}

fn extract_attr(tag_str: &str, attr: &str) -> Option<(String, String)> {
    let tag_lower = tag_str.to_lowercase();
    let pattern1 = format!(" {}=\"", attr);
    let pattern2 = format!("<{}=\"", attr);

    let start = tag_lower
        .find(&pattern1)
        .map(|s| s + 1)
        .or_else(|| tag_lower.find(&pattern2).map(|s| s + 1))?;
    let val_start = start + attr.len() + 2;
    let end = tag_str[val_start..].find('"')?;
    let val = &tag_str[val_start..val_start + end];
    let orig = &tag_str[start..=val_start + end];
    Some((orig.to_string(), val.to_string()))
}

/// Helper to find attributes like `src="..."` or `href="..."` with word boundary checks (E4 Fix).
fn regex_find_attr(html: &str, attr: &str) -> Vec<(String, String)> {
    let mut list = Vec::new();
    let lower = html.to_lowercase();
    let pattern1 = format!(" {}=\"", attr);
    let pattern2 = format!("<{}=\"", attr);
    let mut search_idx = 0;

    while search_idx < html.len() {
        let p1_match = lower[search_idx..]
            .find(&pattern1)
            .map(|s| search_idx + s + 1);
        let p2_match = lower[search_idx..]
            .find(&pattern2)
            .map(|s| search_idx + s + 1);

        let abs_start = match (p1_match, p2_match) {
            (Some(m1), Some(m2)) => m1.min(m2),
            (Some(m1), None) => m1,
            (None, Some(m2)) => m2,
            (None, None) => break,
        };

        let val_start = abs_start + attr.len() + 2;
        if let Some(end) = html[val_start..].find('"') {
            let abs_end = val_start + end;
            let val = &html[val_start..abs_end];
            let orig = &html[abs_start..=abs_end];
            list.push((orig.to_string(), val.to_string()));
            search_idx = abs_end + 1;
        } else {
            break;
        }
    }

    list
}

/// Inline fonts and images inside CSS stylesheet content.
fn process_css_resources(css: &str, css_path: &str, archive: &EpubArchive) -> String {
    let css_dir = if let Some(idx) = css_path.rfind('/') {
        &css_path[..idx]
    } else {
        ""
    };

    let mut replacements = Vec::new();
    let mut search_idx = 0;

    while let Some(url_idx) = css[search_idx..].find("url(") {
        let abs_url = search_idx + url_idx;
        let val_start = abs_url + 4;
        if let Some(close_idx) = css[val_start..].find(')') {
            let abs_close = val_start + close_idx;
            let raw_url = css[val_start..abs_close]
                .trim()
                .trim_matches('\'')
                .trim_matches('"');
            if !raw_url.starts_with("data:") && !raw_url.starts_with("http") {
                let res_path = resolve_relative_path(css_dir, raw_url);
                if let Ok(bytes) = archive.read_bytes(&res_path) {
                    let mime = EpubArchive::get_mime_type(&res_path);
                    let b64 = base64::engine::general_purpose::STANDARD.encode(&bytes);
                    let data_uri = format!("data:{};base64,{}", mime, b64);
                    let target_str = css[abs_url..=abs_close].to_string();
                    let replacement = format!("url(\"{}\")", data_uri);
                    replacements.push((target_str, replacement));
                }
            }
            search_idx = abs_close + 1;
        } else {
            break;
        }
    }

    let mut output = css.to_string();
    for (target, repl) in replacements {
        output = output.replace(&target, &repl);
    }
    output
}

/// Parse `<meta name="viewport" content="width=..., height=...">` from section HTML.
pub fn parse_viewport_meta(html: &str) -> (Option<f64>, Option<f64>) {
    let lower = html.to_lowercase();
    let mut search_idx = 0;

    while let Some(idx) = lower[search_idx..].find("<meta") {
        let abs_idx = search_idx + idx;
        if let Some(close) = html[abs_idx..].find('>') {
            let tag = &html[abs_idx..=abs_idx + close];
            if tag.to_lowercase().contains("viewport") {
                if let Some((_, content)) = extract_attr(tag, "content") {
                    let mut width = None;
                    let mut height = None;

                    for pair in content.split(',') {
                        let parts: Vec<&str> = pair.split('=').map(|s| s.trim()).collect();
                        if parts.len() == 2 {
                            if parts[0].eq_ignore_ascii_case("width") {
                                width = parts[1].parse::<f64>().ok();
                            } else if parts[0].eq_ignore_ascii_case("height") {
                                height = parts[1].parse::<f64>().ok();
                            }
                        }
                    }
                    if width.is_some() || height.is_some() {
                        return (width, height);
                    }
                }
            }
            search_idx = abs_idx + close + 1;
        } else {
            break;
        }
    }

    (None, None)
}

/// Sanitize HTML content by stripping <script> blocks, inline event handlers, and javascript: links (B1 & B2 Fix).
pub fn sanitize_html_scripts(html: &str) -> String {
    let mut output = String::with_capacity(html.len());
    let lower = html.to_lowercase();
    let lower_bytes = lower.as_bytes();
    let len = html.len();

    let mut i = 0;
    let mut in_script = false;

    while i < len {
        if !in_script && lower_bytes[i..].starts_with(b"<script") {
            in_script = true;
            i += 7;
            continue;
        }

        if in_script {
            if lower_bytes[i..].starts_with(b"</script>") {
                in_script = false;
                i += 9;
            } else {
                i += 1;
            }
            continue;
        }

        // B1 Fix: Safely push UTF-8 character instead of casting u8 as char
        if let Some(ch) = html[i..].chars().next() {
            output.push(ch);
            i += ch.len_utf8();
        } else {
            i += 1;
        }
    }

    // B2 Fix: Strip inline event attributes like onload=, onclick= with char-boundary safety
    let mut sanitized = String::with_capacity(output.len());
    let mut search_idx = 0;
    let out_lower = output.to_lowercase();

    while search_idx < output.len() {
        if let Some(on_idx) = out_lower[search_idx..].find(" on") {
            let abs_on = search_idx + on_idx;
            sanitized.push_str(&output[search_idx..abs_on]);

            if let Some(eq_idx) = out_lower[abs_on..].find('=') {
                let attr_name = out_lower[abs_on + 1..abs_on + eq_idx].trim();
                if attr_name.starts_with("on") {
                    let val_start = abs_on + eq_idx + 1;
                    if val_start < output.len() && output.as_bytes()[val_start] == b'"' {
                        if let Some(end_quote) = output[val_start + 1..].find('"') {
                            search_idx = val_start + 1 + end_quote + 1;
                            continue;
                        }
                    }
                }
            }
            sanitized.push_str(" on");
            search_idx = abs_on + 3;
        } else {
            sanitized.push_str(&output[search_idx..]);
            break;
        }
    }

    // Strip href="javascript:..."
    sanitized.replace("href=\"javascript:", "href=\"#disabled_js:")
}