
    e                         d Z ddlZddlmZ d ZddZddZd	 ZddZ G d de          Z	 G d de          Z
 G d de          Z G d de          ZdS )zX
Prototype demo:

python holoviews/ipython/convert.py Conversion_Example.ipynb | python
    N)Preprocessorc                    g }|                                  D ]W}|                                                    d          r|                    d|z              B|                    |           Xd                    |          S )zY
    Utility used to make sure AST parser does not choke on unrecognized
    magics.
    %z# 

splitlinesstrip
startswithappendjoinsourcefilteredlines      ?lib/python3.11/site-packages/holoviews/ipython/preprocessors.pycomment_out_magicsr      s    
 H!!## " "::<<""3'' 	"OODDL))))OOD!!!!99X    {expr}c                 
   t           j        t           j        t           j        t           j        t           j        t           j        f}	 t          j        t          |                     }n# t          $ r | cY S w xY w| 
                                }|j        g k    r|j        d         }t          |t           j                  snt          |j        |          r||j        dz
           d|j                 }||j        dz
           |j        d         }d                    |d|j        dz
           |r|gng z             }d                    |r|gng ||j        d         z             }	|dz   |                    |	          z   S | S )z
    If a cell ends in an expression that could be displaying a HoloViews
    object (as determined using the AST), wrap it with a given prefix
    and suffix string.

    If the cell doesn't end in an expression, return the source unchanged.
       Nr   )expr)astIfExpBoolOpBinOpCallName	Attributeparser   SyntaxErrorr   body
isinstanceExprvaluelineno
col_offsetr   format)
r   templatecell_output_typesnoder   	last_exprexpr_end_sliceexpr_start_slicestartendings
             r   wrap_cell_expressionr1      s    CJ	383=2y+F3344     ""HyBIbM	)SX.. 	?	):;; 		?%i&6q&89:O9;O:OPN'	(8(:;I<P<Q<QRIIh':	(8(:':;5C!K.!1!1M N NEYY6F N!1 2 2B&y'7'8'89 : ; ;F 4<(//v/">">>>Ms   !A( (A76A7Tc                 ,   g g }}|                                  D ]T}|                                                              r|                    |           ?|                    |           U|rfd|D             }d                    |          |fS )z
    Given the source of a cell, filter out the given magic and collect
    the lines using the magic into a list.

    If strip is True, the IPython syntax part of the magic (e.g. %magic
    or %%magic) is stripped from the returned lines.
    c                 <    g | ]}|                     d           S ) )replace).0elmagics     r   
<listcomp>z filter_magic.<locals>.<listcomp>H   s'    BBBrzz%++BBBr   r   r   )r   r8   r	   r   magic_linesr   s    `    r   filter_magicr;   9   s     RkH!!## " "::<<""5)) 	"t$$$$OOD!!!! CBBBBkBBB99X++r   c                     g }|                                  D ],}|                    d          s|                    |           -d                    |          S )zJ
    Given the source of a cell, filter out all cell and line magics.
    r   r   )r   r
   r   r   r   s      r   strip_magicsr=   L   s]     H!!## " "s## 	"OOD!!!99Xr   {line}c                 V   g }|                                  D ]~}|                                                    |          r@|                    |                    |d                    }|                    |           i|                    |           d                    |          S )z
    Given a cell's source, replace line magics using a formatting
    template, where {line} is the string that follows the magic.
    r4   )r   r   )r   r	   r
   r(   r5   r   r   )r   r8   r)   r   r   substitutions         r   replace_line_magicrA   W   s    
 H!!## " "::<<""5)) 	"#??UB0G0G?HHLOOL))))OOD!!!!99Xr   c                       e Zd ZdZd Zd ZdS )OptsMagicProcessorz
    Preprocessor to convert notebooks to Python source to convert use of
    opts magic to use the util.opts utility instead.
    c                 H   |d         dk    rt          |d         dd          }t          |d          \  }}|rad                    d	                    |                              d
d                              dd                    }t          ||          }||d<   ||fS )N	cell_typecoder   z%optszhv.util.opts({line!r})r)   z%%optsz#hv.util.opts({options!r}, {{expr}}) {z{{}z}})options)rA   r;   r(   r   r5   r1   )selfcell	resourcesindexr   
opts_linesr)   s          r   preprocess_cellz"OptsMagicProcessor.preprocess_cellm   s    &&'X1IK K KF!-fh!?!?FJ @@GGHHZ0088TBBJJ3tTT H V V-fh??#DNYr   c                 .    |                      ||          S N
preprocessrL   nbrN   s      r   __call__zOptsMagicProcessor.__call__z       doob.K.K'Kr   N__name__
__module____qualname____doc__rQ   rX    r   r   rC   rC   g   s5         
   LKKKKr   rC   c                       e Zd ZdZd Zd ZdS )OutputMagicProcessorz
    Preprocessor to convert notebooks to Python source to convert use of
    output magic to use the util.output utility instead.
    c                     |d         dk    rNt          |d         dd          }t          |d          \  }}|rd|d	         d
}t          ||          }||d<   ||fS )NrE   rF   r   z%outputzhv.util.output({line!r})rG   z%%outputzhv.util.output(r   z	, {expr}))rA   r;   r1   )rL   rM   rN   rO   r   output_linesr)   s          r   rQ   z$OutputMagicProcessor.preprocess_cell   s    &&'X	1KM M MF#/
#C#C FL @L\"-=LLL-fh??#DNYr   c                 .    |                      ||          S rS   rT   rV   s      r   rX   zOutputMagicProcessor.__call__   rY   r   NrZ   r_   r   r   ra   ra   }   s5         

 
 
 LKKKKr   ra   c                       e Zd ZdZd Zd ZdS )StripMagicsProcessorz
    Preprocessor to convert notebooks to Python source to strips out all
    magics. To be applied after the preprocessors that can handle
    holoviews magics appropriately.
    c                 R    |d         dk    rt          |d                   |d<   ||fS )NrE   rF   r   )r=   )rL   rM   rN   rO   s       r   rQ   z$StripMagicsProcessor.preprocess_cell   s1    &&)$x.99DNYr   c                 .    |                      ||          S rS   rT   rV   s      r   rX   zStripMagicsProcessor.__call__   rY   r   NrZ   r_   r   r   rf   rf      s5           
 LKKKKr   rf   c                   8     e Zd ZdZdZ fdZd Zd Zd Z xZ	S )
SubstituteaT  
    An nbconvert preprocessor that substitutes one set of HTML data
    output for another, adding annotation to the output as required.

    The constructor accepts the notebook format version and a
    substitutions dictionary:

    {source_html:(target_html, annotation)}

    Where the annotation may be None (i.e. no annotation).
    z<center><b>%s</b></center>c                 d    || _         || _         t          t          |           j        di | d S )Nr_   )	nbversionsubstitutionssuperr   __init__)rL   versionrm   kw	__class__s       r   ro   zSubstitute.__init__   s;     **lD!!*00R00000r   c                 .    |                      ||          S rS   rT   rV   s      r   rX   zSubstitute.__call__   s    r),,,r   c                     | j                                         D ];}||k    r3| j        | j         |         d         z  }|| j         |         d         z   c S <|S )z=Given some source html substitute and annotated as applicabler   r   )rm   keys
annotation)rL   srchtmlrv   s       r   r5   zSubstitute.replace   sl    &++-- 	? 	?Dd{{!_t/A#/Fq/II
!D$6s$;A$>>>>>  
r   c                 `   | j         d         dk    }|d         dk    r|d         D ]}|rdnd}|d         |k    rq|s"d	|v r|                     |d	                   |d	<   |                    d
g           D ]6}|r2|dk    r,|                     |d
         d                   }||d
         d<   7||fS )Nr      rE   rF   outputsexecute_resultpyoutoutput_typerx   dataz	text/html)rl   r5   get)	rL   rM   rN   rO   v4r{   
output_keyr   r@   s	            r   rQ   zSubstitute.preprocess_cell   s    nQ1$&&	? 
H 
H24A..'
=)Z77 H&G"3"3*.,,wv*G*G 'F2 6 6 H H H$+"5"5+/<<8T+U+UL;GGFOK8Yr   )
r[   r\   r]   r^   rv   ro   rX   r5   rQ   __classcell__)rr   s   @r   rj   rj      sv        
 
 .J1 1 1 1 1
- - -        r   rj   )r   )T)r>   )r^   r   nbconvert.preprocessorsr   r   r1   r;   r=   rA   rC   ra   rf   rj   r_   r   r   <module>r      sQ   
 


 0 0 0 0 0 0     @, , , ,&      L L L L L L L L,L L L L L< L L L*L L L L L< L L L. . . . . . . . . .r   