mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 18:56:30 +01:00
ODT: a new style derives from the file's style under the file's name
A style WordCraft writes that the file didn't have, based on a style the import read under another name (`Base_20_Style`, WordCraft's `BaseStyle`), named WordCraft's name as its parent. That resolved to WordCraft's own copy of the style, without what WordCraft doesn't model of the file's. Its parent and next style now take the file's name for every style read under another name (`kept::file_names`), not only where an unread namesake collides. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
3 files changed
+43
-2
No files matched your search
@@ -333,7 +333,8 @@ pub(super) fn with_common_as_read(
|
||||
_ => Some(d.to_string()),
|
||||
};
|
||||
// A style of WordCraft's own derives from (or is followed by) the file's style a style was
|
||||
// read from, not an unread namesake of it ([`read_names`]).
|
||||
// read from, under the file's name ([`file_names`]): not WordCraft's copy of it, nor an
|
||||
// unread namesake.
|
||||
let fresh = |k: &str, d: &str| {
|
||||
let family = if k.ends_with("|text") { "|text" } else { "" };
|
||||
renamed(d, &|attr, v| (attr != "style:name").then(|| read.get(&format!("{v}{family}")).cloned()).flatten())
|
||||
@@ -417,6 +418,22 @@ pub(super) fn rewritten_defs(
|
||||
out
|
||||
}
|
||||
|
||||
/// The file's names for the styles the import read under another name than WordCraft writes them
|
||||
/// under (`ours`): by that name with its family's key suffix (`BaseStyle`, `BaseStyle|text`),
|
||||
/// unescaped, the first the import read into it where several were.
|
||||
pub(super) fn file_names(ids: &BTreeMap<String, String>, ours: &dyn Fn(&str) -> String) -> std::collections::HashMap<String, String> {
|
||||
let mut out = std::collections::HashMap::new();
|
||||
for (key, id) in ids {
|
||||
let Some(rest) = key.strip_prefix("common:") else { continue };
|
||||
let (name, family) = rest.strip_suffix("|text").map_or((rest, ""), |n| (n, "|text"));
|
||||
let mine = format!("{}{family}", ours(id));
|
||||
if mine != rest {
|
||||
out.entry(mine).or_insert_with(|| name.to_string());
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The file's names for the styles the import read whose name as WordCraft writes them (`ours`)
|
||||
/// is a style of the file's the import didn't read (`MyCustom` beside `My_20_Custom`): by that
|
||||
/// name with its family's key suffix (`MyCustom`, `MyCustom|text`), unescaped. That namesake keeps
|
||||
|
||||
@@ -848,7 +848,7 @@ fn styles_as_read(doc: &Document, kept: Option<&craft_wordmodel::kept::KeptXml>,
|
||||
// The name WordCraft writes a style id under (`My_20_Custom` was read as `MyCustom`).
|
||||
let ours = |id: &str| super::writer::odf_name(id);
|
||||
let rewritten = super::kept::rewritten_defs(&now, &k.defs, &changed, &k.style_ids, &ours);
|
||||
let read = super::kept::read_names(&k.defs, &k.style_ids, &ours);
|
||||
let read = super::kept::file_names(&k.style_ids, &ours);
|
||||
super::kept::with_common_as_read(&styles, &k.defs, &k.order, &changed, &rewritten, &read)
|
||||
}
|
||||
|
||||
|
||||
@@ -493,6 +493,30 @@ fn a_new_style_based_on_one_beside_an_unused_namesake_derives_from_it() {
|
||||
assert_eq!(got, 9.0, "{saved}\n{}", content_of(&bytes));
|
||||
}
|
||||
|
||||
/// A style made after reading (`NewChild`), based on a file's style WordCraft names otherwise
|
||||
/// (`Base_20_Style`, WordCraft's `BaseStyle`), derives from the file's style, with what WordCraft
|
||||
/// doesn't model of it.
|
||||
#[test]
|
||||
fn a_new_style_based_on_a_renamed_one_derives_from_the_files() {
|
||||
use std::sync::Arc;
|
||||
let s = r#"<style:style style:name="Standard" style:family="paragraph" style:class="text"/><style:style style:name="Base_20_Style" style:display-name="Base Style" style:family="paragraph" style:parent-style-name="Standard"><style:paragraph-properties style:writing-mode="tb-rl"/><style:text-properties fo:font-size="30pt"/></style:style>"#;
|
||||
let mut d = opened(s, &["Base_20_Style"]);
|
||||
let id = d.body[0].as_para().unwrap().props.style.clone().unwrap();
|
||||
let mut child = d.styles.get(&id).unwrap().clone();
|
||||
(child.id, child.name, child.based_on, child.next) = ("NewChild".into(), "NewChild".into(), Some(id.clone()), None);
|
||||
(child.para, child.chr) = Default::default();
|
||||
Arc::make_mut(&mut d.styles).upsert(child);
|
||||
d.restyle(&id, Some("NewChild".to_string()));
|
||||
let bytes = export(&d).unwrap();
|
||||
let saved = styles_of(&bytes);
|
||||
let new = saved.split("<style:style ").find(|t| t.starts_with(r#"style:name="NewChild""#)).and_then(|t| t.split('>').next());
|
||||
assert!(new.is_some_and(|n| n.contains(r#"style:parent-style-name="Base_20_Style""#)), "{saved}");
|
||||
assert!(def_named(&saved, "Base_20_Style").is_some_and(|d| d.contains(r#"style:writing-mode="tb-rl""#)), "as read: {saved}");
|
||||
let back = import(&bytes).unwrap();
|
||||
let got = back.styles.resolve_char(back.body[0].as_para().unwrap().props.style.as_deref(), &Default::default()).size;
|
||||
assert_eq!(got, 30.0, "{saved}");
|
||||
}
|
||||
|
||||
/// The same for a character style: edited text's automatic style derives from the file's
|
||||
/// character style it was read with (`My_20_Accent`), not an unused namesake (`MyAccent`).
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user