o
    N=&b0                     @   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   sX   |   }|r| j }| }|| |  }|r*| j }| }|| d S d S N)generate_headerdocumentZget_decorationZ
get_headerextendgenerate_footerZ
get_footer)selfZheader_nodesZ
decorationheaderZfooter_nodesZfooter r   Ausr/lib/python3.10/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rg }|jr|js|jr:|jr"|j}nt|j	|j}|
tjdd|dtdg |jrRt|jt }|td| d  |jrt|
tdtjddddtd	tjdd
ddtdg tjddg|R  g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
   settings	generator	datestampZsource_linkZ
source_url_sourcer   Z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                   @   $   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   sV   | j jjr'| j | jD ]}| j jjD ]}t||d }|d ur%||d| < qqd S 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                   @      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   sf   | j j}dd |D }|r1tjdgd}|tdd7 }||7 }g | j jd d < |  j |7  _ d S 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   Z
unfilteredmessagesr6   r   r   r   r   z   s   zMessages.applyNr   r    r!   r"   r#   r   r   r   r   r   r.   q       r.   c                   @   r-   )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 ]&}|d | jjjk r/|j| z| jj	|d d = W q	 t
y.   Y q	w q	t | jtjD ]}|d | jj	vrO|j|t|  q9| jtjD ]}d|d v rkt|dkrk|j| qWd S )Nlevelidsr   Zrefidr3   r4      )tupler
   r*   r   Zsystem_messagereporterZreport_levelr/   remover=   
IndexErrorproblematicreplacer   Zastextr6   lenr'   r   r   r   r      s$   zFilterMessages.applyNr9   r   r   r   r   r;      s    r;   c                   @   r-   )TestMessageszk
    Append all post-parse system messages to the end of the document.

    Used for testing purposes.
    ip  c                 C   s&   | j jD ]}|js|  j |7  _ qd S r   )r
   r5   r/   )r   r1   r   r   r   r      s
   zTestMessages.applyNr9   r   r   r   r   rF      s    rF   c                   @   r-   )StripCommentszq
    Remove comment elements from the document tree (only if the
    ``strip_comments`` setting is enabled).
    i  c                 C   s6   | j jjrt| j tjD ]
}|j| qd S d S r   )	r
   r   strip_commentsr?   r*   r   commentr/   rA   r'   r   r   r   r      s
   
zStripComments.applyNr9   r   r   r   r   rG      r:   rG   c                   @   r$   )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h | j jj| _t| j | jD ]}|j| q| j jj	s&d S | j jj	}| j t
jD ]}|D ]}z	|d | W q6 tyJ   Y q6w q2d S )Nr4   )r
   r   Zstrip_elements_with_classesstrip_elementsr?   r*   check_classesr/   rA   strip_classesr   Element
ValueError)r   r(   rM   class_valuer   r   r   r      s$   


zStripClassesAndElements.applyc                 C   s:   t |tjsdS |d d d  D ]
}|| jv r dS qdS )NFr4   T)r&   r   rN   rK   )r   r(   rP   r   r   r   rL      s   
z%StripClassesAndElements.check_classesN)r   r    r!   r"   r#   r   rL   r   r   r   r   rJ      s
    rJ   c                   @   sb   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
   rR   r   r   r   rS     s   zSmartQuotes.__init__c                 c   sZ    |D ]'}t |j| jst |jj| jrdt|fV  qtddt|}d|fV  qd S )Nliteralz(?<= )([-\'".`])z\\\1plain)r&   r/   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y   d}Y nw | j jj}| j jj}|r4tjj	
t| | j tjD ]|}t|| jrDq;t|jtjrLq;dd |tjD }||}|rmd|v ri|dd}n|d7 }t|D ]}|tjj	v r~|} nqr|| jvr| j jjd| |d	 | j| d}tj| || j|d
}	t||	D ]\}
}|
j|
t| qq;| j  d S )Nsmart_quotesFZaltc                 S   s   g | ]}t |jtjs|qS r   )r&   r/   r   Zoption_string)r0   txtnoder   r   r   r2   0  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_skipr/   r   Zget_language_coderD   r   Znormalize_language_tagrU   r@   warningaddZeducate_tokensr^   smartquotes_actionzipclear)r   r_   alternativeZdocument_languageZlc_smartquotesr(   r\   langtagZteacherr`   Znewtextr   r   r   r     sZ   




zSmartQuotes.applyN)r   r    r!   r"   r#   r   ZFixedTextElementSpecialri   imagerV   mathrawrC   rX   rl   rS   r^   r   r   r   r   r   rQ      s    rQ   )r"   Z__docformat__rZ   r   docutilsr   r   Zdocutils.transformsr   Zdocutils.utilsr   r   r%   r.   r;   rF   rG   rJ   rQ   r   r   r   r   <module>   s   ?&