o
    NiLh                     @   sd  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 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.reStructuredText    )PathN)urlopen)URLError)ionodesstatemachineutils)	Directiveconvert_directive_function)
directivesrolesstates)	CodeBlockNumberLines)misc /c                 C   s:   |  drt|}| dd  } nt|j}td ||  S )Nr      )
startswithr   parentr	   relative_path)pathsourceroot_prefixbase r   X/home/ubuntu/.local/lib/python3.10/site-packages/docutils/parsers/rst/directives/misc.py
adapt_path   s
   

r   c                   @   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   r   T)literalcodeencodingparser	tab-width
start-lineend-linestart-after
end-beforenumber-linesclassnameincludec           !      C   s  | j jj}|js| d| j | jd|j}| j jj	}t
| jd }|dr;|dr;d|dd  }| j}n|j}t|||}| jd	|j}|j}z
tj|||d
}W n/ tym   | d| j d| d ty }	 z| d| j dt|	 dd}	~	ww |j| | jdd}
| jdd}z|
s|dur| }d||
| }n| }W n ty }	 z| d| j dt|	 d}	~	ww | jdd}|r| |}|dk r| d| j ||t!| d }| jdd}|r| |}|dk r| d| j |d| }t"j#||dd}t$|D ]\}}t!||j%kr<| d||d f q%d| jv r|dkrO|&|}n|}t'j(||| jdg d}d|_)| *| d | jv rzt+| jd  pud}
W n t,y   | -d!w |
t!| }|d"r|dd }t.g |fg|
|}|D ]\}}|r|t'j/|||d#7 }q|t'0|7 }q|gS |t'0|7 }|gS d$| jv r|| jd%< |dk r|1 }t2| j| j3d$g| j|| j4| j5| j6| j | j7	}|8 S |
|||f}| j jj9}|s|:t;<d|d&f ||f|v r8d'd( t=|D }d)|g|R }| d*| j|f d+| jv rjt;>||}|||fg |_9| jd+  } | ?d"|| |j@A| f |j@B  |jCS |dd,| g7 }| j7D|| |:||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   <>r   r   r"   source_pathr"   error_handlerProblems with "z1" directive path:
Cannot encode input file path "z" (wrong locale?)." directive path:
.Nr%   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)convert_whitespacez,"%s": line %d exceeds the line-length-limit.r    r*   )r   classesr)   z+:number-lines: with non-integer start value
)r:   r!   r   )NNNNc                 s   s    | ]\}}|V  qd S )Nr   ).0pthoptr   r   r   	<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+   optionsget	tab_widthcurrent_sourcer   r   	argumentsr   endswithstandard_include_pathr   r   input_encodinginput_encoding_error_handlerr   	FileInputUnicodeEncodeErrorsevereOSErrorerror_stringrecord_dependenciesadd	readlinesjoinreadUnicodeErrorfindlenr   string2lines	enumerateline_length_limit
expandtabsr   literal_blocklineadd_nameint
ValueErrorerrorr   inlineText
splitlinesr   poplinenocontent_offset
block_textstate_machineruninclude_logappendr	   r   reversednew_documentparsetransformerpopulate_from_componentsapply_transformschildreninsert_input)!selfrB   rG   rH   r   r   r"   r3   include_filerd   	startlineendlinelinesrawtext
after_textafter_indexbefore_textbefore_indexinclude_linesir`   textr_   tokensr:   value	codeblockclip_optionsrn   master_pathsinclusion_chainrA   r#   r   r   r   rm   E   s  














	

zInclude.runN)__name__
__module____qualname____doc__required_argumentsoptional_argumentsfinal_argument_whitespacer   flag	unchangedr"   parser_namerb   unchanged_requiredclass_optionoption_specr   r   __file__r   rK   rm   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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   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.r1   r4   r5   r6   r7   r8   r   z" directive URL "z":
)r   r2   r"   r3   r   r:   r*   )(r@   rA   rB   raw_enabledrC   rE   rD   r+   rV   rI   lowersplitrF   rL   rM   contentrd   r   rH   r   r   rN   rQ   rP   rR   rS   rT   rW   rX   r   r   StringInputassert_has_contentr   rawrl   get_source_and_lineri   r   r`   )rx   rB   
attributesr"   r3   r   r   raw_filerd   r   raw_textraw_noder   r   r   rm      s   









zRaw.runN)r   r   r   r   r   r   r   r   r   urir"   r   r   has_contentrm   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.r;   backrefsz
Error in "z1" directive: may contain a single paragraph only.r`   )
isinstancer@   r   SubstitutionDefrd   r+   r   rV   r   r   Elementnested_parserj   	paragraphsystem_messagero   reporterri   rv   )rx   r   elementnodemessageselemr   r   r   rm   9  s8   

zReplace.runNr   r   r   r   rm   r   r   r   r   r   5      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   rd   r+   rl   r   rE   r   comment_patternr   rI   r   r   r   unicode_coderc   r   rR   rf   rv   )rx   substitution_definitioncodesr   r!   decodedrd   r   r   r   rm   i  s6   






zUnicode.runN)r   r   r   r   r   r   r   r   r   r   recompiler   rm   r   r   r   r   r   V  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   r   rI   rc   rd   r+   r   r   r   r@   r   rj   extendrv   pendingr   ClassAttributerk   rl   rA   note_pendingro   )rx   class_value	node_list	containerr   r   r   r   r   rm     s6   

z	Class.runN)	r   r   r   r   r   r   r   r   rm   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   rI   z[Supplemental directive arguments for "%s" directive not supported (specified by "%r" role).)option_presetszError in "%s" directive:
%s.r*   z(Invalid argument for "%s" directive:
%s.)rj   ri   r   rd   r+   argument_patternmatchgroupr   rolerl   languager@   r   r   r_   rk   generic_custom_rolehasattrr   parse_directive_blockr   MarkupErrorr   r   rc   
CustomRoleregister_local_role)rx   argsr   new_role_namebase_role_namer   	base_rolerd   converted_rolerI   rE   r   rj   detailr   r   r   r   rm     s   



zRole.runN)r   r   r   r   r   r   r   Inliner
simplenamer   rm   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   )rI   r   _rolesr   rl   r   ri   r@   r   rd   r   r_   rk   )rx   	role_namer   r   rd   r   r   r   rm     s"   




zDefaultRole.runN)r   r   r   r   r   r   rm   r   r   r   r   r     s
    r   c                   @   r   )Titler   r   Tc                 C   s   | j d | jjd< g S )Nr   title)rI   rl   rA   )rx   r   r   r   rm     s   z	Title.runN)r   r   r   r   r   r   rm   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.)	parsemetar   )rx   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   r+   r   z*Error parsing meta tag attribute "%s": %s.)parse_field_markerr   unescaper	   escape2nullrl   get_first_known_indentedendmetarV   r`   r   infor_   r   extract_name_valuer   NameValueErrorrd   )rx   r   r+   indentedindentline_offsetr   r   r`   msgr   attnamevaltokenr   r   r   r   r     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   r@   nested_list_parser   rj   SMkwargsrZ   r   rd   r_   rk   ri   rA   first_child_not_matching_classTitularr   rv   )rx   r   new_line_offsetr   rd   indexr   r   r   rm   :  s*   


zMeta.runN)r   r   r   r   r   r  rm   r   r   r   r   r  4  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   rd   r+   rV   r   timestrftimer   rf   )rx   
format_strr   r   r   r   rm   S  s   
zDate.runNr   r   r   r   r   r  O  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   rV   r   r   r+   rI   rE   r   r_   ri   )rx   r   r   r   r   r   rm   v  s    	zTestDirective.runN)r   r   r   r   r   r   r   r   r   r   rm   r   r   r   r   r  m  s    
r  )r   r   )(r   __docformat__pathlibr   r   r  urllib.requestr   urllib.errorr   docutilsr   r   r   r	   docutils.parsers.rstr
   r   r   r   r   $docutils.parsers.rst.directives.bodyr   r   docutils.transformsr   r   r   r   r   r   r   r   r   r   SpecializedBodyr   r  r  r  r   r   r   r   <module>   s4   
 ;W!,&>)