B
    ©‰Ad#  ã               @   s8   d dl mZ d dlmZ d dlmZ G dd„ deƒZdS )é    )Úxpath)ÚElement)ÚQNamec               @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚStructuredDocumentTagsz/Structured Document Tags (aka Content Controls)c             C   s
   || _ d S )N)Údoc)Úselfr   © r   úA/var/www/html/venv/lib/python3.7/site-packages/docxcompose/sdt.pyÚ__init__	   s    zStructuredDocumentTags.__init__c             C   s   t | jjjd| ƒS )z&Get Structured Document Tags by alias.z5.//w:sdt/w:sdtPr/w:alias[@w:val="%s"]/ancestor::w:sdt)r   r   ÚelementÚbody)r   Úaliasr   r   r	   Útags_by_alias   s    z$StructuredDocumentTags.tags_by_aliasc             C   s(  |  ¡ }|  |¡}x|D ]}t|dƒ}|s0q|j}t|d jd|dƒ}t|dƒ}t|dƒ}	|	sfqt|	d dƒ}
|
szq|
d }|  ||	|¡ |  |
¡ |  |¡ |s´| dd	¡}| 	¡ }xbt
|d
dD ]R\}}tt|d dƒƒ}||_| |¡ |t|ƒkrÊtt|d dƒƒ}| |¡ qÊW qW dS )a3  Set the text content of all Structured Document Tags identified by
        an alias. Only plain text SDTs are supported.

        If the SDT has the 'multiLine' property, newlines in `text` will be
        respected, and the SDTs content will be updated with lines separated
        by line breaks.
        z./w:sdtPr/w:textr   z./@w:multiLine)Ú
namespacesz	./w:sdtPrz./w:sdtContentz.//w:rÚ
ú é   )ÚstartÚwÚtÚbrN)Ústripr   r   ÚnsmapÚboolÚ_remove_placeholderÚ_remove_all_runs_except_firstÚ_clean_first_runÚreplaceÚ
splitlinesÚ	enumerater   r   ÚtextÚappendÚlen)r   r   r    ÚtagsÚtagÚ
plain_textr   Zis_multilineÚ
propertiesÚcontentÚrun_elementsÚ	first_runÚlinesÚiÚlineZtxt_noder   r   r   r	   Úset_text   s:    






zStructuredDocumentTags.set_textc             C   sD   t |d dƒ}|r@|d  |d ¡ t |dƒ}|r@| |d ¡ dS )z-Remove placeholder marker and style.
        r   z./w:showingPlcHdrz./w:rPrN)r   Úremove)r   r&   r'   r)   Zshowing_placeholderZ	run_propsr   r   r	   r   P   s    
z*StructuredDocumentTags._remove_placeholderc             C   s(   x"|dd… D ]}|  ¡  |¡ qW dS )z.Remove all runs except the first one.
        r   N)Z	getparentr.   )r   r(   Úrunr   r   r	   r   Z   s    z4StructuredDocumentTags._remove_all_runs_except_firstc             C   s0   x*|  ¡ D ]}t|ƒjdkrq
| |¡ q
W dS )zFRemove all elements from the first run except run formatting.
        ZrPrN)Zgetchildrenr   Ú	localnamer.   )r   r)   Úchildr   r   r	   r   `   s    z'StructuredDocumentTags._clean_first_runc             C   s€   |   |¡}xp|D ]h}t|dƒs qg }t|dƒ}x@|D ]8}t|ƒjdkrT| |j¡ q4t|ƒjdkr4| d¡ q4W d |¡S W dS )zlGet the text content of the first Structured Document Tag identified
           by the given alias.
        z./w:sdtPr/w:textz../w:sdtContent//w:r/*[self::w:t or self::w:br]r   r   r   Ú N)r   r   r   r0   r!   r    Újoin)r   r   r#   r$   ÚtokensZtext_and_brsÚelr   r   r	   Úget_texti   s    




zStructuredDocumentTags.get_textN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r-   r   r   r   r6   r   r   r   r	   r      s   >
	r   N)Zdocxcompose.utilsr   Z
lxml.etreer   r   Úobjectr   r   r   r   r	   Ú<module>   s   