ó
§‰]c           @@ s}  d  Z  d d l m Z m Z d d l Z d d l Z d d l Z d d l m Z m	 Z	 m
 Z
 m Z 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 m Z m Z m Z m Z m Z d d l  m! Z! d d l" m# Z# d g Z$ i e! j% d 6e! j& d 6e! j' d 6e! j( d 6e! j) d 6e! j* d 6e! j+ d 6e! j, d 6e! j- d 6e! j. d 6e! j/ d 6e! j0 d 6e! j1 d 6e! j2 d 6e! j3 d 6e! j4 d 6e! j5 d 6e! j6 d  6e! j7 d! 6e! j8 d" 6e! j9 d# 6e! j: d$ 6e! j; d% 6e! j< d& 6e! j= d' 6e! j> d( 6e! j? d) 6e! j@ d* 6e jA d+ 6e! jB d, 6ZC d- e f d. „  ƒ  YZD d/ e# f d0 „  ƒ  YZE d S(1   sˆ   This module defines TemplateExporter, a highly configurable converter
that uses Jinja2 to export notebook files into different formats.
i    (   t   print_functiont   absolute_importN(   t	   HasTraitst   Unicodet   Listt   Dictt   Boolt   defaultt   observe(   t   Config(   t   import_item(   t	   py3compat(   t   jupyter_path(   t   ensure_dir_exists(   t   TemplateNotFoundt   Environmentt   ChoiceLoadert   FileSystemLoadert
   BaseLoadert
   DictLoader(   t   filtersi   (   t   Exporters   jinja2.ext.loopcontrolst   indentt   markdown2htmlt   markdown2asciidoct	   ansi2htmlt   filter_data_typet	   get_linest   highlight2htmlt   highlight2latext   ipython2pythont
   posix_patht   markdown2latext   markdown2rstt   comment_linest
   strip_ansit   strip_dollarst   strip_files_prefixt	   html2textt
   add_anchort
   ansi2latext	   wrap_textt   escape_latext   citation2latext   path2urlt   add_promptst
   ascii_onlyt   prevent_list_blockst   get_metadatat   convert_pandoct
   json_dumpst   strip_trailing_newlinet   ExtensionTolerantLoaderc           B@ s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sN  A template loader which optionally adds a given extension when searching.

    Constructor takes two arguments: *loader* is another Jinja loader instance
    to wrap. *extension* is the extension, which will be added to the template
    name if finding the template without it fails. This should include the dot,
    e.g. '.tpl'.
    c         C@ s   | |  _  | |  _ d  S(   N(   t   loadert	   extension(   t   selfR5   R6   (    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyt   __init__H   s    	c         C@ sg   y |  j  j | | ƒ SWnI t k
 rb | j |  j ƒ rH t | ƒ ‚ n  |  j  j | | |  j ƒ SXd  S(   N(   R5   t
   get_sourceR   t   endswithR6   (   R7   t   environmentt   template(    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyR9   L   s    c         C@ s   |  j  j ƒ  S(   N(   R5   t   list_templates(   R7   (    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyR=   T   s    (   t   __name__t
   __module__t   __doc__R8   R9   R=   (    (    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyR4   @   s   		t   TemplateExporterc           B@ s  e  Z d  Z e j d d d j e e j ƒ  ƒ ƒ ƒ Z d4 Z	 d4 d „ Z
 e d „  ƒ Z d4 Z d4 d „ Z e d „  ƒ Z e d „  ƒ Z e d	 d
 ƒ j d e d e ƒ Z e d d	 d ƒj d e ƒ Z d Z d Z e d ƒ d „  ƒ Z e d ƒ d „  ƒ Z e d ƒ d „  ƒ Z e d ƒ j d e ƒ Z e d g ƒ j d e d e ƒ Z e e  j! j d d ƒ d	 d ƒj d e ƒ Z" e e  j! j d d d ƒ d	 d ƒj d e ƒ Z# e e$ d d ƒ d	 d ƒj d e ƒ Z% e d ƒ j d e d e ƒ Z& e' e( d	 d  ƒj d e ƒ Z) e' e( d	 d! ƒj d e ƒ Z* e' e( d	 d" ƒj d e ƒ Z+ e' e( d	 d# ƒj d e ƒ Z, e' e( d	 d$ ƒj d e ƒ Z- e' e( d	 d% ƒj d e ƒ Z. e' e( d	 d& ƒj d e ƒ Z/ e' e( d	 d' ƒj d e ƒ Z0 e d	 d( ƒ j d e ƒ Z1 e2 d	 d) ƒ j d e d e ƒ Z3 e d	 d* ƒ j d e ƒ Z4 e d+ ƒ d, „  ƒ Z5 d4 d- „ Z6 d. „  Z7 d4 d/ „ Z8 d0 „  Z9 d1 „  Z: d2 „  Z d3 „  Z; RS(5   sá  
    Exports notebooks into other file formats.  Uses Jinja 2 templating engine
    to output new formats.  Inherit from this class if you are creating a new
    template type along with new filters/preprocessors.  If the filters/
    preprocessors provided by default suffice, there is no need to inherit from
    this class.  Instead, override the template_file and file_extension
    traits via a config file.

    Filters available by default for templates:

    {filters}
    R   s   - s   
    - c         C@ s   d  |  _ d  S(   N(   t   Nonet   _template_cached(   R7   t   change(    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyt   _invalidate_template_cachel   s    c         C@ s(   |  j  d  k r! |  j ƒ  |  _  n  |  j  S(   N(   RC   RB   t   _load_template(   R7   (    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyR<   o   s    c         C@ s   d  |  _ |  j ƒ  d  S(   N(   RB   t   _environment_cachedRE   (   R7   RD   (    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyt   _invalidate_environment_cachew   s    	c         C@ s(   |  j  d  k r! |  j ƒ  |  _  n  |  j  S(   N(   RG   RB   t   _create_environment(   R7   (    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyR;   {   s    c         C@ sE   t  i i t d 6d 6i t d 6d 6ƒ } | j t t |  ƒ j ƒ | S(   Nt   enabledt   RegexRemovePreprocessort   TagRemovePreprocessor(   R	   t   Truet   merget   superRA   t   default_config(   R7   t   c(    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyRP      s    t   helps    Name of the template file to uset   configt   affects_templatet    s   raw template stringt   affects_environments   <memory>t   template_filec         C@ s¶   | d } | d k r& |  j  |  _ d  St j j | ƒ } t j j | ƒ r² t j j | ƒ \ } } | g  |  j D] } t j j | ƒ ^ qo k r¦ | g |  j |  _ n  | |  _ n  d  S(   Nt   newR   (   t   default_templateRW   t   ost   patht   abspatht   isfilet   splitt   template_path(   R7   RD   RX   t	   full_patht   template_dirRW   t   p(    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyt   _template_file_changed—   s    
.c         C@ s   |  j  S(   N(   RY   (   R7   (    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyt   _template_file_default¦   s    t   raw_templatec         C@ s0   | d s" |  j  p |  j |  _ n  |  j ƒ  d  S(   NRX   (   RY   t   _last_template_fileRW   RE   (   R7   RD   (    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyt   _raw_template_changedª   s    
u    t   .s   ..t	   templatess*   Path where the template files are located.t   skeletons3   Path where the template skeleton files are located.t	   nbconverts*   Path where templates can be installed too.s   .tplsN   This allows you to exclude code cell inputs from all templates if set to True.sK   This allows you to exclude input prompts from all templates if set to True.sO   This allows you to exclude code cell outputs from all templates if set to True.sL   This allows you to exclude output prompts from all templates if set to True.sH   This allows you to exclude code cells from all templates if set to True.sL   This allows you to exclude markdown cells from all templates if set to True.sG   This allows you to exclude raw cells from all templates if set to True.sK   This allows you to exclude unknown cells from all templates if set to True.s[   Jinja loaders to find templates. Will be tried in order before the default FileSystem ones.sV   Dictionary of filters, by name and namespace, to add to the Jinja
        environment.s>   formats of raw cells to be included in this Exporter's output.t   raw_mimetypesc         C@ s   |  j  d g S(   NRU   (   t   output_mimetype(   R7   (    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyt   _raw_mimetypes_defaultô   s    c         K@ sj   t  t |  ƒ j d | |  |  j |  j t |  j d t ƒ ƒ ƒ |  j |  j t |  j d t ƒ ƒ ƒ d S(   s”  
        Public constructor

        Parameters
        ----------
        config : config
            User configuration instance.
        extra_loaders : list[of Jinja Loaders]
            ordered list of Jinja loader to find templates. Will be tried in order
            before the default FileSystem ones.
        template : str (optional, kw arg)
            Template to use when exporting.
        RS   RV   RT   N(	   RO   RA   R8   R   RH   t   listt   traitsRM   RE   (   R7   RS   t   kw(    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyR8   ù   s
    c         C@ s²   |  j  ƒ  > |  j |  j k r. |  j |  _ n  |  j rF |  j |  _ n  Wd QX|  j sd t d ƒ ‚ n  |  j } |  j j d | ƒ |  j j d t j	 j
 |  j ƒ ƒ |  j j | ƒ S(   s’   Load the Jinja template object from the template file

        This is triggered by various trait changes that would change the template.
        Ns   No template_file specified!s   Attempting to load template %ss       template_path: %s(   t   hold_trait_notificationsRW   t   _raw_template_keyRf   Re   t
   ValueErrort   logt   debugRZ   t   pathsept   joinR_   R;   t   get_template(   R7   RW   (    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyRF     s    			"c         K@ sà   t  t |  ƒ j | | |  \ } } | j d |  j ƒ i	 |  j d 6|  j d 6|  j d 6|  j d 6|  j	 d 6|  j
 d 6|  j d 6|  j d	 6|  j o¡ |  j d
 6| d <|  j j d | d | ƒ } | j d ƒ } | | f S(   s/  
        Convert a notebook from a notebook node instance.

        Parameters
        ----------
        nb : :class:`~nbformat.NotebookNode`
          Notebook node
        resources : dict
          Additional resources that can be accessed read/write by
          preprocessors and filters.
        Rl   t   include_codet   include_markdownt   include_rawt   include_unknownt   include_inputt   include_outputt   include_input_promptt   include_output_promptt	   no_promptt   global_content_filtert   nbt	   resourcess   
(   RO   RA   t   from_notebook_nodet
   setdefaultRl   t   exclude_code_cellt   exclude_markdownt   exclude_rawt   exclude_unknownt   exclude_inputt   exclude_outputt   exclude_input_promptt   exclude_output_promptR<   t   rendert   lstrip(   R7   R„   R…   Rq   t   nb_copyt   output(    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyR†   '  s    $c         C@ sý   | d k r t d ƒ ‚ n  t | t ƒ } | } | rh t | t j ƒ rh t | ƒ } |  j | | | ƒ S| rŽ t | d ƒ rŽ | | j	 | <| S| rÈ t
 | t ƒ rÈ | d |  ƒ } |  j | | | ƒ n1 | rí | ƒ  } |  j | | | ƒ n t d ƒ ‚ d S(   s9  
        Register a filter.
        A filter is a function that accepts and acts on one string.
        The filters are accessible within the Jinja templating engine.

        Parameters
        ----------
        name : str
            name to give the filter in the Jinja engine
        filter : filter
        t   filtert   __call__t   parentN(   RB   t	   TypeErrort
   isinstancet   typeR   t   string_typesR
   t   _register_filtert   hasattrR   t
   issubclassR   (   R7   t   environt   namet   jinja_filtert   isclasst   constructedt
   filter_clst   filter_instance(    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyR›   F  s"    	c         C@ s   |  j  |  j | | ƒ S(   s9  
        Register a filter.
        A filter is a function that accepts and acts on one string.
        The filters are accessible within the Jinja templating engine.

        Parameters
        ----------
        name : str
            name to give the filter in the Jinja engine
        filter : filter
        (   R›   R;   (   R7   RŸ   R    (    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyt   register_filters  s    c         C@ s
   t  j ƒ  S(   s|  Override in subclasses to provide extra filters.

        This should return an iterable of 2-tuples: (name, class-or-function).
        You should call the method on the parent class and include the filters
        it provides.

        If a name is repeated, the last filter provided wins. Filters from
        user-supplied config win over filters provided by classes.
        (   t   default_filterst   items(   R7   (    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyR¦     s    
c   
      C@ sq  t  j j t  j j t ƒ ƒ } |  j } x6 | D]. } y t | d d ƒWq. t k
 r[ q. Xq. W|  j | t  j j	 | |  j
 ƒ t  j j	 | |  j ƒ g } |  j t t | ƒ |  j ƒ t i |  j |  j 6ƒ g } t d t | ƒ d t ƒ } t j | j d <x- |  j ƒ  D] \ } } |  j | | | ƒ qW|  j rmx3 |  j j ƒ  D] \ } }	 |  j | | |	 ƒ qGWn  | S(   s:   
        Create the Jinja templating environment.
        t   modeiÀ  R5   t
   extensionst   uuid4(   RZ   R[   t   dirnamet   realpatht   __file__t   template_data_pathsR   t   OSErrorR_   Rx   t   default_template_patht   template_skeleton_patht   extra_loadersR4   R   t   template_extensionR   Re   Rs   R   R   t   JINJA_EXTENSIONSt   uuidRª   t   globalsR¦   R›   R   R§   (
   R7   t   heret   additional_pathsR[   t   pathst   loadersR;   t   keyt   valuet   user_filter(    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyRI     s.    	
		N(<   R>   R?   R@   t   formatRx   t   sortedR¦   t   keysRB   RC   RE   t   propertyR<   RG   RH   R;   RP   R   t   tagRM   RW   Re   Rf   Rs   R   Rc   R   Rd   Rg   RY   R   R_   RZ   R[   R°   R±   R   R®   R³   R   t   FalseRŒ   RŽ   R   R   Rˆ   R‰   RŠ   R‹   R²   R   R   Rl   Rn   R8   RF   R†   R›   R¥   RI   (    (    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyRA   X   s–   !																							-		(F   R@   t
   __future__R    R   RZ   Rµ   t   jsont	   traitletsR   R   R   R   R   R   R   t   traitlets.configR	   t   traitlets.utils.importstringR
   t   ipython_genutilsR   t   jupyter_core.pathsR   t   jupyter_core.utilsR   t   jinja2R   R   R   R   R   R   Rk   R   t   exporterR   R´   R   R   R   R   t   DataTypeFilterR   t   Highlight2HTMLt   Highlight2LatexR   R   R    R!   R"   R#   R$   R%   R&   R'   R(   R)   R*   R+   R,   R-   R.   R/   R0   R1   t   dumpsR3   R¦   R4   RA   (    (    (    sC   lib/python2.7/site-packages/nbconvert/exporters/templateexporter.pyt   <module>   s^   4.	




























