
    -Ph7                     h   d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZm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 d
dlmZ d
dlmZ d
dlmZ  ed          Z G d de          Z	  G d de	j         e	j!                  Z" G d dej#                  Z$d Z%d Z&d Z'd Z(d Z)d Z*d Z+dS )zCustom Sphinx directives.    N)
namedtuple)PathPurePosixPath)nodesstatemachine)	Directive
directives)images)ExtensionError)	getLogger   )THUMBNAIL_PARENT_DIVTHUMBNAIL_PARENT_DIV_CLOSE_thumbnail_div)extract_intro_and_title)split_code_and_text_blocks)
_read_jsonzsphinx-galleryc                   L    e Zd ZdZdZdZdZdZej	        ej
        dZd Zd ZdS )	MiniGallerya  Custom directive to insert a mini-gallery.

    The required argument is one or more of the following:

    * fully qualified names of objects
    * pathlike strings to example Python files
    * glob-style pathlike strings to example Python files

    The string list of arguments is separated by spaces.

    The mini-gallery will be the subset of gallery
    examples that make use of that object from that specific namespace

    Options:

    * `add-heading` adds a heading to the mini-gallery.  If an argument is
      provided, it uses that text for the heading.  Otherwise, it uses
      default text.
    * `heading-level` specifies the heading level of the heading as a single
      character.  If omitted, the default heading level is `'^'`.
    r   Tr   )add-headingheading-levelc                    |j         d         }t          |t                    s|g}|j         d         }t          |t                    s|g}g }t          ||          D ]\  }}	t	          ||                              d          }t	          ||	                              d          }		 |                    |                    |          |	f           y# t          $ r Y w xY wt          |          x}
dk    r"|d         d         j
        |d         d         }}n>|
dk    r t          d |D             d 	          \  }}nt          d
| d| d|           d}|d         x}t	          d          k    r|}||z  }|S )z?Get thumbnail target directory, errors when not in example dir.examples_dirsgallery_dirsT)strictr   r   c                 8    g | ]}|d          j         |d         fS )r   r   )parents).0matchs     Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sphinx_gallery/directives.py
<listcomp>z/MiniGallery._get_target_dir.<locals>.<listcomp>V   s(    GGG%%("E!H-GGG    c                 ,    t          | d                   S Nr   )len)xs    r    <lambda>z-MiniGallery._get_target_dir.<locals>.<lambda>W   s    c!A$ii r"   keyz)minigallery directive error: path input 'z' found file: 'z3' but this does not live inside any examples_dirs:  .)sphinx_gallery_conf
isinstancelistzipr   resolveappendrelative_to
ValueErrorr%   r   minr   )selfconfigsrc_dirpathobjr   r   gal_matchesegn_match
ex_parents
target_dirsubdirex_ps                  r    _get_target_dirzMiniGallery._get_target_dir:   s   2?C-.. 	,*OM1.A,-- 	*(>L|44 	 	DAqWa  (((55AWa  (((55A""D$4$4Q$7$7#;<<<<    ;'''GA--Aq!)Aq! #JJ q[[%(GG;GGG''& & &"J

 !#C # ## # # #   qM!Dd3ii//F&(
s   +*C
C#"C#c           	         ddl m} | j        s| j        st	          d          | j        j        j        j        r| j        j        j        j	        s| 
                    d| j         d          | j        j        j        j        j        }|j        d         }|t          
                    d           |j        d	         }g }| j        r<|                    d
 | j        d                                         D                        | j        r$|                    d | j        D                        g }d| j        v r| j        d         }|dk    r"t'          |          dk    rd|d          d}nd}|                    |           | j                            dd          }|                    |t'          |          z             t-          dg d          }	d|rt/          t1          ||d                    fd}
i }|D ]} |
|          x}rV|D ]R} |	|d         |d         |d         d          |t1          |d         |d                                                   <   Set1          |                              |          x}r/|D ],}|                                }||v r |	ddd|          ||<   -t'          |          dk    rg S |                    t6                     |j        d         dn ||j        d          \  t9          |                                rfdnd          D ]\  }}|j        (t?          |j         ||j        |j        |j!                  }n| "                    ||||j#                  }tI          tK          |          d          \  }}tM          tK          |          |d         j                  \  }}t?          |||j        ||          }|                    |           |                    tN                     d (                    |          }tS          j*        |d!"          }| j+        ,                    |tK          |                     g S )#z;Generate mini-gallery from backreference and example files.r   )_get_callablesz$No arguments passed to 'minigallery'"z" directive disabled.backreferences_dirNzj'backreferences_dir' config is None, minigallery directive will resolve all inputs as file paths or globs.r7   c                 6    g | ]}|                                 S  stripr   cs     r    r!   z#MiniGallery.run.<locals>.<listcomp>   s     JJJ1QWWYYJJJr"   r   c                 6    g | ]}|                                 S rH   rI   rK   s     r    r!   z#MiniGallery.run.<locals>.<listcomp>   s     ===1QWWYY===r"   r   r*   zExamples using ``z``z&Examples using one of multiple objectsr   ^ExampleInfo)r?   introtitleargzbackreferences_all.jsonc                 D    dS                      | d           }|r|S dS )NF)get)rR   examplesbackreferences_alls     r    has_backrefsz%MiniGallery.run.<locals>.has_backrefs   s5    !)u)--c488H  5r"            minigallery_sort_orderc                 @     t          | d                             S r$   )str)r&   sortkeys    r    r'   z!MiniGallery.run.<locals>.<lambda>   s    GGC!II.. r"   r(   F)return_node
T)convert_whitespace)-gen_rstrD   	argumentscontentr   statedocumentsettingsraw_enabledfile_insertion_enabledwarningnameenvr6   r,   loggerextendsplitoptionsr%   r1   rT   r   r   r   r0   globr   sorteditemsrP   r   r?   rQ   rB   rR   r   r]   r   r   joinr   string2linesstate_machineinsert_input)r5   rD   r6   rF   r7   arg_listlinesheadingheading_levelrO   rW   
file_pathsrR   rU   r8   pathspath_resolved	path_info	thumbnailr?   _script_blocksrP   rQ   textinclude_linesrV   r^   s                             @@r    runzMiniGallery.runh   s!   ++++++ 	I$, 	I !GHHH 
#,8	E:&/F	E ,,C49CCCDDD $-18#78LM%NNL   ,Y7 > 	LOOJJq0A0G0G0I0IJJJKKK< 	?OO=====>>> DL((l=1G"}}x==A%%A(1+AAAGGFGLL!!! L,,_cBBMLLW5666 0W0W0WXX! 	!+W02KLL" "	 	 	 	 	 
 	W 	WC'<,,,x W$  DCN;#'7$q'adD D DJtDGT!W55==??@@
 w--,,S111 W " W WD$(LLNNM$
22 4?KdDRU4V4V
=11z??aI)*** %&>?GGG'*,D JW  &3:D..... 
  
  
 	$ 	$OD)
 **(IOO 		 "11&'4WW
 $>II5$ $ $ =  7II}Q/7   u +:w	5RWXX	LL####/000yy$1$4PPP''s4yyAAA	r"   N)__name__
__module____qualname____doc__required_argumentshas_contentoptional_argumentsfinal_argument_whitespacer	   	unchangedsingle_char_or_unicodeoption_specrB   r   rH   r"   r    r   r      ss         , K $!+#: K
, , ,\@ @ @ @ @r"   r   c                       e Zd ZdZdS )	imgsgnodez Sphinx Gallery image node class.N)r   r   r   r   rH   r"   r    r   r      s        **Dr"   r   c                   R    e Zd ZdZdZdZdZdZej	        ej
        ej	        dZd ZdS )ImageSga  Implements a directive to allow an optional hidpi image.

    Meant to be used with the `image_srcset` configuration option.

    e.g.::

        .. image-sg:: /plot_types/basic/images/sphx_glr_bar_001.png
            :alt: bar
            :srcset: /plot_types/basic/images/sphx_glr_bar_001.png,
                     /plot_types/basic/images/sphx_glr_bar_001_2_00x.png 2.00x
            :class: sphx-glr-single-img

    The resulting html is::

        <img src="sphx_glr_bar_001_hidpi.png"
            srcset="_images/sphx_glr_bar_001.png,
                    _images/sphx_glr_bar_001_2_00x.png 2x",
            alt="bar"
            class="sphx-glr-single-img" />
    Fr   rY   )srcsetclassaltc                     t                      }| j        d         }| j                            dd          |d<   | j                            dd          |d<   ||d<   | j                            dd          |d<   |gS )zUpdate node contents.r   r   r*   r   Nurir   )r   rc   rp   rT   )r5   
image_nodeimagenms      r    r   zImageSg.run  s    [[
.# L,,UB77
5"l..w==
7 $
5#|//$??
8|r"   N)r   r   r   r   r   r   r   r   r	   r   class_optionr   r   rH   r"   r    r   r      sd         * K %&(# K    r"   r   c                 ^   |                      d          }i }|D ]}|                                                     d          }t          |          dk    r|d         |d<   Ht          |          dk    r)|d         dd         }|d         |t          |          <   t	          d          |S )	z	Parse st., r   r   rX   Nz%srcset argument "{entry}" is invalid.)ro   rJ   r%   floatr   )stentriesr   entrysplmults         r    _parse_srcsetr   (  s    hhsmmGF J Jkkmm!!#&&s88q==AF1IIXX]]q6#2#;D"%a&F5;; !HIIIMr"   c                    |d         |                      |           dS t          | |          \  }}| j        d         }t          j                            | j        j        d          }t          j                            ||          }t          j                            | j        j	        d          }t          j                            ||          }t          j                            |t          j        
                    |                    }	| j        j        dk    r"t          j                            d|	d          }	n t          j                            |	d          }	d|	v r|	                    dd          }	d}
|D ]P}t          j                            ||         d	d                   }|	|z   }|
| z  }
|d
k    r|
dz  }
F|
d|ddz  }
Q|
dd         }
t          j                            |d         d	d                   }|	|z   }|d         }|d         |d         d
         }d| d}nd}d| d|
 d| dd| dz   }| j                            |           dS )zHandle HTML image tag depending on 'srcset' configuration.

    If 'srcset' is not `None`, copy images, generate image html tag with 'srcset'
    and add to HTML `body`. If 'srcset' is `None` run `visit_image` on `node`.
    r   Nsourcer*   dirhtmlz..\/r   r   z, r   z1.2fzx, r   r   r   z	class = "rE   z
<img src="z
" srcset="z" alt="z/>)visit_image_copy_imagesrf   osr8   rt   buildersrcdirrelpathoutdirdirnamerk   replacebasenamebodyr1   )r5   nodeimagedirr   	docsourcesrctop	relsourcedesttopdestimagerelsrcsetstr   nmr   r   r   classst
html_blocks                     r    visit_imgsg_htmlr   8  sa    H~#D$//Hf h'I W\\$,-r22F	622Igll4<.33G7<<++D wx)>)>??H|I%%7<<h337<<"--x##D#..H + +WfTl122.//R-wL 199HH*D*****HH}H 
		$u+abb/	*	*B
R-C
u+CG} w-"(g(((DcDDXDDcDDD7VJIZ     r"   c                    |d         ft          | |          \  }}d}|                                D ]}t          ||          }t          t	          ||                   j                  |d<   |                     |           dS )zNCopy images, set node[uri] to highest resolution image and call `visit_image`.r   Nr   r   )r   keysmaxr]   r   rk   r   )r5   r   r   r   maxmultr)   s         r    visit_imgsg_latexr   v  s    H~!'d33&;;== 	( 	(C'3''GG-w88=>>UTr"   c                    t          |d                   }| j        j        }t          j                            | j        j        d          }t          | j        j        |          }t          j	        |d           |D ]=}t          |||         dd                    }t          j        |||j        z             >||fS )Nr   r*   T)exist_okr   )r   r   r   r   r8   rt   r   r   r   makedirsshutilcopyfilerk   )r5   r   r   r   r   r   abspaths          r    r   r     s    4>**F \ F
 w||DL1266HT\0(;;HK4((((  : :tQRR(899GL!89999Vr"   c                     dS )z"HTML depart node visitor function.NrH   r5   r   s     r    depart_imgsg_htmlr     s    Dr"   c                 0    |                      |           dS )z#LaTeX depart node visitor function.N)depart_imager   s     r    depart_imgsg_latexr     s    dr"   c                 p    |                      t          t          t          ft          t
          f           dS )zHAdd `imgsgnode` to Sphinx app with visitor functions for HTML and LaTeX.)htmllatexN)add_noder   r   r   r   r   )apps    r    imagesg_addnoder     s<    LL 12 "45      r"   ),r   r   r   collectionsr   pathlibr   r   docutilsr   r   docutils.parsers.rstr   r	   docutils.parsers.rst.directivesr
   sphinx.errorsr   sphinx.util.loggingr   backreferencesr   r   r   rb   r   py_source_parserr   utilsr   rm   r   GeneralElementr   Imager   r   r   r   r   r   r   r   rH   r"   r    <module>r      s0     				  " " " " " " ' ' ' ' ' ' ' ' ( ( ( ( ( ( ( ( 6 6 6 6 6 6 6 6 2 2 2 2 2 2 ( ( ( ( ( ( ) ) ) ) ) )         
 - , , , , , 8 8 8 8 8 8      	#	$	$N N N N N) N N Nb
	 	 	 	 	u} 	 	 	/ / / / /fl / / /d   ;! ;! ;!|
 
 
  ,	 	 	
  
    r"   