U
    O’“e1  ã                   @   sÄ   d Z dZddlZddlZddlmZmZ ddlmZ ddl	m
Z
 G dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )a’  
Transforms needed by most or all documents:

- `Decorations`: Generate a document's header & footer.
- `ExposeInternals`: Expose internal attributes.
- `Messages`: Placement of system messages generated after parsing.
- `FilterMessages`: Remove system messages below verbosity threshold.
- `TestMessages`: Like `Messages`, used on test runs.
- `StripComments`: Remove comment elements from the document tree.
- `StripClassesAndElements`: Remove elements with classes
  in `self.document.settings.strip_elements_with_classes`
  and class values in `self.document.settings.strip_classes`.
- `SmartQuotes`: Replace ASCII quotation marks with typographic form.
ÚreStructuredTexté    N)ÚnodesÚutils)Ú	Transform)Úsmartquotesc                   @   s,   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	S )
ÚDecorationszD
    Populate a document's decoration element (header, footer).
    i4  c                 C   sT   |   ¡ }|r(| j ¡ }| ¡ }| |¡ |  ¡ }|rP| j ¡ }| ¡ }| |¡ d S ©N)Úgenerate_headerÚdocumentZget_decorationÚ
get_headerÚextendÚgenerate_footerZ
get_footer)ÚselfZheader_nodesZ
decorationÚheaderZfooter_nodesÚfooter© r   úA/tmp/pip-unpacked-wheel-rro3auwd/docutils/transforms/universal.pyÚapply&   s    


zDecorations.applyc                 C   s   d S r   r   )r   r   r   r   r	   2   s    zDecorations.generate_headerc              
   C   s   | j j}|js |js |js |jrøg }|jr0|js6|jrt|jrD|j}nt |j	|j¡}| 
tjdd|dt d¡g¡ |jr¤t |jt ¡ ¡}| t d| d ¡¡ |jrè| 
t d¡tjddddt d	¡tjdd
ddt d¡g¡ tjd|žŽ gS d S d S )NÚ zView document source)Zrefuriz.
zGenerated on: zGenerated by ZDocutilsz https://docutils.sourceforge.io/z from r   z(https://docutils.sourceforge.io/rst.htmlz	 source.
)r   r   )r
   ÚsettingsÚ	generatorÚ	datestampZsource_linkÚ
source_urlÚ_sourcer   Úrelative_pathZ_destinationr   r   Ú	referenceÚTextÚtimeÚstrftimeÚgmtimeÚappendZ	paragraph)r   r   ÚtextÚsourcer   r   r   r   r   5   sL    ÿÿÿÿÿýÿÿø	zDecorations.generate_footerN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Údefault_priorityr   r	   r   r   r   r   r   r      s
   r   c                   @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚExposeInternalszL
    Expose internal attributes if ``expose_internals`` setting is set.
    iH  c                 C   s   t |tjƒ S r   )Ú
isinstancer   r   ©r   Únoder   r   r   Únot_Texte   s    zExposeInternals.not_Textc                 C   sR   | j jjrN| j  | j¡D ]4}| j jjD ]$}t||d ƒ}|d k	r&||d| < q&qd S )Nz	internal:)r
   r   Zexpose_internalsÚfindallr,   Úgetattr)r   r+   ZattÚvaluer   r   r   r   h   s    
zExposeInternals.applyN)r#   r$   r%   r&   r'   r,   r   r   r   r   r   r(   ]   s   r(   c                   @   s   e Zd ZdZdZdd„ ZdS )ÚMessageszi
    Place any system messages generated after parsing into a dedicated section
    of the document.
    i\  c                 C   sb   | j j}dd„ |D ƒ}|r^tjdgd}|t dd¡7 }||7 }g | j jd d …< |  j |7  _ d S )Nc                 S   s   g | ]}|j s|‘qS r   )Úparent)Ú.0Úmsgr   r   r   Ú
<listcomp>|   s      z"Messages.apply.<locals>.<listcomp>úsystem-messages)Úclassesr   zDocutils System Messages)r
   Útransform_messagesr   ÚsectionÚtitle)r   ÚmessagesZloose_messagesr8   r   r   r   r   z   s    zMessages.applyN©r#   r$   r%   r&   r'   r   r   r   r   r   r0   q   s   r0   c                   @   s   e Zd ZdZdZdd„ ZdS )ÚFilterMessagesz³
    Remove system messages below verbosity threshold.

    Convert <problematic> nodes referencing removed messages to <Text>.
    Remove "System Messages" section if empty.
    if  c              	   C   sÞ   t | j tj¡ƒD ]N}|d | jjjk r|j |¡ z| jj	|d d = W q t
k
r^   Y qX qt | j tj¡ƒD ],}|d | jj	krt|j |t | ¡ ¡¡ qt| j tj¡D ](}d|d kr°t|ƒdkr°|j |¡ q°d S )NÚlevelÚidsr   Zrefidr5   r6   é   )Útupler
   r-   r   Zsystem_messageÚreporterZreport_levelr1   Úremover>   Ú
IndexErrorÚproblematicÚreplacer   Zastextr8   Úlenr*   r   r   r   r   —   s    zFilterMessages.applyNr;   r   r   r   r   r<   Œ   s   r<   c                   @   s    e Zd ZdZdZdZdd„ ZdS )ÚTestMessageszk
    Append all post-parse system messages to the end of the document.

    Used for testing purposes.
    Fip  c                 C   s&   | j jD ]}|js|  j |7  _ qd S r   )r
   r7   r1   )r   r3   r   r   r   r   ´   s    zTestMessages.applyN)r#   r$   r%   r&   Z__test__r'   r   r   r   r   r   rG   §   s   rG   c                   @   s   e Zd ZdZdZdd„ ZdS )ÚStripCommentszq
    Remove comment elements from the document tree (only if the
    ``strip_comments`` setting is enabled).
    iä  c                 C   s2   | j jjr.t| j  tj¡ƒD ]}|j |¡ qd S r   )	r
   r   Ústrip_commentsr@   r-   r   Úcommentr1   rB   r*   r   r   r   r   Ã   s    
zStripComments.applyNr;   r   r   r   r   rH   º   s   rH   c                   @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚStripClassesAndElementszÏ
    Remove from the document tree all elements with classes in
    `self.document.settings.strip_elements_with_classes` and all "classes"
    attribute values in `self.document.settings.strip_classes`.
    i¤  c              
   C   sž   | j jjr<| j jj™| _t| j  | j¡ƒD ]}|j |¡ q*| j jj	sJd S | j jj	}| j  t
j¡D ]6}|D ],}z|d  |¡ W qj tk
r”   Y qjX qjqbd S )Nr6   )r
   r   Zstrip_elements_with_classesÚstrip_elementsr@   r-   Úcheck_classesr1   rB   Ústrip_classesr   ÚElementÚ
ValueError)r   r+   rN   Úclass_valuer   r   r   r   Ó   s    


zStripClassesAndElements.applyc                 C   s:   t |tjƒsdS |d d d … D ]}|| jkr  dS q dS )NFr6   T)r)   r   rO   rL   )r   r+   rQ   r   r   r   rM   æ   s    
z%StripClassesAndElements.check_classesN)r#   r$   r%   r&   r'   r   rM   r   r   r   r   rK   É   s   rK   c                   @   s\   e Zd ZdZdZejejfZejejej	ej
ejejejfZdZdd„ Zdd„ Zdd	„ Zd
S )ÚSmartQuotesz
    Replace ASCII quotation marks with typographic form.

    Also replace multiple dashes with em-dash/en-dash characters.
    iW  ZqDec                 C   s   t j| ||d tƒ | _d S )N)Ú	startnode)r   Ú__init__ÚsetÚunsupported_languages)r   r
   rS   r   r   r   rT     s    zSmartQuotes.__init__c                 c   sX   |D ]N}t |j| jƒs&t |jj| jƒr6dt|ƒfV  qt ddt|ƒ¡}d|fV  qd S )NÚliteralz(?<= )([-\'".`])z\\\1Úplain)r)   r1   Úliteral_nodesÚstrÚreÚsub)r   Útxtnodesr+   Útxtr   r   r   Ú
get_tokens  s    ÿzSmartQuotes.get_tokensc                 C   sˆ  | j j dd¡}|sd S z| d¡}W n tk
r>   d}Y nX | j jj}| j jj}|rjtjj	 
t|ƒ¡ | j  tj¡D  ]þ}t|| jƒrŒqxt|jtjƒrœqxdd„ | tj¡D ƒ}| |¡}|rÞd|krÖ| dd¡}n|d7 }t |¡D ]}|tjj	krè|} q6qè|| jkr&| j jjd| |d	 | j |¡ d}tj|  |¡| j|d
}	t||	ƒD ]\}
}|
j |
t |¡¡ qXqx| j ¡  d S )NÚsmart_quotesFZaltc                 S   s   g | ]}t |jtjƒs|‘qS r   )r)   r1   r   Úoption_string)r2   Útxtnoder   r   r   r4   3  s
    ÿÿz%SmartQuotes.apply.<locals>.<listcomp>z
-x-altquotr   z*No smart quotes defined for language "%s".)Z	base_node)ÚattrÚlanguage) r
   r   Ú
setdefaultÚ
startswithÚAttributeErrorZlanguage_codeZsmartquotes_localesr   Z
smartcharsÚquotesÚupdateÚdictr-   r   ZTextElementr)   Únodes_to_skipr1   r   Zget_language_coderE   r   Znormalize_language_tagrV   rA   ÚwarningÚaddZeducate_tokensr_   Úsmartquotes_actionÚzipÚclear)r   r`   ÚalternativeZdocument_languageZlc_smartquotesr+   r]   ÚlangÚtagZteacherrb   Znewtextr   r   r   r     sV    
ÿ



þ þzSmartQuotes.applyN)r#   r$   r%   r&   r'   r   ZFixedTextElementÚSpecialrk   ÚimagerW   ÚmathÚrawrD   rY   rn   rT   r_   r   r   r   r   r   rR   ï   s      þrR   )r&   Z__docformat__r[   r   Zdocutilsr   r   Zdocutils.transformsr   Zdocutils.utilsr   r   r(   r0   r<   rG   rH   rK   rR   r   r   r   r   Ú<module>   s   ?&