
    -Ph.                     V   d Z ddlZddlZddlZddl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mZ ddlZddlmZ ddlmZmZmZmZmZ ddlmZ  ee          j        d	         Zd
 Zd Zd Zd Zd Z ej!        "                    ddd          d             Z#d Z$d Z%d Z&d Z'd Z(dS )z$Testing the Jupyter notebook parser.    N)defaultdict)count)Path)ExtensionError)jupyter_notebookpromote_jupyter_cell_magicpython_to_jupyter_clirst2mdsave_notebook)split_code_and_text_blocks   c                 v    d}d}|t          || di           k    sJ d}d}|t          || di           k    sJ dS )z2Test Latex parsing from rst into Jupyter Markdown.z:math:`T<0` and :math:`U>0`z$T<0$ and $U>0$ z5
.. math::
   \mathcal{H} &= 0 \\
   \mathcal{G} &= Dz@
\begin{align}\mathcal{H} &= 0 \\
   \mathcal{G} &= D\end{align}Nr
   )gallery_confdouble_inline_rstdouble_inline_jmdalign_eqalign_eq_jmds        b/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sphinx_gallery/tests/test_notebook.pytest_latex_conversionr      sc    6*'8,B O OOOOOH
"L 6(L"bAAAAAAAA    c                      t          j        d          } t          | i di           t          j        d          k    sJ dS )zNTest `rst2md` results in ``` code format so Jupyter syntax highlighting works.a)  
        Regular text
            .. code-block::

               # Bash code

          More regular text
        .. code-block:: cpp

          //cpp code

          //more cpp code
        non-indented code blocks are not valid
        .. code-block:: cpp

        // not a real code block
    r   a  
        Regular text
        ```
        # Bash code
        ```
          More regular text
        ```cpp
        //cpp code

        //more cpp code
        ```
        non-indented code blocks are not valid
        .. code-block:: cpp

        // not a real code block
    N)textwrapdedentr
   )rsts    r   test_code_conversionr   0   s\    
/	 C& #r2r""ho	' '      r   c                 >    d}d}t          || di           |k    sJ dS )z!Test reST conversion to markdown.a  hello

.. contents::
    :local:

This is :math:`some` math :math:`stuff`.

.. note::
    Interpolation is a linear operation that can be performed also on
    Raw and Epochs objects.

.. warning::
    Go away

For more details on interpolation see the page :ref:`channel_interpolation`.
.. _foo: bar

.. image:: foobar
  :alt: me
  :whatever: you
  :width: 200px
  :class: img_class

`See more  <https://en.wikipedia.org/wiki/Interpolation>`_.
a  hello

This is $some$ math $stuff$.

<div class="alert alert-info"><h4>Note</h4><p>Interpolation is a linear operation that can be performed also on
    Raw and Epochs objects.</p></div>

<div class="alert alert-danger"><h4>Warning</h4><p>Go away</p></div>

For more details on interpolation see the page `channel_interpolation`.

<img src="file://foobar" alt="me" whatever="you" width="200px" class="img_class">

[See more](https://en.wikipedia.org/wiki/Interpolation).
r   Nr   )r   r   markdowns      r   test_convertr    Y   s7    C4H #|R,,888888r   c                     t          j        d                              d          } t          d          t	          fd          }t          | i d|          }|                    d          sJ d|v sJ d	|v sJ d
|v sJ d|vsJ d|vsJ d|v sJ d|v sJ d|v sJ d|v sJ d|vsJ d|v sJ d|vsJ d|v sJ d S )Na      =========
    Heading 1
    =========

    Heading 2
    =========

    =============
     Heading 1-2
    =============

    Heading 3
    ---------

    =============
    Not a Heading
    -------------
    Mismatch top and bottom

    Not another heading
    -=-=-=-=-=-=-=-=-=-
    Multiple characters

    -------
     Bad heading but okay
    -------------
    Over and under mismatch, not rendered and warning raised by Sphinx

    Another bad heading, but passable
    ^^^^^^^^^^^^^^^^^^^^^
    Too short, warning raised but is rendered by Sphinx

    A
    *

    BC
    **

    Some text
    And then a heading
    ------------------
    Not valid with no blank line above

    =======================
           Centered
    =======================

    ------------------------

    ------------------------
    Blank heading above.

    {0}
    ====================
      White space above
    ====================

    z               )startc                  "    t                     S )N)next)heading_level_counters   r   <lambda>ztest_headings.<locals>.<lambda>   s    .C)D)D r   r   z# Heading 1
z
## Heading 2
z
# Heading 1-2
z
### Heading 3
z# Not a Headingz# Not another Headingz
#### Bad heading but okay
z)
##### Another bad heading, but passable
z

###### A
z
###### BC
z# And then a heading
z
# Centered
z#
Blank heading above.z# White space above
)r   r   formatr   r   r
   
startswith)r   heading_levelstextr&   s      @r   test_headingsr,      sc   
/:	< <x f^y @ "NNN !D!D!D!DEEN#r2~..D???+++++%%%%$&&&&$&&&&D(((("$....*d22228D@@@@T!!!!d""""#4////t####$D0000"d******r   c                     t          j        d          } dg i}t          ||            |d         }t          |          dk    sJ |d         d         dk    sJ d|d         d         d         v sJ |d	         d         d
k    sJ |d	         d         d         dk    sJ |d         d         d
k    sJ |d         d         dk    sJ |d         d         d         dk    sJ |d         d         d
k    sJ d S )Na      # Should be rendered as text
    ``` bash
    # This should be rendered as normal
    ```
    ``` bash
    %%bash
    # bash magic
    ```
    ```cpp
    %%writefile out.cpp
    // This c++ cell magic will write a file
    // There should NOT be a text block above this
    ```
    Interspersed text block
    ```javascript
    %%javascript
    // Should also be a code block
    // There should NOT be a trailing text block after this
    ```
    cells   r   	cell_typer   z``` bashsourcer"   codez%%bash
# bash magicr      zInterspersed text block   )r   r   r   len)r   work_notebookr.   s      r   test_cell_magic_promotionr7      s7   	 H. bMM}h777'"Eu::????8K J....q(+A.....8K F****8Ha $:::::8K F****8K J....8Ha $=====8K F******r   zrst_path,md_path,prefix_enabled))../_static/image.pngzfile://../_static/image.pngF)/_static/image.pngzfile://_static/image.pngF)r8   %https://example.com/_static/image.pngT)r9   r:   T)https://example.com/image.pngr;   F)r;   r;   T)rel_no_prefixabs_no_prefix
rel_prefix
abs_prefixurl_no_prefix
url_prefix)idsc                 J   |r|                                  } d| d<   t          j                            | d         | d                   }t	          j        d                              |          }t          || |i           }d| d|v sJ d|v sJ d	|v sJ d
|v sJ d|v sJ d S )Nzhttps://example.com/notebook_imagessrc_dirgallery_dirszl    .. image:: {}
       :alt: My Image
       :width: 100px
       :height: 200px
       :class: image
    zsrc=""zalt="My Image"zwidth="100px"zheight="200px"zclass="image")copyospathjoinr   r   r(   r
   )r   rst_pathmd_pathprefix_enabled
target_dirr   r   s          r   test_notebook_images_prefixrP     s    (  A#((***@&'l95|N7STTJ
/	  fX  c<R88H7))))x''''h&&&&x''''h&&&&&&r   c                    |                                  } d| d<   t          j                            | d         | d                   }t          j                            t          j                            t
                    dddd          }t          j                            | d         d          }t          j        |           t          j                            |d          }t          j        ||           t          j        	                    || d                   }d	|
                    t          j        d	          z   }t          j        d
                              |          }t          || |i           }d|v sJ d|v sJ t!          |d          5 }	t#          j        |	                                          }
d d d            n# 1 swxY w Y   |
                    d          |v sJ t          j        d          }t+          j        t.                    5  t          || |i            d d d            d S # 1 swxY w Y   d S )NTrD   rE   rF   	tinybuilddoc_static_nonstandardzdemo.png/z+    .. image:: {}
       :width: 100px
    datazsrc="data:image/png;base64,rbasciizC    .. image:: /this/image/is/missing.png
       :width: 500px
    )rH   rI   rJ   rK   dirname__file__mkdirshutilcopyfilerelpathreplacesepr   r   r(   r
   openbase64	b64encodereaddecodepytestraisesr   )r   rO   
test_imagedest_dir
dest_imagetest_image_reltest_image_absr   r   	test_filerV   s              r   test_notebook_images_data_urirn   .  s   $$&&L&*L"#l95|N7STTJ
!!;7Lj J
 w||L35JKKHHXh
33J
OJ
+++W__Zi1HIIN>11"&#>>>N
/	 
 f^  c<R88HX(H4444	j$		 29	 0 0112 2 2 2 2 2 2 2 2 2 2 2 2 2 2;;w8++++
/	 C 
~	&	& 2 2sL*b1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2s$   'GGGH<<I I c                 t   t          t          dz  dz            \  }}d}t          || |          }t          j        dd          5 }t          ||j                   ddd           n# 1 swxY w Y   	 t          |j                  5 }t          j	        |          |k    sJ 	 ddd           n# 1 swxY w Y   t          j        |j                   n# t          j        |j                   w xY wd}|| d<   t          || |          }|                    d	          d
         d         d
         |k    sJ d}|| d<   t          || |          }|                    d	          d
         d         d
         }t          j        d|          sJ d}|| d<   t          || |          }|                    d	          d         d         d
         |k    sJ d}|| d<   t          || |          }|                    d	          d         d         d
         }t          j        d|          sJ d| d<   t          || |          }|                    d	          d         }	|	d         dk    sJ |	d         d
         dk    sJ |                    d	          d         }
|
d         dk    sJ d|
d         d
         v sJ dS )zETest that written ipython notebook file corresponds to python object.	tutorialszplot_parse.pywF)deleteNz# testing
%matplotlib notebookfirst_notebook_cellr.   r   r1   z ^[
]?# Alternating text and codez# testing last celllast_notebook_cellz*^Last text block.

That[\\]?'s all folks !Tpromote_jupyter_magicr0   r2   z%%bash
# This could be run!r   zCode blocks containing)r   rootr   tempfileNamedTemporaryFiler   namera   jsonloadrI   removegetrematch)r   	file_confblocksrO   
example_nbffname	test_textcell_src
bash_blockmd_above_bash_blocks              r   test_jupyter_notebookr   W  sm   24+3E3WXXIvJ!&,
CCJ		$S	7	7	7 *1j!&)))* * * * * * * * * * * * * * *!&\\ 	2U9U##z11111	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 		!&	!& 2I*3L&'!&,
CCJ>>'""1%h/2i???? I*3L&'!&,
CCJ~~g&&q)(3A6H87BBBBB &I)2L%&!&,
CCJ>>'""2&x03y@@@@ I)2L%&!&,
CCJ~~g&&r*84Q7H8DhOOOOO -1L()!&,
CCJ((,Jk"f,,,,h"&DDDDD %..11"5{+z9999#':8'DQ'GGGGGGGsB   A))A-0A-5C 	B1%C 1B55C 8B59C C1c                      t          j        t                    5  t          g            ddd           dS # 1 swxY w Y   dS )z3User passes no args, should fail with `SystemExit`.N)rf   rg   
SystemExitr	    r   r   test_with_empty_argsr     s    	z	"	" " "b!!!" " " " " " " " " " " " " " " " " "s   7;;c                      t          j        t                    5 } t          dg           ddd           n# 1 swxY w Y   |                     d           dS )zDUser passes non existing file, should fail with `FileNotFoundError`.z	nofile.pyNz%No such file or directory.+nofile\.py)rf   rg   FileNotFoundErrorr	   r   )excinfos    r   test_missing_filer     s    	(	)	) -W{m,,,- - - - - - - - - - - - - - -MM:;;;;;s   7;;c                     t          | dz            }t          j        t          dz  dz  |           t	          |g           t
          j                            |dd          d          sJ dS )z>Check notebook file created when user passes good python file.zplot_0_sin.pyexamplesNrx   z.ipynb)strr\   r]   ry   r	   rI   rJ   isfile)tmp_pathouts     r   test_file_is_generatedr     sq    
h(
)
)C
OD:%7===3%   7>>S"X---.......r   ))__doc__rb   r}   rI   r   r\   rz   r   collectionsr   	itertoolsr   pathlibr   rf   sphinx.errorsr   sphinx_gallery.notebookr   r   r	   r
   r   sphinx_gallery.py_source_parserr   rZ   parentsry   r   r   r    r,   r7   markparametrizerP   rn   r   r   r   r   r   r   r   <module>r      s   , +   				 				    # # # # # #              ( ( ( ( ( (              G F F F F FtH~~a B B B"& & &R+9 +9 +9\R+ R+ R+j$+ $+ $+N %	   &' '' &'0&2 &2 &2R2H 2H 2Hr" " "< < </ / / / /r   