o
    ZNYbh                     @   s>  d Z d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mZ ddlmZmZmZ ddlmZ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jZG dd deZ G dd deZ!G dd  d eZ"dS )!zMiscellaneous directives.ZreStructuredText    N)ionodesstatemachineutils)	Directiveconvert_directive_function)
directivesrolesstates)	CodeBlockNumberLines)miscc                   @   sl   e Zd ZdZdZdZdZejej	ej
ejeeeejejej	ejej	dZejejejdZdd Zd	S )
Includea]  
    Include content read from a separate source file.

    Content may be parsed by the parser, or included as a literal
    block.  The encoding of the included file can be specified.  Only
    a part of the given file argument may be included by specifying
    start and end line or text to match before and/or after the text
    to be used.
       r   T)literalcodeencodingparser	tab-width
start-lineend-linestart-after
end-beforenumber-linesclassnameincludec               
   C   sN  | j jjjs| d| j | jj| j	| jj
 d }tjtj|}t| jd }|drD|drDtj| j|dd }tjtj||}td|}| jd| j jjj}| j jjj}| jd	| j jjj}z
tj|||d
}W n) ty   |  d| j|f  t!y } z|  d| jt"|f d}~ww | j jjj#$| | jdd}	| jdd}
z|	s|
dur|% }d||	|
 }n|& }W n t'y } z|  d| jt"|f d}~ww | jdd}|r|(|}|dk r|  d| j ||t)| d }| jdd}|r8|(|}|dk r2|  d| j |d| }t*j+||dd}t,|D ]\}}t)|| j jjj-kr^| d||d f qDd| jv r|dkrq|.|}n|}t/j0||| jdg d}d|_1| 2| d| jv rzt3| jd pd}	W n t4y   | 5dw |	t)| }
|dr|dd }t6g |fg|	|
}|D ]\}}|r|t/j7|||d7 }q|t/8|7 }q|gS |t/8|7 }|gS d| jv r|| jd < |dk r|9 }t:| j| j;dg| j|| j	| j<| j=| j | j	}|> S |	|
||f}| j jj?}|s7|@td|d!f ||f|v rZd"d# tA|D }d$|g|R }| d%| j|f d&| jv rtB|| j jj}|||fg |_?| jd&  }|Cd|| |jDE|f |jDF  |jGS |dd'| g7 }| jH|| |@||f g S )(zInclude a file as part of the content of this reST file.

        Depending on the options, the file (or a clipping) is
        converted to nodes and returned or inserted into the input stream.
        "%s" directive disabled.r   r   <>Nr   r   source_pathr   error_handlerzVProblems with "%s" directive path:
Cannot encode input file path "%s" (wrong locale?).&Problems with "%s" directive path:
%s.r   r    Problem with "%s" directive:
%sr   zDProblem with "start-after" option of "%s" directive:
Text not found.r   zCProblem with "end-before" option of "%s" directive:
Text not found.T)Zconvert_whitespacez,"%s": line %d exceeds the line-length-limit.r   r   )sourceclassesr   z+:number-lines: with non-integer start value
)r(   r   r'   )NNNNc                 s   s    | ]\}}|V  qd S )N ).0Zpthoptr*   r*   Husr/lib/python3.10/site-packages/docutils/parsers/rst/directives/misc.py	<genexpr>   s    zInclude.run.<locals>.<genexpr>z
> z(circular inclusion in "%s" directive:
%sr   z.. end of inclusion from "%s")Istatedocumentsettingsfile_insertion_enabledwarningr   state_machineZinput_linesr'   linenoZinput_offsetospathdirnameabspathr   	arguments
startswithendswithjoinstandard_include_pathnormpathr   relative_pathoptionsgetinput_encodinginput_encoding_error_handler	tab_widthr   	FileInputUnicodeEncodeErrorsevereOSErrorerror_stringrecord_dependenciesadd	readlinesreadUnicodeErrorfindlenr   Zstring2lines	enumerateZline_length_limit
expandtabsr   literal_blocklineZadd_nameint
ValueErrorerrorr   inlineText
splitlinesr   popcontent_offset
block_textruninclude_logappendreversedZnew_documentparseZtransformerZpopulate_from_componentsZapply_transformschildrenZinsert_input) selfr'   
source_dirr7   r   	e_handlerrE   Zinclude_filerX   	startlineZendlinelinesZrawtextZ
after_textZafter_indexZbefore_textZbefore_indexZinclude_linesirU   textrT   tokensr(   valueZ	codeblockZclip_optionsr`   Zmaster_pathsZinclusion_chainr0   r   r*   r*   r-   r_   3   s  











	

zInclude.runN)__name__
__module____qualname____doc__required_argumentsoptional_argumentsfinal_argument_whitespacer   flagZ	unchangedr   Zparser_namerV   unchanged_requiredclass_optionoption_specr6   r7   r=   r8   r
   __file__r>   r_   r*   r*   r*   r-   r      s,    
r   c                   @   s>   e Zd ZdZdZdZdZejej	ej
ejdZdZdd ZdS )	Rawz
    Pass through content unchanged

    Content is included in output based on type argument

    Content may be included inline (content section of directive) or
    imported from a file or url.
    r   r   T)fileurlr   r   c                 C   s  | j jjjr| j jjjsd| jv sd| jv r| d| j dd| j	d 
  i}| jd| j jjj}| j jjj}| jrYd| jv sJd| jv rR| d| j d	| j}nd| jv rd| jv rk| d
| j tjtj| j jj}tjtj|| jd }td |}z
tj|||d}W n ty } z| d| jt|f d }~ww | j jjj| z|  }W n t!y } z| d| jt|f d }~ww ||d< nsd| jv rM| jd }	ddl"m#}
 ddl$m%} z|
|	  }W n  |tfy } z| d| j| jd t|f d }~ww tj&||	||d}z|  }W n t!yG } z| d| jt|f d }~ww |	|d< n| '  t(j)d|fd| jdg i|}| j*+| j,\|_-|_.|gS )Nr{   r|   r   format r   r   zF"%s" directive may not both specify an external file and have content.r)   zXThe "file" and "url" options may not be simultaneously specified for the "%s" directive.r!   r$   r&   r'   )urlopen)URLErrorz*Problems with "%s" directive URL "%s":
%s.)r'   r"   r   r#   r%   r(   r   )/r/   r0   r1   Zraw_enabledr2   rA   r3   r   r=   r:   lowersplitrB   rC   rD   contentrX   r6   r7   r8   r9   Zcurrent_sourcer?   r   r@   r   rF   rI   rH   rJ   rK   rL   rN   rO   Zurllib.requestr   Zurllib.errorr   ZStringInputassert_has_contentr   rawr4   Zget_source_and_liner5   r'   rU   )re   
attributesr   rg   rk   rf   r7   Zraw_filerX   r'   r   r   Zraw_textZraw_noder*   r*   r-   r_      s   








zRaw.runN)rn   ro   rp   rq   rr   rs   rt   r   r7   Zurir   rw   rx   has_contentr_   r*   r*   r*   r-   rz      s    	rz   c                   @      e Zd ZdZdd ZdS )ReplaceTc                 C   s   t | jtjs| d| j |   d| j}t	
|}| j| j| j| d }g }|D ]-}|s;t |t	jr;|}q.t |t	jrKg |d< || q.| jjd| j d| jdg  S |rc||j S |S )NVInvalid context: the "%s" directive can only be used within a substitution definition.r)   Zbackrefsz
Error in "z1" directive: may contain a single paragraph only.rU   )
isinstancer/   r
   SubstitutionDefrX   r   r   r=   r   r   Elementnested_parser]   Z	paragraphZsystem_messagera   reporterr5   rd   )re   rk   elementnodemessageselemr*   r*   r-   r_   2  s8   

zReplace.runNrn   ro   rp   r   r_   r*   r*   r*   r-   r   .      r   c                   @   s@   e Zd ZdZdZdZdZejejejdZ	e
dZdd Zd	S )
Unicodea{  
    Convert Unicode character codes (numbers) to characters.  Codes may be
    decimal numbers, hexadecimal numbers (prefixed by ``0x``, ``x``, ``\x``,
    ``U+``, ``u``, or ``\u``; e.g. ``U+262E``), or XML-style numeric character
    entities (e.g. ``&#x262E;``).  Text following ".." is a comment and is
    ignored.  Spaces are ignored, and any other text remains as-is.
    r   r   T)trimltrimrtrimz( |\n|^)\.\. c                 C   s   t | jtjs| d| j | jj}d| jv r"d|j	d< d|j	d< d| jv r,d|j	d< d| jv r6d|j	d< | j
| jd d  }t }|D ])}zt|}W n tyj } z| d|t|f d }~ww |t|7 }qI|jS )Nr   r   r   r   r   r   zInvalid character code: %s
%s)r   r/   r
   r   rX   r   r4   r   rA   r   comment_patternr   r:   r   r   r   Zunicode_coderW   r   rJ   rZ   rd   )re   Zsubstitution_definitioncodesr   r   decodedrX   r*   r*   r-   r_   b  s6   






zUnicode.runN)rn   ro   rp   rq   rr   rs   rt   r   ru   rx   recompiler   r_   r*   r*   r*   r-   r   O  s    
r   c                   @   s(   e Zd ZdZdZdZdZdZdd ZdS )Classz
    Set a "class" attribute on the directive content or the next element.
    When applied to the next element, a "pending" element is inserted, and a
    transform does the work later.
    r   r   Tc              	   C   s   z
t | jd }W n ty   | d| j| jd f w g }| jrFt }| j	
| j| j| |D ]	}|d | q4||j |S ttj|| jd| j}| jj| || |S )Nr   z7Invalid class attribute value for "%s" directive: "%s".r(   )r   	directive)r   rw   r:   rW   rX   r   r   r   r   r/   r   r]   extendrd   pendingr   ZClassAttributer^   r4   r0   Znote_pendingra   )re   Zclass_valueZ	node_list	containerr   r   r*   r*   r-   r_     s6   

z	Class.runN)	rn   ro   rp   rq   rr   rs   rt   r   r_   r*   r*   r*   r-   r   {  s    r   c                   @   s0   e Zd ZdZedejjfd  Z	dd Z
dS )RoleTz(%s)\s*(\(\s*(%s)\s*\)\s*)?$   c              
   C   s
  | j | jks	| js| d| j | jd }| j|}|s(| d| j|f |d}|d}g }|rbt	|| j
j| j| jj\}}|du ra| jjjd| t| j| j| jd}||g S ntj}t|d	rsJ d
| j|f zt|}| jj| jdd | j |i d\}	}
}}W n- tjy } z | jjd| j|f t| j| j| jd}||g W  Y d}~S d}~ww d|
vrz	t||
d< W n, ty } z | jjd| j|f t| j| j| jd}||g W  Y d}~S d}~ww t|||
|}t|| |S )z?Dynamically create and register a custom interpreted text role.z4"%s" directive requires arguments on the first line.r   z4"%s" directive arguments not valid role names: "%s".r      N#Unknown interpreted text role "%s".r   r:   z[Supplemental directive arguments for "%s" directive not supported (specified by "%r" role).)Zoption_presetszError in "%s" directive:
%s.r   z(Invalid argument for "%s" directive:
%s.)r]   r5   r   rX   r   argument_patternmatchgroupr	   roler4   languager/   r   r   rT   r^   Zgeneric_custom_rolehasattrr   Zparse_directive_blockr
   ZMarkupErrorr   rw   rW   Z
CustomRoleZregister_local_role)re   argsr   Znew_role_nameZbase_role_namer   Z	base_rolerX   Zconverted_roler:   rA   r   r]   detailr   r*   r*   r-   r_     s   



zRole.runN)rn   ro   rp   r   r   r   r
   ZInlinerZ
simplenamer   r_   r*   r*   r*   r-   r     s    r   c                   @       e Zd ZdZdZdZdd ZdS )DefaultRolez&Set the default interpreted text role.r   Fc                 C   s   | j sdtjv rtjd= g S | j d }t|| jj| j| jj\}}|d u r>| jjj	d| t
| j| j| jd}||g S |tjd< |S )Nr%   r   r   r   )r:   r	   Z_rolesr   r4   r   r5   r/   r   rX   r   rT   r^   )re   Z	role_namer   r   rX   r*   r*   r-   r_     s"   




zDefaultRole.runN)rn   ro   rp   rq   rs   rt   r_   r*   r*   r*   r-   r     s
    r   c                   @   r   )Titler   r   Tc                 C   s   | j d | jjd< g S )Nr   title)r:   r4   r0   )re   r*   r*   r-   r_     s   z	Title.runN)rn   ro   rp   rr   rs   rt   r_   r*   r*   r*   r-   r     s
    r   c                   @   s   e Zd Zdd Zdd ZdS )MetaBodyc                 C   s&   |  |\}}|  j|7  _g |g fS )zMeta element.)	parsemetaparent)re   r   contextZ
next_stater   blank_finishr*   r*   r-   field_marker  s   
zMetaBody.field_markerc                 C   sf  |  |}tt|}| j| \}}}}t }ttd	||d< |sA| jj
}| jd| t||}	|	|fS | }
zt|
d d \}}||| < W n tjyh   |
d |d< Y nw |
dd  D ]?}zt|d \}}||| < W qo tjy } z| jj
}| jd||f t||}	|	|fW  Y d }~  S d }~ww ||fS )Nr~   r   zNo content for meta tag "%s".r   r   r   z*Error parsing meta tag attribute "%s": %s.)Zparse_field_markerr   unescaper   Zescape2nullr4   Zget_first_known_indentedendmetar=   rU   r   inforT   r   Zextract_name_valuer   ZNameValueErrorrX   )re   r   r   Zindentedindentline_offsetr   r   rU   msgrl   Zattnamevaltokenr   r*   r*   r-   r     sL   




zMetaBody.parsemetaN)rn   ro   rp   r   r   r*   r*   r*   r-   r     s    r   c                   @   s"   e Zd ZdZdefiZdd ZdS )MetaTZstate_classesc                 C   s   |    t }| jj| j| j|dd| jd\}}|| j t| jkr7| j	j
dt| j| j| jd}||7 }| jjtjtjfpCd}|j| jj||< g S )Nr   T)Zinitial_stater   Zstate_machine_kwargszInvalid meta directive.r   r   )r   r   r   r/   Znested_list_parser   r]   SMkwargsrQ   r   rX   rT   r^   r5   r0   Zfirst_child_not_matching_classZTitularr   rd   )re   r   Znew_line_offsetr   rX   indexr*   r*   r-   r_   3  s*   


zMeta.runN)rn   ro   rp   r   r   r   r_   r*   r*   r*   r-   r   -  s    
r   c                   @   r   )DateTc                 C   sD   t | jtjs| d| j d| jpd}t	|}t
|gS )Nr   r)   z%Y-%m-%d)r   r/   r
   r   rX   r   r=   r   timestrftimer   rZ   )re   Z
format_strrk   r*   r*   r-   r_   L  s   
zDate.runNr   r*   r*   r*   r-   r   H  r   r   c                   @   s.   e Zd ZdZdZdZdejiZdZ	dd Z
dS )TestDirectivez3This directive is useful only for testing purposes.r   Toptionc                 C   sl   | j r"d| j }| jjd| j| j| jf t||| j	d}|gS | jjd| j| j| jf | j	d}|gS )Nr)   zBDirective processed. Type="%s", arguments=%r, options=%r, content:r   zGDirective processed. Type="%s", arguments=%r, options=%r, content: None)
r   r=   r   r   r   r:   rA   r   rT   r5   )re   rk   r   r*   r*   r-   r_   o  s    	zTestDirective.runN)rn   ro   rp   rq   rs   rt   r   rv   rx   r   r_   r*   r*   r*   r-   r   f  s    
r   )#rq   Z__docformat__os.pathr6   r   r   docutilsr   r   r   r   Zdocutils.parsers.rstr   r   r   r	   r
   Z$docutils.parsers.rst.directives.bodyr   r   Zdocutils.transformsr   r   rz   r   r   r   r   r   r   ZSpecializedBodyr   r   r   r   r*   r*   r*   r-   <module>   s.    <`!,&>)