
    b%                         d Z ddlZddlZddlZddl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  G d	 d
e      Zd Z G d de      Zy)zExport to PDF via latex    N)TemporaryDirectory)BoolInstanceIntegerListUnicodedefault   )_contextlib_chdir   )LatexExporterc                   "    e Zd ZdZd Zd Zd Zy)LatexFailedzRException for failed latex run

    Captured latex output is in error.output.
    c                     || _         y Noutput)selfr   s     9/usr/lib/python3/dist-packages/nbconvert/exporters/pdf.py__init__zLatexFailed.__init__   s	        c                      d| j                   z  S )Nz.PDF creating failed, captured latex output:
%sr   r   s    r   __unicode__zLatexFailed.__unicode__   s    @4;;NNr   c                 &    | j                         }|S r   )r   )r   us     r   __str__zLatexFailed.__str__   s    r   N)__name__
__module____qualname____doc__r   r   r    r   r   r   r      s    
Or   r   c                 \    |sy|t         j                  z   |j                  | d      z   || <   y)zAdd value to the environment variable varname in envdict

    e.g. prepend_to_env_search_path('BIBINPUTS', '/home/sally/foo', os.environ)
    N )ospathsepget)varnamevalueenvdicts      r   prepend_to_env_search_pathr+   #   s-    
 rzz)GKK,DDGGr   c                       e Zd ZdZdZ edd      j                  d      Z eg dd	      j                  d      Z	 ed
dgd      j                  d      Z
 edd      j                  d      Z ed      Z eddddi      ZdZ e       Z ed      d        Z ed      d        ZddZefdZddZd fd	Z xZS ) PDFExporterzWriter designed to write to PDF files.

    This inherits from `LatexExporter`. It creates a LaTeX file in
    a temporary directory using the template machinery, and then runs LaTeX
    to create a pdf.
    zPDF via LaTeX   z$How many times latex will be called.)helpT)config)xelatex
{filename}z-quietz$Shell command used to compile latex.bibtexr2   z!Shell command used to run bibtex.Fz0Whether to display the output of latex commands.z.texinputs dir. A notebook's directory is addedznbconvert.writers.FilesWriterr"   build_directory.)argskwzapplication/pdffile_extensionc                      y)N.pdfr"   r   s    r   _file_extension_defaultz#PDFExporter._file_extension_defaultK   s    r   template_extensionc                      y)Nz.tex.j2r"   r   s    r   _template_extension_defaultz'PDFExporter._template_extension_defaultO   s    r   c           
      R   |D cg c]  }|j                  |       }}t        j                  |d         }|!d}	t        dj                  |d   |	            |dk(  rdnd	}
| j                  j                  d
|d   ||
|       t        j                  dk(  }|rt        j                  |      }t        j                  j                         }t        d| j                  |       t        d| j                  |       t        d| j                  |       t        t        j                   d      5 }| j"                  st        j$                  nd}t'        |      D ]  }t        j(                  ||t        j*                  |||      }|j-                         \  }}|j.                  sL| j"                  rd}n|j1                  dd      } |||       | j2                  j5                  |       |r |dj                  ||             ddd       y 	 ddd       yc c}w # 1 sw Y   yxY w)a  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).
        )filenamer   NzFhttps://nbconvert.readthedocs.io/en/latest/install.html#installing-texz{formatter} not found on PATH, if you have not installed {formatter} you may need to do so. Find further instructions at {link}.)	formatterlinkr   timetimeszRunning %s %i %s: %swin32	TEXINPUTS	BIBINPUTS	BSTINPUTSrb)stdoutstderrstdinshellenvr$   zutf-8replacez+Failed to run "{command}" command:
{output})commandr   FT)formatshutilwhichOSErrorloginfosysplatform
subprocesslist2cmdliner%   environcopyr+   	texinputsopendevnullverbosePIPErangePopenSTDOUTcommunicate
returncodedecode_captured_outputappend)r   command_listr@   countlog_functionraise_on_failurecrP   cmdrB   rD   rM   rN   nullrJ   _pouts                     r   run_commandzPDFExporter.run_commandS   s   , 9EE1188X8.EE ll<?+;[D#Vl1oDVI   1*',l1oueWU' --g6Gjjoo";D";D";D"**d# 	!t,0LLZ__dF5\ !$$!%,, Q<<|| !!jj)< #.))005'.JQQ(/ R  
 !7	! 	!!	!8 g F.	!8 s   H-A5H#A!HHH&c                 `      fd} j                   j                  | j                  ||      S )z#Run xelatex self.latex_count times.c                 H    j                   j                  d| d   | |       y )Nz%s failed: %s
%sr   )rU   criticalrP   rs   r   s     r   	log_errorz(PDFExporter.run_latex.<locals>.log_error   s     HH171:wLr   )rt   latex_commandlatex_countr   r@   rm   ry   s   `   r   	run_latexzPDFExporter.run_latex   s6    	M $*:*:IGW
 	
r   c                      t         j                  j                  |      d   } fd} j                   j                  |d||      S )zRun bibtex one time.r   c                     j                   j                  d| d          j                   j                  d| d   | |       y )Nz<%s had problems, most likely because there were no citationsr   z%s output: %s
%s)rU   warningdebugrx   s     r   ry   z&PDFExporter.run_bib.<locals>.log_error   s>    HHNPWXYPZ HHNN.
GSIr   r   )r%   pathsplitextrt   bib_commandr|   s   `   r   run_bibzPDFExporter.run_bib   sD    77##H-a0	J  0 0(AyJZ[[r   c                    t        |   |fd|i|\  }}|r/|j                  di       j                  d      r|d   d   | _        nt	        j
                         | _        g | _        t               5 }t        j                  |      5  d}d|d<   | j                  j                  |||      }| j                  j                  d       | j                  |       | j                  |      r| j                  |       |d	z   }t        j                   j#                  |      s$t%        d
j'                  | j(                              | j                  j                  d       t+        |d      5 }	|	j-                         }
d d d        d d d        d d d        d	|d<   |j/                  dd        
|fS # 1 sw Y   4xY w# 1 sw Y   8xY w# 1 sw Y   <xY w)N	resourcesmetadatar   notebookz.texoutput_extension)notebook_namezBuilding PDFr:   
zPDF successfully createdrI   outputs)superfrom_notebook_noder'   r]   r%   getcwd_captured_outputsr   r   chdirwriterwriterU   rV   r}   r   r   isfiler   joinrh   r^   readpop)r   nbr   r7   latextdr   tex_filepdf_filefpdf_data	__class__s              r   r   zPDFExporter.from_notebook_node   s    75bTITQSTyz26::6B&z26:DNYY[DN!#! 	$R):)@)@)D 	$&M,2I(){{((	(WHHHMM.)NN8$||H%x($v-H77>>(+!$))D,A,A"BCCHHMM45h% $668$	$ 	$$ )/	$% 	i&""$ $	$ 	$ 	$ 	$s=   2GC#F;+F/<F;G/F84F;;G	 GGr   )F)r   r   r    r!   export_from_notebookr   tagr{   r   rz   r   r   r`   r   r]   r   r   output_mimetyperh   r	   r;   r>   rt   r   r}   r   r   __classcell__)r   s   @r   r-   r-   .   s    +!"HIMMUYMZK+2X	cc  ,/6YZ^^ _ K 5QRVV^bVcGMNI5BDUWZC[\F'Ov  !" #IV 4? 

\ #  #r   r-   )r!   r%   rR   rY   rW   tempfiler   	traitletsr   r   r   r   r   r	   utilsr   r   r   IOErrorr   r+   r-   r"   r   r   <module>r      sE    
 
   
 ' E E %  ' "Ef#- f#r   