ó
§‰]c           @   sÍ   d  Z  d d l Z d d l Z d d l Z d d l m Z m Z 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 e f d	 „  ƒ  YZ d
 „  Z d e f d „  ƒ  YZ d S(   s   Export to PDF via latexiÿÿÿÿN(   t   whicht   cast_bytes_py2t   getcwd(   t   Integert   Listt   Boolt   Instancet   Unicodet   default(   t   TemporaryWorkingDirectoryi   (   t   LatexExportert   LatexFailedc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sV   Exception for failed latex run
    
    Captured latex output is in error.output.
    c         C   s   | |  _  d  S(   N(   t   output(   t   selfR   (    (    s6   lib/python2.7/site-packages/nbconvert/exporters/pdf.pyt   __init__   s    c         C   s   d |  j  S(   Nu.   PDF creating failed, captured latex output:
%s(   R   (   R   (    (    s6   lib/python2.7/site-packages/nbconvert/exporters/pdf.pyt   __unicode__   s    c         C   s   |  j  ƒ  } t | ƒ S(   N(   R   R   (   R   t   u(    (    s6   lib/python2.7/site-packages/nbconvert/exporters/pdf.pyt   __str__   s    (   t   __name__t
   __module__t   __doc__R   R   R   (    (    (    s6   lib/python2.7/site-packages/nbconvert/exporters/pdf.pyR      s   		c         C   s5   | s
 d St  | ƒ t j | j |  d ƒ | |  <d S(   sŽ   Add value to the environment variable varname in envdict

    e.g. prepend_to_env_search_path('BIBINPUTS', '/home/sally/foo', os.environ)
    Nt    (   R   t   ost   pathsept   get(   t   varnamet   valuet   envdict(    (    s6   lib/python2.7/site-packages/nbconvert/exporters/pdf.pyt   prepend_to_env_search_path   s    t   PDFExporterc           B   s  e  Z d  Z d Z e d d d ƒj d e ƒ Z e d d d g d d	 ƒj d e ƒ Z	 e d
 d g d d ƒj d e ƒ Z
 e e d d ƒj d e ƒ Z e d d ƒ Z e d d d d i d d 6ƒZ d Z e ƒ  Z e d ƒ d „  ƒ Z d d „ Z e d „ Z e d „ Z d d „ Z RS(   s×   Writer designed to write to PDF files.

    This inherits from :class:`LatexExporter`. It creates a LaTeX file in
    a temporary directory using the template machinery, and then runs LaTeX
    to create a pdf.
    s   PDF via LaTeXi   t   helps$   How many times latex will be called.t   configu   xelatexu
   {filename}s   -quiets$   Shell command used to compile latex.u   bibtexs!   Shell command used to run bibtex.s0   Whether to display the output of latex commands.s.   texinputs dir. A notebook's directory is addeds   nbconvert.writers.FilesWritert   argst   kwt   .t   build_directorys   application/pdft   file_extensionc         C   s   d S(   Ns   .pdf(    (   R   (    (    s6   lib/python2.7/site-packages/nbconvert/exporters/pdf.pyt   _file_extension_defaultH   s    c         C   s[  g  | D] } | j  d | ƒ ^ q } t j d k rh t j d k  rh g  | D] } | j d ƒ ^ qJ } n  t | d ƒ } | d k r¯ d }	 t d j  d | d d	 |	 ƒ ƒ ‚ n  | d
 k rÁ d n d }
 |  j j	 d | d | |
 | ƒ t j d k } | rt
 j | ƒ } n  t j j ƒ  } t d |  j | ƒ t d |  j | ƒ t d |  j | ƒ t t j d ƒ í } |  j s}t
 j n d } xË t | ƒ D]½ } t
 j | d | d t
 j d | d | d | ƒ} | j ƒ  \ } } | j r|  j ród } n | j d d ƒ } | | | ƒ |  j j | ƒ | rI| d j  d | d | ƒ ƒ ‚ n  t SqWWd QXt S(   sÑ  Run command_list count times.

        Parameters
        ----------
        command_list : list
            A list of args to provide to Popen. Each element of this
            list will be interpolated with the filename to convert.
        filename : unicode
            The name of the file to convert.
        count : int
            How many times to run the command.
         raise_on_failure: Exception class (default None)
            If provided, will raise the given exception for if an instead of
            returning False on command failure.

        Returns
        -------
        success : bool
            A boolean indicating if the command was successful (True)
            or failed (False).
        t   filenamet   win32i   i    t   cp1252sF   https://nbconvert.readthedocs.io/en/latest/install.html#installing-texs€   {formatter} not found on PATH, if you have not installed {formatter} you may need to do so. Find further instructions at {link}.t	   formattert   linki   t   timet   timess   Running %s %i %s: %st	   TEXINPUTSt	   BIBINPUTSt	   BSTINPUTSt   rbt   stdoutt   stderrt   stdint   shellt   envu    s   utf-8t   replaces+   Failed to run "{command}" command:
{output}t   commandR   N(   i   i    (   t   formatt   syst   platformt   version_infot   encodeR    t   Nonet   OSErrort   logt   infot
   subprocesst   list2cmdlineR   t   environt   copyR   t	   texinputst   opent   devnullt   verboset   PIPEt   ranget   Popent   STDOUTt   communicatet
   returncodet   decodet   _captured_outputt   appendt   Falset   True(   R   t   command_listR&   t   countt   log_functiont   raise_on_failuret   cR7   t   cmdR*   R,   R4   R5   t   nullR1   t   indext   pt   outt   _(    (    s6   lib/python2.7/site-packages/nbconvert/exporters/pdf.pyt   run_commandL   sF    %% 				c            s.   ‡  f d †  } ˆ  j  ˆ  j | ˆ  j | | ƒ S(   s#   Run xelatex self.latex_count times.c            s!   ˆ  j  j d |  d |  | ƒ d  S(   Nu   %s failed: %s
%si    (   R?   t   critical(   R7   R]   (   R   (    s6   lib/python2.7/site-packages/nbconvert/exporters/pdf.pyt	   log_error˜   s    (   R_   t   latex_commandt   latex_count(   R   R&   RW   Ra   (    (   R   s6   lib/python2.7/site-packages/nbconvert/exporters/pdf.pyt	   run_latex•   s    c            sA   t  j j | ƒ d } ‡  f d †  } ˆ  j ˆ  j | d | | ƒ S(   s   Run bibtex one time.i    c            s8   ˆ  j  j d |  d ƒ ˆ  j  j d |  d |  | ƒ d  S(   Ns<   %s had problems, most likely because there were no citationsi    u   %s output: %s
%s(   R?   t   warningt   debug(   R7   R]   (   R   (    s6   lib/python2.7/site-packages/nbconvert/exporters/pdf.pyRa   ¢   s    i   (   R   t   patht   splitextR_   t   bib_command(   R   R&   RW   Ra   (    (   R   s6   lib/python2.7/site-packages/nbconvert/exporters/pdf.pyt   run_bibž   s    c   
   	   K   sw  t  t |  ƒ j | d | | \ } } | r\ | j d i  ƒ j d ƒ r\ | d d |  _ n t ƒ  |  _ g  |  _ t ƒ  × d } d | d <|  j j	 | | d | ƒ} |  j
 j d ƒ |  j | ƒ |  j | ƒ râ |  j | ƒ n  | d	 } t j j | ƒ st d
 j |  j ƒ ƒ ‚ n  |  j
 j d ƒ t | d ƒ  } | j ƒ  }	 Wd  QXWd  QXd	 | d <| j d d  ƒ |	 | f S(   Nt	   resourcest   metadataRg   t   notebooks   .text   output_extensiont   notebook_names   Building PDFs   .pdfs   
s   PDF successfully createdR0   t   outputs(   t   superR   t   from_notebook_nodeR   RE   R   t   _captured_outputsR	   t   writert   writeR?   R@   Rd   Rj   R   Rg   t   isfileR   t   joinRP   RF   t   readt   popR=   (
   R   t   nbRk   R!   t   latexRo   t   tex_filet   pdf_filet   ft   pdf_data(    (    s6   lib/python2.7/site-packages/nbconvert/exporters/pdf.pyRr   ©   s.    !	



(    N(   R   R   R   t   export_from_notebookR   t   tagRS   Rc   R   Rb   Ri   R   RR   RH   R   RE   R   Rt   t   output_mimetypeRP   R   R%   R=   R_   R   Rd   Rj   Rr   (    (    (    s6   lib/python2.7/site-packages/nbconvert/exporters/pdf.pyR   (   s.   							I	(   R   RA   R   R9   t   ipython_genutils.py3compatR    R   R   t	   traitletsR   R   R   R   R   R   t   testpath.tempdirR	   R{   R
   t   IOErrorR   R   R   (    (    (    s6   lib/python2.7/site-packages/nbconvert/exporters/pdf.pyt   <module>   s   .	
