Skip to content

Commit 52b0a55

Browse files
committed
Make document resource name matching flexible
1 parent 9c29458 commit 52b0a55

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

src/xml_util.rs

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl XMLUtil {
3434
None => src_file
3535
};
3636

37-
Self::snr_xml(Mode::Value, dir, src_file, Some(vec!("word/document.xml")), Some(pattern), Some(replace), Some(out_file));
37+
Self::snr_xml(Mode::Value, dir, src_file, Some(vec!("word/document(\\d*).xml")), Some(pattern), Some(replace), Some(out_file));
3838
}
3939

4040
pub fn replace_attr(dir: &str, src_file: &str, pattern: &str, replace: &str, output_file: &Option<&str>) {
@@ -43,7 +43,7 @@ impl XMLUtil {
4343
None => src_file
4444
};
4545

46-
Self::snr_xml(Mode::Attribute, dir, src_file, Some(vec!("word/_rels/document.xml.rels")), Some(pattern), Some(replace), Some(out_file));
46+
Self::snr_xml(Mode::Attribute, dir, src_file, Some(vec!("word/_rels/document(\\d*).xml.rels")), Some(pattern), Some(replace), Some(out_file));
4747
}
4848

4949
fn snr_xml(mode: Mode, dir: &str, src_file: &str, files: Option<Vec<&str>>, pattern: Option<&str>, replace: Option<&str>, output_file: Option<&str>) {
@@ -64,7 +64,7 @@ impl XMLUtil {
6464
let sub_path = FileUtil::get_sub_path(entry.path(), &base_dir);
6565

6666
if let Some(file_list) = &files {
67-
if !file_list.contains(&sub_path.as_str()) {
67+
if !Self::list_matches(&file_list, &sub_path.as_str()) {
6868
continue;
6969
}
7070
} else {
@@ -186,6 +186,17 @@ impl XMLUtil {
186186
let mut file = File::open(path).unwrap();
187187
Bom::from(&mut file)
188188
}
189+
190+
fn list_matches(file_list: &[&str], name: &str) -> bool {
191+
for file_pat in file_list {
192+
let regex = Regex::new(*file_pat).unwrap();
193+
if regex.is_match(name) {
194+
return true;
195+
}
196+
}
197+
198+
false
199+
}
189200
}
190201

191202
#[cfg(test)]
@@ -283,7 +294,7 @@ mod tests {
283294
"www.example.com", "foobar.org",
284295
&Some(&testdir.join("output-2.docx").to_string_lossy()));
285296

286-
let after_doc = fs::read_to_string("./src/test/test_tree2/word/document.xml")?;
297+
let after_doc = fs::read_to_string(testdir.join("word/document.xml"))?;
287298
let after = fs::read_to_string(testdir.join("word/_rels/document.xml.rels"))?;
288299

289300
assert!(after.contains("Target=\"http://foobar.org/\""));
@@ -292,6 +303,48 @@ mod tests {
292303
Ok(())
293304
}
294305

306+
#[test]
307+
fn test_replace_both() -> io::Result<()> {
308+
let orgdir = "./src/test/test_tree3";
309+
let testdir = testdir!();
310+
311+
copy_dir_all(orgdir, &testdir)?;
312+
313+
let before = fs::read_to_string("./src/test/test_tree3/word/document2.xml")?;
314+
assert!(before.contains("And some some more text"), "Precondition");
315+
assert!(before.contains("and then some"), "Precondition");
316+
assert!(before.contains("Something here"), "Precondition");
317+
assert!(before.contains(">some<"), "Precondition");
318+
assert!(before.contains(">Some <"), "Precondition");
319+
assert!(before.contains(">www.example.com<"), "Precondition");
320+
assert!(!before.contains("zzz"), "Precondition");
321+
322+
let before_rels = fs::read_to_string("./src/test/test_tree3/word/_rels/document3.xml.rels")?;
323+
assert!(before_rels.contains("Target=\"http://www.example.com/\""), "Precondition");
324+
325+
XMLUtil::replace_xml(&testdir.to_string_lossy(), "my-source.docx",
326+
"[Ss]ome", "zzz",
327+
&Some(&testdir.join("output.docx").to_string_lossy()));
328+
XMLUtil::replace_attr(&testdir.to_string_lossy(), "my-source.docx",
329+
"www.example.com", "foobar.org",
330+
&Some(&testdir.join("output-2.docx").to_string_lossy()));
331+
332+
// Check that the replacement worked as expected
333+
let after = fs::read_to_string(testdir.join("word/document2.xml"))?;
334+
assert!(after.contains("And zzz zzz more text"));
335+
assert!(after.contains("and then zzz"));
336+
assert!(after.contains("zzzthing here"));
337+
assert!(after.contains(">zzz"));
338+
assert!(after.contains(">www.example.com<"), "Should not have changed the document text");
339+
assert!(!after.contains("some"));
340+
assert!(!after.contains("Some"));
341+
342+
let after_rels = fs::read_to_string(testdir.join("word/_rels/document3.xml.rels"))?;
343+
assert!(after_rels.contains("Target=\"http://foobar.org/\""));
344+
345+
Ok(())
346+
}
347+
295348
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
296349
fs::create_dir_all(&dst)?;
297350
for entry in fs::read_dir(src)? {

0 commit comments

Comments
 (0)