boko 0.3.0

Fast ebook conversion library for EPUB and Kindle formats
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//! HTML to IR compiler pipeline.
//!
//! This module transforms HTML content with CSS stylesheets into the
//! normalized IR (Intermediate Representation) format.
//!
//! # Example
//!
//! ```
//! use boko::dom::{compile_html, Stylesheet, Origin};
//!
//! let html = "<html><body><p>Hello, World!</p></body></html>";
//! let css = "p { color: blue; }";
//!
//! let author_css = Stylesheet::parse(css);
//! let chapter = compile_html(html, &[(author_css, Origin::Author)]);
//!
//! // The chapter now contains normalized IR nodes
//! assert!(chapter.node_count() > 1);
//! ```

mod arena;
pub mod element_ref;
pub mod optimize;
mod role_map;
mod transform;
mod tree_sink;

pub use arena::{ArenaDom, ArenaNode, ArenaNodeData, ArenaNodeId};
pub use element_ref::{BokoSelectors, ElementRef};
pub use optimize::optimize;
pub use transform::user_agent_stylesheet;

// Re-export style types for convenience
pub use crate::style::{Declaration, Origin, Specificity, Stylesheet};

use html5ever::driver::ParseOpts;
use html5ever::tendril::TendrilSink;

use crate::model::Chapter;
use tree_sink::ArenaSink;

/// Check if content looks like XHTML/XML based on the first ~500 bytes.
///
/// Checks for XML declaration (`<?xml`) or XHTML namespace (`xmlns=`).
fn looks_like_xhtml(html: &str) -> bool {
    let end = html.floor_char_boundary(500);
    let prefix = &html[..end];
    prefix.contains("<?xml") || prefix.contains("xmlns=")
}

/// Parse HTML/XHTML into an ArenaDom.
///
/// Uses xml5ever for XHTML content (detected by `<?xml` or `xmlns=` in the
/// first 500 bytes), falling back to html5ever for plain HTML. This correctly
/// handles self-closing tags like `<script/>` which are valid in XHTML but
/// cause content loss with HTML5 parsing.
fn parse_dom(html: &str) -> ArenaDom {
    if looks_like_xhtml(html) {
        let sink = ArenaSink::new();
        let result =
            xml5ever::driver::parse_document(sink, xml5ever::driver::XmlParseOpts::default())
                .from_utf8()
                .one(html.as_bytes());
        let dom = result.into_dom();

        // Verify xml5ever produced a usable tree (has a body with children).
        // Fall through to html5ever if not.
        if let Some(body) = dom.find_by_tag("body")
            && dom.children(body).next().is_some()
        {
            return dom;
        }
    }

    // Fallback: html5ever (permissive HTML5 parser)
    let sink = ArenaSink::new();
    let result = html5ever::parse_document(sink, ParseOpts::default())
        .from_utf8()
        .one(html.as_bytes());
    result.into_dom()
}

/// Compile HTML content to IR.
///
/// This is the main entry point for the compiler pipeline.
/// Automatically detects XHTML and uses the appropriate parser.
///
/// # Arguments
///
/// * `html` - The HTML content to parse
/// * `stylesheets` - Author stylesheets with their origins (user-agent stylesheet is added automatically)
///
/// # Returns
///
/// A `Chapter` containing the normalized content tree.
///
/// # Example
///
/// ```
/// use boko::dom::{compile_html, Stylesheet, Origin};
///
/// let html = "<p class='intro'>Welcome!</p>";
/// let css = ".intro { font-weight: bold; }";
///
/// let author = Stylesheet::parse(css);
/// let chapter = compile_html(html, &[(author, Origin::Author)]);
/// ```
pub fn compile_html(html: &str, author_stylesheets: &[(Stylesheet, Origin)]) -> Chapter {
    let dom = parse_dom(html);

    // Build complete stylesheet list with UA defaults
    let ua = transform::user_agent_stylesheet();
    let mut all_stylesheets: Vec<(Stylesheet, Origin)> = vec![(ua, Origin::UserAgent)];
    for (sheet, origin) in author_stylesheets {
        all_stylesheets.push((sheet.clone(), *origin));
    }

    // Transform to IR
    let mut chapter = transform::transform(&dom, &all_stylesheets);

    // Optimize: merge adjacent text nodes with identical styles
    optimize::optimize(&mut chapter);

    chapter
}

/// Compile HTML bytes to IR.
///
/// Convenience wrapper that handles byte-to-string conversion with proper
/// encoding detection. Supports UTF-8, Windows-1252, and other encodings
/// via the XML declaration.
pub fn compile_html_bytes(html: &[u8], author_stylesheets: &[(Stylesheet, Origin)]) -> Chapter {
    // Extract encoding from XML declaration if present
    let hint_encoding = crate::util::extract_xml_encoding(html);

    // Decode with proper encoding support
    let html_str = crate::util::decode_text(html, hint_encoding);

    compile_html(&html_str, author_stylesheets)
}

/// Extract stylesheet links and inline styles from HTML.
///
/// Returns a list of (href, media) tuples for linked stylesheets,
/// and a list of inline CSS content.
pub fn extract_stylesheets(html: &str) -> (Vec<String>, Vec<String>) {
    let dom = parse_dom(html);

    let mut linked = Vec::new();
    let mut inline = Vec::new();

    // Find all link[rel=stylesheet] and style elements
    let mut stack = vec![dom.document()];
    while let Some(id) = stack.pop() {
        if let Some(node) = dom.get(id)
            && let ArenaNodeData::Element { name, attrs, .. } = &node.data
        {
            match name.local.as_ref() {
                "link" => {
                    let is_stylesheet = attrs
                        .iter()
                        .any(|a| a.name.local.as_ref() == "rel" && a.value == "stylesheet");
                    if is_stylesheet
                        && let Some(href) = attrs
                            .iter()
                            .find(|a| a.name.local.as_ref() == "href")
                            .map(|a| a.value.clone())
                    {
                        linked.push(href);
                    }
                }
                "style" => {
                    // Collect text content
                    let mut text = String::new();
                    for child in dom.children(id) {
                        if let Some(t) = dom.text_content(child) {
                            text.push_str(t);
                        }
                    }
                    if !text.trim().is_empty() {
                        inline.push(text);
                    }
                }
                _ => {}
            }
        }

        // Add children to stack (reverse for left-to-right order)
        let children: Vec<_> = dom.children(id).collect();
        for child in children.into_iter().rev() {
            stack.push(child);
        }
    }

    (linked, inline)
}

/// Resolve a relative path against a base path logically (no filesystem access).
///
/// This is used to canonicalize paths like `../images/photo.jpg` relative to
/// a chapter file like `OEBPS/text/ch1.html` into an absolute archive path
/// like `OEBPS/images/photo.jpg`.
///
/// # Arguments
///
/// * `base` - The base file path (e.g., `OEBPS/text/ch1.html`)
/// * `rel` - The relative path to resolve (e.g., `../images/photo.jpg`)
///
/// # Returns
///
/// The resolved path as a string, normalized with forward slashes.
///
/// # Examples
///
/// ```
/// use boko::dom::resolve_path;
///
/// assert_eq!(
///     resolve_path("OEBPS/text/ch1.html", "../images/logo.png"),
///     "OEBPS/images/logo.png"
/// );
/// assert_eq!(
///     resolve_path("OEBPS/content.html", "images/photo.jpg"),
///     "OEBPS/images/photo.jpg"
/// );
/// assert_eq!(
///     resolve_path("ch1.html", "/images/absolute.png"),
///     "images/absolute.png"
/// );
/// ```
pub fn resolve_path(base: &str, rel: &str) -> String {
    use std::path::{Component, Path};

    let rel_path = Path::new(rel);

    // If absolute (starts with /), treat as archive root
    if rel_path.has_root() {
        return rel.trim_start_matches('/').to_string();
    }

    // If it's a URL (http://, https://, data:, etc.), return as-is
    if rel.contains("://") || rel.starts_with("data:") {
        return rel.to_string();
    }

    // Pop the filename from base to get the directory
    let base_path = Path::new(base);
    let mut stack: Vec<&str> = base_path
        .parent()
        .unwrap_or(Path::new(""))
        .components()
        .filter_map(|c| {
            if let Component::Normal(s) = c {
                s.to_str()
            } else {
                None
            }
        })
        .collect();

    // Process relative path components
    for component in rel_path.components() {
        match component {
            Component::ParentDir => {
                stack.pop(); // Handle ".."
            }
            Component::Normal(c) => {
                if let Some(s) = c.to_str() {
                    stack.push(s);
                }
            }
            Component::CurDir => {} // Handle "." (no-op)
            _ => {}
        }
    }

    // Join with forward slashes for ZIP compatibility
    stack.join("/")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::Role;

    #[test]
    fn test_compile_simple_html() {
        let html = "<html><body><p>Test paragraph</p></body></html>";
        let chapter = compile_html(html, &[]);

        // Should have at least root + p (Text) + text content
        assert!(chapter.node_count() >= 3);

        // Verify there's at least one Text node
        let mut found_text = false;
        for id in chapter.iter_dfs() {
            if chapter.node(id).unwrap().role == Role::Text {
                found_text = true;
            }
        }
        assert!(found_text);
    }

    #[test]
    fn test_compile_with_css() {
        let html = "<p class='highlight'>Styled</p>";
        let css = ".highlight { font-weight: bold; }";

        let author = Stylesheet::parse(css);
        let chapter = compile_html(html, &[(author, Origin::Author)]);

        // Find a styled Paragraph node and check its style
        for id in chapter.iter_dfs() {
            let node = chapter.node(id).unwrap();
            if node.role == Role::Paragraph {
                let style = chapter.styles.get(node.style).unwrap();
                if style.font_weight == crate::style::FontWeight::BOLD {
                    return; // Found the styled paragraph
                }
            }
        }
        panic!("Styled paragraph not found");
    }

    #[test]
    fn test_extract_stylesheets() {
        let html = r#"
            <html>
            <head>
                <link rel="stylesheet" href="styles.css">
                <link rel="stylesheet" href="theme.css">
                <style>p { color: red; }</style>
            </head>
            <body><p>Content</p></body>
            </html>
        "#;

        let (linked, inline) = extract_stylesheets(html);

        assert_eq!(linked.len(), 2);
        assert!(linked.contains(&"styles.css".to_string()));
        assert!(linked.contains(&"theme.css".to_string()));

        assert_eq!(inline.len(), 1);
        assert!(inline[0].contains("color: red"));
    }

    #[test]
    fn test_compile_html_bytes() {
        let html = b"<p>Bytes test</p>";
        let chapter = compile_html_bytes(html, &[]);

        assert!(chapter.node_count() > 1);
    }

    #[test]
    fn test_resolve_path_parent_dir() {
        assert_eq!(
            resolve_path("OEBPS/text/ch1.html", "../images/logo.png"),
            "OEBPS/images/logo.png"
        );
    }

    #[test]
    fn test_resolve_path_same_dir() {
        assert_eq!(
            resolve_path("OEBPS/content.html", "images/photo.jpg"),
            "OEBPS/images/photo.jpg"
        );
    }

    #[test]
    fn test_resolve_path_absolute() {
        assert_eq!(
            resolve_path("ch1.html", "/images/absolute.png"),
            "images/absolute.png"
        );
    }

    #[test]
    fn test_resolve_path_multiple_parent() {
        assert_eq!(
            resolve_path("a/b/c/file.html", "../../images/test.png"),
            "a/images/test.png"
        );
    }

    #[test]
    fn test_resolve_path_current_dir() {
        assert_eq!(
            resolve_path("OEBPS/ch1.html", "./images/test.png"),
            "OEBPS/images/test.png"
        );
    }

    #[test]
    fn test_optimizer_merges_sibling_text_nodes() {
        // The optimizer merges adjacent sibling Text nodes with the same style.
        // Note: <b>A</b><b>B</b> creates separate Inline containers, so those
        // Text nodes are NOT siblings and won't be merged. This tests the case
        // where Text nodes are actual siblings (e.g., from text interspersed
        // with inline elements that get stripped).

        // Direct test of the optimizer unit tests cover the merge logic.
        // This integration test verifies the optimizer runs without corrupting
        // the tree structure.
        let html = r#"
            <html><body>
                <p>Hello, <b>World</b>!</p>
            </body></html>
        "#;
        let chapter = compile_html(html, &[]);

        // Collect all text content
        let mut text_content = String::new();
        for id in chapter.iter_dfs() {
            let node = chapter.node(id).unwrap();
            if node.role == Role::Text && !node.text.is_empty() {
                text_content.push_str(chapter.text(node.text));
            }
        }

        // All text should be preserved
        assert!(
            text_content.contains("Hello"),
            "Missing 'Hello' in: {}",
            text_content
        );
        assert!(
            text_content.contains("World"),
            "Missing 'World' in: {}",
            text_content
        );
    }

    #[test]
    fn test_optimizer_preserves_tree_structure() {
        // The optimizer should not corrupt the tree structure
        let html = r#"
            <html><body>
                <p>First paragraph</p>
                <p>Second paragraph</p>
            </body></html>
        "#;
        let chapter = compile_html(html, &[]);

        // Collect all text content via DFS traversal
        let mut text_content = String::new();
        for id in chapter.iter_dfs() {
            let node = chapter.node(id).unwrap();
            if node.role == Role::Text && !node.text.is_empty() {
                text_content.push_str(chapter.text(node.text));
            }
        }

        // Both paragraphs should be present
        assert!(
            text_content.contains("First paragraph"),
            "Missing 'First paragraph' in: {}",
            text_content
        );
        assert!(
            text_content.contains("Second paragraph"),
            "Missing 'Second paragraph' in: {}",
            text_content
        );
    }

    #[test]
    fn test_resolve_path_url_passthrough() {
        assert_eq!(
            resolve_path("ch1.html", "https://example.com/image.png"),
            "https://example.com/image.png"
        );
        assert_eq!(
            resolve_path("ch1.html", "data:image/png;base64,abc"),
            "data:image/png;base64,abc"
        );
    }

    #[test]
    fn test_br_survives_optimizer() {
        // Verify Break nodes survive the full compile_html pipeline (including optimizer)
        let chapter = compile_html(
            r#"<html xmlns="http://www.w3.org/1999/xhtml">
            <body>
                <blockquote>
                    <p>
                        <span>Line 1</span>
                        <br/>
                        <span>Line 2</span>
                    </p>
                </blockquote>
            </body></html>"#,
            &[],
        );

        // Should have a Break node
        let mut found_break = false;
        for id in chapter.iter_dfs() {
            if chapter.node(id).unwrap().role == Role::Break {
                found_break = true;
                break;
            }
        }
        assert!(found_break, "Break node lost during optimization");
    }

    #[test]
    fn test_xhtml_self_closing_script_preserves_content() {
        // EPUB XHTML files often have self-closing <script/> tags.
        // In HTML5 parsing, <script/> swallows everything after it.
        // xml5ever handles this correctly.
        let html = r#"<html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <script src="book.js"/>
            </head>
            <body><p>Hello World</p></body>
        </html>"#;
        let chapter = compile_html(html, &[]);

        let mut found_text = false;
        for id in chapter.iter_dfs() {
            let node = chapter.node(id).unwrap();
            if node.role == Role::Text && !node.text.is_empty() {
                let text = chapter.text(node.text);
                if text.contains("Hello World") {
                    found_text = true;
                }
            }
        }
        assert!(
            found_text,
            "Self-closing <script/> in XHTML swallowed body content"
        );
    }

    #[test]
    fn test_looks_like_xhtml() {
        assert!(looks_like_xhtml(
            r#"<?xml version="1.0"?><html><body>Hi</body></html>"#
        ));
        assert!(looks_like_xhtml(
            r#"<html xmlns="http://www.w3.org/1999/xhtml"><body>Hi</body></html>"#
        ));
        assert!(!looks_like_xhtml(
            "<html><body><p>Plain HTML</p></body></html>"
        ));
    }

    #[test]
    fn test_plain_html_still_works() {
        // Plain HTML without xmlns should use html5ever and still work fine
        let html = "<html><body><p>Plain HTML</p></body></html>";
        let chapter = compile_html(html, &[]);

        let mut found_text = false;
        for id in chapter.iter_dfs() {
            let node = chapter.node(id).unwrap();
            if node.role == Role::Text && !node.text.is_empty() {
                let text = chapter.text(node.text);
                if text.contains("Plain HTML") {
                    found_text = true;
                }
            }
        }
        assert!(found_text, "Plain HTML content should be preserved");
    }

    #[test]
    fn test_xhtml_extract_stylesheets() {
        // Stylesheet extraction should also work with XHTML
        let html = r#"<html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <link rel="stylesheet" href="style.css"/>
                <script src="book.js"/>
                <style>p { color: red; }</style>
            </head>
            <body><p>Content</p></body>
        </html>"#;

        let (linked, inline) = extract_stylesheets(html);
        assert_eq!(linked.len(), 1);
        assert!(linked.contains(&"style.css".to_string()));
        assert_eq!(inline.len(), 1);
        assert!(inline[0].contains("color: red"));
    }
}