U
    ZShf                     @   sZ  d Z dZddlmZ ddlZddlZd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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.reStructuredText    )PathN)urlopen)URLError)ionodesstatemachineutils)	Directiveconvert_directive_function)
directivesrolesstates)	CodeBlockNumberLines)miscc                   @   sd   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ej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.

    https://docutils.sourceforge.io/docs/ref/rst/directives.html#including-an-external-document-fragment
       r   T)literalcodeencodingparser	tab-width
start-lineend-linestart-after
end-beforenumber-linesclassnameincludec                  C   sL  | j jjjs| d| j | j jj}t| j	d }|
dr^|dr^| j}|dd }n
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 np tk
r   | d| j d| dY nV tk
r4 } z$| d| j dt| dW 5 d}~X Y nX | j jjj| | jdd}	| jdd}
z8|	sv|
dk	r| }d||	|
 }n|  }W nB t!k
r } z"| d| j dt| W 5 d}~X Y nX | jdd}|r,|"|}|dk r| d| j ||t#| d }| jdd}|rp|"|}|dk rd| d| j |d| }t$j%||dd}t&|D ]6\}}t#|| j jjj'kr| d||d f qd| jkr|dkr|(|}n|}t)j*||| jdg d}d|_+| ,| d| jkrzt-| jd p.d}	W n  t.k
rT   | /d Y nX |	t#| }
|d!rz|dd }t0g |fg|	|
}|D ]4\}}|r|t)j1|||d"7 }n|t)2|7 }qn|t)2|7 }|gS d#| jkr<|| jd$< |dk r|3 }t4| j| j5d#g| j|| j6| j7| j8| j | j9	}|: S |	|
||f}| j jj;}|sn|<td|d%f ||f|krd&d' t=|D }d(|f|}| d)| j|f d*| jkrt>|| j jj}|||fg |_;| jd*  }|?d!|| |j@A|f |j@B  |jCS |dd+| g7 }| j9D|| |<||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_handlerProblems with "z1" directive path:
Cannot encode input file path "z" (wrong locale?)." directive path:
.r   r    Problem with "" directive:
r   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 ).0pthoptr1   r1   W/root/rtd-docs/venv/lib/python3.8/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")Estatedocumentsettingsfile_insertion_enabledwarningr   current_sourcer   path	arguments
startswithendswithstandard_include_pathr   parentr	   relative_pathoptionsgetinput_encodinginput_encoding_error_handler	tab_widthr   	FileInputUnicodeEncodeErrorsevereOSErrorerror_stringrecord_dependenciesadd	readlinesjoinreadUnicodeErrorfindlenr   Zstring2lines	enumerateZline_length_limit
expandtabsr   literal_blocklineZadd_nameint
ValueErrorerrorr   inlineText
splitlinesr   poplinenocontent_offset
block_textstate_machineruninclude_logappendreversedZnew_documentparseZtransformerZpopulate_from_componentsZapply_transformschildrenZinsert_input) selfr<   r=   _baser   	e_handlerrH   Zinclude_filer\   	startlineendlinelinesZrawtextZ
after_textZafter_indexZbefore_textZbefore_indexZinclude_linesirY   textrX   tokensr/   valueZ	codeblockZclip_optionsrf   Zmaster_pathsZinclusion_chainr8   r   r1   r1   r5   re   7   s   

 
 

2






 




	

zInclude.runN)__name__
__module____qualname____doc__required_argumentsoptional_argumentsfinal_argument_whitespacer   flagZ	unchangedr   parser_namerZ   unchanged_requiredclass_optionoption_specr   r   __file__rB   rA   re   r1   r1   r1   r5   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ks,d| jkr<| d| j dd| j	d 
  i}| jd| j jjj}| j jjj}| jrd| jksd| jkr| d| j d	| j}nd| jkrd| jkr| d
| j | jd }t| j jjj}td || }ztj|||d}W nD tk
r\ } z$| d| j dt| dW 5 d }~X Y nX | j jjj| z| }W nB tk
r } z"| d| j dt| W 5 d }~X Y nX ||d< nd| jkr| jd }	zt|	 }
W nT t tfk
rD } z0| d| j d| jd  dt| dW 5 d }~X Y nX tj!|
|	||d}z| }W nB tk
r } z"| d| j dt| W 5 d }~X Y nX |	|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.r0   zXThe "file" and "url" options may not be simultaneously specified for the "%s" directive.r%   r(   r)   r*   r,   r-   r.   z" directive URL "z":
)r.   r&   r   r'   r+   r/   r   )*r7   r8   r9   Zraw_enabledr:   rD   r;   r   rQ   r>   lowersplitrE   rF   rG   contentr\   r   r<   rB   r	   rC   r   rI   rL   rK   rM   rN   rO   rR   rS   r   r   ZStringInputassert_has_contentr   rawrd   Zget_source_and_linera   r.   rY   )rk   
attributesr   rm   rr   r=   rl   Zraw_filer\   r.   Zraw_textZraw_noder1   r1   r5   re      s    
 



2

>
zRaw.runN)ru   rv   rw   rx   ry   rz   r{   r   r=   urir   r   r   has_contentre   r1   r1   r1   r5   r      s   	r   c                   @   s   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 ]\}|svt |t	jrv|}q\t |t	jrg |d< || q\| jjd| j d| jdg  S q\|r||j S |S )NVInvalid context: the "%s" directive can only be used within a substitution definition.r0   Zbackrefsz
Error in "z1" directive: may contain a single paragraph only.rY   )
isinstancer7   r   SubstitutionDefr\   r   r   rQ   r   r   Elementnested_parserb   Z	paragraphZsystem_messagerg   reporterra   rj   )rk   rr   elementnodemessageselemr1   r1   r5   re   -  s8    


zReplace.runNru   rv   rw   r   re   r1   r1   r1   r5   r   )  s   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krDd|j	d< d|j	d< d| jkrXd|j	d< d| jkrld|j	d< | j
| jd d  }t }|D ]\}zt|}W n: tk
r } z| d|t|f W 5 d }~X Y nX |t|7 }q|jS )Nr   r   r   r   r   r   zInvalid character code: %s
%s)r   r7   r   r   r\   r   rd   r   rD   r   comment_patternr   r>   r   r   r   Zunicode_coder[   r   rM   r^   rj   )rk   Zsubstitution_definitioncodesr   r   decodedr\   r1   r1   r5   re   ]  s2    






zUnicode.runN)ru   rv   rw   rx   ry   rz   r{   r   r|   r   recompiler   re   r1   r1   r1   r5   r   J  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k
rB   | d| j| jd f Y nX g }| jrt }| j	
| j| j| |D ]}|d | qn||j n2ttj|| jd| j}| jj| || |S )Nr   z7Invalid class attribute value for "%s" directive: "%s".r/   )r   	directive)r   r   r>   r[   r\   r   r   r   r   r7   r   rb   extendrj   pendingr   ZClassAttributerc   rd   r8   Znote_pendingrg   )rk   Zclass_valueZ	node_list	containerr   r   r1   r1   r5   re     s2    


z	Class.runN)	ru   rv   rw   rx   ry   rz   r{   r   re   r1   r1   r1   r5   r   v  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|}|sP| d| j|f |d}|d}g }|rt	|| j
j| j| jj\}}|dkr| jjjd| t| j| j| jd}||g S ntj}t|d	rtd
| j|f z4t|}| jj| jdd | j |i d\}	}
}}W n^ tjk
rx } z<| jjd| j|f t| j| j| jd}||g  W Y S d}~X Y nX d|
krzt||
d< W n\ tk
r } z<| jjd| j|f t| j| j| jd}||g  W Y S d}~X Y nX 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.)rb   ra   r   r\   r   argument_patternmatchgroupr   rolerd   languager7   r   r   rX   rc   Zgeneric_custom_rolehasattrAssertionErrorr   Zparse_directive_blockr   MarkupErrorr   r   r[   Z
CustomRoleZregister_local_role)rk   argsr   Znew_role_nameZbase_role_namer   Z	base_roler\   Zconverted_roler>   rD   r   rb   detailr   r1   r1   r5   re     s    


     
 zRole.runN)ru   rv   rw   r   r   r   r   ZInlinerZ
simplenamer   re   r1   r1   r1   r5   r     s
   r   c                   @   s    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krtjd= g S | j d }t|| jj| j| jj\}}|d kr|| jjj	d| t
| j| j| jd}||g S |tjd< |S )Nr+   r   r   r   )r>   r   Z_rolesr   rd   r   ra   r7   r   r\   r   rX   rc   )rk   Z	role_namer   r   r\   r1   r1   r5   re     s$    

 

zDefaultRole.runN)ru   rv   rw   rx   rz   r{   re   r1   r1   r1   r5   r     s   r   c                   @   s    e Zd ZdZdZdZdd ZdS )Titler   r   Tc                 C   s   | j d | jjd< g S )Nr   title)r>   rd   r8   )rk   r1   r1   r5   re     s    z	Title.runN)ru   rv   rw   ry   rz   r{   re   r1   r1   r1   r5   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.)	parsemetarB   )rk   r   contextZ
next_stater   blank_finishr1   r1   r5   field_marker  s    zMetaBody.field_markerc                 C   sn  |  |}tt|}| j| \}}}}t }ttd	||d< |s| jj
}| jd| t||}	|	|fS | }
z&t|
d d \}}||| < W n" tjk
r   |
d |d< Y nX |
dd  D ]}z"t|d \}}||| < W q tjk
rb } z:| jj
}| jd||f t||}	|	|f W Y   S d }~X Y qX q||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escape2nullrd   Zget_first_known_indentedendmetarQ   rY   r   inforX   r   Zextract_name_valuer   ZNameValueErrorr\   )rk   r   r   Zindentedindentline_offsetr   r   rY   msgrs   Zattnamevaltokenr   r1   r1   r5   r     sF    




$zMetaBody.parsemetaN)ru   rv   rw   r   r   r1   r1   r1   r5   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rn| j	j
dt| j| j| jd}||7 }| jjtjtjfpd}|j| jj||< g S )Nr   T)Zinitial_stater   Zstate_machine_kwargszInvalid meta directive.r   r   )r   r   r   r7   Znested_list_parser   rb   SMkwargsrU   r   r\   rX   rc   ra   r8   Zfirst_child_not_matching_classZTitularr   rj   )rk   r   Znew_line_offsetr   r\   indexr1   r1   r5   re   .  s0       

zMeta.runN)ru   rv   rw   r   r   r   re   r1   r1   r1   r5   r   (  s   
r   c                   @   s   e Zd ZdZdd ZdS )DateTc                 C   sD   t | jtjs| d| j d| jp,d}t	|}t
|gS )Nr   r0   z%Y-%m-%d)r   r7   r   r   r\   r   rQ   r   timestrftimer   r^   )rk   
format_strrr   r1   r1   r5   re   G  s    
zDate.runNr   r1   r1   r1   r5   r   C  s   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   sh   | j r@d| j }| jjd| j| j| jf t||| j	d}n"| jjd| j| j| jf | j	d}|gS )Nr0   zBDirective processed. Type="%s", arguments=%r, options=%r, content:r   zGDirective processed. Type="%s", arguments=%r, options=%r, content: None)
r   rQ   r   r   r   r>   rD   r   rX   ra   )rk   rr   r   r1   r1   r5   re   j  s     
 zTestDirective.runN)ru   rv   rw   rx   rz   r{   r   r~   r   r   re   r1   r1   r1   r5   r   a  s   
r   )'rx   __docformat__pathlibr   r   r   urllib.requestr   urllib.errorr   Zdocutilsr   r   r   r	   Zdocutils.parsers.rstr
   r   r   r   r   Z$docutils.parsers.rst.directives.bodyr   r   Zdocutils.transformsr   r   r   r   r   r   r   r   r   ZSpecializedBodyr   r   r   r   r1   r1   r1   r5   <module>   s0    <X!,&>)