U
    \ShZ                  "   @  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
 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 ddlmZ ddlmZ erddlmZ ddlmZ ddl m!Z! e"e#Z$e%dej&Z'e'Z(G dd dZ)dddddZ*dddddddZ+ddddd Z,ej-ej.ej/ej0fZ1dd!dd"d#Z2ej.ej/ej3ej4fZ5ej6fZ7dd$d%d&d'Z8dddd(d)Z9dddd*d+Z:ddd,d-d.d/d0Z;dd1dd2d3Z<dd4d%d5d6Z=dd,d7dddd8d9d:Z>dddd;d<Z?dd=d>d?d@Z@dAdBdCdDdEdFgZAdddGdHdIdJZBdKdLddMdNdOdMdPdQdRZCdddSdTdUZDe%dVZEe%dWZFdXdYdZd[d\d]d^d^d_dYd`dad\dbdcd]d]dddedfded\dbd]dgd_dhdedidgdjdkdddl!ZGdmdndodpdqdrZHddtdMdduddvdwdxZIdyddzd{d|d}ZJddKdddud~duddddZKddddddZLdddddddZMddddddZNejOejPejQej6ej4ejRejSfZTdd!dddZUddddddZVdddddZWeWej_XdS )z3Docutils node-related utility functions for Sphinx.    )annotationsN)TYPE_CHECKINGAnyCallableIterable)nodes)ElementNode)	Directive)Inliner)
StringList)addnodes)__)logging)Builder)BuildEnvironment)Tagsz^(.+?)\s*(?<!\x00)<([^<]*?)>$c                   @  sB   e Zd ZdZddddddZdd	d
ddZdd	d
ddZdS )NodeMatcheraE  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')
        doctree.findall(matcher)
        # => [<reference ...>, <reference ...>, ...]

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

        from __future__ import annotations
from typing import Any
        matcher = NodeMatcher(nodes.reference, refdomain=Any)
        doctree.findall(matcher)
        # => [<reference ...>, <reference ...>, ...]
    z
type[Node]r   None)node_classesattrsreturnc                 O  s   || _ || _d S N)classesr   )selfr   r    r   D/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/util/nodes.py__init__8   s    zNodeMatcher.__init__r	   boolnoder   c                 C  s   z|| j rt|| j sW dS | jrxt|tjs2W dS | j D ]:\}}||krT W dS |tkr`q<q<|||kr< W dS q<W dS  tk
r   Y dS X d S )NFT)	r   
isinstancer   r   r   itemsr   get	Exception)r   r    keyvaluer   r   r   match<   s     
zNodeMatcher.matchc                 C  s
   |  |S r   )r'   )r   r    r   r   r   __call__R   s    zNodeMatcher.__call__N)__name__
__module____qualname____doc__r   r'   r(   r   r   r   r   r   !   s   r   r	   strr   c                 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
    .)r*   	__class__r)   r    r   r   r   get_full_module_nameV   s    r1   P   int)r    lengthr   c                 C  sR   z|    }W n tk
r,   t| }Y nX |rNt||krN|d| d }|S )a  
    return DOM XML representation of the specified node like:
    '<paragraph translatable="False"><inline classes="versionmodified">New 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...)ZasdomZtoxmlr$   r-   len)r    r4   textr   r   r   repr_domxml`   s    r7   r   r   c              	   C  sb  t | tjrN| jsNtdt| t|  | j}|j	| _	|j
d | _
|  | _nt | tjrj| j	sj| jj	| _	t | tjr| j	d krtdt| t|  | jj	| jj
 | _	| _
t | tjr| j	d krtdt| t|  | jj	| jj
 | _	| _
t | tjrPtdt| t|  tt| jtjD ]&}tdt|  d| j| _q(t | tjr| j	d krtdt| t|  | jj	| jj
 | _	| _
t | tjr| j	d krtt t| | _	W 5 Q R X | js|  | _| j	r| jrd S t | tjtj
tjtjfr^tdt| t|  zt| | _	W n tk
rR   d| _	Y nX 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debugr1   r7   parentsourcelineastextimagetitletermreversedlistfindallresubescapeZtopicliteral_block
contextlibsuppress
ValueErrorget_node_sourceZrubric
field_name)r    Zdefinition_list_itemr:   r   r   r   apply_source_workaroundt   s|     
    
 
 rQ   r   c                 C  s  t | tjrdS t | tjr4| ds0| dr4dS t | tjrLd| krLdS t | tjr| jszt	
dt| t|  dS t | trd| krt	
dt| t|  dS | ddst	
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rdS d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   Zorphanz&[i18n] SKIP %r because orphan node: %s)r!   r   rR   r   rB   r#   ZInlineZTextElementr?   r<   r=   r1   r7   IGNORED_NODESrP   childrenmetar0   r   r   r   is_translatable   sF         rW   zIterable[tuple[Element, str]])doctreer   c                 c  s   |  tD ]}t|tjr4| D ]}||fV  q"q
t|trR|j}|s| }njt|t	j
r|drv||d fV  |drd|d  }qd}n(t|t	jr|d }n|jdd }|r
||fV  q
d	S )
z3Extract translatable messages from a document tree.rS   rR   z.. image:: %surir9   content
 N)rG   rW   r!   r   rR   Zextract_original_messagesLITERAL_TYPE_NODESr;   rA   r   rB   r#   rV   replacestrip)rX   r    msgr   r   r   extract_messages   s(    




ra   c                 C  s*   t | D ]}|jr|j  S qtdd S )Nznode source not found)traverse_parentr?   rN   r    Zpnoder   r   r   rO     s    rO   c                 C  s*   t | D ]}|jr|j  S qtdd S )Nznode line not found)rb   r@   rN   rc   r   r   r   get_node_line  s    rd   r   zIterable[Element])r    clsr   c                 c  s(   | r$|d kst | |r| V  | j} q d S r   )r!   r>   )r    re   r   r   r   rb     s    rb   zNode | Nonec                 C  s*   | j | }|dkr"| j |d  S d S d S )Nr   r8   )r>   index)r    posr   r   r   get_prev_node$  s    rh   zEIterable[tuple[Element, list[tuple[str, str, str, str, str | None]]]]c                 c  sF   t tjdd}| |D ](}d|kr.|d }n|d }||fV  qdS )z6Traverse translatable index node from a document tree.F)inlineZraw_entriesentriesN)r   r   rf   rG   )rX   Zmatcherr    rj   r   r   r   traverse_translatable_index,  s    
rk   r   )staterZ   r    content_offsetr   c                 C  sN   | j j}| j j}g | j _d| j _z| j|||ddW S || j _|| j _X dS )zVersion 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.
    r   r8   )Zmatch_titlesN)memoZtitle_stylesZsection_levelZnested_parse)rl   rZ   r    rm   Zsurrounding_title_stylesZsurrounding_section_levelr   r   r   nested_parse_with_titles9  s    	ro   c                 C  sL   |   } | tjD ]}d|d< qt| tjD ]}|j| q2|  S )z&Like node.astext(), but ignore images.r9   rS   )	deepcopyrG   r   rB   rF   rawr>   removerA   )r    Zimgrq   r   r   r   clean_astextM  s    
rs   ztuple[bool, str, str])r6   r   c                 C  s.   t | }|r$d|d|dfS d| | fS )z3Split role content into title and target, if given.Tr8      F)explicit_title_rer'   group)r6   r'   r   r   r   split_explicit_titleW  s    
rw   singlepairdoubleZtripleZseeZseealsoz+list[tuple[str, str, str, str, str | None]])entrytargetidr   c                 C  sl  ddl m} g }|  } | }d}| dr>d}| dd   } |D ]p}| | drB| t|d d   }||  d| }tjtd	|| |d
d |	d|||d f  qhqBt
D ]R}| | dr| t|d d   }|dkrd}|	||||d f  qhq|dD ]P}| }d}|drFd}|dd   }|sPq|	d|||d f q|S )Nr   )pairindextypesr9   !mainr8   :z; zK%r is deprecated for index entries (from entry %r). Use 'pair: %s' instead.rf   )typery   rz   ,rx   )Zsphinx.domains.pythonr}   r_   
startswithlstripr5   r<   warningr   append
indextypessplit)r{   r|   r}   ZindexentriesZoentryr   Z
index_typer&   r   r   r   process_index_entryd  sJ    

   r   r   zset[str]znodes.documentr   z	list[str])builder
docnamesetdocnametree	colorfunc	traversedr   c              
   C  s  |  }t|tjD ]}g }tt|d }|D ]}	|	|kr2zH||	 tj	||	d dd t
| ||	| j|	||}
||	 W n( tk
r   tjtd|	|d Y q2X tj|	d}|
j|_|tjD ]}d|kr|	|d< q|| q2|j|| q|S )	zwInline all toctrees in the *tree*.

    Record all docnames in *docnameset*, and output docnames with *colorfunc*.
    includefilesr\   T)Znonlz+toctree contains ref to nonexisting file %rlocation)r   r   )rp   rF   rG   r   Ztoctreemapr-   r   r<   infoinline_all_toctreesenvZget_doctreeaddr$   r   r   Zstart_of_filerU   r   sectionr>   r^   )r   r   r   r   r   r   ZtoctreenodeZnewnodesr   ZincludefileZsubtreeZsofZsectionnoder   r   r   r     s8    

 
 
r   )stringr   c                 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.
    ZNFKDasciiignore-r\   r9   )	translate_non_id_translate_digraphs_non_id_translateunicodedata	normalizeencodedecode_non_id_charsrI   joinr   _non_id_at_endsr-   )r   idr   r   r   _make_id  s    

r   z[^a-zA-Z0-9._]+z^[-0-9._]+|-+$odhiltbcfknpyzgjseqr)!   i  i'  i1  iB  ig  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i%  i4  i5  i6  i7  i<  i?  i@  iG  iI  iK  iM  iO  szZaeZoedbZqp)      iS  i8  i9  r9   r   z
str | None)r   documentprefixrD   r   c                 C  s   d}|r|d }n|j jpdd }|rD|rDt|| }||kr\d}n|r\t|}|dkr\d}|dksn||jkr~|| | }q\|S )z>Generate an appropriate node_id for given *prefix* and *term*.Nz-%sr   z%sr9   )settingsZ	id_prefixr   ZidsZnew_serialno)r   r   r   rD   Znode_idZidformatr   r   r   make_id  s    
r   zaddnodes.pending_xrefzElement | None)r    	conditionr   c                 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   Zpending_xref_conditionr#   )r    r   Zsubnoder   r   r   find_pending_xref_condition  s    
r   zNode | list[Node]znodes.reference)r   fromdocname	todocnamer|   childrC   r   c                 C  sl   t jdddd}||kr&|r&||d< n.|rD| ||d | |d< n| |||d< |r`||d< ||7 }|S )z$Shortcut to create a reference node.r9   T)ZinternalZrefid#ZrefuriZreftitle)r   	referenceZget_relative_uri)r   r   r   r|   r   rC   r    r   r   r   make_refnode  s    


r   r
   )	directiver    r   c                 C  s   | j | j\|_|_d S r   )Zstate_machineget_source_and_linelinenor?   r@   )r   r    r   r   r   set_source_info1  s    r   r   )inlinerr   r    r   c                 C  s   | j |\|_|_d S r   )reporterr   r?   r@   )r   r   r    r   r   r   set_role_source_info6  s    r   )srcdstr   c              	   C  s.   t t t| |_t| |_W 5 Q R X d S r   )rL   rM   rN   rO   r?   rd   r@   )r   r   r   r   r   copy_source_info:  s    
r   c                 C  sN   t | jD ]*}t|tr dS |dddkr
 dS q
t| dddkrJdS dS )z0Check whether the node is smart-quotable or not.FZsupport_smartquotesNT)rb   r>   r!   NON_SMARTQUOTABLE_PARENT_NODESr#   getattrrc   r   r   r   is_smartquotableK  s    
r   r   )r   tagsr   c                 C  s   |  tjD ]}z||d }W nJ tk
rl } z,tjtd||d ||j	pXt
  W 5 d}~X Y qX |r||j	pt
  q|t
  qdS )z0Filter ``only`` nodes which do not match *tags*.exprz8exception while evaluating only directive expression: %sr   N)rG   r   onlyZeval_conditionr$   r<   r   r   Zreplace_selfrU   r   comment)r   r   r    reterrr   r   r   process_only_nodesY  s    &r   )r   r   c                 C  s,   | j f d| ji| j}| 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
    r;   )r/   r;   
attributesr?   r@   )r   Znewnoder   r   r   _copy_except__documentm  s    r   )r2   )N)r   )r9   N)N)Yr,   
__future__r   rL   rH   r   typingr   r   r   r   Zdocutilsr   Zdocutils.nodesr   r	   Zdocutils.parsers.rstr
   Zdocutils.parsers.rst.statesr   Zdocutils.statemachiner   Zsphinxr   Zsphinx.localer   Zsphinx.utilr   Zsphinx.buildersr   Zsphinx.environmentr   Zsphinx.util.tagsr   	getLoggerr)   r<   compileDOTALLru   Zcaption_ref_rer   r1   r7   rQ   Z	InvisiblerK   Zdoctest_blockZversionmodifiedrT   rW   Z
math_blockrq   r]   rB   ZIMAGE_TYPE_NODESra   rO   rd   rb   rh   rk   ro   rs   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ZFixedTextElementliteralmathZproblematicZnot_smartquotabler   r   r   r   copyr   r   r   r   <module>   s   
5
C	* 
	     +"

$
    