
    -e%                    6   d Z ddlmZ ddlZ ej        e          ZddlmZ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mZ ddlmZ ddlmZ ddlmZmZmZm Z  ddl!m"Z" dZ# G d dej$        ej%                  Z& G d de          Z'd Z(dS )aJ   Make javascript code blocks also include a live link to codepen.io for instant experiementation.

This directive takes a title to use for the codepen example:

.. code-block:: rest

    .. bokehjs-content::
        :title: Some Code

        alert('this is called in the codepen');

This directive is identical to the standard ``code-block`` directive
that Sphinx supplies, with the addition of one new option:

title : string
    A title for the codepen.
js_file : string
    location of javascript source file
include_html: string
    if present, this code block will be emitted as a complete HTML template with
    js inside a script block
disable_codepen: string
    if present, this code block will not have a 'try on codepen' button.  Currently
    necessary when 'include_html' is turned on.

Examples
--------

The inline example code above produces the following output:

.. bokehjs-content::
    :title: Some Code

    alert('this is called in the codepen');

To enable this extension, add `"bokeh.sphinxext.bokehjs_content"` to the
extensions list in your Sphinx configuration module.

    )annotationsN)basenamejoin)nodes)	unchanged)	CodeBlockcontainer_wrapperdedent_lines)SphinxError)__)loggingparselinenos)set_source_info   )PARALLEL_SAFE)BJS_CODEPEN_INITBJS_EPILOGUEBJS_HTMLBJS_PROLOGUE)get_sphinx_resources)bokehjs_contentBokehJSContentsetupc                  V    e Zd Zed             Zed             Zej        ej        fZdS )r   c                   |d         rHt          d          }| j                            t          j        |j        |j                             | j                            t          j        |d         |d                              d S )Ninclude_bjs_headerTinclude_bokehjs_api)	css_filesjs_files	target_idtitle)idr"   )r   bodyappendr   renderr   r    r   )visitornode	resourcess      ?lib/python3.11/site-packages/bokeh/sphinxext/bokehjs_content.py
visit_htmlzbokehjs_content.visit_htmlg   s    $% 	u,FFFIL 0 7)BU`i`r s s stttL/43DDQXMZZZ[[[[[    c                    | j                             t          j        |d         |d          |d                              d S )Nr"   disable_codepen	js_source)r"   enable_codepenr/   )r$   r%   r   r&   )r'   r(   s     r*   depart_htmlzbokehjs_content.depart_htmlp   se    L/d7mX\]nXoTo{  AL  |M  N  N  N  	O  	O  	O  	O  	Or,   N)__name__
__module____qualname__staticmethodr+   r1   __func__html r,   r*   r   r   e   sZ        \ \ \\ O O \O  44DDDr,   r   c                      e Zd ZdZdZdZej        Ze                    e	           e                    e	           e                    e	           e                    e	           d Z
d	 Zd
 Zd ZdS )r   Tr   r   )r"   )js_file)include_html)r.   c                j   | j         j        }| j                            | j                  }| j                            d          }|r	 t          |                    d                    t          |          }t          fd|D                       r<| j        d         }t                              t          d d|          |           fd|D             }nH# t          $ r9}|j                            t!          |          | j                  gcY d	}~S d	}~ww xY wd	}d
| j        v rf| j                            | j                  }|                    d          }	t#          |	| j        d
         |          }	d                    |	          }t'          j        ||          }
||
d<   d| j        v pd| j        v |
d<   |
dxx         | j                            dg           z  cc<   i x}|
d<   |||d<   d| j        v r| j        d         |d<   t+          | |
           | j                            d          }|rY	 t-          | |
|          }
nF# t          $ r9}|j                            t!          |          | j                  gcY d	}~S d	}~ww xY w|                     |
           |
gS )zthis is copied from sphinx.directives.code.CodeBlock.run

        it has been changed to accept code and language as an arguments instead
        of reading from self

        zemphasize-lines
c              3  $   K   | ]
}|k    V  d S )Nr8   ).0inliness     r*   	<genexpr>z4BokehJSContent.get_codeblock_node.<locals>.<genexpr>   s'      55qqF{555555r,   z#line number spec is out of range(1-z): )locationc                &    g | ]}|k     |d z   S )r   r8   )r?   xrA   s     r*   
<listcomp>z5BokehJSContent.get_codeblock_node.<locals>.<listcomp>   s"    BBBaq6zzAEzzzr,   )lineNdedentlanguagelinenoszlineno-startclassesclasshighlight_argshl_lineslinenostartcaption)statedocumentstate_machineget_source_and_linelinenooptionsgetlensplitr   anylogwarningr   
ValueErrorreporterstrr
   r   r   literal_blockr   r	   add_name)selfcoderI   rR   rC   linespecrN   
emph_lineserrlinesliteral
extra_argsrP   excrA   s                 @r*   get_codeblock_nodez!BokehJSContent.get_codeblock_node   s    :&%99$+FF<##$566 		OTZZ--..'&995555H55555 x!%.?!@JKK#b#b#bT^#b#b c cnvKwwwBBBB8BBB O O O )11#c((1MMNNNNNNNO Ht|##)==dkJJHJJt$$E X(>RRRE99U##D%dD11&
&$,6X.DL:X		dl..w;;;133
W-.%-Jz"T\))(,^(DJ}%g&&&,""9-- 	OO+D'7CC O O O )11#c((1MMNNNNNNNO
 	gys=   
BC" "
D%,.D D% D%I 
J!.JJJc                   | j                             dd          }|r| j        rt          d          |rt                              d| j        j        d|            |}|                    d          st          | j        j
        j        |          }t          |                                          }nAt                              d| j        j                   d	                    | j                  }|S )	Nr:   Fz?bokehjs-content:: directive can't have both js_file and contentz/[bokehjs-content] handling external example in z: /z-[bokehjs-content] handling inline example in r=   )rV   rW   contentr   r[   debugenvdocname
startswithr   appsrcdiropenread)rb   r:   pathr/   s       r*   get_js_sourcezBokehJSContent.get_js_source   s    ,""9e44 	at| 	a_``` 	0IIgHXgg^egghhhD%%c** 7DHL/66T

))IIIIZdhFVZZ[[[		$,//Ir,   c                    |                                  }| j                            dd          r;t          d          }t	          j        |j        |j        |j        |          }|dgS |dgS )zL
        This is largely copied from bokeh.sphinxext.bokeh_plot.run
        r;   FTr   )r   r    hashes
bjs_scriptr7   
javascript)	rx   rV   rW   r   r   r&   r   r    rz   )rb   r/   r)   html_sources       r*   get_code_languagez BokehJSContent.get_code_language   s     &&((	<NE22 	-,FFFI"/I4GR[Rdmvm}  KT  U  U  UK((|,,r,   c                   | j         j        j        d         }t          |          }| j                            d          }| d| }|                    dd          }t          j        dd|g          }t                      }||d<   | j
                            d	d
          |d	<   d|d<   | j
                            dd          |d<   |                                 |d<   | j         j        j        }t          |d          sd|_        d|d<   |                                 \  }}	|                     ||	          }
|                    |
d                    |j                            |
d                    ||gS )Nsourceccbz.ccb-.- )idsr!   r"   zbokehjs exampleFr   r.   r/   bjs_seenTr   )rS   r(   rR   r   rp   new_serialnoreplacer   targetr   rV   rW   rx   hasattrr   r~   rk   setup_childchildrenr%   )rb   
rst_sourcerst_filename	serial_nor!   target_noder(   
source_doccode_contentrI   cbs              r*   runzBokehJSContent.run   s   ',5h?

++H))%00	#55)55	%%c3//	l2r	{;;;  %[((2CDDW%*!""&,"2"23De"L"L ..00[',5
z:.. 	. #'J)-D%&!%!7!7!9!9h$$\8<<ARU###T""r,   N)r2   r3   r4   has_contentoptional_argumentsrequired_argumentsr   option_specupdater   rk   rx   r~   r   r8   r,   r*   r   r   w   s        K'KY'''y)))I...y1116 6 6p  $
- 
- 
-# # # # #r,   r   c                    |                      t          t          j                   |                     dt                     t
          S )z+ Required Sphinx extension setup function. )r7   zbokehjs-content)add_noder   r7   add_directiver   r   )rs   s    r*   r   r      s8    LL';L<<<'888r,   ))__doc__
__future__r   r   	getLoggerr2   r[   os.pathr   r   docutilsr   docutils.parsers.rst.directivesr   sphinx.directives.coder   r	   r
   sphinx.errorsr   sphinx.localer   sphinx.utilr   sphinx.util.nodesr   r   r   	templatesr   r   r   r   utilr   __all__GeneralElementr   r   r   r8   r,   r*   <module>r      s  & &V # " " " " " g!! # " " " " " " "       5 5 5 5 5 5 M M M M M M M M M M % % % % % %       - - - - - - - - - - - - - -                  ' & & & & &5 5 5 5 5emU] 5 5 5$@# @# @# @# @#Y @# @# @#F    r,   