o
    iF5                     @  s   d Z ddlm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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.
- `Validate`: Validate the document tree, report violations as warning.
    )annotationsreStructuredTextN)nodesutils)	Transform)smartquotesc                   @  s.   e Zd ZdZdZdddZdd Zd	d
 ZdS )DecorationszD
    Populate a document's decoration element (header, footer).
    i4  returnNonec                 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   c/var/www/html/karishye-ai-python/venv/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   5   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output_pathr   r   	referenceTexttimestrftimegmtimeappend	paragraph)r   r   textsourcer!   r   r   r   r   8   sL   	zDecorations.generate_footerNr	   r
   )__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dZddd	Zd
S )ExposeInternalszL
    Expose internal attributes if ``expose_internals`` setting is set.
    iH  r	   boolc                 C  s   t |tj S r   )
isinstancer   r(   r   noder   r   r   not_Texth   s   zExposeInternals.not_Textr
   c                 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findallr<   getattr)r   r;   attvaluer   r   r   r   k   s   
zExposeInternals.applyNr	   r8   r0   )r1   r2   r3   r4   r5   r<   r   r   r   r   r   r7   `   s
    
r7   c                   @     e Zd ZdZdZdddZdS )	MessageszHandle "loose" messages.

    Place system messages generated by parsing or transforms that are not
    attached to the document tree into a dedicated section of the document.
    i\  r	   r
   c                 C  sr   g | j j| j j}dd |D }|r7t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   parse_messagestransform_messagesr   sectiontitle)r   messagesloose_messagesrM   r   r   r   r   ~   s   
zMessages.applyNr0   r1   r2   r3   r4   r5   r   r   r   r   r   rD   t   s    rD   c                   @  rC   )	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  r	   r
   c                 C  s   g }t | jtjD ]%}|d | jjjk r0|j| |d D ]}| jj	
|d  || q qt | jtjD ]}d|v rR|d |v rR|j|t|  q:| jtjD ]}d|d v rnt|dkrn|j| qZd S )NlevelidsrefidrI   rJ      )tupler   r>   r   system_messagereporterreport_levelrE   removerT   popr,   problematicreplacer(   astextrM   len)r   removed_idsr;   _idr   r   r   r      s"   zFilterMessages.applyNr0   rQ   r   r   r   r   rR      s    rR   c                   @  s"   e Zd ZdZdZdZd	ddZdS )
TestMessageszk
    Append all post-parse system messages to the end of the document.

    Used for testing purposes.
    Fip  r	   r
   c                 C  s&   | j jD ]}|js|  j |7  _ qd S r   )r   rL   rE   )r   rG   r   r   r   r      s
   zTestMessages.applyNr0   )r1   r2   r3   r4   __test__r5   r   r   r   r   r   rc      s
    rc   c                   @  rC   )	StripCommentszq
    Remove comment elements from the document tree (only if the
    ``strip_comments`` setting is enabled).
    i  r	   r
   c                 C  s6   | j jjrt| j tjD ]
}|j| qd S d S r   )	r   r   strip_commentsrW   r>   r   commentrE   r[   r:   r   r   r   r      s
   
zStripComments.applyNr0   rQ   r   r   r   r   re      s    re   c                   @  r6   )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  r	   r
   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 )NrJ   )r   r   strip_elements_with_classesstrip_elementsrW   r>   check_classesrE   r[   strip_classesr   Element
ValueError)r   r;   rl   class_valuer   r   r   r      s$   


zStripClassesAndElements.applyr8   c                 C  s:   t |tjsdS |d d d  D ]
}|| jv r dS qdS )NFrJ   T)r9   r   rm   rj   )r   r;   ro   r   r   r   rk      s   
z%StripClassesAndElements.check_classesNr0   rB   )r1   r2   r3   r4   r5   r   rk   r   r   r   r   rh      s
    
rh   c                   @  sf   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dZdd	 Zdd
dZdS )SmartQuotesz
    Replace ASCII quotation marks with typographic form.

    Also replace multiple dashes with em-dash/en-dash characters.
    iW  qDer	   r
   c                 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)r9   rE   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_quotesFaltc                 S  s   g | ]}t |jtjs|qS r   )r9   rE   r   option_string)rF   txtnoder   r   r   rH   2  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dictr>   r   TextElementr9   nodes_to_skiprE   r(   get_language_coder^   r   normalize_language_tagru   rY   warningaddeducate_tokensr~   smartquotes_actionzipclear)r   r   alternativedocument_languagelc_smartquotesr;   r|   langtagteacherr   newtextr   r   r   r     sZ   




zSmartQuotes.applyNr0   )r1   r2   r3   r4   r5   r   FixedTextElementSpecialr   imagerv   mathrawr]   rx   r   rs   r~   r   r   r   r   r   rp      s    
rp   c                   @  rC   )	ValidatezC
    Validate the document tree, report violations as warning.
    iC  r	   r
   c                 C  sz   t | jjdds
d S | j D ]+}z|jdd W q tjy: } z| jjjt	||j
p-|d W Y d }~qd }~ww d S )NvalidateF)	recursiver   )r?   r   r   r>   r   r   ValidationErrorrY   r   ry   problematic_element)r   r;   er   r   r   r   _  s   zValidate.applyNr0   rQ   r   r   r   r   r   W  s    r   )r4   
__future__r   __docformat__rz   r)   docutilsr   r   docutils.transformsr   docutils.utilsr   r   r7   rD   rR   rc   re   rh   rp   r   r   r   r   r   <module>   s"   ?&i