o
    in                     @  s  d Z ddlmZ dZddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZ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 dZer\ddlmZ d-d.dd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.    )annotationsreStructuredTextN)Path)urlopen)URLError)frontendionodesstatemachineutils)	Directiveconvert_directive_function)
directivesrolesstates)	CodeBlockNumberLines)miscF)StrPath pathstrreturnc                 C  s>   |r|  drt|}| dd  } nt|j}td ||  S )N/   )
startswithr   parentr   relative_path)r   sourceroot_prefixbase r!   j/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/docutils/parsers/rst/directives/misc.py
adapt_path   s
   
r#   c                   @  s   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deejej	dZeejjd Zdd
dZdddZdddZdddZd ddZd!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#include
    r   r   TN)literalcodeencodingparser	tab-width
start-lineend-linestart-after
end-beforenumber-linesclassnameincluder   listc                 C  s   | j jj | _}|js| d| j | jd|j| _| jdd| jdd| jdd| jddf| _	t
| jd	 }|d
rU|drUd|dd  }| j}n|j}t|| j jj|}|| jd< | |}d| jv ru| |S d| jv r| |S d| jv r| |S | | g S )zInclude a file as part of the content of this reST file.

        Depending on the options, the file content (or a clipping) is
        converted to nodes and returned or inserted into the input stream.
        "%s" directive disabled.r*   r+   Nr,   r-   r   r.   r   <>r   r   r   r&   r'   r)   )statedocumentsettingsfile_insertion_enabledwarningr1   optionsget	tab_widthclip_optionsr   r   	argumentsr   endswithstandard_include_pathr   r#   current_source	read_fileas_literal_blockas_code_blockcustom_parseinsert_into_input_lines)selfr:   r   r   inputstringr!   r!   r"   runK   s8   








zInclude.runr   r   r   c                 C  s  | j d| jj}| jj}z
tj|||d}W n/ ty+   | d| j	 d| d t
yF } z| d| j	 dt| dd}~ww | jj| z| }W n tyo } z| d	| j	 d
t| d}~ww | j\}}}	}
|s}|dur| }d||| }|	r||	}|dk r| d| j	 d||t|	 d }|
r||
}|dk r| d| j	 d|d| }|S )zQRead text file at `path`. Clip and return content.

        Provisional.
        r(   source_pathr(   error_handlerProblems with "z1" directive path:
Cannot encode input file path "z" (wrong locale?)." directive path:
.NProblem with "" directive:

r   z&Problem with "start-after" option of "z" directive:
Text not found.z%Problem with "end-before" option of ")r=   r>   r:   input_encodinginput_encoding_error_handlerr   	FileInputUnicodeEncodeErrorseverer1   OSErrorerror_stringrecord_dependenciesaddreadUnicodeErrorr@   
splitlinesjoinfindlen)rJ   r   r(   rO   include_fileerrortext	startlineendline	starttextendtextlinesafter_indexbefore_indexr!   r!   r"   rE   o   sZ   






zInclude.read_filerg   list[nodes.literal_block]c           	      C  s   | j d }| jdkr|| j}tjd|| j dg d}||_| j ddd |_| | d| j v ro| j d p:d}|	d	}|t
|  }tg |fg||}|D ]\}}|rd|tjd||d
7 }qS|t|7 }qS|gS |t|7 }|gS )zOReturn list with literal_block containing `text`.

        Provisional
        r   r   r   r0   )r   classesr+   r   r/   rU   )rp   )r=   r?   
expandtabsr	   literal_blockr>   r   lineadd_nameremovesuffixrd   ra   r   inlineText)	rJ   rg   r   rr   	firstlinelastlinetokensrp   valuer!   r!   r"   rF      s*   




zInclude.as_literal_blockc              
   C  sV   | j dkr|| j }t| j| jdg| j|dg| j| j| j	| j
| j	}| S )zNPass `text` to the `CodeBlock` directive class.

        Provisional.
        r   r'   rU   )r?   rq   r   r1   r=   popru   linenocontent_offset
block_textr8   state_machinerL   )rJ   rg   	codeblockr!   r!   r"   rG      s   


zInclude.as_code_blockc                 C  s   | j d  }t|}| jj D ]
\}}t||| q| j d |_t	|j|}| j
jj|_| j
jj|_| j
jj|_| j
jj|_||| | j
jj|j |j|f |j  | j
jj|j |jS )zParse with custom parser.

        Parse with ``self.options['parser']`` into a new (dummy) document,
        apply the parser's default transforms,
        return child elements.

        Provisional.
        r)   r   )r=   r   get_default_settingsr:   __dict__itemssetattr_sourcer   new_documentr8   r9   include_logidsnameids	nametypesparseparse_messagesextendtransformerpopulate_from_componentsapply_transformstransform_messageschildren)rJ   rg   r)   r:   kvr9   r!   r!   r"   rH      s$   	


zInclude.custom_parseNonec                 C  s  | j d }tj|| jdd}t|D ]#\}}t|| jjkr5|d | j dd }| 	d| d| d	q| j
jj}|sMtd
| j
jj}||df || jf|v rrdd t|D }	d|g|	R }
| 	d| j d|
 ||| jf |dd| dg7 }| j|| d
S )z\Insert file content into the rST input of the calling parser.

        Provisional.
        r   T)convert_whitespacer   r+   r   "z": line z exceeds the line-length-limit.N)NNr   r   c                 s  s    | ]\}}|V  qd S r%   r!   ).0pthoptr!   r!   r"   	<genexpr>  s    z2Include.insert_into_input_lines.<locals>.<genexpr>z
> zcircular inclusion in "rT   r   z.. end of inclusion from ")r=   r
   string2linesr?   	enumeraterd   r:   line_length_limitr>   r<   r8   r9   r   r   r   rD   appendr@   reversedrb   r1   r   insert_input)rJ   rg   r   	textlinesirs   line_nor   rD   source_chaininclusion_chainr!   r!   r"   rI      s0   



zInclude.insert_into_input_linesN)r   r3   )r   r   r   r   )rg   r   r   ro   )rg   r   r   r3   )rg   r   r   r   )__name__
__module____qualname____doc__required_argumentsoptional_argumentsfinal_argument_whitespacer   flag	unchangedr(   parser_nameintunchanged_requiredvalue_orclass_optionoption_specr   r   __file__r   rC   rL   rE   rF   rG   rH   rI   r!   r!   r!   r"   r$   *   s2    


$
0

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(   r0   c                 C  s  | j jj}|jr|jsd| jv sd| jv r| d| j dd| j	d 
  i}| jd|j}|j}| jrRd| jv sCd| jv rK| d| j d	| j}nd| jv rd| jv rd| d
| j t| jd | j jj|j}z
tj|||d}W n ty } z| d| j dt| dd }~ww |j| z| }W n ty } z| d| j dt| d }~ww ||d< nmd| jv r.| jd }	zt|	 }
W n$ ttfy } z| d| j d| jd  dt| dd }~ww tj|
|	||d}z| }W n ty( } z| d| j dt| d }~ww |	|d< n|    t!j"d|fd| jdg i|}| j#$| j%\|_&|_'|gS )Nr   r   r4   format r   r(   zF"%s" directive may not both specify an external file and have content.rU   zXThe "file" and "url" options may not be simultaneously specified for the "%s" directive.rM   rP   rQ   rR   rS   rT   r   z" directive URL "z":
)r   rN   r(   rO   r   rp   r0   )(r8   r9   r:   raw_enabledr;   r=   r<   r1   rb   rA   lowersplitr>   rV   rW   contentrf   r#   rD   r   r   rX   r[   rZ   r\   r]   r^   r_   r`   r   r   StringInputassert_has_contentr	   rawr   get_source_and_liner}   r   rs   )rJ   r:   
attributesr(   rO   rg   r   raw_filerf   r   raw_textraw_noder!   r!   r"   rL   "  s   









zRaw.runN)r   r   r   r   r   r   r   r   r   urir(   r   r   has_contentrL   r!   r!   r!   r"   r     s    	r   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.rU   backrefsz
Error in "z1" directive: may contain a single paragraph only.rs   )
isinstancer8   r   SubstitutionDefrf   r1   r   rb   r   r	   Elementnested_parser~   	paragraphsystem_messager   reporterr}   r   )rJ   rg   elementnodemessageselemr!   r!   r"   rL   i  s8   

zReplace.runNr   r   r   r   rL   r!   r!   r!   r"   r   e      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   r8   r   r   rf   r1   r   r   r=   r   comment_patternr   rA   r	   r   r   unicode_code
ValueErrorr   r\   rw   r   )rJ   substitution_definitioncodesr   r'   decodedrf   r!   r!   r"   rL     s6   






zUnicode.runN)r   r   r   r   r   r   r   r   r   r   recompiler   rL   r!   r!   r!   r"   r     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".rp   )r0   	directive)r   r   rA   r   rf   r1   r   r	   r   r8   r   r~   r   r   pendingr   ClassAttributer   r   r9   note_pendingr   )rJ   class_value	node_list	containerr   r   r!   r!   r"   rL     s6   

z	Class.runN)	r   r   r   r   r   r   r   r   rL   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   rA   z[Supplemental directive arguments for "%s" directive not supported (specified by "%r" role).)option_presetszError in "%s" directive:
%s.r0   z(Invalid argument for "%s" directive:
%s.)r~   r}   r   rf   r1   argument_patternmatchgroupr   roler   languager8   r   r	   rr   r   generic_custom_rolehasattrr   parse_directive_blockr   MarkupErrorr   r   r   
CustomRoleregister_local_role)rJ   argsr   new_role_namebase_role_namer   	base_rolerf   converted_rolerA   r=   r   r~   detailr   r!   r!   r"   rL     s   



zRole.runN)r   r   r   r   r   r   r   Inliner
simplenamer   rL   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   )rA   r   _rolesr   r   r   r}   r8   r   rf   r	   rr   r   )rJ   	role_namer   r   rf   r!   r!   r"   rL     s"   




zDefaultRole.runN)r   r   r   r   r   r   rL   r!   r!   r!   r"   r
    s
    r
  c                   @  r	  )Titler   r   Tc                 C  s   | j d | jjd< g S )Nr   title)rA   r   r9   )rJ   r!   r!   r"   rL   6  s   z	Title.runN)r   r   r   r   r   r   rL   r!   r!   r!   r"   r  0  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.)	parsemetar   )rJ   r   context
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   r1   r   z*Error parsing meta tag attribute "%s": %s.)parse_field_markerr	   unescaper   escape2nullr   get_first_known_indentedendmetarb   rs   r   inforr   r   extract_name_valuer   NameValueErrorrf   )rJ   r   r1   indentedindentline_offsetr  r   rs   msgrz   attnamevaltokenr  r!   r!   r"   r  C  sL   




zMetaBody.parsemetaN)r   r   r   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 )MetaT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)initial_stater  state_machine_kwargszInvalid meta directive.r   r   )r   r	   r   r8   nested_list_parser   r~   SMkwargsrd   r   rf   rr   r   r}   r9   first_child_not_matching_classTitularr  r   )rJ   r   new_line_offsetr  rf   indexr!   r!   r"   rL   j  s*   


zMeta.runN)r   r   r   r   r  r*  rL   r!   r!   r!   r"   r%  d  s    
r%  c                   @  r   )DateTc                 C  sD   t | jtjs| d| j d| jpd}t	|}t
|gS )Nr   rU   z%Y-%m-%d)r   r8   r   r   rf   r1   rb   r   timestrftimer	   rw   )rJ   
format_strrg   r!   r!   r"   rL     s   
zDate.runNr   r!   r!   r!   r"   r/    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 )NrU   zBDirective processed. Type="%s", arguments=%r, options=%r, content:r   zGDirective processed. Type="%s", arguments=%r, options=%r, content: None)
r   rb   r   r  r1   rA   r=   r	   rr   r}   )rJ   rg   r  r!   r!   r"   rL     s    	zTestDirective.runN)r   r   r   r   r   r   r   r   r   r   rL   r!   r!   r!   r"   r3    s    
r3  )r   r   )r   r   r   r   ).r   
__future__r   __docformat__r   r0  pathlibr   urllib.requestr   urllib.errorr   docutilsr   r   r	   r
   r   docutils.parsers.rstr   r   r   r   r   $docutils.parsers.rst.directives.bodyr   r   docutils.transformsr   TYPE_CHECKINGdocutils.nodesr   r#   r$   r   r   r   r   r   r
  r  SpecializedBodyr  r%  r/  r3  r!   r!   r!   r"   <module>   s<    eW!,&>)