U
    BeC]                     @   sL  d Z dZddl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 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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Z%G dd deZ&dS ) zMiscellaneous directives.reStructuredText    N)ionodesstatemachineutils)
SafeStringErrorString)locale_encoding)	Directiveconvert_directive_function)
directivesrolesstates)	CodeBlockNumberLines)set_classes)miscc                   @   sh   e Zd ZdZdZdZdZ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	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r|drtj| j|dd }tjtj||}td|}t|}| jd| j jjj}| j jjj}| jd	| j jjj}z&| j jjj | t!j"|||d
}W nv t#k
rN } z| $d| jt%|f W 5 d}~X Y n< t&k
r } z| $d| jt'|f W 5 d}~X Y nX | jdd}	| jdd}
z8|	s|
dk	r|( }d||	|
 }n|) }W n< t*k
r } z| $d| jt'|f W 5 d}~X Y nX | jdd}|rh|+|}|dk rT| $d| j ||t,| d }| jdd}|r|+|}|dk r| $d| j |d| }t-j.||dd}d| jkr|dkr|/|}n|}tj0||| jdg d}d|_1| 2| d| jkrzt3| jd p*d}	W n  t4k
rP   | 5dY nX |	t,| }
|drv|dd }t6g |fg|	|
}|D ]6\}}|r|tj7|||d7 }n|t8||7 }qn|t8||7 }|gS d| jkr<|| jd< |dk r|9 }t:| j| j;dg| j|| j	| j<| j=| j | j	}|> S | j?|| g S ) z8Include a file as part of the content of this reST file."%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_whitespacer   r   )sourceclassesr   z+:number-lines: with non-integer start value
)r,   r   r+   )@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_pathr   Zreprunicodeoptionsgetinput_encodinginput_encoding_error_handler	tab_widthrecord_dependenciesaddr   	FileInputUnicodeEncodeErrorseverer   IOErrorr   	readlinesreadUnicodeErrorfindlenr   Zstring2lines
expandtabsliteral_blocklineZadd_nameint
ValueErrorerrorr   inlineText
splitlinesr   popcontent_offset
block_textrunZinsert_input)selfr+   
source_dirr6   r   	e_handlerrD   Zinclude_filerU   	startlineendlinelinesZrawtextZ
after_textZafter_indexZbefore_textZbefore_indexZinclude_linestextrQ   tokensr,   valueZ	codeblock rf   /var/www/nmhs-web.org.in/public_html/infoladakh/backend/venv/lib/python3.8/site-packages/docutils/parsers/rst/directives/misc.pyr\   5   s    
 
 











	zInclude.runN)__name__
__module____qualname____doc__required_argumentsoptional_argumentsfinal_argument_whitespacer   flagZ	unchangedr   rS   unchanged_requiredclass_optionoption_specr5   r6   r<   r7   r   __file__r=   r\   rf   rf   rf   rg   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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   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 tjtj| j jj}tjtj|| jd }td |}z&tj|||d}| j jjj| W n< tk
r~ } z| d| jt|f W 5 d }~X Y nX z|  }W n< t!k
r } z| d| jt|f W 5 d }~X Y nX ||d< n d| jkr| jd }	dd l"}
dd l#}
dd l$}
z|
j%&|	  }W nN |
jj'tt(fk
rf } z$| d| j| jd t|f W 5 d }~X Y nX tj)||	||d}z|  }W n< t!k
r } z| d| jt|f W 5 d }~X Y nX |	|d< n| *  t+j,d|f|}| j-.| j/\|_0|_1|gS )Nru   rv   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+   z*Problems with "%s" directive URL "%s":
%s.)r+   r&   r   r'   r)   )2r.   r/   r0   Zraw_enabledr1   r@   r2   r   r<   r9   lowersplitrA   rB   rC   contentrU   r5   r6   r7   r8   Zcurrent_sourcer>   r   r?   r   rG   rE   rF   rJ   rI   r   rL   rM   urllib.requesturllib.errorurllib.parserequesturlopenURLErrorOSErrorZStringInputassert_has_contentr   rawr3   Zget_source_and_liner4   r+   rR   )r]   
attributesr   r_   rc   r^   r6   Zraw_filerU   r+   urllibZraw_textZraw_noderf   rf   rg   r\      s    
 



zRaw.runN)rh   ri   rj   rk   rl   rm   rn   r   r6   urir   rr   has_contentr\   rf   rf   rf   rg   rt      s   	rt   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 ]Z}|svt |t	jrv|}q\t |t	jrg |d< || q\| jjjd| j | jdg  S q\|r||j S |S )NVInvalid context: the "%s" directive can only be used within a substitution definition.r-   Zbackrefsz=Error in "%s" directive: may contain a single paragraph only.rR   )
isinstancer.   r   SubstitutionDefrU   r   r   r<   r{   r   Elementnested_parserZ   Z	paragraphZsystem_messageappendr3   reporterr4   children)r]   rc   elementnodemessageselemrf   rf   rg   r\     s<    


zReplace.runNrh   ri   rj   r   r\   rf   rf   rf   rg   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 ]b}zt|}W n8 tk
r } z| d|t|f W 5 d }~X Y nX |tt||7 }q|jS )Nr   r   r   r   r   r   zInvalid character code: %s
%s)r   r.   r   r   rU   r   r3   r   r@   r   comment_patternrz   r9   r   r   r   Zunicode_coderT   r   rW   r   unescaper   )r]   Zsubstitution_definitioncodesr   r   decodedrU   rf   rf   rg   r\   1  s2    







zUnicode.runN)rh   ri   rj   rk   rl   rm   rn   r   ro   rr   recompiler   r\   rf   rf   rf   rg   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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   rq   r9   rT   rU   r   r{   r   r   r.   r   rZ   extendr   pendingr   ZClassAttributer[   r3   r/   Znote_pendingr   )r]   Zclass_valueZ	node_list	containerr   r   rf   rf   rg   r\   W  s2    


z	Class.runN)	rh   ri   rj   rk   rl   rm   rn   r   r\   rf   rf   rf   rg   r   J  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
rz } z>| j
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 nb tk
r } zB| j
jjd| jt|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   r9   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.)rZ   r4   r{   rU   r   argument_patternmatchgroupr   roler3   languager.   r   r   rQ   r[   Zgeneric_custom_rolehasattrAssertionErrorr   Zparse_directive_blockr   MarkupErrorr   rq   rT   r   Z
CustomRoleZregister_local_role)r]   argsr   Znew_role_nameZbase_role_namer   Z	base_rolerU   Zconverted_roler9   r@   r{   rZ   detailr   rf   rf   rg   r\   w  s    


     
  zRole.runN)rh   ri   rj   r   r   r   r   ZInlinerZ
simplenamer   r\   rf   rf   rf   rg   r   p  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   )r9   r   Z_rolesr   r3   r   r4   r.   r   rU   r   rQ   r[   )r]   Z	role_namer   r   rU   rf   rf   rg   r\     s$    

 

zDefaultRole.runN)rh   ri   rj   rk   rm   rn   r\   rf   rf   rf   rg   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)r9   r3   r/   )r]   rf   rf   rg   r\     s    z	Title.runN)rh   ri   rj   rl   rm   rn   r\   rf   rf   rf   rg   r     s   r   c                   @   s   e Zd ZdZdd ZdS )DateTc                 C   s   t | jtjs| d| j d| jp,d}tj	dk rnz|
tpDd}W n" tk
rl   | dt Y nX t|}tj	dk rz|tpd}W n6 tk
r   |tpdd}| d|tf Y nX t|gS )	Nr   r-   z%Y-%m-%d)r   r   zutf-8z;Cannot encode date format string with locale encoding "%s".replacez-Error decoding "%s"with locale encoding "%s".)r   r.   r   r   rU   r   r<   r{   sysversion_infoencoder	   rH   r2   timestrftimedecodeUnicodeDecodeErrorr   rW   )r]   
format_strrc   rf   rf   rg   r\     s0    


zDate.runNr   rf   rf   rf   rg   r     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   sl   | j rBd| j }| jjjd| j| j| jf t	||| j
d}n$| j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<   r3   r   infor   r9   r@   r   rQ   r4   )r]   rc   r   rf   rf   rg   r\   	  s     
 zTestDirective.runN)rh   ri   rj   rk   rm   rn   r   rp   rr   r   r\   rf   rf   rf   rg   r      s   
r   )'rk   __docformat__r   os.pathr5   r   r   Zdocutilsr   r   r   r   Zdocutils.utils.error_reportingr   r   r	   Zdocutils.parsers.rstr
   r   r   r   r   Z$docutils.parsers.rst.directives.bodyr   r   Zdocutils.parsers.rst.rolesr   Zdocutils.transformsr   r   rt   r   r   r   r   r   r   r   r   rf   rf   rf   rg   <module>   s0    Z ,&=.