@@ -34,7 +34,7 @@ impl XMLUtil {
34
34
None => src_file
35
35
} ;
36
36
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) ) ;
38
38
}
39
39
40
40
pub fn replace_attr ( dir : & str , src_file : & str , pattern : & str , replace : & str , output_file : & Option < & str > ) {
@@ -43,7 +43,7 @@ impl XMLUtil {
43
43
None => src_file
44
44
} ;
45
45
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) ) ;
47
47
}
48
48
49
49
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 {
64
64
let sub_path = FileUtil :: get_sub_path ( entry. path ( ) , & base_dir) ;
65
65
66
66
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 ( ) ) {
68
68
continue ;
69
69
}
70
70
} else {
@@ -186,6 +186,17 @@ impl XMLUtil {
186
186
let mut file = File :: open ( path) . unwrap ( ) ;
187
187
Bom :: from ( & mut file)
188
188
}
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
+ }
189
200
}
190
201
191
202
#[ cfg( test) ]
@@ -283,7 +294,7 @@ mod tests {
283
294
"www.example.com" , "foobar.org" ,
284
295
& Some ( & testdir. join ( "output-2.docx" ) . to_string_lossy ( ) ) ) ;
285
296
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") ) ?;
287
298
let after = fs:: read_to_string ( testdir. join ( "word/_rels/document.xml.rels" ) ) ?;
288
299
289
300
assert ! ( after. contains( "Target=\" http://foobar.org/\" " ) ) ;
@@ -292,6 +303,48 @@ mod tests {
292
303
Ok ( ( ) )
293
304
}
294
305
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
+
295
348
fn copy_dir_all ( src : impl AsRef < Path > , dst : impl AsRef < Path > ) -> io:: Result < ( ) > {
296
349
fs:: create_dir_all ( & dst) ?;
297
350
for entry in fs:: read_dir ( src) ? {
0 commit comments