o
    M=&b                     @   s   d Z dZddlmZ zddlZW n% ey5   zddlZG dd dZee_W n ey2   dZY nw Y nw ddl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 G dd deZG dd deZdS )z+
Directives for figures and simple images.
ZreStructuredText    )url2pathnameNc                   @   s   e Zd ZdS )PILN)__name__
__module____qualname__ r   r   Jusr/lib/python3.10/site-packages/docutils/parsers/rst/directives/images.pyr      s    r   )nodes)fully_normalize_namewhitespace_normalize_name)	Directive)
directivesstates)set_classesc                	   @   s\   e Zd ZdZdZee Zdd ZdZdZdZ	e
je
je
je
jee
je
je
jdZd	d
 ZdS )Image)leftcenterright)topmiddleZbottomc                 C      t | tjS N)r   choicer   align_valuesZargumentr   r   r   align$   s   zImage.align   r   T)ZaltheightwidthZscaler   targetclassnamec                 C   s  d| j v rAt| jtjr'| j d | jvr&| d| j| j d d| jf n| j d | j	vrA| d| j| j d d| j	f g }t
| jd }|| j d< d }d| j v rt| j d  }dd	 |D }| j|| j| j\}}|d
krtj|d}n|dkrtjt|t|d}||_| jj| n|| | j d= t| j  tj| jfi | j }| | |r||7 }||g S ||g S )Nr   zError in "%s" directive: "%s" is not a valid value for the "align" option within a substitution definition.  Valid values for "align" are: "%s".z", "zoError in "%s" directive: "%s" is not a valid value for the "align" option.  Valid values for "align" are: "%s".r   urir   c                 S   s   g | ]}|qS r   r   ).0liner   r   r   
<listcomp>N   s    zImage.run.<locals>.<listcomp>refuri)r&   refname)r'   r!   )options
isinstancestater   ZSubstitutionDefalign_v_valueserrorr!   joinalign_h_valuesr   r"   	argumentsZescape2null
splitlinesZparse_target
block_textlinenor	   	referencer
   r   Zindirect_reference_namedocumentZnote_refnameappendr   imageZadd_name)selfmessagesr3   Zreference_nodeblockZtarget_typedata
image_noder   r   r   run6   sf   










z	Image.runN)r   r   r   r.   r+   r   r   Zrequired_argumentsZoptional_argumentsZfinal_argument_whitespacer   Z	unchangedZlength_or_unitless length_or_percentage_or_unitlessZ
percentageZunchanged_requiredclass_optionoption_specr<   r   r   r   r   r      s$    	r   c                   @   sL   e Zd Zdd Zdd Zej Zeed< ej	ed< eed< dZ
d	d
 ZdS )Figurec                 C   r   r   )r   r   r@   r.   r   r   r   r   r   h   s   zFigure.alignc                 C   s   |   dkrdS t| dS )Nr6   Zpx)lowerr   r=   r   r   r   r   figwidth_valuek   s   zFigure.figwidth_valuefigwidthfigclassr   Tc              	   C   s  | j dd }| j dd }| j dd }t| \}t|tjr$|gS td|}|dkrwtrv| j	j
jjrvt|d }z!tj|}d|jd  |d	< W d    n1 sWw   Y  W n ttfyh   Y nw | j	j
jj|d
d n|d ur||d	< |r|d  |7  < |r||d< | jrt }| j	| j| j| |d }	t|	tjrtj|	jdg|	jR  }
|	j|
_|	j|
_||
7 }n t|	tjrt |	dks| j!j"dt#| j$| j$| j%d}||gS t |dkr|tj&dg|dd  R  7 }|gS )NrC   rD   r    r6   r"   z%dpxr   r   \/classesz4Figure caption must be a paragraph or empty comment.)r$   r   )'r(   popr   r<   r)   r	   Zsystem_messageZfigurer   r*   r4   settingsZfile_insertion_enabledr   opensizeOSErrorUnicodeEncodeErrorZrecord_dependenciesaddreplacecontentZElementZnested_parseZcontent_offsetZ	paragraphcaptionZ	rawsourcechildrensourcer$   commentlenZreporterr,   Zliteral_blockr1   r2   Zlegend)r7   rC   Z
figclassesr   r;   Zfigure_nodeZ	imagepathZimgnodeZ
first_noderR   r,   r   r   r   r<   w   sd   

z
Figure.runN)r   r   r   r   rB   r   r?   copyr   r>   Zhas_contentr<   r   r   r   r   r@   f   s    

r@   )__doc__Z__docformat__Zurllib.requestr   Z	PIL.Imager   ImportErrorr   docutilsr	   Zdocutils.nodesr
   r   Zdocutils.parsers.rstr   r   r   Zdocutils.parsers.rst.rolesr   r@   r   r   r   r   <module>   s,   
H