DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
IDML: TOC styles read InDesign's $ID/-prefixed separators and Properties-child entry styles
Aykut Firatd98b2cce0130
diff --git a/crates/idml/src/import.rs b/crates/idml/src/import.rsindex 5cd3746a9..cc438de6a 100644--- a/crates/idml/src/import.rs+++ b/crates/idml/src/import.rs@@ -1506,9 +1506,10 @@ impl<'r> Importer<'r> {_ => TocPageNumber::After,};let separator = e.get("Separator").map(crate::names::toc_separator_in).filter(|s| s != "\t" && s.chars().count() <= 64);- let entry_style = known(e.get("FormatStyle")).filter(|n| *n != format!("TOC Level {level}"));- let page_number_style = known_chars(e.get("PageNumberStyle"));- let separator_style = known_chars(e.get("SeparatorStyle"));+ // InDesign writes these as `Properties` children; attributes are read too.+ let entry_style = known(e.prop("FormatStyle").as_deref()).filter(|n| *n != format!("TOC Level {level}"));+ let page_number_style = known_chars(e.prop("PageNumberStyle").as_deref());+ let separator_style = known_chars(e.prop("SeparatorStyle").as_deref());let sort = e.get("Sort").is_some_and(|v| v.trim() == "true");Some(TocEntry { style, level, entry_style, page_number, separator, page_number_style, separator_style, sort })})diff --git a/crates/idml/src/names.rs b/crates/idml/src/names.rsindex 70d02e7c0..c242a1626 100644--- a/crates/idml/src/names.rs+++ b/crates/idml/src/names.rs@@ -843,6 +843,8 @@ pub fn toc_separator_out(s: &str) -> String {}/// IDML TOC separator → text (`^t` and `^y`, the right indent tab, are tabs; other codes stay).pub fn toc_separator_in(s: &str) -> String {+ // InDesign writes its built-in values with the `$ID/` prefix (`$ID/^y`, the right indent tab).+ let s = s.strip_prefix("$ID/").unwrap_or(s);let mut out = String::new();let mut it = s.chars();while let Some(c) = it.next() {diff --git a/crates/idml/src/tests.rs b/crates/idml/src/tests.rsindex 3f5b7977d..785bf458f 100644--- a/crates/idml/src/tests.rs+++ b/crates/idml/src/tests.rs@@ -4189,3 +4189,35 @@ mod liquid_page_rules {}}}++/// A TOC style as InDesign writes it: `$ID/`-prefixed separator (`^y`, the right indent tab) and entry style.+const TOC_STYLES: &str = r#"<idPkg:Styles xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging">+<RootParagraphStyleGroup>+ <ParagraphStyle Self="ParagraphStyle/Heading 1" Name="Heading 1"/>+ <ParagraphStyleGroup Self="psg-toc" Name="TOC">+ <ParagraphStyle Self="ParagraphStyle/TOC%3aTOC body text" Name="TOC:TOC body text" Capitalization="AllCaps"/>+ <ParagraphStyle Self="ParagraphStyle/TOC%3aTOC Header" Name="TOC:TOC Header"/>+ </ParagraphStyleGroup>+</RootParagraphStyleGroup>+<RootCharacterStyleGroup><CharacterStyle Self="CharacterStyle/TOC Page Number" Name="TOC Page Number"/></RootCharacterStyleGroup>+<TOCStyle Self="TOCStyle/$ID/DefaultTOCStyleName" TitleStyle="ParagraphStyle/TOC%3aTOC Header" Title="Table of Contents" Name="$ID/DefaultTOCStyleName">+ <TOCStyleEntry Self="e1" Name="Heading 1" Level="1" PageNumberPosition="AfterEntry" Separator="$ID/^y">+ <Properties><FormatStyle type="object">ParagraphStyle/$ID/TOC%3aTOC body text</FormatStyle>+ <PageNumberStyle type="object">CharacterStyle/TOC Page Number</PageNumberStyle>+ <SeparatorStyle type="string">$ID/[Same Style]</SeparatorStyle></Properties>+ </TOCStyleEntry>+</TOCStyle></idPkg:Styles>"#;++const TOC_STORY: &str = r#"<idPkg:Story xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging">+<Story Self="s1" AppliedTOCStyle="TOCStyle/$ID/DefaultTOCStyleName"><ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/TOC%3aTOC body text"><CharacterStyleRange><Content>Welcome</Content></CharacterStyleRange></ParagraphStyleRange></Story></idPkg:Story>"#;++#[test]+fn toc_style_entries_read_id_prefixed_separators_and_styles() {+ let d = import_idml(&fixture_with_table_styles(TOC_STYLES, TOC_STORY)).unwrap();+ let toc = d.toc.as_ref().expect("the story's TOC style is imported");+ let entry = toc.entries.iter().find(|e| e.style == "Heading 1").unwrap();+ assert_eq!(entry.separator, None, "`$ID/^y` is the right indent tab, the default separator, not text: {:?}", entry.separator);+ assert_eq!(entry.entry_style.as_deref(), Some("TOC/TOC body text"));+ assert_eq!(entry.page_number_style.as_deref(), Some("TOC Page Number"));+ assert_eq!(toc.title_style.as_deref(), Some("TOC/TOC Header"));+}