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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//! Section tree extraction from book IR.
//!
//! Transforms boko's flat IR tree (where headings are siblings of paragraphs)
//! into a hierarchical section tree (where content is nested under headings).

use std::io;

use super::Book;
use super::chapter::Chapter;
use super::node::{NodeId, Role};
use crate::util::strip_ebook_chars;

// ============================================================================
// Public Types
// ============================================================================

/// A book's content as a hierarchical section tree.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "cli", derive(serde::Serialize))]
pub struct SectionTree {
    pub title: String,
    pub authors: Vec<String>,
    pub language: String,
    #[cfg_attr(feature = "cli", serde(skip_serializing_if = "Vec::is_empty"))]
    pub preamble: Vec<ContentBlock>,
    pub sections: Vec<SectionNode>,
}

/// A section defined by a heading and its content.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "cli", derive(serde::Serialize))]
pub struct SectionNode {
    /// Heading level (1-6).
    pub level: u8,
    /// Heading text.
    pub title: String,
    /// Content blocks before any child section.
    #[cfg_attr(feature = "cli", serde(skip_serializing_if = "Vec::is_empty"))]
    pub content: Vec<ContentBlock>,
    /// Subsections (headings at a deeper level).
    #[cfg_attr(feature = "cli", serde(skip_serializing_if = "Vec::is_empty"))]
    pub children: Vec<SectionNode>,
}

/// An atomic content block.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "cli", derive(serde::Serialize))]
#[cfg_attr(feature = "cli", serde(tag = "type", rename_all = "snake_case"))]
pub enum ContentBlock {
    Paragraph {
        text: String,
    },
    CodeBlock {
        #[cfg_attr(feature = "cli", serde(skip_serializing_if = "Option::is_none"))]
        language: Option<String>,
        code: String,
    },
    BlockQuote {
        text: String,
    },
    List {
        ordered: bool,
        items: Vec<String>,
    },
    Table {
        #[cfg_attr(feature = "cli", serde(skip_serializing_if = "Vec::is_empty"))]
        headers: Vec<String>,
        rows: Vec<Vec<String>>,
    },
    Image {
        src: String,
        alt: String,
    },
    Rule,
}

// ============================================================================
// Extraction
// ============================================================================

/// Extract a section tree from a book.
///
/// Walks each chapter's IR tree, splits on heading nodes, and nests content
/// under headings to form a hierarchical section tree.
pub fn extract_section_tree(book: &mut Book) -> io::Result<SectionTree> {
    let meta = book.metadata().clone();
    let spine: Vec<_> = book.spine().to_vec();

    let mut events = Vec::new();
    for entry in &spine {
        let chapter = book.load_chapter(entry.id)?;
        collect_events(&chapter, NodeId::ROOT, &mut events);
    }

    let (preamble, sections) = nest_events(&events);

    Ok(SectionTree {
        title: meta.title,
        authors: meta.authors,
        language: meta.language,
        preamble,
        sections,
    })
}

// ============================================================================
// Event collection (pass 1: flatten IR into heading/content events)
// ============================================================================

enum Event {
    Heading { level: u8, title: String },
    Content(ContentBlock),
}

/// Walk the IR tree, emitting heading and content events.
/// Containers and Root are transparent — their children are processed directly.
fn collect_events(chapter: &Chapter, node_id: NodeId, events: &mut Vec<Event>) {
    let Some(node) = chapter.node(node_id) else {
        return;
    };

    match node.role {
        Role::Root | Role::Container => {
            for child_id in chapter.children(node_id) {
                collect_events(chapter, child_id, events);
            }
        }

        Role::Heading(level) => {
            let title = collect_text(chapter, node_id).trim().to_string();
            if !title.is_empty() {
                events.push(Event::Heading { level, title });
            }
        }

        Role::Paragraph | Role::Caption => {
            let text = collect_text(chapter, node_id).trim().to_string();
            if !text.is_empty() {
                events.push(Event::Content(ContentBlock::Paragraph { text }));
            }
        }

        Role::CodeBlock => {
            let code = collect_text_verbatim(chapter, node_id);
            let language = chapter.semantics.language(node_id).map(String::from);
            if !code.trim().is_empty() {
                events.push(Event::Content(ContentBlock::CodeBlock { language, code }));
            }
        }

        Role::BlockQuote | Role::Sidebar => {
            let text = collect_text(chapter, node_id).trim().to_string();
            if !text.is_empty() {
                events.push(Event::Content(ContentBlock::BlockQuote { text }));
            }
        }

        Role::OrderedList | Role::UnorderedList => {
            let ordered = node.role == Role::OrderedList;
            let items = collect_list_items(chapter, node_id);
            if !items.is_empty() {
                events.push(Event::Content(ContentBlock::List { ordered, items }));
            }
        }

        Role::DefinitionList => {
            let items = collect_definition_items(chapter, node_id);
            if !items.is_empty() {
                events.push(Event::Content(ContentBlock::List {
                    ordered: false,
                    items,
                }));
            }
        }

        Role::Table => {
            let (headers, rows) = collect_table(chapter, node_id);
            if !rows.is_empty() || !headers.is_empty() {
                events.push(Event::Content(ContentBlock::Table { headers, rows }));
            }
        }

        Role::Image => {
            let src = chapter.semantics.src(node_id).unwrap_or("").to_string();
            let alt = chapter.semantics.alt(node_id).unwrap_or("").to_string();
            if !src.is_empty() {
                events.push(Event::Content(ContentBlock::Image { src, alt }));
            }
        }

        Role::Figure => {
            for child_id in chapter.children(node_id) {
                collect_events(chapter, child_id, events);
            }
        }

        Role::Rule => {
            events.push(Event::Content(ContentBlock::Rule));
        }

        // Inline-level or structural nodes handled by their parents.
        _ => {}
    }
}

// ============================================================================
// Nesting (pass 2: group flat events into a section tree)
// ============================================================================

fn nest_events(events: &[Event]) -> (Vec<ContentBlock>, Vec<SectionNode>) {
    let mut preamble = Vec::new();
    let mut i = 0;

    // Collect preamble (content before first heading)
    while i < events.len() {
        match &events[i] {
            Event::Content(block) => {
                preamble.push(block.clone());
                i += 1;
            }
            Event::Heading { .. } => break,
        }
    }

    let (sections, _) = parse_siblings(events, i, 0);
    (preamble, sections)
}

/// Parse sibling sections. Stops when a heading with level < min_level is hit.
fn parse_siblings(events: &[Event], mut i: usize, min_level: u8) -> (Vec<SectionNode>, usize) {
    let mut sections = Vec::new();

    while i < events.len() {
        match &events[i] {
            Event::Heading { level, .. } if *level < min_level => break,
            Event::Heading { level, title } => {
                let level = *level;
                let title = title.clone();
                let mut content = Vec::new();
                i += 1;

                // Collect content until next heading
                while i < events.len() {
                    match &events[i] {
                        Event::Heading { .. } => break,
                        Event::Content(block) => {
                            content.push(block.clone());
                            i += 1;
                        }
                    }
                }

                // Recurse for child sections (deeper headings)
                let (children, next_i) = parse_siblings(events, i, level + 1);
                i = next_i;

                sections.push(SectionNode {
                    level,
                    title,
                    content,
                    children,
                });
            }
            Event::Content(_) => {
                i += 1;
            }
        }
    }

    (sections, i)
}

// ============================================================================
// Text collection helpers
// ============================================================================

fn collect_text(chapter: &Chapter, node_id: NodeId) -> String {
    let mut result = String::new();
    collect_text_recursive(chapter, node_id, &mut result);
    strip_ebook_chars(&result)
}

fn collect_text_recursive(chapter: &Chapter, node_id: NodeId, result: &mut String) {
    let Some(node) = chapter.node(node_id) else {
        return;
    };

    if node.role == Role::Footnote {
        return;
    }

    if node.role == Role::Break {
        if !result.is_empty() && !result.ends_with(' ') {
            result.push(' ');
        }
        return;
    }

    if node.role == Role::Text && !node.text.is_empty() {
        let text = chapter.text(node.text);
        let has_leading = text.starts_with(char::is_whitespace);
        let has_trailing = text.ends_with(char::is_whitespace);
        let words: Vec<&str> = text.split_whitespace().collect();

        if !words.is_empty() {
            if has_leading && !result.is_empty() && !result.ends_with(' ') {
                result.push(' ');
            }
            result.push_str(&words.join(" "));
            if has_trailing {
                result.push(' ');
            }
        } else if !text.is_empty() && !result.is_empty() && !result.ends_with(' ') {
            result.push(' ');
        }
    }

    for child_id in chapter.children(node_id) {
        collect_text_recursive(chapter, child_id, result);
    }
}

fn collect_text_verbatim(chapter: &Chapter, node_id: NodeId) -> String {
    let mut result = String::new();
    collect_text_verbatim_recursive(chapter, node_id, &mut result);
    strip_ebook_chars(&result)
}

fn collect_text_verbatim_recursive(chapter: &Chapter, node_id: NodeId, result: &mut String) {
    let Some(node) = chapter.node(node_id) else {
        return;
    };

    if node.role == Role::Text && !node.text.is_empty() {
        result.push_str(chapter.text(node.text));
    }

    for child_id in chapter.children(node_id) {
        collect_text_verbatim_recursive(chapter, child_id, result);
    }
}

// ============================================================================
// Structured content extraction
// ============================================================================

fn collect_list_items(chapter: &Chapter, list_id: NodeId) -> Vec<String> {
    chapter
        .children(list_id)
        .filter_map(|child_id| {
            let child = chapter.node(child_id)?;
            if child.role == Role::ListItem {
                let text = collect_text(chapter, child_id).trim().to_string();
                if text.is_empty() { None } else { Some(text) }
            } else {
                None
            }
        })
        .collect()
}

fn collect_definition_items(chapter: &Chapter, dl_id: NodeId) -> Vec<String> {
    let mut items = Vec::new();
    let mut current_term: Option<String> = None;

    for child_id in chapter.children(dl_id) {
        let Some(child) = chapter.node(child_id) else {
            continue;
        };
        match child.role {
            Role::DefinitionTerm => {
                current_term = Some(collect_text(chapter, child_id).trim().to_string());
            }
            Role::DefinitionDescription => {
                let desc = collect_text(chapter, child_id).trim().to_string();
                if let Some(term) = current_term.take() {
                    items.push(format!("{}: {}", term, desc));
                } else if !desc.is_empty() {
                    items.push(desc);
                }
            }
            _ => {}
        }
    }

    items
}

fn collect_table(chapter: &Chapter, table_id: NodeId) -> (Vec<String>, Vec<Vec<String>>) {
    let mut headers = Vec::new();
    let mut rows = Vec::new();

    for section_id in chapter.children(table_id) {
        let Some(section) = chapter.node(section_id) else {
            continue;
        };
        match section.role {
            Role::TableRow => {
                let cells = collect_row_cells(chapter, section_id);
                if is_header_row(chapter, section_id) && headers.is_empty() {
                    headers = cells;
                } else {
                    rows.push(cells);
                }
            }
            Role::TableHead | Role::TableBody => {
                for row_id in chapter.children(section_id) {
                    let Some(row) = chapter.node(row_id) else {
                        continue;
                    };
                    if row.role == Role::TableRow {
                        let cells = collect_row_cells(chapter, row_id);
                        if section.role == Role::TableHead && headers.is_empty() {
                            headers = cells;
                        } else {
                            rows.push(cells);
                        }
                    }
                }
            }
            _ => {}
        }
    }

    (headers, rows)
}

fn collect_row_cells(chapter: &Chapter, row_id: NodeId) -> Vec<String> {
    chapter
        .children(row_id)
        .filter_map(|id| {
            let node = chapter.node(id)?;
            if node.role == Role::TableCell {
                Some(collect_text(chapter, id).trim().to_string())
            } else {
                None
            }
        })
        .collect()
}

fn is_header_row(chapter: &Chapter, row_id: NodeId) -> bool {
    chapter
        .children(row_id)
        .any(|id| chapter.semantics.is_header_cell(id))
}

// ============================================================================
// Tests
// ============================================================================

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

    fn make_section_tree(
        build: impl FnOnce(&mut Chapter),
    ) -> (Vec<ContentBlock>, Vec<SectionNode>) {
        let mut chapter = Chapter::new();
        build(&mut chapter);
        let mut events = Vec::new();
        collect_events(&chapter, NodeId::ROOT, &mut events);
        nest_events(&events)
    }

    fn add_heading(chapter: &mut Chapter, level: u8, text: &str) {
        let h = chapter.alloc_node(Node::new(Role::Heading(level)));
        chapter.append_child(NodeId::ROOT, h);
        let range = chapter.append_text(text);
        let t = chapter.alloc_node(Node::text(range));
        chapter.append_child(h, t);
    }

    fn add_paragraph(chapter: &mut Chapter, text: &str) {
        let p = chapter.alloc_node(Node::new(Role::Paragraph));
        chapter.append_child(NodeId::ROOT, p);
        let range = chapter.append_text(text);
        let t = chapter.alloc_node(Node::text(range));
        chapter.append_child(p, t);
    }

    #[test]
    fn preamble_only() {
        let (preamble, sections) = make_section_tree(|ch| {
            add_paragraph(ch, "No headings here.");
        });
        assert_eq!(preamble.len(), 1);
        assert!(sections.is_empty());
    }

    #[test]
    fn single_section() {
        let (preamble, sections) = make_section_tree(|ch| {
            add_heading(ch, 1, "Title");
            add_paragraph(ch, "Content");
        });
        assert!(preamble.is_empty());
        assert_eq!(sections.len(), 1);
        assert_eq!(sections[0].title, "Title");
        assert_eq!(sections[0].level, 1);
        assert_eq!(sections[0].content.len(), 1);
    }

    #[test]
    fn nested_sections() {
        let (_, sections) = make_section_tree(|ch| {
            add_heading(ch, 1, "Chapter");
            add_paragraph(ch, "Intro");
            add_heading(ch, 2, "Section A");
            add_paragraph(ch, "A content");
            add_heading(ch, 2, "Section B");
            add_paragraph(ch, "B content");
        });
        assert_eq!(sections.len(), 1);
        assert_eq!(sections[0].content.len(), 1); // "Intro"
        assert_eq!(sections[0].children.len(), 2);
        assert_eq!(sections[0].children[0].title, "Section A");
        assert_eq!(sections[0].children[1].title, "Section B");
    }

    #[test]
    fn sibling_top_level() {
        let (_, sections) = make_section_tree(|ch| {
            add_heading(ch, 1, "One");
            add_paragraph(ch, "First");
            add_heading(ch, 1, "Two");
            add_paragraph(ch, "Second");
        });
        assert_eq!(sections.len(), 2);
        assert_eq!(sections[0].title, "One");
        assert_eq!(sections[1].title, "Two");
    }

    #[test]
    fn skipped_levels() {
        let (_, sections) = make_section_tree(|ch| {
            add_heading(ch, 1, "Top");
            add_heading(ch, 3, "Deep");
            add_paragraph(ch, "Content");
        });
        assert_eq!(sections.len(), 1);
        assert_eq!(sections[0].children.len(), 1);
        assert_eq!(sections[0].children[0].level, 3);
        assert_eq!(sections[0].children[0].content.len(), 1);
    }

    #[test]
    fn preamble_then_sections() {
        let (preamble, sections) = make_section_tree(|ch| {
            add_paragraph(ch, "Preamble");
            add_heading(ch, 1, "Chapter 1");
            add_paragraph(ch, "Content");
        });
        assert_eq!(preamble.len(), 1);
        assert_eq!(sections.len(), 1);
    }

    #[test]
    fn deep_nesting() {
        let (_, sections) = make_section_tree(|ch| {
            add_heading(ch, 1, "H1");
            add_heading(ch, 2, "H2");
            add_heading(ch, 3, "H3");
            add_paragraph(ch, "Deep content");
        });
        assert_eq!(sections.len(), 1);
        assert_eq!(sections[0].children.len(), 1);
        assert_eq!(sections[0].children[0].children.len(), 1);
        assert_eq!(sections[0].children[0].children[0].title, "H3");
        assert_eq!(sections[0].children[0].children[0].content.len(), 1);
    }

    #[test]
    fn content_between_children() {
        let (_, sections) = make_section_tree(|ch| {
            add_heading(ch, 1, "Parent");
            add_paragraph(ch, "Parent intro");
            add_heading(ch, 2, "Child A");
            add_paragraph(ch, "A content");
            add_paragraph(ch, "Still A");
            add_heading(ch, 2, "Child B");
            add_paragraph(ch, "B content");
        });
        assert_eq!(sections[0].content.len(), 1); // "Parent intro"
        assert_eq!(sections[0].children[0].content.len(), 2); // "A content" + "Still A"
        assert_eq!(sections[0].children[1].content.len(), 1); // "B content"
    }

    #[test]
    fn container_transparency() {
        let (_, sections) = make_section_tree(|ch| {
            // Wrap heading and paragraph in a Container (like <section> or <div>)
            let container = ch.alloc_node(Node::new(Role::Container));
            ch.append_child(NodeId::ROOT, container);

            let h = ch.alloc_node(Node::new(Role::Heading(1)));
            ch.append_child(container, h);
            let range = ch.append_text("Inside Container");
            let t = ch.alloc_node(Node::text(range));
            ch.append_child(h, t);

            let p = ch.alloc_node(Node::new(Role::Paragraph));
            ch.append_child(container, p);
            let range = ch.append_text("Container content");
            let t = ch.alloc_node(Node::text(range));
            ch.append_child(p, t);
        });
        assert_eq!(sections.len(), 1);
        assert_eq!(sections[0].title, "Inside Container");
        assert_eq!(sections[0].content.len(), 1);
    }

    #[test]
    fn empty_headings_skipped() {
        let (_, sections) = make_section_tree(|ch| {
            // Heading with no text content
            let h = ch.alloc_node(Node::new(Role::Heading(1)));
            ch.append_child(NodeId::ROOT, h);

            add_heading(ch, 1, "Real Title");
            add_paragraph(ch, "Content");
        });
        assert_eq!(sections.len(), 1);
        assert_eq!(sections[0].title, "Real Title");
    }
}