+
    #jz                         R t ^ RIHu Ht ^ RIt^ RIHt ^ RIt	Rt
R R ltR R ltR R	 ltR
 R ltR R ltR tR R ltR R ltR R ltRR R lltR# )a  Simplify tracked changes by merging adjacent w:ins or w:del elements.

Merges adjacent <w:ins> elements from the same author into a single element.
Same for <w:del> elements. This makes heavily-redlined documents easier to
work with by reducing the number of tracked change wrappers.

Rules:
- Only merges w:ins with w:ins, w:del with w:del (same element type)
- Only merges if same author (ignores timestamp differences)
- Only merges if truly adjacent (only whitespace between them)
N)Pathz<http://schemas.openxmlformats.org/wordprocessingml/2006/mainc                R    V ^8  d   QhR\         R\        \        \         3,          /# )   	input_dirreturn)strtupleint)formats   "ځ/opt/hermes-venv/lib/python3.14/site-packages/../../../skills/productivity/powerpoint/scripts/office/helpers/simplify_redlines.py__annotate__r      s"          sCx      c                    \        V 4      R ,          R,          pVP                  4       '       g	   ^ RV R23#  \        P                  P	                  VP                  RR7      4      pVP                  p^ p\        VR4      \        VR4      ,           pV F)  pV\        VR4      ,          pV\        VR	4      ,          pK+  	  VP                  VP                  R
R7      4       VRV R23#   \         d   p^ RT 23u Rp?# Rp?ii ; i)worddocument.xmlzError: z
 not foundzutf-8)encodingptcinsdelzUTF-8zSimplified z tracked changesN)r   exists
defusedxmlminidomparseString	read_textdocumentElement_find_elements_merge_tracked_changes_inwrite_bytestoxml	Exception)r   doc_xmldomrootmerge_count
containers	containeres   &       r   simplify_redlinesr(      s   9o&7G>>GG9J///   ,,W->->->-PQ""#D#.d1KK
#I4YFFK4YFFK $ 	CIIwI78k+6FGGG  GA3- s   B1C+ +D6D<DDc                0    V ^8  d   QhR\         R\        /# r   tagr   )r   r	   )r
   s   "r   r   r   /   s      c c r   c                    ^ pV P                    Uu. uF5  pVP                  VP                  8X  g   K   \        W14      '       g   K3  VNK7  	  pp\	        V4      ^8  d   ^ # ^ pV\	        V4      ^,
          8  ds   WE,          pWE^,           ,          p\        Wg4      '       d@   \        Wg4       V P                  V4       VP                  V^,           4       V^,          pK~  V^,          pK  V# u upi )    )	
childNodesnodeTypeELEMENT_NODE_is_elementlen_can_merge_tracked_merge_tracked_contentremoveChildpop)r&   r+   r$   childtrackedicurr	next_elems   &&      r   r   r   /   s    K )))E>>U/// 	4?4K 	)   7|a	A
c'lQ
zEN	d.."43!!),KKA1KFA-s   C-C-C-c                0    V ^8  d   QhR\         R\        /# r*   )r   bool)r
   s   "r   r   r   K   s     3 33 34 3r   c                     V P                   ;'       g    V P                  pW!8H  ;'       g    VP                  R V 24      # ):)	localNametagNameendswith)noder+   names   && r   r1   r1   K   s6    >>))T\\D;22$--!C5	22r   c                $    V ^8  d   QhR\         /# r   r   )r   )r
   s   "r   r   r   P   s       r   c                     V P                  R 4      pV'       gb   V P                  P                  4        FC  pVP                  R8X  g$   VP                  P                  R4      '       g   K7  VP                  u # 	  V# )zw:authorauthorz:author)getAttribute
attributesvaluesr@   rD   rB   value)elemrH   attrs   &  r   _get_authorrO   P   s]    z*FOO**,D~~)TYY-?-?	-J-Jzz! - Mr   c                $    V ^8  d   QhR\         /# rF   )r=   )r
   s   "r   r   r   Y   s       r   c                 @   \        V 4      \        V4      8w  d   R # V P                  pV'       do   W!8w  di   VP                  VP                  8X  d   R # VP                  VP                  8X  d#   VP
                  P                  4       '       d   R # VP                  pKv  R# )FT)rO   nextSiblingr/   r0   	TEXT_NODEdatastrip)elem1elem2rC   s   && r   r3   r3   Y   so    5[//D
4===D---==DNN*tyy/@/@r   c                     VP                   '       d1   VP                   pVP                  V4       V P                  V4       KB  R # )N)
firstChildr5   appendChild)targetsourcer7   s   && r   r4   r4   h   s:    



!!5!5! r   c                0    V ^8  d   QhR\         R\        /# r*   )r   list)r
   s   "r   r   r   o   s      c d r   c                 0   aaa . oVVV3R  loS! V 4       S# )c                   < V P                   V P                  8X  dp   V P                  ;'       g    V P                  pVS8X  g   VP	                  R S 24      '       d   SP                  V 4       V P                   F  pS! V4       K  	  R# R# )r?   N)r/   r0   r@   rA   rB   appendr.   )rC   rD   r7   resultsr+   traverses   &  r   rc    _find_elements.<locals>.traverser   sk    ==D--->>11T\\Ds{dmmauI66t$ )	 .r    )r#   r+   rb   rc   s   &f@@r   r   r   o   s    G  TNNr   c                R    V ^8  d   QhR\         R\        \        \        3,          /# )r   doc_xml_pathr   r   dictr   r	   )r
   s   "r   r   r   ~   s"      T d38n r   c                    V P                  4       '       g   / #  \        P                  ! V 4      pVP                  4       pR \
        /pR\
         R2p/ pR FU  pTP                  RT 2T4       F9  pTP                  T4      pT'       g   K  TP                  T^ 4      ^,           YX&   K;  	  KW  	  T#   \        P                   d    / u # i ; i)w{}author.//w:r   r   )r   ETparsegetroot
ParseErrorWORD_NSfindallget)	rg   treer#   
namespacesauthor_attrauthorsr+   rM   rH   s	   &        r   get_tracked_change_authorsr{   ~   s      	xx%||~ wJwix(K GLL5
;DXXk*Fv")++fa"81"< <  N == 	s   &B1 1C
Cc                R    V ^8  d   QhR\         R\        \        \        3,          /# )r   	docx_pathr   rh   )r
   s   "r   r   r      s"      d tCH~ r   c           
          \         P                  ! V R 4      ;_uu_ 4       pRVP                  4       9  d   / uuRRR4       # VP                  R4      ;_uu_ 4       p\        P
                  ! V4      pVP                  4       pR\        /pR\         R2p/ pR FU  pVP                  RV 2V4       F9  p	V	P                  V4      p
V
'       g   K  VP                  V
^ 4      ^,           Wz&   K;  	  KW  	  VuuRRR4       uuRRR4       #   + '       g   i     M; i RRR4       R#   + '       g   i     R# ; i  \         P                  \        P                  3 d    / u # i ; i)rzword/document.xmlNrk   rl   rm   rn   ro   )zipfileZipFilenamelistopenrp   rq   rr   rt   ru   rv   
BadZipFilers   )r}   zffrw   r#   rx   ry   rz   r+   rM   rH   s   &          r   _get_authors_from_docxr      s   __Y,,""++-7 -, ,--xx{||~!7^
 "7)84*,)C $uSE]J G!%+!6!6.5kk&!.Dq.HGO !H *
  .- -, .-- -,,," . 	s^   D: D&
D: D&A.D	$D	3
D&=
D: DD&D: &D7	1D: 7D: :'E$#E$c                H    V ^8  d   QhR\         R\         R\        R\        /# )r   modified_diroriginal_docxdefaultr   )r   r   )r
   s   "r   r   r      s)      t D 3 VY r   c                 `   V R ,          R,          p\        V4      pV'       g   V# \        V4      p/ pVP                  4        F,  w  rxVP                  V^ 4      p	W,
          p
V
^ 8  g   K(  WV&   K.  	  V'       g   V# \	        V4      ^8X  d   \        \        V4      4      # \        RV R24      h)r   r   z$Multiple authors added new changes: z(. Cannot infer which author to validate.)r{   r   itemsrv   r2   nextiter
ValueError)r   r   r   modified_xmlmodified_authorsoriginal_authorsnew_changesrH   countoriginal_countdiffs   &&&        r   infer_authorr      s    &(>9L1,?-m<"$K)//1)--fa8%!8"&	 2 
;1D%&&

.{m <1 	1 r   )Claude)__doc__xml.etree.ElementTreeetreeElementTreerp   r   pathlibr   defusedxml.minidomr   rt   r(   r   r1   rO   r3   r4   r   r{   r   r   re   r   r   <module>r      sW   
 # "   
H 283
".. r   