Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge branch 'fix/769-epub-nav-headings' into fix/768-epub-chapters

Gert Pellin8f0a88f628b6
diff --cc crates/epub/src/lib.rs
index 3f6dd820d,429560655..e7ab914f5
--- a/crates/epub/src/lib.rs
+++ b/crates/epub/src/lib.rs
@@@ -998,71 -926,18 +999,82 @@@ mod tests
let lid = plain.default_layer();
plain.add_text_frame(SpreadRef::Doc(0), Rect::new(36.0, 36.0, 300.0, 100.0), lid, "Opening\nrest", ParaFormat::default()).unwrap();
let nav = read(&plain, &EpubOptions::default(), "OEBPS/nav.xhtml");
- assert!(nav.contains(&format!("<h1>Contents</h1><ol><li>{}</li></ol>", link("s0", "Opening"))), "{nav}");
+ assert!(nav.contains(&format!("<h1>Contents</h1><ol><li>{}</li></ol>", link("content.xhtml", "s0", "Opening"))), "{nav}");
+ // A heading shallower than the one before it but deeper than its parent stays under the
+ // parent: h1 A, h3 B, h2 C nest B and C under A.
+ let e = |t: &str, l: u8| (t.to_string(), t.to_string(), l);
+ assert_eq!(
- nav_list(&[e("A", 1), e("B", 3), e("C", 2), e("D", 1)]),
- format!("<li>{}<ol><li>{}</li><li>{}</li></ol></li><li>{}</li>", link("A", "A"), link("B", "B"), link("C", "C"), link("D", "D"))
++ nav_list(&[e("A", 1), e("B", 3), e("C", 2), e("D", 1)], &BTreeMap::new()),
++ format!("<li>{}<ol><li>{}</li><li>{}</li></ol></li><li>{}</li>", link("content.xhtml", "A", "A"), link("content.xhtml", "B", "B"), link("content.xhtml", "C", "C"), link("content.xhtml", "D", "D"))
++ );
++ assert_eq!(
++ nav_list(&[e("A", 1), e("B", 2), e("C", 3), e("D", 1)], &BTreeMap::new()),
++ format!("<li>{}<ol><li>{}<ol><li>{}</li></ol></li></ol></li><li>{}</li>", link("content.xhtml", "A", "A"), link("content.xhtml", "B", "B"), link("content.xhtml", "C", "C"), link("content.xhtml", "D", "D"))
++ );
+ }
+
+ /// #768: the book is split into one file per chapter, before each h1, with well-formed
+ /// sections; the spine and manifest list the files in order.
+ #[test]
+ fn chapters_are_split_at_h1() {
+ let mut d = Document::new(&NewDocument { pages: 3, ..Default::default() });
+ let lid = d.default_layer();
+ d.styles_mut().paragraph.push(designcraft_doc::ParagraphStyle {
+ name: "Chapter".into(),
+ based_on: None,
+ next_style: None,
+ para: Default::default(),
+ chars: CharAttrs::default(),
+ shortcut: String::new(),
+ });
+ d.styles_mut().export_tags.insert("p:Chapter".into(), designcraft_doc::ExportTag { tag: "h1".into(), class: String::new() });
+ // A title page without a heading, then a story that opens with a chapter and has a
+ // second one in the middle.
+ d.add_text_frame(SpreadRef::Doc(0), Rect::new(36.0, 36.0, 300.0, 100.0), lid, "Title page", ParaFormat::default()).unwrap();
+ let (_, sid) = d
+ .add_text_frame(SpreadRef::Doc(0), Rect::new(36.0, 200.0, 300.0, 400.0), lid, "One\nFirst text\nTwo\nSecond text", ParaFormat::default())
+ .unwrap();
+ let st = d.story_mut(sid).unwrap();
+ st.paras[0].style = "Chapter".into();
+ st.paras[2].style = "Chapter".into();
+ let mut z = zip::ZipArchive::new(std::io::Cursor::new(export_epub(&d, &EpubOptions::default()).unwrap())).unwrap();
+ let mut read = |path: &str| {
+ let mut s = String::new();
+ z.by_name(path).unwrap().read_to_string(&mut s).unwrap();
+ s
+ };
+ let (c1, c2, c3) = (read("OEBPS/content.xhtml"), read("OEBPS/content2.xhtml"), read("OEBPS/content3.xhtml"));
+ assert!(c1.contains("Title page") && !c1.contains("One"), "{c1}");
+ assert!(c2.contains("<section id=\"s1\">\n<h1 id=\"s1-p0\"") && c2.contains("First text") && !c2.contains("Two"), "{c2}");
+ assert!(c3.contains("<section>\n<h1 id=\"s1-p2\""), "{c3}");
+ for c in [&c1, &c2, &c3] {
+ assert_eq!(c.matches("<section").count(), c.matches("</section>").count(), "{c}");
+ assert!(!c.contains(CHAPTER_MARK));
+ }
+ let opf = read("OEBPS/content.opf");
+ assert!(opf.contains("<itemref idref=\"content\"/>\n<itemref idref=\"content2\"/>\n<itemref idref=\"content3\"/>\n</spine>"), "{opf}");
+ assert!(opf.contains("<item id=\"content3\" href=\"content3.xhtml\""), "{opf}");
+ let nav = read("OEBPS/nav.xhtml");
+ assert!(nav.contains("href=\"content2.xhtml#s1-p0\"") && nav.contains("href=\"content3.xhtml#s1-p2\""), "{nav}");
+ assert!(z.by_name("OEBPS/content4.xhtml").is_err());
+ // HTML stays one page, without the marks.
+ let html = export_html(&d, &HtmlOptions::default());
+ assert!(!html.contains(CHAPTER_MARK) && html.contains("Second text"));
+ }
+
+ #[test]
+ fn split_chapters_keeps_sections_well_formed() {
+ let m = CHAPTER_MARK;
+ assert_eq!(split_chapters("<section id=\"s0\">\n<p>a</p>\n</section>\n"), vec!["<section id=\"s0\">\n<p>a</p>\n</section>\n"]);
+ assert_eq!(
+ split_chapters(&format!("<section id=\"s0\">\n{m}<h1>A</h1>\n</section>\n")),
+ vec!["<section id=\"s0\">\n<h1>A</h1>\n</section>\n"]
);
assert_eq!(
- nav_list(&[e("A", 1), e("B", 2), e("C", 3), e("D", 1)]),
- format!("<li>{}<ol><li>{}<ol><li>{}</li></ol></li></ol></li><li>{}</li>", link("A", "A"), link("B", "B"), link("C", "C"), link("D", "D"))
+ split_chapters(&format!("<section id=\"s0\">\n<p>a</p>\n{m}<h1>B</h1>\n</section>\n")),
+ vec!["<section id=\"s0\">\n<p>a</p>\n</section>\n", "<section>\n<h1>B</h1>\n</section>\n"]
);
+ assert_eq!(split_chapters(""), vec![String::new()]);
}
#[test]