
    Rig                       d Z ddlmZ ddlZddlZddlZddlmZ ddl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	r6d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jT                  e+      Z, ejZ                  dej\                        Z/e/Z0 d        Z1dpdZ2dqdrdZ3dsdZ4ejj                  ejl                  ejn                  ejp                  fZ9dtdZ:ejl                  ejn                  ejv                  ejx                  fZ=ej|                  fZ?dudZ@dvdZAdwdZBdxdZCdydzdZDd{dZE	 	 	 	 d|d ZF	 d}	 	 	 	 	 	 	 	 	 d~d!ZGdwd"ZHdd#ZIg d$ZJ	 	 	 	 	 	 dd%ZK	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd&ZLdd'ZM ejZ                  d(      ZN ejZ                  d)      ZOi d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d7d9d:d;d-d<d=d>d?d@d3dAdBdCdDdEd5dFd5i dGdHdIdJdKdLdMdJdNd3dOdBdPd5dQdRdSd:dTdUdVdJdWdXdYdRdZd[d\d]d^dHZPd_d`dadbdcddZQ	 	 d	 	 	 	 	 	 	 	 	 ddeZR	 	 	 	 	 	 ddfZS	 dy	 	 	 	 	 	 	 	 	 	 	 	 	 ddgZTddhZUddiZVddjZWej                  ej                  ej                  ej|                  ejx                  ej                  ej                  fZ]dtdkZ^ddlZ_ddmZ`ddnZaeaej8                  _b        ddoZcecej8                  _d        y)z3Docutils node-related utility functions for Sphinx.    )annotationsN)StringIO)TYPE_CHECKINGAnycast)nodesNode)addnodes)__)logging)_fresh_title_style_context)CallableIterableIterator)Element)	Directive)InlinerRSTState)
StringList)Builder)BuildEnvironment)Tagsz^(.+?)\s*(?<!\x00)<([^<]*?)>$c                 4    d d x} f G fdd
}|      S )Nc                     t         S Nr	        I/home/agent/.friday_env/lib/python3.12/site-packages/sphinx/util/nodes.pyr   r   '   s    T'r   c                  8    e Zd ZW  ZdZddZddZddZd	dZy)
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 ...>, ...]
    c                     || _         || _        y r   )classesattrs)selfnode_classesr%   s      r    __init__zNodeMatcher.__init__<   s    #
r   c                F   	 | j                   rt        || j                         sy| j                  rbt        |t        j                        sy| j                  j                         D ]*  \  }}||vr y|t        u r|j                  |      |k7  s* y y# t        $ r Y yw xY w)NFT)	r$   
isinstancer%   r   r   itemsr   get	Exception)r&   nodekeyvalues       r    matchzNodeMatcher.match@   s    	||JtT\\$Bzz!$6 "&**"2"2"4 %JC$$# #%/$%  		s.   "B &B %B 2B B B 	B B c                $    | j                  |      S r   )r1   )r&   r.   s     r    __call__zNodeMatcher.__call__V   s    zz$r   c              3  T   K   |j                  |       D ]  }t        d|        yw)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.
        r   N)findallr   )r&   r.   founds      r    r5   zNodeMatcher.findallY   s-      \\$' 	#EsE""	#s   &(N)r'   ztype[N]r%   r   returnNoner.   r
   r7   bool)r.   r
   r7   zIterator[N])	__name__
__module____qualname____type_params____doc__r(   r1   r3   r5   ).type_paramss   r    r"   r"   '   s    (, #r   r"   r   )r   .generic_baser@   s     @r    #<generic parameters of NodeMatcher>rB   '   s    ' 9# 9# 9#r   c                L    | j                    d| j                  j                   S )zReturn full module dotted path like: 'docutils.nodes.paragraph'

    :param nodes.Node node: target node
    :return: full module dotted path
    .)r<   	__class__r;   r.   s    r    get_full_module_namerG   c   s%     ooa 7 7899r   c                    	 | j                         j                         }|rt	        |      |kD  r|d| dz   }|S # t        $ r t        |       }Y 0w xY w)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-   strlen)r.   lengthtexts      r    repr_domxmlrO   l   sZ    zz|!!# #d)f$GV}u$K	  4ys   : AAc                   t        | t        j                        r|| j                  spt        j                  dt        |       t        |              | j                  }|j                  | _	        |j                  dz
  | _
        | j                         | _        nAt        | t        j                        r'| j                  s| j                  j                  | _	        t        | t        j                        rl| j                  `t        j                  dt        |       t        |              | j                  j                  | j                  j                  c| _	        | _
        t        | t        j                        rl| j                  `t        j                  dt        |       t        |              | j                  j                  | j                  j                  c| _	        | _
        t        | t        j                        rt        j                  dt        |       t        |              t        t!        | j                  j#                  t        j                                    D ]L  }t%        j&                  dt%        j(                  |j                               z  d| j                        | _        N t        | t        j*                        rl| j                  `t        j                  dt        |       t        |              | j                  j                  | j                  j                  c| _	        | _
        t        | t        j,                        r>| j                  2t/        j0                  t2              5  t5        |       | _	        d d d        | j                  s| j                         | _        | j                  r| j                  ry t        | t        j6                  t        j                  t        j                  t        j8                  f      rBt        j                  dt        |       t        |              	 t5        |       | _	        d	| _
        y y # 1 sw Y   xY w# t2        $ r d| _	        Y d	| _
        y w xY w)
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debugrG   rO   parentsourcelineastextimagetitletermreversedlistr5   resubescapetopicliteral_block
contextlibsuppress
ValueErrorget_node_sourcerubric
field_name)r.   definition_list_itemrS   s      r    apply_source_workaroundrl      sC    $(()$..E &	

  ${{*11(--1		D%**	+DKKkk(($$)<7 &	

 "&!3!3T[[5E5ETY$$)<1 &	

 "&!3!3T[[5E5ETY$

#4 &	
 #4(;(;E<L<L(M#NO 	JVVryy):):)<==r4>>DN	 $$)<7 &	

 "&!3!3T[[5E5ETY $++,1D  , 	0)$/DK	0 >>{{t~~  LLJJKK		
 	: &	

	)$/DK 	/	0 	0>  	DK		s   6Q;Q  Q Q:9Q:c                h   t        | t        j                        ryt        | t        j                        r#| j                  d      s| j                  d      ryt        | t        j                        rd| vryt        | t        j                        r| j                  s*t        j                  dt        |       t        |              yt        | t              r.d| vr*t        j                  dt        |       t        |              y| j                  dd      s*t        j                  dt        |       t        |              yt        | t        j                        r<| j                  d   d	k(  r*t        j                  d
t        |       t        |              yy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   rn   r   r[   r,   InlineTextElementrX   rU   rV   rG   rO   IGNORED_NODESrj   childrenmetarF   s    r    is_translatablerv      sS   $--. $$$((>*Bdhhuo$%.*D$))*{{LL;$T*D!
 dM*~T/ILL2$T*D!	 xx-LLE$T*D!
  dE,,-4==3Cx3OLL8$T*D!
 dEJJ''r   c                2   d| v rt               }| j                  d      D ]  }|j                  dd      }t        |      dk(  r|\  }}t	        j
                  d|      rA|j                  |j                  d             |j                  |j                  d             |s!|j                  |j                  d             |j                  |       |j                  |        |j                         } | j                  d	d
      j                         S )z5Remove trailing backslashes from each line of *text*.\T)keependsrQ   )maxsplit   z
^[ \t]*\s$z 	
 )r   
splitlinesrsplitrL   r`   r1   writerstriplstripgetvaluereplacestrip)rN   bufferrY   splitprefixsuffixs         r    _clean_extracted_messager   "  s    t| OOTO2 	#DKKqK1E5zQ!&88M62 LLu!56LLu!56LLu!56 LL&T"#	#$  <<c"((**r   c              #  V  K   | j                  t              D ]  }t        |t        j                        r|j                         D ]  }||f 
 :t        |t              r|j                  }|s|j                         }nt        |t        j                        rI|j                  d      r	||d   f |j                  d      r|j                  d|d         }d| }n:d}n7t        |t        j                        r|d   }n|j                  }t        |      }|s||f  yw)	z3Extract translatable messages from a document tree.ro   rn   original_uriuriz.. image:: rR   contentN)r5   rv   r*   r   rn   extract_original_messagesLITERAL_TYPE_NODESrT   rZ   r   r[   r,   ru   r   )doctreer.   msg	image_urirN   s        r    extract_messagesr   ?  s    0 dH112557  Ci d./..Ckkmekk*xxDK''xx' HH^T%[A	#I;/ejj)y/C>>D*40C )O3s   DD)
D)c                p    t        |       D ]  }|j                  s|j                  c S  d}t        |      )Nznode source not found)traverse_parentrX   rg   r.   pnoder   s      r    rh   rh   ]  s8     &  <<<<  "C
S/r   c                p    t        |       D ]  }|j                  s|j                  c S  d}t        |      )Nznode line not found)r   rY   rg   r   s      r    get_node_liner   e  s8     & ::::  C
S/r   c              #  T   K   | r"|t        | |      r|  | j                  } | r!y y wr   )r*   rW   )r.   clss     r    r   r   m  s*     
;*T3/J{{ s   #((c                h    | j                   j                  |       }|dkD  r| j                   |dz
     S y )Nr   rQ   )rW   index)r.   poss     r    get_prev_noder   t  s3    
++

D
!C
Qw{{37##r   c              #     K   t        t        j                  d      }|j                  |       D ]  }d|v r|d   }n|d   }||f  yw)z6Traverse translatable index node from a document tree.F)inlineraw_entriesentriesN)r"   r   r   r5   )r   matcherr.   r   s       r    traverse_translatable_indexr   |  sU      (..7G( D =)G9oGGms   AA
c                r    t        |       5  | j                  |||d      }ddd       |S # 1 sw Y   S xY w)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)stater   r.   content_offsetrets        r    nested_parse_with_titlesr     sB     
$E	* S  .$T RSJSJs   ,6c                $   | j                         } | j                  t        j                        D ]  }d|d<   	 t	        | j                  t        j
                              D ]  }|j                  j                  |        | j                         S )z&Like node.astext(), but ignore images.rR   ro   )	deepcopyr5   r   r[   r_   rawrW   removerZ   )r.   imgr   s      r    clean_astextr     sq    ==?D||EKK( E
DLL+, 

#;;=r   c                    t         j                  |       }|r#d|j                  d      |j                  d      fS d| | fS )z3Split role content into title and target, if given.TrQ   r{   F)explicit_title_rer1   group)rN   r1   s     r    split_explicit_titler     s?    ##D)EU[[^U[[^33$r   )singlepairdoubletripleseeseealsoc                   g }| j                         } | }d}| j                  d      rd}| dd  j                         } dD ]c  }| j                  | d      s| t        |      dz   d  j                         }|dk(  rd| }n| d	| }t	        d
      || |fz  }t        |       t        D ]U  }| j                  | d      s| t        |      dz   d  j                         }|dk(  rd}|j                  ||||d f        |S  |j                  d      D ]S  }|j                         }d}|j                  d      rd}|dd  j                         }|s>|j                  d|||d f       U |S )NrR   !mainrQ   )modulekeywordoperatorobject	exception	statementbuiltin:r   zbuilt-in function; z; zT%r is no longer supported for index entries (from entry %r). Use 'pair: %s' instead.r   r   ,r   )	r   
startswithr   rL   r   rg   
indextypesappendr   )entrytargetidindexentriesoentryr   
index_typer0   r   s           r    process_index_entryr     s    ACLKKMEFDab	  " "
 zl!,-#j/A-/0668EY&-eW5%,b0* UE*+C S/!'"* ! I
zl!,-#j/A-/0668EX%#
UHdD IJ %I \\#& 	IEKKMED$ab	((*5(D$ GH	I r   c                   |j                         }t        |j                  t        j                              D ]  }g }t        t        |d         }	|dz  }|	D ]  }
|
|vs	 |j                  |
       t        j                  | ||
      z          t        | ||
| j                  j                  |
      |||      }|j                  |
       t        j                  |
      }|j                  |_        |j                  t         j"                        D ]  }d|vs|
|d<    |j                  |        |j*                  j-                  ||        |S # t$        $ r' t        j'                  t)        d      |
|dd       Y +w xY w)	zwInline all toctrees in the *tree*.

    Record all docnames in *docnameset*, and output docnames with *colorfunc*.
    includefilesr}   )docnamer   z+toctree contains ref to nonexisting file %rtocnot_readable)locationtypesubtype)r   r_   r5   r   toctreemaprK   r   rU   infoinline_all_toctreesenvget_doctreeaddstart_of_filert   r   sectionr-   warningr   rW   r   )builder
docnamesetr   tree	colorfunc	traversedindenttoctreenodenewnodesr   includefilesubtreesofsectionnodes                 r    r   r     s    ==?DDLL)9)9:; ":3N ;<#' 	)K)+)$$[1KK;)? ?@1"#//<!!G NN;/ #00EC#*#3#3CL'*{{5=='A A$K75@K	2A OOC(;	)< 	"";9E":F K! ! NNHI#!(" . # s   "A*E,FFc                h   | j                  t              }|j                  t              }t        j                  d|      j                  dd      j                  d      }t        j                  ddj                  |j                                     }t        j                  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-r}   rR   )	translate_non_id_translate_digraphs_non_id_translateunicodedata	normalizeencodedecode_non_id_charsra   joinr   _non_id_at_endsrK   )stringids     r    _make_idr    s      
		4	5B	'	(B 
		vr	*	1	1'8	D	K	KG	TB			3 4	5B			R	$Br7Nr   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  c                   d}|r|dz   }n|j                   j                  xs ddz   }|r|rt        ||z        }||k(  rd}n|rt        |      }|sd}|||j                  v r&|| j	                  |      z  }|||j                  v r&|S )z>Generate an appropriate node_id for given *prefix* and *term*.Nz-%sr  z%s)settings	id_prefixr  idsnew_serialno)r   documentr   r]   node_ididformats         r    make_idr'  b  s     GE>%%//744? $8d?+fG	4.G
/W4S--f55 /W4 Nr   c                x    | D ]5  }t        |t        j                        s|j                  d      |k(  s3|c S  y)zBPick matched pending_xref_condition node up from the pending_xref.	conditionN)r*   r   pending_xref_conditionr,   )r.   r)  subnodes      r    find_pending_xref_conditionr,    s>      w ? ?@K(I5N r   c                    t        j                  ddd      }||k(  r|r||d<   n3|r| j                  ||      dz   |z   |d<   n| j                  ||      |d<   |r||d<   ||z  }|S )z$Shortcut to create a reference node.rR   T)internalrefid#refurireftitle)r   	referenceget_relative_uri)r   fromdocname	todocnamer   childr\   r.   s          r    make_refnoder8    s     ??2rD1DiH W((i@3FQ N %55k9MDN ZEMDKr   c                h    | j                   j                  | j                        \  |_        |_        y r   )state_machineget_source_and_linelinenorX   rY   )	directiver.   s     r    set_source_infor>    s*    &44HHDKr   c                b    | j                   j                  } ||      \  }}||_        ||_        y r   )reporterr;  rX   rY   )inlinerr<  r.   gsalrX   rY   s         r    set_role_source_inforC    s.    //D<LFDDKDIr   c                    t        j                  t              5  t        |       |_        t        |       |_        d d d        y # 1 sw Y   y xY wr   )re   rf   rg   rh   rX   r   rY   )srcdsts     r    copy_source_inforG    s=    			Z	( &$S)
 %& & &s   !AAc                    t        | j                        D ]*  }t        |t              r y|j	                  dd      du s* y t        | dd      duS )z0Check whether the node is smart-quotable or not.Fsupport_smartquotesN)r   rW   r*   NON_SMARTQUOTABLE_PARENT_NODESr,   getattr)r.   r   s     r    is_smartquotablerL    sV     - e;<99*D1U:	 4.5UBBr   c                   | j                  t        j                        D ]c  }t        ||      r2|j	                  |j
                  xs t        j                                A|j	                  t        j                                e y)z0Filter ``only`` nodes which do not match *tags*.N)r5   r   only_only_node_keep_childrenreplace_selfrt   r   comment)r$  tagsr.   s      r    process_only_nodesrS    s]      / /#D$/dmm>u}}? emmo./r   c                    	 |j                  | d         S # t        $ r+}t        j                  t	        d      ||        Y d}~yd}~ww xY w)z%Keep children if tags match or error.exprz8exception while evaluating only directive expression: %s)r   NT)eval_conditionr-   rU   r   r   )r.   rR  errs      r    rO  rO    sQ    ""4<00 IJ 	 	

 s    	A
!AA
c                ^   t         j                  | j                        }g |_        | j                  |_        | j
                  |_        | j                  j                         D ci c]  \  }}||dvr|n|dd  c}}|_        | j                  |_        | j                  |_	        |S c c}}w )zMonkey-patch ```nodes.Element.copy``` to not copy the ``_document``
    attribute.

    See: https://github.com/sphinx-doc/sphinx/issues/11116#issuecomment-1376767086
    >   r"  namesr$   backrefsdupnamesN)
r   __new__rE   rt   rT   tagname
attributesr+   rY   rX   )elnewnoder  vs       r    _copy_except__documentrb    s     nnR\\*GGGjjGO MM'')Aq 	
MMASTUVSWWG 77GLYYGNNs   &B)c                   | j                         }| j                  D cg c]  }|j                          c}|_        |j                  D ]v  }||_        | j                  s| j                  |_        |j
                  | j                  j                  |_        |j                  \| j                  j                  |_        x |S c c}w )z4Monkey-patch ```nodes.Element.deepcopy``` for speed.)	copyrt   r   rW   r$  rX   current_sourcerY   current_line)r_  r`  r7  s      r    	_deepcopyrg    s    ggiG68kkBU(BG!! 6;;[[EN||#!{{99zz![[55
6 N Cs   C)r.   r
   r7   rK   )P   )r.   r
   rM   intr7   rK   )r.   r   r7   r8   r9   )rN   rK   r7   rK   )r   r   r7   zIterable[tuple[Element, str]])r.   r   r7   rK   )r.   r   r7   ri  r   )r.   r   r   r   r7   zIterable[Element])r.   r
   r7   zNode | None)r   r   r7   zEIterable[tuple[Element, list[tuple[str, str, str, str, str | None]]]])r   )
r   r   r   r   r.   r   r   ri  r7   ri  )rN   rK   r7   ztuple[bool, str, str])r   rK   r   rK   r7   z+list[tuple[str, str, str, str, str | None]])rR   )r   r   r   zset[str]r   rK   r   nodes.documentr   zCallable[[str], str]r   z	list[str]r   rK   r7   rj  )r   rK   r7   rK   )rR   N)
r   r   r$  rj  r   rK   r]   
str | Noner7   rK   )r.   zaddnodes.pending_xrefr)  rK   r7   zElement | None)r   r   r5  rK   r6  rK   r   rk  r7  zNode | list[Node]r\   rk  r7   znodes.reference)r=  r   r.   r
   r7   r8   )rA  r   r<  ri  r.   r
   r7   r8   )rE  r   rF  r   r7   r8   )r$  r
   rR  r   r7   r8   )r.   zaddnodes.onlyrR  r   r7   r:   )r_  r   r7   r   )er?   
__future__r   re   r`   r   ior   typingr   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   	getLoggerr;   rU   compileDOTALLr   caption_ref_rer"   rG   rO   rl   	Invisiblerd   doctest_blockversionmodifiedrs   rv   
math_blockr   r   r[   IMAGE_TYPE_NODESr   r   rh   r   r   r   r   r   r   r   r   r   r   r  r   r   r   r   r'  r,  r8  r>  rC  rG  FixedTextElementliteralmathproblematicnot_smartquotablerJ  rL  rS  rO  rb  rd  rg  r   r   r   r    <module>r     s   9 "  	   + +      :<<&.=0'3%			8	$ BJJ?K "9#x:&Z| 
OO			/(f 
			II	  
KK 
+:<

J
 PQ(07IL" F
333 13z 111 1 	1
 $1 1 1 1h6 

,-"**-."
C"
C" C" C	"
 C" C" C" C" C" C" C" C" C" C" C"  C!"" C#"$ C%"& C'"( C)"* C+", C-". C/"0 C1"2 C3"4 C5"6 C7"8 C9": C;"< C="> C?"@ CA"B CC" H   		  	
 	<


,/

&   	
   2& 
	MM	JJ	KK	II	" C
/
* ,  # r   