[format][library] Books: EPUB/FB2/TXT/MOBI/AZW3 into one sanitized model, CFI anchors, the event-log library, stats, CSV import, export
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
//! Mandatory test 2 (the core's half): a highlight's position survives re-rendering and re-import, and a changed
|
||||
//! edition degrades to "approximate" or "lost", never to other text. Font size and theme do not exist here: they
|
||||
//! never reach the tree (the app's test renders the same chapter under different settings).
|
||||
|
||||
mod common;
|
||||
use common::*;
|
||||
use liber_format::position::{Point, anchor, offset_of, point_at, resolve};
|
||||
use liber_format::{Limits, Resolved, open};
|
||||
|
||||
const CH0: &str = "<h1>Prolog</h1><p>Úvodní slovo.</p>";
|
||||
const CH1: &str = "<h1>Kapitola první</h1>\
|
||||
<p>Bylo to v <em>temné a bouřlivé</em> noci, když 😀 robot poprvé promluvil.</p>\
|
||||
<p>Druhý odstavec s <a href=\"#n1\" epub:type=\"noteref\">1</a> poznámkou.</p>\
|
||||
<aside epub:type=\"footnote\" id=\"n1\"><p>Poznámka pod čarou.</p></aside>";
|
||||
const CH2: &str = "<h1>Kapitola druhá</h1><p>Konec.</p>";
|
||||
|
||||
fn book(ch1: &str) -> Vec<u8> {
|
||||
Epub::new("R.U.R.").chapter("Prolog", CH0).chapter("Kapitola první", ch1).chapter("Kapitola druhá", CH2).build()
|
||||
}
|
||||
|
||||
fn find16(text: &str, needle: &str) -> (usize, usize) {
|
||||
let b = text.find(needle).expect("needle");
|
||||
let s = text[..b].encode_utf16().count();
|
||||
(s, s + needle.encode_utf16().count())
|
||||
}
|
||||
|
||||
fn quote_at(doc: &liber_format::Document, r: &Resolved) -> String {
|
||||
match r {
|
||||
Resolved::Exact { chapter, start, end } | Resolved::Approximate { chapter, start, end } => {
|
||||
let t: Vec<u16> = doc.chapters[*chapter].text().encode_utf16().collect();
|
||||
String::from_utf16(&t[*start..*end]).unwrap()
|
||||
}
|
||||
Resolved::Lost => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cfi_round_trips_on_every_text_offset() {
|
||||
let d = open(&book(CH1), None, &Limits::default()).unwrap();
|
||||
let ch = &d.chapters[1];
|
||||
let n = ch.text_len;
|
||||
for off in 0..=n {
|
||||
for end in [false, true] {
|
||||
let p = point_at(1, &ch.root, off, end);
|
||||
let parsed = Point::parse(&p.to_cfi()).unwrap();
|
||||
assert_eq!(parsed, p);
|
||||
// Offsets inside a surrogate pair do not exist in the text; every other offset maps back.
|
||||
let t = ch.text();
|
||||
if liber_format::text::byte_at_utf16(&t, off).is_some() {
|
||||
assert_eq!(offset_of(&ch.root, &parsed), Some(off), "offset {off} cfi {}", p.to_cfi());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cfi_shape() {
|
||||
let d = open(&book(CH1), None, &Limits::default()).unwrap();
|
||||
let t = d.chapters[1].text();
|
||||
let (s, e) = find16(&t, "temné a bouřlivé");
|
||||
let a = anchor(&d, 1, s, e).unwrap();
|
||||
// Chapter 1 → /6/4; root /4; the first <p> is the second element (after <h1>): /4; <em> is its first element: /2.
|
||||
assert_eq!(a.start, "epubcfi(/6/4!/4/4/2/1:0)");
|
||||
assert_eq!(a.end, "epubcfi(/6/4!/4/4/2/1:16)");
|
||||
assert_eq!(a.quote.exact, "temné a bouřlivé");
|
||||
assert_eq!(a.quote.prefix, "Kapitola prvníBylo to v ");
|
||||
assert!(a.quote.suffix.starts_with(" noci, když 😀"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_highlight_survives_reimport_of_the_same_file() {
|
||||
let bytes = book(CH1);
|
||||
let d1 = open(&bytes, None, &Limits::default()).unwrap();
|
||||
let t = d1.chapters[1].text();
|
||||
let (s, e) = find16(&t, "noci, když 😀 robot");
|
||||
let a = anchor(&d1, 1, s, e).unwrap();
|
||||
for _ in 0..3 {
|
||||
let d2 = open(&bytes, None, &Limits::default()).unwrap();
|
||||
assert_eq!(d2.chapters[1].xhtml, d1.chapters[1].xhtml, "the sanitizer is deterministic");
|
||||
assert_eq!(resolve(&d2, &a), Resolved::Exact { chapter: 1, start: s, end: e });
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_highlight_across_elements() {
|
||||
let d = open(&book(CH1), None, &Limits::default()).unwrap();
|
||||
let t = d.chapters[1].text();
|
||||
let (s, e) = find16(&t, "v temné a bouřlivé noci");
|
||||
let a = anchor(&d, 1, s, e).unwrap();
|
||||
assert_ne!(Point::parse(&a.start).unwrap().steps, Point::parse(&a.end).unwrap().steps);
|
||||
let r = resolve(&d, &a);
|
||||
assert_eq!(r, Resolved::Exact { chapter: 1, start: s, end: e });
|
||||
assert_eq!(quote_at(&d, &r), "v temné a bouřlivé noci");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_changed_edition_gives_approximate_at_the_same_words() {
|
||||
let d1 = open(&book(CH1), None, &Limits::default()).unwrap();
|
||||
let t = d1.chapters[1].text();
|
||||
let (s, e) = find16(&t, "robot poprvé promluvil");
|
||||
let a = anchor(&d1, 1, s, e).unwrap();
|
||||
// The new edition adds a paragraph before it and re-wraps the emphasis.
|
||||
let edited = CH1
|
||||
.replace("<p>Bylo to", "<p>Nový úvodní odstavec.</p><p>Bylo to")
|
||||
.replace("<em>temné a bouřlivé</em>", "temné a bouřlivé");
|
||||
let d2 = open(&book(&edited), None, &Limits::default()).unwrap();
|
||||
let r = resolve(&d2, &a);
|
||||
assert!(matches!(r, Resolved::Approximate { chapter: 1, .. }), "{r:?}");
|
||||
assert_eq!(quote_at(&d2, &r), "robot poprvé promluvil");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_moved_chapter_is_found_in_another_chapter() {
|
||||
let d1 = open(&book(CH1), None, &Limits::default()).unwrap();
|
||||
let t = d1.chapters[1].text();
|
||||
let (s, e) = find16(&t, "Druhý odstavec");
|
||||
let a = anchor(&d1, 1, s, e).unwrap();
|
||||
let moved = Epub::new("R.U.R.")
|
||||
.chapter("Kapitola první", CH1)
|
||||
.chapter("Prolog", CH0)
|
||||
.chapter("Kapitola druhá", CH2)
|
||||
.build();
|
||||
let d2 = open(&moved, None, &Limits::default()).unwrap();
|
||||
let r = resolve(&d2, &a);
|
||||
assert!(matches!(r, Resolved::Approximate { chapter: 0, .. }), "{r:?}");
|
||||
assert_eq!(quote_at(&d2, &r), "Druhý odstavec");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removed_text_is_lost_never_elsewhere() {
|
||||
let d1 = open(&book(CH1), None, &Limits::default()).unwrap();
|
||||
let t = d1.chapters[1].text();
|
||||
let (s, e) = find16(&t, "robot poprvé promluvil");
|
||||
let a = anchor(&d1, 1, s, e).unwrap();
|
||||
// Same length, same structure, different words: the CFI still resolves, but to other text.
|
||||
let reworded = CH1.replace("robot poprvé promluvil", "stroj zase mlčel dlouho");
|
||||
let d2 = open(&book(&reworded), None, &Limits::default()).unwrap();
|
||||
assert_eq!(resolve(&d2, &a), Resolved::Lost);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_ambiguous_quote_is_lost() {
|
||||
let ch = "<p>ano ano</p><p>ano ano</p>";
|
||||
let d1 = open(&Epub::new("A").chapter("A", ch).build(), None, &Limits::default()).unwrap();
|
||||
let a = anchor(&d1, 0, 0, 3).unwrap();
|
||||
// The same file: exact.
|
||||
assert!(matches!(resolve(&d1, &a), Resolved::Exact { .. }));
|
||||
// A new edition where the context no longer tells the occurrences apart.
|
||||
let d2 =
|
||||
open(&Epub::new("A").chapter("A", "<p>x</p><p>ano ano</p><p>ano ano</p>").build(), None, &Limits::default())
|
||||
.unwrap();
|
||||
let r = resolve(&d2, &a);
|
||||
assert!(r == Resolved::Lost || quote_at(&d2, &r) == "ano", "{r:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn footnotes_and_links() {
|
||||
let d = open(&book(CH1), None, &Limits::default()).unwrap();
|
||||
assert!(d.chapters[1].xhtml.contains("data-href=\"1#n1\""), "{}", d.chapters[1].xhtml);
|
||||
assert_eq!(d.footnotes.get("1#n1").map(|s| s.contains("Poznámka pod čarou")), Some(true));
|
||||
}
|
||||
Reference in New Issue
Block a user