
    -Ph;                         d Z ddlZddlZddlZddlm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  ed	          Ze	j        j        j        e	j        j        e	j        j        j        fZ G d
 d          ZdS )zOBlockParser divides non `.py` source files into blocks of code and markup text.    N)Path)dedent)ExtensionError)	getLogger   )	FLAG_BODYBlockzsphinx-galleryc                   D    e Zd ZdZd ZddZd Zd Zd Zd Z	d	 Z
d
 ZdS )BlockParsera  
    A parser that breaks a source file into blocks of code and markup text.

    Determines the source language and identifies comment blocks using pygments.

    Parameters
    ----------
    source_file : str
        A file name that has a suffix compatible with files that are subsequently parsed
    gallery_conf : dict
        Contains the configuration of Sphinx-Gallery.
    c                 |   t          |          }|d                             |j                  x}r,t          j                            |                      | _        n+t          j                            |                      | _        | j        j        | _	        g d}g | _
        g }t          j        t          d                    | _        |D ]\  }}}}	t          | j                            |                    d         t"          v rO| j
                            |           |rt          j        d|           | _        |	r|                    |	           | j	        dk    r|                    d           d| j
        v rt          j        d          | _        nt          j        d	          | _        d
                    | j
                  }
d
                    |          }|r!t          j        d|
 d| d          | _        nt          j        d|
 d          | _        t          j        d|
 d          | _        d|
 d}t          j        |t.          z   t          j                  | _        |dz   | _        |dz   | _        t          j        | j         d| j         dt          j                  | _        d S )Nfiletype_parsers))z#= comment =#z#=z=#N)z	# comment#Nz#{20,})z
// commentz//Nz/{20,})z/* comment *//\*z\*/z	/\*{20,}/)z	% comment%Nz%{20,})z	! comment!Nz!{20,})zc     commentz^c(?:$|     )NNr   z(.*?)\s*Matlabz
%%(?:$|\s)r   z
\s*/?\*\s*z\s*|z(?:(?:z) ?%% ?|z)(.*)z(?:z) ?%% ?(.*)z) ?(.*)z^[\ \t]*(?:z)\s*sphinx_gallery_start_ignoresphinx_gallery_end_ignorez(?:[\s\S]*?)z\n?)r   getsuffixpygmentslexersfind_lexer_class_by_namelexerfind_lexer_class_for_filenamenamelanguageallowed_commentsrecompilechrmultiline_endnext
get_tokensCOMMENT_TYPESappendmultiline_cleanupjoinstart_specialcontinue_textr   	MULTILINEinfile_config_patternstart_ignore_flagend_ignore_flagignore_block_pattern)selfsource_filegallery_confr   comment_testsallowed_specialteststartendspecialcomment_start
flag_starts               [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sphinx_gallery/block_parser.py__init__zBlockParser.__init__)   s   ;'' 23778JKKK4 	V!AA$GGIIDJJ!FF{SSUUDJ

 
 
 !#ZA//)6 	4 	4%D%gDJ))$//003}DD%,,U333 G)+4E4E4E)F)FD& 4#**7333=H$$ ""=111T*** &(Z%>%>D""%'Z%7%7D"!677((?33 	N!#FFFFFF" "D "$,L-,L,L,L!M!MDZ(Dm(D(D(DEE 8M777
%'Z
Y0F%U%U"!+.K!K),GG$&J&MMD4HMMML%
 %
!!!    Fc                     t          j        |dd          5 }|                                }ddd           n# 1 swxY w Y   |                    dd          }|                     |          S )a  Return list with source file separated into code and text blocks.

        Parameters
        ----------
        source_file : str
            Path to the source file.
        return_node : bool
            Ignored; returning an ast node is not supported

        Returns
        -------
        file_conf : dict
            File-specific settings given in source file comments as:
            ``# sphinx_gallery_<name> = <value>``
        blocks : list
            (label, content, line_number)
            List where each element is a tuple with the label ('text' or 'code'),
            the corresponding content string of block and the leading line number.
        node : None
            Returning an ast node is not supported.
        rzutf-8Nz

)codecsopenreadreplace_split_content)r1   r2   return_nodefidcontents        r<   split_code_and_text_blocksz&BlockParser.split_code_and_text_blocksv   s    , [c733 	!shhjjG	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! //&$//""7+++s   8<<c              #   ^  K   g }t           j        j        }| j                            |          D ]\  }}|t           j        j        k    r|}d|v r|                    d          }|                    |                    d                     |d                    |          fV  |                                g}|D ]-}|	                                st           j        j        }||fV  .|d         	                                st           j        j        }|                    |           dS )z
        Combine individual tokens into lines.

        Use the first non-whitespace token (if any) as the characteristic token type for
        the line.
        rA   r    N)
r   token
Whitespacer   r%   splitr'   popr)   strip)r1   rI   current_line
line_tokenrM   text
text_lineslns           r<   _get_content_lineszBlockParser._get_content_lines   s8      ^.
:0099 	* 	*KE4X^666"
t||!ZZ--
##JNN1$5$5666 "'',"7"77777 * 0 01$ ) )B88:: ?%-^%>
$b.((((#A,,.. ;!)!:J##D))))'	* 	*r>   c              #   J   K    j         d fd fd}g }d}t                               |                    D ]\  \  }}|dk    r(|t          j        j        v r|r |||          V  dg }}7|dk    r|t          v r                    |          x}r|r |||          V  dg }}|                    d          x}ω j         k    r j	                            |          x}	rS|	                    d          
                                x}
r*|                    |
dt          |
          z  df           |                    |           |
                                x}
r(|                    |
d	t          |
          z  df           [|dk    r4|t          v r* j	                            |          x}rS|                    d          
                                x}
r(|                    |
d	t          |
          z  df           |t          j        j        j        k    r_ j                            |          x}r,|                    |                    d                     d
=|                    |           T|                     j                             |                              d                     |dk    r|r |||          V  d|g}}|                    |           |r |||          V  dS dS )z
        Generate a sequence of "blocks" from the lines in ``content``.

        Each block is a tuple of (label, content, line_number), matching the format
        ultimately returned by `split_code_and_text_blocks`.
        Fc                    
	j         k    rdnd}t          d | D                       }d}t          | |d                    D ]h\  }}	j                            |          x}rGd}t          |                    d                    x}t          |          k     rt          ||          }i|r4|r2t          | |d          |          D ]\  }}| |         |d          | |<   d| S )Nr   r   c              3   4   K   | ]}t          |          V  d S )N)len).0lines     r<   	<genexpr>zEBlockParser._get_blocks.<locals>.cleanup_multiline.<locals>.<genexpr>   s(      66#d))666666r>   FT)r7   )r+   max	enumerater(   matchr[   groupmin)lines
first_linelongestmatchedir]   mnneeds_multiline_cleanupr1   
start_texts           r<   cleanup_multilinez2BlockParser._get_blocks.<locals>.cleanup_multiline   s   (D,>>>AJ6666666GG$U:;;%788 2 24.44T:::1 2"G __,D		99"%gq// 27 2(z{{);:NNN 2 2GAt$Qx1E!HH&+#Lr>   c                     | dk    r	r |           
j         d}t          |          D ]\  }}|}|                                r nd }t          t          |                    D ] \  }}| pd }|                                r n!|                    d           t          d                    |||                             }nd                    |          }t          | |t          |          z
            S )NrT   r   rL   rA   )	r*   r`   rQ   reversedr'   r   r)   r	   r[   )modeblockfirstrh   r]   lastrT   rm   rj   rk   r1   rl   s          r<   finalize_blockz/BlockParser._get_blocks.<locals>.finalize_block   s   v~~* -%%e,,, "/
 (//  GAtEzz|| (%99  GAt2:Dzz|| R   diieDj(9::;;yy''tQU^444r>   NrT   r   =rL   -Tcode)r+   r`   rW   r   rM   rN   r&   searchrb   r*   rQ   extendr[   r'   Comment	Multiliner#   )r1   rI   rt   rq   rp   rM   rT   ri   trailing_text	delimitedheadingrm   rj   rk   rl   s   `          @@@@r<   _get_blockszBlockParser._get_blocks   s      '
"'	 	 	 	 	 	 	"	5 	5 	5 	5 	5 	5 	5 	5 	54  )$*A*A'*J*J K K 3	# 3	#A}tv~~%8>+D"D"D 6(.u55555"Be]**$++D111Q +  6(.u55555$be%&WWQZZ/M<!T%777)-);)B)B4)H)HHI 8'0q'9'9'?'?'A'AAG8 "LL'3W3Er)JKKKK "LL7777$1$7$7$9$99 HgsS\\/A2%FGGGE]$:$:*11$7771 K #$''!**"2"2"4"44w HgsS\\/A2%FGGGhn4>>> .55d;;;q +QWWQZZ00026//T****LL!3!:!:4!@!@!F!Fq!I!IJJJJ 6(.u55555$tfe T""""  	. .u-------	. 	.r>   c                    |                      |          }t          |                     |                    }t          |          dk    r|d         j        dk    r|d         j        dk    r|d         |d         c|d<   |d<   t          |          dk    r`|d         j        dk    rOt          d|d         j         d|d         j         |d         j                  |d<   |                    d           ||dfS )	z
        Split the input content into blocks.

        Return a tuple of (file_conf, blocks, None) that corresponds to the return
        values of ``split_code_and_text_blocks``.
           r   rw   r   rT      rA   N)	extract_file_configlistr   r[   typer	   rI   linenorP   )r1   rI   	file_confblockss       r<   rF   zBlockParser._split_content   s     ,,W55	d&&w//00
 v;;!q	& 8 8VAY^v=U=U#)!9fQi F1Ivay6{{aF1INf$<$<!ay(??F1I,=??1I$ q	
 

1&$&&r>   c                 8   i }t          j        | j        |          D ]|}|                    d          }|                    d          }|/	 t	          j        |          }|||<   J# t          t          f$ r t          	                    d||           Y yw xY w|S )z=Pull out the file-specific config specified in the docstring.r   r   Nz4Sphinx-gallery option %s was passed invalid value %s)
r    finditerr-   rb   astliteral_evalSyntaxError
ValueErrorloggerwarning)r1   rI   r   ra   r   values         r<   r   zBlockParser.extract_file_config9  s    	[!;WEE 	( 	(E;;q>>DKKNNE}((// #(	$  ,   JDRW     s   A''-BBc                 
   t          t          j        | j        |                    }t          t          j        | j        |                    }||k    rt          d          t          j        | j        d|          d         S )a  
        Return the content of *code_block* with ignored areas removed.

        An ignore block starts with ``?? sphinx_gallery_start_ignore`` and ends with
        ``?? sphinx_gallery_end_ignore`` where ``??`` is the active language's line
        comment marker. These lines and anything in between them will be removed, but
        surrounding empty lines are preserved.

        Parameters
        ----------
        code_block : str
            A code segment.
        z^All "sphinx_gallery_start_ignore" flags must have a matching "sphinx_gallery_end_ignore" flag!rL   r   )r[   r    findallr.   r/   r   subnr0   )r1   
code_blocknum_start_flagsnum_end_flagss       r<   remove_ignore_blocksz BlockParser.remove_ignore_blocksK  s|     bj)?LLMMBJt';ZHHIIm++ 4   wt0"jAA!DDr>   c                 B    t          j        | j        d|          \  }}|S )ac  
        Return the content of *code_block* with in-file config comments removed.

        Comment lines with the pattern ``sphinx_gallery_[option] = [val]`` after the
        line comment character are removed, but surrounding empty lines are preserved.

        Parameters
        ----------
        code_block : str
            A code segment.
        rL   )r    r   r-   )r1   r   parsed_code_s       r<   remove_config_commentsz"BlockParser.remove_config_commentsc  s$     !;RLLQr>   N)F)__name__
__module____qualname____doc__r=   rJ   rW   r   rF   r   r   r    r>   r<   r   r      s         K
 K
 K
Z, , , ,8* * *<n. n. n.`' ' '2  $E E E0    r>   r   )r   r   rB   r    pathlibr   textwrapr   pygments.lexersr   pygments.tokensphinx.errorsr   sphinx.util.loggingr   py_source_parserr   r	   r   rM   rz   Singler{   r&   r   r   r>   r<   <module>r      s   U U 



  				                     ( ( ( ( ( ( ) ) ) ) ) ) . . . . . . . .	#	$	$
 N!NN$U U U U U U U U U Ur>   