@@ -498,6 +498,24 @@ def clean(document):
498
498
499
499
return newdocument
500
500
501
+ def findTypeParent (element , tag ):
502
+ """ Finds fist parent of element of the given type
503
+
504
+ @param object element: etree element
505
+ @param string the tag parent to search for
506
+
507
+ @return object element: the found parent or None when not found
508
+ """
509
+
510
+ p = element
511
+ while True :
512
+ p = p .getparent ()
513
+ if p .tag == tag :
514
+ return p
515
+
516
+ # Not found
517
+ return None
518
+
501
519
def advReplace (document ,search ,replace ,bs = 3 ):
502
520
'''Replace all occurences of string with a different string, return updated document
503
521
@@ -606,15 +624,19 @@ def advReplace(document,search,replace,bs=3):
606
624
# The match occurred in THIS element. Puth in the
607
625
# whole replaced text
608
626
if isinstance (replace , etree ._Element ):
609
- # If I'm replacing with XML, clear the text in the
610
- # tag and append the element
611
- searchels [i ].text = re .sub (search ,'' ,txtsearch )
612
- searchels [i ].append (replace )
613
- elif type (replace ) == list or type (replace ) == tuple :
627
+ # Convert to a list and process it later
628
+ replace = [ replace , ]
629
+ if type (replace ) == list or type (replace ) == tuple :
614
630
# I'm replacing with a list of etree elements
631
+ # clear the text in the tag and append the element after the
632
+ # parent paragraph
633
+ # (because t elements cannot have childs)
634
+ p = findTypeParent (searchels [i ], '{%s}p' % nsprefixes ['w' ])
615
635
searchels [i ].text = re .sub (search ,'' ,txtsearch )
636
+ insindex = p .getparent ().index (p ) + 1
616
637
for r in replace :
617
- searchels [i ].append (r )
638
+ p .getparent ().insert (insindex , r )
639
+ insindex += 1
618
640
else :
619
641
# Replacing with pure text
620
642
searchels [i ].text = re .sub (search ,replace ,txtsearch )
0 commit comments