U
    [Sh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   P/root/rtd-docs/venv/lib/python3.8/site-packages/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rg }|jr0|js6|jrt|jrD|j}nt|j	|j}|
tjdd|dtdg |jrt|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	datestamp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_messagesr9   r   r   r   r   z   s    zMessages.applyNr$   r%   r&   r'   r(   r   r   r   r   r   r1   q   s   r1   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rt|dkr|j| qd S )Nlevelidsr   Zrefidr6   r7      )tupler
   r.   r   Zsystem_messagereporterZreport_levelr2   remover?   
IndexErrorproblematicreplacer   Zastextr9   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
   r8   r2   )r   r4   r   r   r   r      s    zTestMessages.applyN)r$   r%   r&   r'   Z__test__r(   r   r   r   r   r   rH      s   rH   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_commentsrA   r.   r   commentr2   rC   r+   r   r   r   r      s    
zStripComments.applyNr<   r   r   r   r   rI      s   rI   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 )Nr7   )r
   r   Zstrip_elements_with_classesstrip_elementsrA   r.   check_classesr2   rC   strip_classesr   Element
ValueError)r   r,   rO   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 )NFr7   T)r*   r   rP   rM   )r   r,   rR   r   r   r   rN      s    
z%StripClassesAndElements.check_classesN)r$   r%   r&   r'   r(   r   rN   r   r   r   r   rL      s   rL   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
   rT   r   r   r   rU     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*   r2   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rqxt|jtjrqxdd |tjD }||}|rd|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*   r2   r   option_string)r3   txtnoder   r   r   r5   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_skipr2   r   Zget_language_coderF   r   Znormalize_language_tagrW   rB   warningaddZeducate_tokensr`   smartquotes_actionzipclear)r   ra   alternativeZdocument_languageZlc_smartquotesr,   r^   langtagZteacherrc   Znewtextr   r   r   r     sV    




 zSmartQuotes.applyN)r$   r%   r&   r'   r(   r   ZFixedTextElementSpecialrl   imagerX   mathrawrE   rZ   ro   rU   r`   r   r   r   r   r   rS      s      rS   )r'   __docformat__r\   r   Zdocutilsr   r   Zdocutils.transformsr   Zdocutils.utilsr   r   r)   r1   r=   rH   rI   rL   rS   r   r   r   r   <module>   s   ?&