//! A table of contents in an ODT file (ODF 1.3 §8.3 `text:table-of-content`) opens as one //! WordCraft updates: References › Update Table regenerates its entries from the document's //! headings, with or without a title, and it is saved back as an ODF table of contents. The ODT //! is original and synthetic, shaped like LibreOffice's. use serde_json::json; use wordcraft_engine::{Session, io}; const NS: &str = r#"xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0""#; /// A document with a table of contents whose entries are stale (made before the headings were /// renamed), titled or not. fn odt(titled: bool) -> Vec { let title = if titled { r#"Contents"# } else { "" }; let content = format!( r#" {title}Old chapter1 PlanningText. BudgetMore text. Too deepLast. "# ); let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str())] { z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } z.finish().unwrap() } /// The text of the body's paragraphs in the TOC styles (without an untitled table's field /// paragraph, which the engine keeps where the table started). fn entries(s: &Session) -> Vec { s.doc .body .iter() .filter_map(|b| b.as_para()) .filter(|p| p.props.style.as_deref().is_some_and(|st| st.starts_with("TOC") && st != "TOCHeading")) .map(|p| p.text.split('\t').next().unwrap_or("").trim_matches(wordcraft_doc::para::OBJ).to_string()) .filter(|t| !t.is_empty()) .collect() } #[test] fn an_odt_table_of_contents_updates_from_the_headings() { for titled in [true, false] { let doc = io::open_bytes("toc.odt", &odt(titled)).unwrap(); let mut s = Session::new(doc); assert_eq!(entries(&s), ["Old chapter"], "titled: {titled}"); s.run("references.updateToc", &json!({})).unwrap(); // Two levels, as the file's source says. assert_eq!(entries(&s), ["Planning", "Budget"], "titled: {titled}"); // Saved and opened again, it is still a table of contents. let again = io::open_bytes("toc.odt", &io::save_bytes("toc.odt", &s.doc).unwrap()).unwrap(); let mut s2 = Session::new(again); assert_eq!(entries(&s2), ["Planning", "Budget"], "titled: {titled}"); s2.run("references.updateToc", &json!({})).unwrap(); assert_eq!(entries(&s2), ["Planning", "Budget"], "titled: {titled}"); } } #[test] fn an_odt_index_updates_from_its_marks() { // An alphabetical index whose entries are index marks in the text (`XE` fields once open): // Update Index lists them, rather than emptying the index. let content = format!( r#" stale, 9 Clay is fired; glazes shine. "# ); let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str())] { z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } let mut s = Session::new(io::open_bytes("index.odt", &z.finish().unwrap()).unwrap()); s.run("references.updateIndex", &json!({})).unwrap(); let text: Vec = s.doc.body.iter().filter_map(|b| b.as_para()).map(|p| p.text.replace(wordcraft_doc::para::OBJ, "")).collect(); assert!(text.iter().any(|t| t.starts_with("glazes")), "{text:?}"); assert!(text.iter().any(|t| t.starts_with("kilns")), "{text:?}"); assert!(!text.iter().any(|t| t.contains("stale")), "{text:?}"); } #[test] fn an_odt_bibliography_updates_from_its_citations_sources() { // A bibliography and a citation carrying its source: Update lists the source. let content = format!( r#" Clay [Smi20]. stale "# ); let mut z = craft_zip::Writer::new(); for (name, data) in [("mimetype", "application/vnd.oasis.opendocument.text"), ("content.xml", content.as_str())] { z.add(name, data.as_bytes(), craft_zip::Compression::Stored).unwrap(); } let mut s = Session::new(io::open_bytes("bib.odt", &z.finish().unwrap()).unwrap()); s.run("references.updateIndex", &json!({})).unwrap(); let text: Vec = s.doc.body.iter().filter_map(|b| b.as_para()).map(|p| p.text.replace(wordcraft_doc::para::OBJ, "")).collect(); assert!(text.iter().any(|t| t.contains("Kilns and Fire")), "{text:?}"); assert!(!text.iter().any(|t| t.contains("stale")), "{text:?}"); }