o
    Ni]0                     @   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documentget_decoration
get_headerextendgenerate_footer
get_footer)selfheader_nodes
decorationheaderfooter_nodesfooter r   Q/home/ubuntu/.local/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)refuriz.
zGenerated on: zGenerated by Docutilsz https://docutils.sourceforge.io/z from r   z(https://docutils.sourceforge.io/rst.htmlz	 source.
)r
   settings	generator	datestampsource_link
source_url_sourcer   relative_path_destinationr   r   	referenceTexttimestrftimegmtimeappend	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   expose_internalsfindallr7   getattr)r   r6   attvaluer   r   r   r   h   s   
zExposeInternals.applyN)r-   r.   r/   r0   r1   r7   r   r   r   r   r   r3   ]   s
    r3   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   messagesloose_messagesrF   r   r   r   r   z   s   zMessages.applyNr-   r.   r/   r0   r1   r   r   r   r   r   r>   q       r>   c                   @   r=   )FilterMessagesz
    Remove system messages below verbosity threshold.

    Also convert <problematic> nodes referencing removed messages
    to <Text> nodes and 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   refidrC   rD      )tupler
   r9   r   system_messagereporterreport_levelr?   removerN   
IndexErrorproblematicreplacer%   astextrF   lenr5   r   r   r   r      s$   zFilterMessages.applyNrJ   r   r   r   r   rL      s    rL   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
   rE   r?   )r   rA   r   r   r   r      s
   zTestMessages.applyN)r-   r.   r/   r0   __test__r1   r   r   r   r   r   r[      s
    r[   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_commentsrQ   r9   r   commentr?   rU   r5   r   r   r   r      s
   
zStripComments.applyNrJ   r   r   r   r   r]      rK   r]   c                   @   r2   )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 )NrD   )r
   r   strip_elements_with_classesstrip_elementsrQ   r9   check_classesr?   rU   strip_classesr   Element
ValueError)r   r6   rd   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 )NFrD   T)r4   r   re   rb   )r   r6   rg   r   r   r   rc      s   
z%StripClassesAndElements.check_classesN)r-   r.   r/   r0   r1   r   rc   r   r   r   r   r`      s
    r`   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  qDec                 C   s   t j| ||d t | _d S )N)	startnode)r   __init__setunsupported_languages)r   r
   rj   r   r   r   rk     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)r4   r?   literal_nodesstrresub)r   txtnodesr6   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_quotesFaltc                 S   s   g | ]}t |jtjs|qS r   )r4   r?   r   option_string)r@   txtnoder   r   r   rB   -  s
    
z%SmartQuotes.apply.<locals>.<listcomp>z
-x-altquotr   z*No smart quotes defined for language "%s".)	base_node)attrlanguage) r
   r   
setdefault
startswithAttributeErrorlanguage_codesmartquotes_localesr   
smartcharsquotesupdatedictr9   r   TextElementr4   nodes_to_skipr?   r%   get_language_coderX   r   normalize_language_tagrm   rS   warningaddeducate_tokensrv   smartquotes_actionzipclear)r   rw   alternativedocument_languagelc_smartquotesr6   rt   langtagteacherrz   newtextr   r   r   r     sZ   




zSmartQuotes.applyN)r-   r.   r/   r0   r1   r   FixedTextElementSpecialr   imagern   mathrawrW   rp   r   rk   rv   r   r   r   r   r   rh      s    rh   )r0   __docformat__rr   r&   docutilsr   r   docutils.transformsr   docutils.utilsr   r   r3   r>   rL   r[   r]   r`   rh   r   r   r   r   <module>   s   ?&