boko 0.5.0

Fast native ebook converter for EPUB, KFX, AZW3, and MOBI — the only KFX writer that needs no Kindle Previewer
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
//! Filepos handling for MOBI format.
//!
//! MOBI files use `filepos=NNNNN` attributes in anchor tags to reference
//! byte positions in the decompressed text stream. This module provides
//! functions matching KindleUnpack's approach:
//! 1. Collect all filepos target positions from links
//! 2. Insert `<a id="fileposNNNNN" />` anchor tags at exact byte positions
//! 3. Convert `filepos=NNNNN` to `href="#fileposNNNNN"`

use std::collections::{BTreeMap, HashSet};

/// Collect all filepos target values from `<a filepos=NNNNN>` attributes.
///
/// Returns a set of byte positions that are referenced as link targets.
/// Matches KindleUnpack's link_pattern: `<[^<>]+filepos=['"{0,1}(\d+)[^<>]*>`
pub fn collect_filepos_targets(html: &[u8]) -> HashSet<usize> {
    let mut targets = HashSet::new();

    // SIMD-scan for each `filepos=` occurrence rather than testing every byte
    // position over the whole decompressed text stream.
    for pos in memchr::memmem::find_iter(html, b"filepos=") {
        let val_start = pos + 8;
        let mut start = val_start;

        // Skip optional quote
        if start < html.len() && (html[start] == b'"' || html[start] == b'\'') {
            start += 1;
        }

        // Skip leading zeros
        while start < html.len() && html[start] == b'0' {
            start += 1;
        }

        // Parse digits
        let mut val_end = start;
        while val_end < html.len() && html[val_end].is_ascii_digit() {
            val_end += 1;
        }

        // If we only had zeros, back up to include at least one
        if val_end == start && start > val_start && html[start - 1] == b'0' {
            start -= 1;
        }

        if val_end > start {
            if let Ok(filepos) = String::from_utf8_lossy(&html[start..val_end]).parse::<usize>() {
                targets.insert(filepos);
            }
        } else if val_end == start {
            // Just "0" or empty after zeros
            targets.insert(0);
        }
    }

    targets
}

/// Transform MOBI HTML matching KindleUnpack's approach:
/// 1. Insert `<a id="fileposNNNNN" />` anchor tags at exact byte positions
/// 2. Convert `filepos=NNNNN` to `href="#fileposNNNNN"`
/// 3. Convert `recindex=NNNNN` to proper image paths
///
/// This matches KindleUnpack's findAnchors() + insertHREFS() methods.
///
/// `extra_anchor_positions` allows inserting additional anchors (e.g. from NCX
/// index entries) at byte positions that may not have corresponding `filepos=N`
/// attributes in the HTML.
pub fn transform_mobi_html(
    html: &[u8],
    assets: &[String],
    extra_anchor_positions: &[u32],
) -> Vec<u8> {
    use std::collections::HashMap;

    // Step 1: Collect all filepos targets
    let targets = collect_filepos_targets(html);

    // Step 2: Build position map for anchor insertion
    // KindleUnpack inserts anchors at exact byte positions
    let mut position_map: BTreeMap<usize, Vec<u8>> = BTreeMap::new();
    for &position in &targets {
        if position > 0 && position <= html.len() {
            let anchor = format!("<a id=\"filepos{}\" />", position);
            position_map
                .entry(position)
                .or_default()
                .extend_from_slice(anchor.as_bytes());
        }
    }

    // Also insert anchors at extra positions (NCX entries, etc.)
    for &position in extra_anchor_positions {
        let pos = position as usize;
        if pos > 0 && pos <= html.len() {
            position_map
                .entry(pos)
                .or_insert_with(|| format!("<a id=\"filepos{}\" />", pos).into_bytes());
        }
    }

    // Step 3: Build recindex -> asset path mapping
    let mut recindex_map: HashMap<String, String> = HashMap::new();
    for (i, asset) in assets.iter().enumerate() {
        let recindex = format!("{:05}", i + 1);
        recindex_map.insert(recindex, asset.clone());
    }

    // Step 4: Insert anchors at positions (like KindleUnpack's dataList building)
    let mut with_anchors = Vec::with_capacity(html.len() + position_map.len() * 30);
    let mut last_pos = 0;

    for (&end_pos, anchor_bytes) in &position_map {
        if end_pos == 0 || end_pos > html.len() {
            continue;
        }
        with_anchors.extend_from_slice(&html[last_pos..end_pos]);
        with_anchors.extend_from_slice(anchor_bytes);
        last_pos = end_pos;
    }
    with_anchors.extend_from_slice(&html[last_pos..]);

    // Step 5: Convert filepos=NNNNN to href="#fileposNNNNN" and handle recindex
    let mut output = Vec::with_capacity(with_anchors.len());
    let mut pos = 0;

    while pos < with_anchors.len() {
        // Look for filepos= pattern
        if pos + 8 < with_anchors.len() && with_anchors[pos..].starts_with(b"filepos=") {
            let val_start = pos + 8;
            let mut start = val_start;
            let mut has_quote = false;

            // Skip optional quote
            if start < with_anchors.len()
                && (with_anchors[start] == b'"' || with_anchors[start] == b'\'')
            {
                has_quote = true;
                start += 1;
            }

            // Parse digits (including leading zeros which we strip in output)
            let digit_start = start;
            while start < with_anchors.len() && with_anchors[start].is_ascii_digit() {
                start += 1;
            }

            // Skip closing quote if present
            let mut end = start;
            if has_quote
                && end < with_anchors.len()
                && (with_anchors[end] == b'"' || with_anchors[end] == b'\'')
            {
                end += 1;
            }

            if start > digit_start {
                // Parse the number, stripping leading zeros
                let num_str = String::from_utf8_lossy(&with_anchors[digit_start..start]);
                if let Ok(filepos_num) = num_str.trim_start_matches('0').parse::<u64>() {
                    output.extend_from_slice(b"href=\"#filepos");
                    output.extend_from_slice(filepos_num.to_string().as_bytes());
                    output.push(b'"');
                    pos = end;
                    continue;
                } else if num_str.chars().all(|c| c == '0') {
                    // All zeros = position 0
                    output.extend_from_slice(b"href=\"#filepos0\"");
                    pos = end;
                    continue;
                }
            } else {
                // Empty or malformed filepos (no digits) - skip the entire attribute
                // This removes `filepos=""` or `filepos=` leaving the anchor tag
                // which will be cleaned up later or rendered as plain text
                pos = end;
                continue;
            }
        }

        // Look for recindex=" pattern
        if pos + 10 < with_anchors.len() && with_anchors[pos..].starts_with(b"recindex=\"") {
            let val_start = pos + 10;
            if let Some(val_end_rel) = with_anchors[val_start..].iter().position(|&b| b == b'"') {
                let val_end = val_start + val_end_rel;
                let recindex =
                    String::from_utf8_lossy(&with_anchors[val_start..val_end]).to_string();

                if let Some(path) = recindex_map.get(&recindex) {
                    output.extend_from_slice(b"src=\"");
                    output.extend_from_slice(path.as_bytes());
                    output.push(b'"');
                    pos = val_end + 1;
                    continue;
                }
            }
        }

        // Copy byte as-is
        output.push(with_anchors[pos]);
        pos += 1;
    }

    // Step 6: Remove empty anchors (like KindleUnpack does)
    remove_empty_anchors(&mut output);

    // Step 7: Fix stray solidi between attributes (real AZW3 quirk)
    fix_stray_attribute_solidus(&mut output);

    output
}

/// Fix a misplaced self-closing solidus between a quoted attribute value
/// and a following attribute: `<img src="x.gif"/ alt="">` (seen verbatim in
/// real AZW3 markup). Left alone, the XML parse path glues the next
/// attribute into the value (`src="x.gifalt=""`), breaking the reference;
/// merely dropping it leaves the tag unclosed and the XML parser swallows
/// following content as children. The solidus is relocated to the end of
/// the tag (`<img src="x.gif" alt=""/>`), which is what the author meant.
pub(crate) fn fix_stray_attribute_solidus(html: &mut Vec<u8>) {
    let mut cleaned = Vec::with_capacity(html.len());
    let mut in_tag = false;
    let mut in_quote = false;
    let mut relocate_solidus = false;
    let mut i = 0;
    while i < html.len() {
        let b = html[i];
        if in_tag && in_quote {
            cleaned.push(b);
            i += 1;
            if b == b'"' {
                in_quote = false;
                // Just closed a value: a solidus that doesn't close the tag
                // is a misplaced self-closing marker.
                if html.get(i) == Some(&b'/') && html.get(i + 1).is_some_and(|&n| n != b'>') {
                    i += 1;
                    relocate_solidus = true;
                    if html.get(i).is_some_and(|n| !n.is_ascii_whitespace()) {
                        cleaned.push(b' ');
                    }
                }
            }
            continue;
        }
        match b {
            b'<' => in_tag = true,
            b'>' => {
                in_tag = false;
                if relocate_solidus {
                    relocate_solidus = false;
                    if cleaned.last() != Some(&b'/') {
                        cleaned.push(b'/');
                    }
                }
            }
            b'"' if in_tag => in_quote = true,
            _ => {}
        }
        cleaned.push(b);
        i += 1;
    }
    *html = cleaned;
}

/// Remove empty anchor tags: `<a />` and `<a></a>`
///
/// Operates on raw bytes: the text may be CP1252 (the MOBI default), and the
/// previous `from_utf8_lossy` round-trip irreversibly replaced every
/// non-ASCII byte (curly quotes, accents) with U+FFFD. The patterns are pure
/// ASCII, so a single byte-level pass is both correct and allocation-light.
fn remove_empty_anchors(html: &mut Vec<u8>) {
    const PATTERNS: [&[u8]; 4] = [b"<a />", b"<a  />", b"<a></a>", b"<a ></a>"];

    let mut cleaned = Vec::with_capacity(html.len());
    let mut pos = 0;
    'outer: while pos < html.len() {
        if html[pos] == b'<' {
            for pattern in PATTERNS {
                if html[pos..].starts_with(pattern) {
                    pos += pattern.len();
                    continue 'outer;
                }
            }
        }
        cleaned.push(html[pos]);
        pos += 1;
    }
    *html = cleaned;
}

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

    #[test]
    fn test_collect_filepos_targets() {
        let html = b"<a filepos=1234>Link1</a> text <a filepos=5678>Link2</a>";
        let targets = collect_filepos_targets(html);

        assert!(targets.contains(&1234));
        assert!(targets.contains(&5678));
        assert_eq!(targets.len(), 2);
    }

    #[test]
    fn test_collect_filepos_with_quotes() {
        let html = b"<a filepos=\"0001234\">Link</a>";
        let targets = collect_filepos_targets(html);

        assert!(targets.contains(&1234));
    }

    #[test]
    fn test_transform_inserts_anchor_at_position() {
        // Position 50 should have an anchor inserted
        let mut html = vec![b' '; 100];
        html[0..6].copy_from_slice(b"<html>");
        html[50..60].copy_from_slice(b"<p>Hello</");
        // Add a link pointing to position 50
        let link = b"<a filepos=50>Link</a>";
        html.extend_from_slice(link);

        let result = transform_mobi_html(&html, &[], &[]);
        let result_str = String::from_utf8_lossy(&result);

        // Should have anchor at position 50
        assert!(
            result_str.contains("<a id=\"filepos50\" />"),
            "Should insert anchor: {}",
            result_str
        );
        // Should convert filepos to href
        assert!(
            result_str.contains("href=\"#filepos50\""),
            "Should convert href: {}",
            result_str
        );
    }

    #[test]
    fn test_transform_filepos_to_href() {
        let html = b"<a filepos=1234>Link</a>";
        let result = transform_mobi_html(html, &[], &[]);
        let result_str = String::from_utf8_lossy(&result);

        assert!(result_str.contains("href=\"#filepos1234\""));
        assert!(!result_str.contains("filepos="));
    }

    #[test]
    fn test_transform_recindex() {
        let assets = vec!["images/image_0000.jpg".to_string()];
        let html = b"<img recindex=\"00001\">";
        let result = transform_mobi_html(html, &assets, &[]);
        let result_str = String::from_utf8_lossy(&result);

        assert!(result_str.contains("src=\"images/image_0000.jpg\""));
        assert!(!result_str.contains("recindex"));
    }

    /// A stray solidus between a quoted attribute value and further
    /// attributes (`src="x.gif"/ alt=""`, seen verbatim in real AZW3s) must
    /// not survive: the XML parse path glues the next attribute into the
    /// value, breaking the image reference.
    #[test]
    fn test_transform_stray_attribute_solidus() {
        let assets = vec!["images/image_0000.gif".to_string()];

        // After the recindex rewrite. The misplaced self-closing solidus is
        // relocated to the tag end so the XML parse neither glues the next
        // attribute into the value nor leaves the tag unclosed.
        let html = b"<img id=\"c1\" recindex=\"00001\"/ alt=\"\">";
        let result = transform_mobi_html(html, &assets, &[]);
        let result_str = String::from_utf8_lossy(&result);
        assert!(
            result_str.contains("src=\"images/image_0000.gif\" alt=\"\"/>"),
            "solidus must be relocated: {result_str}"
        );

        // Directly in source markup, with and without a following space.
        let html = b"<img src=\"images/x.gif\"/ alt=\"\"><img src=\"images/y.gif\"/alt=\"a\">";
        let result = transform_mobi_html(html, &[], &[]);
        let result_str = String::from_utf8_lossy(&result);
        assert!(
            result_str.contains("src=\"images/x.gif\" alt=\"\"/>"),
            "{result_str}"
        );
        assert!(
            result_str.contains("src=\"images/y.gif\" alt=\"a\"/>"),
            "{result_str}"
        );

        // Legitimate self-closing tags are untouched; quotes in text too.
        let html = b"<img src=\"images/x.gif\"/><p>a \"quoted\"/ thing</p>";
        let result = transform_mobi_html(html, &[], &[]);
        let result_str = String::from_utf8_lossy(&result);
        assert!(
            result_str.contains("src=\"images/x.gif\"/>"),
            "{result_str}"
        );
        assert!(result_str.contains("\"quoted\"/ thing"), "{result_str}");
    }

    #[test]
    fn test_transform_with_leading_zeros() {
        let html = b"<a filepos=0000100>Link</a>";
        let result = transform_mobi_html(html, &[], &[]);
        let result_str = String::from_utf8_lossy(&result);

        // Should strip leading zeros in href
        assert!(result_str.contains("href=\"#filepos100\""));
    }

    #[test]
    fn test_transform_empty_filepos_quoted() {
        // Empty filepos with quotes should be removed, leaving plain anchor
        let html = b"<a filepos=\"\">Link text</a>";
        let result = transform_mobi_html(html, &[], &[]);
        let result_str = String::from_utf8_lossy(&result);

        // The empty filepos="" attribute should be stripped
        assert!(
            !result_str.contains("filepos"),
            "Empty filepos should be removed: {}",
            result_str
        );
        // The link text should remain
        assert!(
            result_str.contains("Link text"),
            "Link text should remain: {}",
            result_str
        );
    }

    #[test]
    fn test_transform_empty_filepos_unquoted() {
        // Empty filepos without quotes (malformed) should be handled
        let html = b"<a filepos=>Link text</a>";
        let result = transform_mobi_html(html, &[], &[]);
        let result_str = String::from_utf8_lossy(&result);

        // The empty filepos= attribute should be stripped
        assert!(
            !result_str.contains("filepos"),
            "Empty filepos should be removed: {}",
            result_str
        );
        // The link text should remain
        assert!(
            result_str.contains("Link text"),
            "Link text should remain: {}",
            result_str
        );
    }

    #[test]
    fn test_transform_whitespace_only_filepos() {
        // filepos with only whitespace should be handled
        let html = b"<a filepos=\"  \">Link text</a>";
        let result = transform_mobi_html(html, &[], &[]);
        let result_str = String::from_utf8_lossy(&result);

        // The whitespace-only filepos should be stripped
        assert!(
            !result_str.contains("filepos"),
            "Whitespace-only filepos should be removed: {}",
            result_str
        );
    }
}