σ
mάJ]c           @` sά  d  Z  d d l m Z m Z m Z 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 d d l m Z m Z m Z d d	 l m Z d d
 l m Z d d l 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$ m% Z% d d l& m' Z' e( rd d l) m* Z* m+ Z+ m, Z, m- Z- d d l. m/ Z/ d d l0 m1 Z1 n  d" Z2 d e j3 e j4 f d     YZ5 d e f d     YZ6 d   Z7 d    Z8 d!   Z9 d S(#   uΛ   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');

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   basenamet   join(   t   nodes(   t	   unchanged(   t	   text_type(   t	   CodeBlockt   dedent_linest   container_wrapper(   t   __(   t   logging(   t   parselinenos(   t   set_source_info(   t   SphinxErrori   (   t   get_sphinx_resources(   t   BJS_PROLOGUEt   BJS_EPILOGUEt   BJS_CODEPEN_INITt   BJS_HTMLi   (   t   decode_utf8(   t   Anyt   Dictt   Listt   Tuple(   t   Sphinx(   t   Configu   bokehjs_contentu   BokehJSContentu   html_depart_bokehjs_contentu   html_visit_bokehjs_contentu   setupt   bokehjs_contentc           B` s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s>   lib/python2.7/site-packages/bokeh/sphinxext/bokehjs_content.pyR   e   s   t   BokehJSContentc           B` s   e  Z e Z d  Z d Z e j Z e j d e	  e j d e	  e j d e	  e j d e	  d   Z
 d   Z d   Z d	   Z RS(
   i   i    t   titlet   js_filet   include_htmlt   disable_codepenc         ` s   |  j  j } |  j j |  j  } |  j j d  } | ry t | j d     t	 |    } t
   f d   | D  r© t j t d    |  j d f d | n  g  | D] } |   k  r° | d ^ q° } Wqt k
 r} | j j t |  d |  j g SXn d } d |  j k rr|  j j |  j  } | j d  }	 t |	 |  j d d | }	 d j |	  } n  t j | |  }
 | |
 d	 <d
 |  j k p©d |  j k |
 d
 <|
 d c |  j j d g   7<i  } |
 d <| d k	 rφ| | d <n  d |  j k r|  j d | d <n  t |  |
  |  j j d  } | ry t |  |
 |  }
 Wqt k
 r} | j j t |  d |  j g SXn  |  j |
  |
 g S(   u°   this 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

        u   emphasize-linesu   
c         3` s   |  ] } |   k Vq d  S(   N(    (   t   .0t   i(   t   nlines(    s>   lib/python2.7/site-packages/bokeh/sphinxext/bokehjs_content.pys	   <genexpr>   s    u*   line number spec is out of range(1-%d): %rt   locationi   t   lineu   dedentu   languageu   linenosu   lineno-startu   classesu   classu   highlight_argsu   hl_linesu   linenostartu   captionN(   t   statet   documentt   state_machinet   get_source_and_linet   linenot   optionst   gett   lent   splitR   t   anyt   logt   warningR   t
   ValueErrort   reportert   strt   NoneR
   R   R   t   literal_blockR   R   R   t   add_name(   t   selft   codet   languageR+   R(   t   linespect   hl_linest   xt   errt   linest   literalt
   extra_argst   captiont   exc(    (   R'   s>   lib/python2.7/site-packages/bokeh/sphinxext/bokehjs_content.pyt   get_codeblock_nodeu   sN    	
-&
&c         C` sΫ   |  j  j j j } |  j j d t  } | rE |  j rE t d   n  | r© t	 j
 d | j |  | } | j d  s t | j j |  } n  t t |  j    } n. t	 j
 d | j  | j } d j |  j  } | S(   Nu   js_fileu?   bokehjs-content:: directive can't have both js_file and contentu5   [bokehjs-content] handling external example in %r: %su   /u/   [bokehjs-content] handling inline example in %ru   
(   R*   R+   t   settingst   envR/   R0   t   Falset   contentR   R4   t   debugt   docnamet
   startswithR   t   appt   srcdirR   t   opent   readt   bokeh_plot_auxdir(   R<   RJ   R"   t   patht	   js_source(    (    s>   lib/python2.7/site-packages/bokeh/sphinxext/bokehjs_content.pyt   get_js_source°   s    	c         C` sl   |  j    } |  j j d t  r^ t d t  } t j d | j d | j	 d |  } | d g S| d g Sd S(	   uL   
        This is largely copied from bokeh.sphinxext.bokeh_plot.run
        u   include_htmlt   include_bokehjs_apit	   css_filest   js_filest
   bjs_scriptu   htmlu
   javascriptN(
   RW   R/   R0   RK   R   t   TrueR   t   renderRY   RZ   (   R<   RV   t	   resourcest   html_source(    (    s>   lib/python2.7/site-packages/bokeh/sphinxext/bokehjs_content.pyt   get_code_languageΔ   s    				
c         C` s]  |  j  j j j } |  j j j d } t |  } d | | j d  f } | j d d  } t	 j
 d d d | g } t   } | | d <|  j j d	 d
  | d	 <t | d <|  j j d t  | d <|  j   | d <|  j j j } t | d  s
t | _ t | d <n  |  j   \ } }	 |  j | |	  }
 | j |
 d  | j j |
 d  | | g S(   Nu   sourceu	   %s.ccb-%du   ccbu   .u   -u    t   idsu	   target_idu   titleu   bokehjs exampleu   include_bjs_headeru   disable_codepenu	   js_sourceu   bjs_seeni    (   R*   R+   RI   RJ   R,   t   nodeR   t   new_serialnot   replaceR   t   targetR   R/   R0   RK   RW   t   hasattrR\   t   bjs_seenR`   RH   t   setup_childt   childrent   append(   R<   RJ   t
   rst_sourcet   rst_filenamet	   target_idt   target_nodeRb   t
   source_doct   code_contentR>   t   cb(    (    s>   lib/python2.7/site-packages/bokeh/sphinxext/bokehjs_content.pyt   runΤ   s,    	

	(   R   R   R\   t   has_contentt   optional_argumentst   required_argumentsR	   t   option_spect   updateR   RH   RW   R`   Rr   (    (    (    s>   lib/python2.7/site-packages/bokeh/sphinxext/bokehjs_content.pyR    i   s   		;		c         C` sr   | d rD t  d t  } |  j j t j d | j d | j   n  |  j j t j d | d d | d   d  S(	   Nu   include_bjs_headerRX   RY   RZ   t   idu	   target_idR!   u   title(	   R   R\   t   bodyRj   R   R]   RY   RZ   R   (   R<   Rb   R^   (    (    s>   lib/python2.7/site-packages/bokeh/sphinxext/bokehjs_content.pyt   html_visit_bokehjs_contentφ   s    
			
c      	   C` s9   |  j  j t j d | d d | d d | d   d  S(   NR!   u   titlet   enable_codepenu   disable_codepenRV   u	   js_source(   Ry   Rj   R   R]   (   R<   Rb   (    (    s>   lib/python2.7/site-packages/bokeh/sphinxext/bokehjs_content.pyt   html_depart_bokehjs_content  s    
c         C` s-   |  j  t d t t f |  j d t  d S(   u+    Required Sphinx extension setup function. t   htmlu   bokehjs-contentN(   t   add_nodeR   Rz   R|   t   add_directiveR    (   RP   (    (    s>   lib/python2.7/site-packages/bokeh/sphinxext/bokehjs_content.pyt   setup  s
    
(   u   bokehjs_contentu   BokehJSContentu   html_depart_bokehjs_contentu   html_visit_bokehjs_contentu   setup(:   t   __doc__t
   __future__R    R   R   R   R   t	   getLoggerR   R4   t   os.pathR   R   t   docutilsR   t   docutils.parsers.rst.directivesR   t   sixR   t   sphinx.directives.codeR	   R
   R   t   sphinx.localeR   t   sphinx.utilR   t   sphinx.util.nodesR   t   sphinx.errorsR   t   utilR   t	   templatesR   R   R   R   t   util.stringR   RK   t   typingR   R   R   R   t   sphinx.applicationR   t   sphinx.configR   t   __all__t   Generalt   ElementR   R    Rz   R|   R   (    (    (    s>   lib/python2.7/site-packages/bokeh/sphinxext/bokehjs_content.pyt   <module>*   s<   """    		