B
    [                 @   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 ddlmZ ddlmZ G dd	 d	eZd
d ZG dd deZdS )zExport to PDF via latex    N)whichcast_bytes_py2getcwd)IntegerListBoolInstanceUnicode)TemporaryWorkingDirectory   )LatexExporterc               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	LatexFailedzVException for failed latex run
    
    Captured latex output is in error.output.
    c             C   s
   || _ d S )N)output)selfr    r   6lib/python3.7/site-packages/nbconvert/exporters/pdf.py__init__   s    zLatexFailed.__init__c             C   s
   d| j  S )Nz.PDF creating failed, captured latex output:
%s)r   )r   r   r   r   __unicode__   s    zLatexFailed.__unicode__c             C   s   |   }t|S )N)r   r   )r   ur   r   r   __str__   s    zLatexFailed.__str__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      s   r   c             C   s*   |sdS t |tj || d || < dS )zAdd value to the environment variable varname in envdict

    e.g. prepend_to_env_search_path('BIBINPUTS', '/home/sally/foo', os.environ)
    N )r   ospathsepget)ZvarnamevalueZenvdictr   r   r   prepend_to_env_search_path   s    r   c                   s   e Zd ZdZdZedddjddZedd	gd
djddZ	edd	gddjddZ
edddjddZeddZeddddidZe Zdd Zdd Zdd Zd fdd	Z  ZS )PDFExporterzWriter 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.
    Zpdf   z$How many times latex will be called.)helpT)ZconfigZxelatexz
{filename}z$Shell command used to compile latex.Zbibtexz!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   Zbuild_directory.)argskwc          
      sr   fdd|D }t jdkr4t jdk r4dd |D }t|d }|dkrbd}td	j|d |d
|dkrndnd}| jd|d ||| t jdk}	|	rt	|}t
j }
td| j|
 td| j|
 td| j|
 tt
jd}| jstjnd}xnt|D ]b}tj||tj||	|
d}| \}}|jr| jr:d}n|dd}||| | j| dS qW W dQ R X dS )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.
        
        Returns
        -------
        success : bool
            A boolean indicating if the command was successful (True)
            or failed (False).
        c                s   g | ]}|j  d qS ))filename)format).0c)r&   r   r   
<listcomp>Y   s    z+PDFExporter.run_command.<locals>.<listcomp>Zwin32)r!   r   c             S   s   g | ]}| d qS )cp1252)encode)r(   r)   r   r   r   r*   a   s    r   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}.)Z	formatterlinkr   ZtimetimeszRunning %s %i %s: %sZ	TEXINPUTSZ	BIBINPUTSZ	BSTINPUTSrb)stdoutstderrstdinshellenvr   zutf-8replaceFT)sysplatformversion_infor   OSErrorr'   loginfo
subprocessZlist2cmdliner   environcopyr   	texinputsopendevnullverbosePIPErangePopenZSTDOUTZcommunicate
returncodedecode_captured_outputappend)r   Zcommand_listr&   countZlog_functioncommandcmdr-   r.   r3   r4   Znullr0   indexpout_r   )r&   r   run_commandF   s>    



zPDFExporter.run_commandc                s     fdd}   j| j|S )z#Run xelatex self.latex_count times.c                s    j d| d | | d S )Nz%s failed: %s
%sr   )r:   Zcritical)rK   rO   )r   r   r   	log_error   s    z(PDFExporter.run_latex.<locals>.log_error)rQ   latex_commandlatex_count)r   r&   rR   r   )r   r   	run_latex   s    
zPDFExporter.run_latexc                s.   t j|d } fdd}  j|d|S )z"Run bibtex self.latex_count times.r   c                s,    j d| d   j d| d | | d S )Nz<%s had problems, most likely because there were no citationsr   z%s output: %s
%s)r:   Zwarningdebug)rK   rO   )r   r   r   rR      s    
z&PDFExporter.run_bib.<locals>.log_errorr   )r   pathsplitextrQ   bib_command)r   r&   rR   r   )r   r   run_bib   s    zPDFExporter.run_bibNc          
      s  t t| j|fd|i|\}}|rF|di drF|d d | _nt | _g | _t  d}| jj	|||d}| j
d | |}|r| |}|r| |}|d }tj|std| j| j
d	 t|d
}	|	 }
W d Q R X W d Q R X d|d< |dd  |
|fS )N	resourcesZmetadatarW   Znotebook)notebook_namezBuilding PDFz.pdf
zPDF successfully createdr/   Zoutput_extensionZoutputs)superr    from_notebook_noder   r?   r   Z_captured_outputsr
   writerwriter:   r;   rU   rZ   r   rW   isfiler   joinrH   r@   readpop)r   Znbr[   r%   latexr\   Ztex_fileZrcZpdf_filefZpdf_data)	__class__r   r   r_      s0    



zPDFExporter.from_notebook_node)N)r   r   r   r   Zexport_from_notebookr   tagrT   r   rS   rY   r   rB   r	   r?   r   r`   rH   rQ   rU   rZ   r_   __classcell__r   r   )rh   r   r    (   s*   
B	r    )r   r<   r   r6   Zipython_genutils.py3compatr   r   r   Z	traitletsr   r   r   r   r	   Ztestpath.tempdirr
   rf   r   IOErrorr   r   r    r   r   r   r   <module>   s   
