o
    NiQ                     @  sL  U d Z ddlmZ ddlZddlmZmZ ddl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mZ ddlmZ dd	lmZmZ ersdd
lmZ ddlmZ ddlmZmZmZmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z% ej&ej'ej(ej)ej&ej)ej)ej&ej(d	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Z0dGd d!Z1G d"d# d#eZ2G d$d% d%e2Z3G d&d' d'eZ4G d(d) d)eZ5G d*d+ d+eZ6		,dHdId9d:Z7ej8j9j:j;ej8j9j:j<d;e7_=e+ej>d<e,d=d>e- e. e/ e0 e2 e3 e4 e4 e5 e6 d?Z?d@e@dA< dJdEdFZAdS )Kz#Handlers for additional ReST roles.    )annotationsN)TYPE_CHECKINGAny)nodesutils)addnodes)___)ws_re)ReferenceRole
SphinxRole)Sequence)Final)ElementNodeTextElementsystem_message)Sphinx)BuildEnvironment)ExtensionMetadataRoleFunction)	commanddfnkbd
mailheadermakevarmimetype	newsgroupprogramregexpc                      s   e Zd ZU dZejZded< ej	Z
ded< 					d,d- fddZd.ddZd/ddZd/ddZd/ddZd0d$d%Zd1d*d+Z  ZS )2XRefRoleaA  
    A generic cross-referencing role.  To create a callable that can be used as
    a role function, create an instance of this class.

    The general features of this role are:

    * Automatic creation of a reference and a content node.
    * Optional separation of title and target with `title <target>`.
    * The implementation is a class rather than a function to make
      customization easier.

    Customization can be done in two ways:

    * Supplying constructor parameters:
      * `fix_parens` to normalize parentheses (strip from target, and add to
        title if configured)
      * `lowercase` to lowercase the target
      * `nodeclass` and `innernodeclass` select the node classes for
        the reference and the content node

    * Subclassing and overwriting `process_link()` and/or `result_nodes()`.
    ztype[Element]	nodeclassztype[TextElement]innernodeclassFN
fix_parensbool	lowercasetype[Element] | Nonetype[TextElement] | Nonewarn_danglingreturnNonec                   s<   || _ || _|| _|d ur|| _|d ur|| _t   d S N)r#   r%   r(   r!   r"   super__init__)selfr#   r%   r!   r"   r(   	__class__ @/home/ubuntu/.local/lib/python3.10/site-packages/sphinx/roles.pyr-   H   s   zXRefRole.__init__titlestrtargettuple[str, str]c                 C  s>   | j s| jjr|ds|d7 }n|d}|d}||fS )Nz())has_explicit_titleconfigadd_function_parenthesesendswithremovesuffix)r.   r3   r5   r1   r1   r2   update_title_and_targetZ   s   


z XRefRole.update_title_and_target'tuple[list[Node], list[system_message]]c                 C  sp   d| j vrd| j | _| _d| jg| _n| j dd\| _| _d| j| j d| j g| _| jr4|  S |  S )N: xref   -)name	refdomainreftypeclassessplitdisabledcreate_non_xref_nodecreate_xref_noder.   r1   r1   r2   rung   s   
zXRefRole.runc                 C  s\   t | jdd  }| jrd| _| |d\}}| j| j|| jd}| j	| j
j| j|ddS )NrA   Fr?   rF   is_ref)r   unescapetextr#   r7   r<   r"   rawtextrF   result_nodesinlinerdocumentenv)r.   rQ   r5   noder1   r1   r2   rI   t   s   zXRefRole.create_non_xref_nodec                 C  s   | j }| j}| jr| }| jr| ||\}}| jj| j| j	| j
| jd}| j| jfi |}| | | | j|| j
||\}}||d< || j| j|| jd7 }| j| jj| j|ddS )N)refdocrD   rE   refexplicitrefwarn	reftargetrM   TrN   )r5   r3   r%   lowerr#   r<   rV   docnamerD   rE   r7   r(   r!   rR   set_source_infoprocess_linkr"   rF   rS   rT   rU   )r.   r5   r3   optionsrefnoder1   r1   r2   rJ   }   s(   
zXRefRole.create_xref_noderV   r   ra   r   r7   c                 C  s   |t d|fS )zCalled after parsing title and target text, and creating the
        reference node (given in *refnode*).  This method can alter the
        reference node and must return a new (or the same) ``(title, target)``
        tuple.
         )r
   sub)r.   rV   ra   r7   r3   r5   r1   r1   r2   r_      s   zXRefRole.process_linkrU   nodes.documentrW   rO   c                 C  s
   |gg fS )a(  Called before returning the finished nodes.  *node* is the reference
        node if one was created (*is_ref* is then true), else the content node.
        This method can add other nodes and must return a ``(nodes, messages)``
        tuple (the usual return value of a role function).
        r1   )r.   rU   rV   rW   rO   r1   r1   r2   rS      s   
zXRefRole.result_nodes)FFNNF)r#   r$   r%   r$   r!   r&   r"   r'   r(   r$   r)   r*   )r3   r4   r5   r4   r)   r6   r)   r=   rV   r   ra   r   r7   r$   r3   r4   r5   r4   r)   r6   )
rU   rd   rV   r   rW   r   rO   r$   r)   r=   )__name__
__module____qualname____doc__r   pending_xrefr!   __annotations__r   literalr"   r-   r<   rL   rI   rJ   r_   rS   __classcell__r1   r1   r/   r2   r    -   s    
 



	
r    c                      s   e Zd Zd fddZ  ZS )AnyXRefRolerV   r   ra   r   r7   r$   r3   r4   r5   r)   r6   c                   s&   t  |||||}|j|j |S r+   )r,   r_   
attributesupdateref_context)r.   rV   ra   r7   r3   r5   resultr/   r1   r2   r_      s   zAnyXRefRole.process_linkrf   )rg   rh   ri   r_   rn   r1   r1   r/   r2   ro      s    ro   c                   @  .   e Zd ZU dZded< dddZdd	d
ZdS )CVEz%https://www.cve.org/CVERecord?id=CVE-r   	_BASE_URLr)   r=   c           
      C    d| j d }dtd| j |dd fg}tj|d}tjdd|gd}| jj	| z-| 
 }tjddd|d	gd
}| jrJ|t| j| j7 }nd| j }|t||7 }W n( ty   | jjjtd| j | jd}| j| j| j|}	|	g|gf Y S w |||gg fS )Nindex-indexsinglez,Common Vulnerabilities and Exposures; CVE %sr?   entriesidsFcveinternalrefurirF   zCVE zinvalid CVE number %slinerV   new_serialnor   r5   r   ry   r   rT   rU   note_explicit_target	build_uri	referencer7   strongr3   
ValueErrorreportererrorr	   linenoproblematicrR   
r.   	target_idr|   ry   r5   r   r   r3   msgprbr1   r1   r2   rL      :   
zCVE.runr4   c                 C  sH   | j dd}t|dkrtj |d  d|d  S tj |d  S N#rA      r   )r5   rG   lenru   rv   r.   retr1   r1   r2   r      s   zCVE.build_uriNre   r)   r4   rg   rh   ri   rv   rl   rL   r   r1   r1   r1   r2   ru         
 
#ru   c                   @  rt   )CWEz'https://cwe.mitre.org/data/definitions/r   rv   r)   r=   c           
      C  rw   )Nrx   ry   rz   z#Common Weakness Enumeration; CWE %sr?   r{   r}   Fcwer   zCWE zinvalid CWE number %sr   r   r   r1   r1   r2   rL      r   zCWE.runr4   c                 C  sR   | j dd}t|dkrtj t|d  d|d  S tj t|d  dS )Nr   rA   r   r   z.html#z.html)r5   rG   r   r   rv   intr   r1   r1   r2   r     s    zCWE.build_uriNre   r   r   r1   r1   r1   r2   r      r   r   c                   @      e Zd Zd	ddZd
ddZdS )PEPr)   r=   c           
      C  s  d| j d }dtd| j |dd fg}tj|d}tjdd|gd}| jj	| z,| 
 }tjddd|d	gd
}| jrI|t| j| j7 }nd| j }|t||7 }W n( ty   | jjjtd| j | jd}| j| j| j|}	|	g|gf Y S w |||gg fS )Nindex-%sry   rz   z$Python Enhancement Proposals; PEP %sr?   r{   r}   Fpepr   zPEP zinvalid PEP number %sr   r   r   r1   r1   r2   rL   #  s:   

zPEP.runr4   c                 C  sV   | j jjj}| jdd}t|dkr!|dt|d |d f  S |dt|d   S )Nr   rA   r   zpep-%04d/#%sr   z	pep-%04d/)rT   rU   settingspep_base_urlr5   rG   r   r   r.   base_urlr   r1   r1   r2   r   F  s
   zPEP.build_uriNre   r   rg   rh   ri   rL   r   r1   r1   r1   r2   r   "  s    
#r   c                   @  r   )RFCr)   r=   c                 C  s  d| j d }t| j}dd| |dd fg}tj|d}tjdd|gd}| jj	| z)| 
 }tjddd|d	gd
}| jrL|t| j| j7 }n
|}|t||7 }W n( ty   | jjjtd| j | jd}	| j| j| j|	}
|
g|	gf Y S w |||gg fS )Nr   ry   rz   zRFC; r?   r{   r}   Frfcr   zinvalid RFC number %sr   )rV   r   _format_rfc_targetr5   r   ry   r   rT   rU   r   r   r   r7   r   r3   r   r   r   r	   r   r   rR   )r.   r   formatted_targetr|   ry   r5   r   r   r3   r   r   r1   r1   r2   rL   P  s0   
zRFC.runr4   c                 C  sb   | j jjj}| jdd}t|dkr%|| j jt|d   d |d  S || j jt|d   S r   )	rT   rU   r   rfc_base_urlr5   rG   r   rfc_urlr   r   r1   r1   r2   r   l  s
   $zRFC.build_uriNre   r   r   r1   r1   r1   r2   r   O  s    
r   r5   r4   r)   c                C  sh   |  d\}}}|r/| d\}}}|dv r/|r%d| d|  d| S d| d|  S d|  S )z
    Takes an RFC number with an optional anchor (like ``123#section-2.5.3``)
    and attempts to produce a human-friendly title for it.

    We have a set of known anchors that we format nicely,
    everything else we leave alone.
    r   rB   >   pagesectionappendixzRFC rb   )	partitionr3   )r5   numberr   anchorfirst	remainingr1   r1   r2   r   u  s   
r   c                   @      e Zd ZedZdddZdS )GUILabelz(?<!&)&(?![&\s])r)   r=   c                 C  s   t j| j| jgd}| j| j}|t |d7 }|D ](}|	dd}t |d }t jdd|dgd}||7 }|t |dd  7 }q|gg fS )	N)rR   rF   r   z&&&r?   acceleratorrM   rA   )
r   inlinerR   rC   amp_rerG   rQ   Textpopreplace)r.   rW   spansspanletterr   r1   r1   r2   rL     s   
zGUILabel.runNre   )rg   rh   ri   recompiler   rL   r1   r1   r1   r2   r         
r   c                      s"   e Zd ZdZd fddZ  ZS )MenuSelectionu   ‣r)   r=   c                   s   | j d| j| _ t  S )Nz-->)rQ   r   BULLET_CHARACTERr,   rL   rK   r/   r1   r2   rL     s   
zMenuSelection.runre   )rg   rh   ri   r   rL   rn   r1   r1   r/   r2   r     s    r   c                   @  s*   e Zd ZedZdddZdd	d
ZdS )EmphasizedLiteralz(\\\\|\\{|\\}|{|})r)   r=   c                 C  s>   |  | j}tj| jdg|R | j | jgd}|gg fS )Nr?   )rolerF   )parserQ   r   rm   rR   rC   r\   )r.   childrenrW   r1   r1   r2   rL     s   
zEmphasizedLiteral.runrQ   r4   
list[Node]c                 C  sf  g }dg}| j |D ]}|dkr|d  d7  < q|dkr9t|dkr3|d dkr3|d  d7  < q|d q|d	kr|t|d
krp|d dkrpt|d dkrp|d r_|t|d  |t|d |d  dg}q|d	 d|g}q|dkr|d  d7  < q|dkr|d  d	7  < q|d  |7  < qd|rd|}|t| |S )Nr?   z\\\{r   )r   r?   }   rA   r   z\{z\})		parens_rerG   r   extendappendr   r   emphasisjoin)r.   rQ   rs   stackpartr1   r1   r2   r     s4   (


zEmphasizedLiteral.parseNre   )rQ   r4   r)   r   )rg   rh   ri   r   r   r   rL   r   r1   r1   r1   r2   r     s    

r   c                   @  s$   e Zd ZedejZdddZdS )Abbreviationz	\((.*)\)$r)   r=   c                 C  sd   | j  }| j| j}|r!| jd |   }|d|d< n| j}tj	| j
|fi |gg fS )NrA   explanation)r`   copyabbr_researchrQ   startstripgroupr   abbreviationrR   )r.   r`   matchedrQ   r1   r1   r2   rL     s   
zAbbreviation.runNre   )rg   rh   ri   r   r   DOTALLr   rL   r1   r1   r1   r2   r     s    r   c                   @  r   )Manpagez5^(?P<path>(?P<page>.+)[(.](?P<section>[1-9]\w*)?\)?)$r)   r=   c                 C  s   t d| j}| j| }r| }n||dd}| jr$| jdd  n| j}| jsA| jj	rA| jj	
|}tjd|| jg|d}nt|}tj| jd|fd| jgi|}|gg fS )Nrb   r?   )pathr   r   rA   )rF   r   rF   )r
   rc   r5   _manpage_rematch	groupdictrH   r3   r8   manpages_url
format_mapr   r   rC   r   r   manpagerR   )r.   r   minforQ   uriinnerrW   r1   r1   r2   rL     s   

 
zManpage.runNre   )rg   rh   ri   r   r   r   rL   r1   r1   r1   r2   r     r   r   r1   rC   rR   rQ   r   r   rT   #docutils.parsers.rst.states.Inlinerr`   dict[str, Any] | NonecontentSequence[str]r=   c           
      C  s   |d u ri }|  }tjjj| |dd}dg}|r"|d d|v r-||d  |r8||vr8|| t	j
||||d}	|	gg fS )Nlanguager?   code	highlightrF   )rF   r   )r   docutilsparsersrstrolesset_classesgetr   r   r   rm   )
rC   rR   rQ   r   rT   r`   r   r   rF   rW   r1   r1   r2   	code_role  s   	


r   )classr   )r!   T)r(   )downloadanyr   r   r   r   guilabelmenuselectionfilesampabbrr   zdict[str, RoleFunction]specific_docrolesappr   r   c                 C  s   ddl m} t D ]\}}|||}|||d|gi}||| q
t D ]
\}}||| q)|dt	 ddddS )Nr   )r   rF   r   builtinT)versionparallel_read_safeparallel_write_safe)
docutils.parsers.rstr   generic_docrolesitemsGenericRole
CustomRoleregister_local_roler  register_canonical_roler   )r	  r   rolenamer!   genericr   funcr1   r1   r2   setupG  s   r  )r5   r4   r)   r4   )Nr1   )rC   r4   rR   r4   rQ   r4   r   r   rT   r   r`   r   r   r   r)   r=   )r	  r   r)   r   )Brj   
__future__r   r   typingr   r   docutils.parsers.rst.directivesr   docutils.parsers.rst.rolesdocutils.parsers.rst.statesr   r   sphinxr   sphinx.localer   r	   sphinx.utilr
   sphinx.util.docutilsr   r   collections.abcr   r   docutils.nodesr   r   r   r   sphinx.applicationr   sphinx.environmentr   sphinx.util.typingr   r   literal_strongr   rm   literal_emphasisr  r    ro   ru   r   r   r   r   r   r   r   r   r   r   r   r   
directivesclass_option	unchangedr`   download_referencer  rl   r  r1   r1   r1   r2   <module>   s|     ---
&24


