o
    Nia                     @  s  d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ er~ddlmZmZmZ ddlmZ ddlmZ ddlm Z m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z' ddl(m)Z) e*e+Z,e-dej.Z/e/Z0e
dedZ1G dd de	e1 Z2dddZ3ddd!d"Z4dd%d&Z5ej6ej7ej8ej9fZ:dd(d)Z;ej7ej8ej<ej=fZ>ej?fZ@dd,d-ZAdd.d/ZBdd0d1ZCddd5d6ZDdd8d9ZEdd;d<ZF	dddBdCZGddDdEZHddHdIZIg dJZJddNdOZK	Pddd]d^ZLdd`daZMe-dbZNe-dcZOi dddedfdgdhdidjdkdldmdndodpdqdrdqdsdtdudgdvdwdxdydzdmd{d|d}d~ddoddoi ddddddddddmdd|ddoddddtddddddddddddddZPddddddZQ	P	ddddZRdddZS	ddddZTdddZUdddZVdddZWejXejYejZej?ej=ej[ej\fZ]dddZ^dddƄZ_dddɄZ`ddd̄Zaeaej_bddd΄Zcecej_ddS )z3Docutils node-related utility functions for Sphinx.    )annotationsN)TYPE_CHECKINGAnyGenericTypeVarcast)nodes)Node)addnodes)__)logging)_fresh_title_style_context)CallableIterableIterator)Element)	Directive)InlinerRSTState)
StringList)Builder)BuildEnvironment)Tagsz^(.+?)\s*(?<!\x00)<([^<]*?)>$N)boundc                   @  s8   e Zd ZdZddd	ZdddZdddZdddZdS )NodeMatchera  A helper class for Node.findall().

    It checks that the given node is an instance of the specified node-classes and
    has the specified node-attributes.

    For example, following example searches ``reference`` node having ``refdomain``
    and ``reftype`` attributes::

        matcher = NodeMatcher(nodes.reference, refdomain='std', reftype='citation')
        matcher.findall(doctree)
        # => [<reference ...>, <reference ...>, ...]

    A special value ``typing.Any`` matches any kind of node-attributes.  For example,
    following example searches ``reference`` node having ``refdomain`` attributes::

        matcher = NodeMatcher(nodes.reference, refdomain=Any)
        matcher.findall(doctree)
        # => [<reference ...>, <reference ...>, ...]
    node_classestype[N]attrsr   returnNonec                 O  s   || _ || _d S N)classesr   )selfr   r    r$   E/home/ubuntu/.local/lib/python3.10/site-packages/sphinx/util/nodes.py__init__>   s   
zNodeMatcher.__init__noder	   boolc                 C  s   z=| j rt|| j sW dS | jr;t|tjsW dS | j D ]\}}||vr* W dS |tu r/q|||kr: W dS qW dS  tyG   Y dS w )NFT)	r"   
isinstancer   r   r   itemsr   get	Exception)r#   r'   keyvaluer$   r$   r%   matchB   s$   zNodeMatcher.matchc                 C  s
   |  |S r!   )r/   )r#   r'   r$   r$   r%   __call__X   s   
zNodeMatcher.__call__Iterator[N]c                 c  s"    | | D ]}tt|V  qdS )zAn alternative to `Node.findall` with improved type safety.

        While the `NodeMatcher` object can be used as an argument to `Node.findall`, doing so
        confounds type checkers' ability to determine the return type of the iterator.
        N)findallr   r   )r#   r'   foundr$   r$   r%   r2   [   s   zNodeMatcher.findallN)r   r   r   r   r   r    r'   r	   r   r(   )r'   r	   r   r1   )__name__
__module____qualname____doc__r&   r/   r0   r2   r$   r$   r$   r%   r   )   s    


r   r'   r	   r   strc                 C  s   | j  d| jj S )z
    Return full module dotted path like: 'docutils.nodes.paragraph'

    :param nodes.Node node: target node
    :return: full module dotted path
    .)r6   	__class__r5   r'   r$   r$   r%   get_full_module_namee   s   r=   P   lengthintc                 C  sP   z|    }W n ty   t| }Y nw |r&t||kr&|d| d }|S )a  
    return DOM XML representation of the specified node like:
    '<paragraph translatable="False"><inline classes="versionadded">Added in version...'

    :param nodes.Node node: target node
    :param int length:
       length of return value to be striped. if false-value is specified, repr_domxml
       returns full of DOM XML representation.
    :return: DOM XML representation
    Nz...)asdomtoxmlr,   r9   len)r'   r?   textr$   r$   r%   repr_domxmlo   s   rE   r   r    c                 C  sf  t | tjr'| js'tdt| t|  | j}|j	| _	|j
d | _
|  | _nt | tjr5| j	s5| jj	| _	t | tjrV| j	d u rVtdt| t|  | jj	| jj
| _	| _
t | tjrw| j	d u rwtdt| t|  | jj	| jj
| _	| _
t | tjrtdt| t|  tt| jtjD ]}tdt|  d| j| _qt | tjr| j	d u rtdt| t|  | jj	| jj
| _	| _
t | tjr| j	d u rtt t| | _	W d    n1 sw   Y  | js|  | _| j	r| jrd S t | tjtj
B tjB tjB r1tdt| t|  zt| | _	W n ty+   d| _	Y nw d	| _
d S d S )
Nz7[i18n] PATCH: %r to have source, line and rawsource: %s   z)[i18n] PATCH: %r to have source, line: %sz#[i18n] PATCH: %r to have source: %sz&[i18n] PATCH: %r to have rawsource: %sz	\s*:\s*%s z,[i18n] PATCH: %r to have source and line: %sr   )r)   r   
classifier	rawsourceloggerdebugr=   rE   parentsourcelineastextimagetitletermreversedlistr2   resubescapetopicliteral_block
contextlibsuppress
ValueErrorget_node_sourcerubric
field_name)r'   definition_list_itemrH   r$   r$   r%   apply_source_workaround   s   

	
ra   r(   c                 C  s  t | tjrdS t | tjr| ds| drdS t | tjr&d| vr&dS t | tjr| js<t	
dt| t|  dS t | trRd| vrRt	
dt| t|  dS | ddset	
dt| t|  dS t | tjr| jd d	krt	
d
t| t|  dS dS t | tjS )NTtranslatablealtFz)[i18n] SKIP %r because no node.source: %szO[i18n] SKIP %r because node is in IGNORED_NODES and no node['translatable']: %sz3[i18n] SKIP %r because not node['translatable']: %sr   orphanz&[i18n] SKIP %r because orphan node: %s)r)   r
   rb   r   rP   r+   InlineTextElementrM   rJ   rK   r=   rE   IGNORED_NODESr_   childrenmetar<   r$   r$   r%   is_translatable   sJ    rj   doctreeIterable[tuple[Element, str]]c                 c  s    |  tD ]g}t|tjr| D ]}||fV  qqt|tr*|j}|s)| }n<t|t	j
rR|dr<||d fV  |drO|d|d }d| }nd}nt|t	jr]|d }n	|jdd	 }|rm||fV  qd
S )z3Extract translatable messages from a document tree.rc   rb   original_uriuriz.. image:: rG   content
 N)r2   rj   r)   r
   rb   extract_original_messagesLITERAL_TYPE_NODESrI   rO   r   rP   r+   ri   replacestrip)rk   r'   msg	image_urir$   r$   r%   extract_messages!  s2   




rx   c                 C  *   t | D ]
}|jr|j  S qd}t|)Nznode source not found)traverse_parentrM   r\   r'   pnoderv   r$   r$   r%   r]   >     
r]   c                 C  ry   )Nznode line not found)rz   rN   r\   r{   r$   r$   r%   get_node_lineF  r}   r~   clsr   Iterable[Element]c                 c  s0    | r|d u st | |r| V  | j} | sd S d S r!   )r)   rL   )r'   r   r$   r$   r%   rz   N  s   rz   Node | Nonec                 C  s&   | j | }|dkr| j |d  S d S )Nr   rF   )rL   index)r'   posr$   r$   r%   get_prev_nodeU  s   r   EIterable[tuple[Element, list[tuple[str, str, str, str, str | None]]]]c                 c  sH    t tjdd}|| D ]}d|v r|d }n|d }||fV  qdS )z6Traverse translatable index node from a document tree.F)inlineraw_entriesentriesN)r   r
   r   r2   )rk   matcherr'   r   r$   r$   r%   traverse_translatable_index]  s   
r   stater   ro   r   content_offsetc                 C  s@   t |  | j|||dd}W d   |S 1 sw   Y  |S )ax  Version of state.nested_parse() that allows titles and does not require
    titles to have the same decoration as the calling document.

    This is useful when the parsed content comes from a completely different
    context, such as docstrings.

    This function is retained for compatibility and will be deprecated in
    Sphinx 8. Prefer ``nested_parse_to_nodes()``.
    T)match_titlesN)r   nested_parse)r   ro   r'   r   retr$   r$   r%   nested_parse_with_titlesj  s   

r   c                 C  sL   |   } | tjD ]}d|d< q
t| tjD ]}|j| q|  S )z&Like node.astext(), but ignore images.rG   rc   )	deepcopyr2   r   rP   rT   rawrL   removerO   )r'   imgr   r$   r$   r%   clean_astext{  s   
r   rD   tuple[bool, str, str]c                 C  s.   t | }|rd|d|dfS d| | fS )z3Split role content into title and target, if given.TrF      F)explicit_title_rer/   group)rD   r/   r$   r$   r%   split_explicit_title  s   

r   )singlepairdoubletripleseeseealsoentrytargetid+list[tuple[str, str, str, str, str | None]]c                 C  sd  ddl m} g }|  } | }d}| drd}| dd   } |D ]8}| | drY| t|d d   }||  d| }tjtd	|| |d
d |	d|||d f  |S q!t
D ])}| | dr| t|d d   }|dkrxd}|	||||d f  |S q\|dD ]$}| }d}|drd}|dd   }|sq|	d|||d f q|S )Nr   )pairindextypesrG   !mainrF   :z; zK%r is deprecated for index entries (from entry %r). Use 'pair: %s' instead.r   )typer   r   ,r   )sphinx.domains.pythonr   ru   
startswithlstriprC   rJ   warningr   append
indextypessplit)r   r   r   indexentriesoentryr   
index_typer.   r$   r$   r%   process_index_entry  sX   


r   rG   builderr   
docnamesetset[str]docnametreenodes.document	colorfuncCallable[[str], str]	traversed	list[str]indentc              
   C  s
  |  }t|tjD ]v}g }tt|d }	|d7 }|	D ]]}
|
|vrzz#||
 t	|||
  t
| ||
| j|
|||}||
 W n tyY   tjtd|
|d Y qw tj|
d}|j|_|tjD ]
}d|vrt|
|d< qj|| q|j|| q|S )zwInline all toctrees in the *tree*.

    Record all docnames in *docnameset*, and output docnames with *colorfunc*.
    includefilesrq   z+toctree contains ref to nonexisting file %rlocation)r   r   )r   rT   r2   r
   toctreemapr9   r   rJ   infoinline_all_toctreesenvget_doctreeaddr,   r   r   start_of_filerh   r   sectionrL   rt   )r   r   r   r   r   r   r   toctreenodenewnodesr   includefilesubtreesofsectionnoder$   r$   r%   r     sJ   

	

r   stringc                 C  sX   |  t}| t}td|ddd}tdd	|
 }td|}t|S )a  Convert `string` into an identifier and return it.

    This function is a modified version of ``docutils.nodes.make_id()`` of
    docutils-0.16.

    Changes:

    * Allow to use capital alphabet characters
    * Allow to use dots (".") and underscores ("_") for an identifier
      without a leading character.

    # Author: David Goodger <goodger@python.org>
    # Maintainer: docutils-develop@lists.sourceforge.net
    # Copyright: This module has been placed in the public domain.
    NFKDasciiignore-rq   rG   )	translate_non_id_translate_digraphs_non_id_translateunicodedata	normalizeencodedecode_non_id_charsrV   joinr   _non_id_at_endsr9   )r   idr$   r$   r%   _make_id  s   

r   z[^a-zA-Z0-9._]+z^[-0-9._]+|-+$   oi  di'  hi1  iiB  lig  ti  bi  i  ci  i  fi  ki  i  ni  pi  i  i  yi  zi  gi%  i4  i5  i6  i7  ji<  i?  si@  iG  eiI  iK  qiM  riO  szaeoedbqp)      iS  i8  i9  r   r   documentprefixrR   
str | Nonec                 C  s   d}|r	|d }n|j jpdd }|r"|r"t|| }||kr!d}n|r.t|}|dkr.d}|du s7||jv rG|| | }|du s7||jv s7|S )z>Generate an appropriate node_id for given *prefix* and *term*.Nz-%sr   z%srG   )settings	id_prefixr   idsnew_serialno)r   r   r   rR   node_ididformatr$   r$   r%   make_id?  s"   
r  addnodes.pending_xref	conditionElement | Nonec                 C  s0   | D ]}t |tjr|d|kr|  S qdS )zBPick matched pending_xref_condition node up from the pending_xref.r
  N)r)   r
   pending_xref_conditionr+   )r'   r
  subnoder$   r$   r%   find_pending_xref_condition]  s   
r  fromdocname	todocnamechildNode | list[Node]rQ   nodes.referencec                 C  sl   t jdddd}||kr|r||d< n|r"| ||d | |d< n| |||d< |r0||d< ||7 }|S )z$Shortcut to create a reference node.rG   T)internalrefid#refurireftitle)r   	referenceget_relative_uri)r   r  r  r   r  rQ   r'   r$   r$   r%   make_refnodej  s   	
r  	directiver   c                 C  s   | j | j\|_|_d S r!   )state_machineget_source_and_linelinenorM   rN   )r  r'   r$   r$   r%   set_source_info  s   r   inlinerr   r  c                 C  s   | j j}||\|_|_d S r!   )reporterr  rM   rN   )r!  r  r'   gsalr$   r$   r%   set_role_source_info  s   r$  srcdstc                 C  sD   t t t| |_t| |_W d    d S 1 sw   Y  d S r!   )rZ   r[   r\   r]   rM   r~   rN   )r%  r&  r$   r$   r%   copy_source_info  s   
"r'  c                 C  sF   t | jD ]}t|tr dS |dddu r dS qt| ddduS )z0Check whether the node is smart-quotable or not.Fsupport_smartquotesN)rz   rL   r)   NON_SMARTQUOTABLE_PARENT_NODESr+   getattr)r'   r|   r$   r$   r%   is_smartquotable  s   
r+  tagsr   c                 C  sD   |  tjD ]}t||r||jpt  q|t  qdS )z0Filter ``only`` nodes which do not match *tags*.N)r2   r
   only_only_node_keep_childrenreplace_selfrh   r   comment)r   r,  r'   r$   r$   r%   process_only_nodes  s
   
r1  addnodes.onlyc              
   C  sL   z| | d W S  ty% } ztjtd|| d W Y d}~dS d}~ww )z%Keep children if tags match or error.exprz8exception while evaluating only directive expression: %sr   NT)eval_conditionr,   rJ   r   r   )r'   r,  errr$   r$   r%   r.    s   r.  elc                 C  sL   t | j}g |_| j|_| j|_dd | j D |_| j|_| j	|_	|S )zMonkey-patch ```nodes.Element.copy``` to not copy the ``_document``
    attribute.

    xref: https://github.com/sphinx-doc/sphinx/issues/11116#issuecomment-1376767086
    c                 S  s*   i | ]\}}||d vr|n|dd qS )>   r  namesr"   backrefsdupnamesNr$   ).0r   vr$   r$   r%   
<dictcomp>  s    z*_copy_except__document.<locals>.<dictcomp>)
object__new__r;   rh   rI   tagname
attributesr*   rN   rM   )r6  newnoder$   r$   r%   _copy_except__document  s   rB  c                 C  sf   |   }dd | jD |_|jD ] }||_| jr0| j|_|jdu r&| jj|_|jdu r0| jj|_q|S )z4Monkey-patch ```nodes.Element.deepcopy``` for speed.c                 S  s   g | ]}|  qS r$   )r   )r:  r  r$   r$   r%   
<listcomp>  s    z_deepcopy.<locals>.<listcomp>N)copyrh   rL   r   rM   current_sourcerN   current_line)r6  rA  r  r$   r$   r%   	_deepcopy  s   




rG  )r'   r	   r   r9   )r>   )r'   r	   r?   r@   r   r9   )r'   r   r   r    r4   )rk   r   r   rl   )r'   r   r   r9   )r'   r   r   r@   r!   )r'   r   r   r   r   r   )r'   r	   r   r   )rk   r   r   r   )r   )
r   r   ro   r   r'   r	   r   r@   r   r9   )rD   r9   r   r   )r   r9   r   r9   r   r   )rG   )r   r   r   r   r   r9   r   r   r   r   r   r   r   r9   r   r   )r   r9   r   r9   )rG   N)
r   r   r   r   r   r9   rR   r  r   r9   )r'   r	  r
  r9   r   r  )r   r   r  r9   r  r9   r   r  r  r  rQ   r  r   r  )r  r   r'   r	   r   r    )r!  r   r  r@   r'   r	   r   r    )r%  r   r&  r   r   r    )r   r	   r,  r   r   r    )r'   r2  r,  r   r   r(   )r6  r   r   r   )er8   
__future__r   rZ   rU   r   typingr   r   r   r   r   docutilsr   docutils.nodesr	   sphinxr
   sphinx.localer   sphinx.utilr   sphinx.util.parsingr   collections.abcr   r   r   r   docutils.parsers.rstr   docutils.parsers.rst.statesr   r   docutils.statemachiner   sphinx.buildersr   sphinx.environmentr   sphinx.util.tagsr   	getLoggerr5   rJ   compileDOTALLr   caption_ref_rer   r   r=   rE   ra   	InvisiblerY   doctest_blockversionmodifiedrg   rj   
math_blockr   rs   rP   IMAGE_TYPE_NODESrx   r]   r~   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r   r$  r'  FixedTextElementliteralmathproblematicnot_smartquotabler)  r+  r1  r.  rB  rD  rG  r   r$   r$   r$   r%   <module>   s8   

<

Y
	3








;
2

	
 !$








