o
    tf                     @  s   d Z ddlm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
 d
eZdd ZG dd deZdS )zExport to PDF via latex    )annotationsN)TemporaryDirectory)BoolInstanceIntegerListUnicodedefault)_contextlib_chdir   )LatexExporterc                   @  s(   e Zd ZdZdd Zdd Zdd ZdS )	LatexFailedzRException for failed latex run

    Captured latex output is in error.output.
    c                 C  s
   || _ dS )zInitialize the error.Noutput)selfr    r   `/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/nbconvert/exporters/pdf.py__init__      
zLatexFailed.__init__c                 C  s
   d| j  S )zUnicode representation.z.PDF creating failed, captured latex output:
%sr   r   r   r   r   __unicode__   r   zLatexFailed.__unicode__c                 C  s   |   S )zString representation.)r   r   r   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 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 )ospathsepget)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g d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dZee Zeddd Zeddd Zd%ddZefdd Zd&d!d"Zd% fd#d$	Z  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)Zxelatex
{filename}z-quietz$Shell command used to compile latex.bibtexr'   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   Zbuild_directory.)argskwzapplication/pdfZfile_extensionc                 C     dS )N.pdfr   r   r   r   r   _file_extension_defaultO      z#PDFExporter._file_extension_defaultZtemplate_extensionc                 C  r,   )Nz.tex.j2r   r   r   r   r   _template_extension_defaultS   r/   z'PDFExporter._template_extension_defaultNc              
     s   fdd|D }t |d }|du r)d}|d  d|d  d| d}	t|	|d	kr/d
nd}
| jd|d ||
| tjdk}|rIt|}t	j
 }td| j| td| j| td| j| tt	jd[}| jsptjnd}t|D ]E}tj||tj|||d}| \}}|jr| jrd}n|dd}||| | j| |rd| d| }	||	 W d   dS qvW d   dS 1 sw   Y  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.
        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).
        c                   s   g | ]}|j  d qS )filename)format).0cr1   r   r   
<listcomp>m   s    z+PDFExporter.run_command.<locals>.<listcomp>r   NzFhttps://nbconvert.readthedocs.io/en/latest/install.html#installing-texz. not found on PATH, if you have not installed z5 you may need to do so. Find further instructions at r)   r   timetimeszRunning %s %i %s: %swin32Z	TEXINPUTSZ	BIBINPUTSZ	BSTINPUTSrb)stdoutstderrstdinshellenvr   zutf-8replacezFailed to run "z" command:
FT)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   Zcommand_listr2   countZlog_functionraise_on_failurecommandcmdlinkmsgr8   r>   r?   nullr;   _poutZout_strr   r1   r   run_commandW   sb   





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   )rD   criticalr[   rb   r   r   r   	log_error   s   z(PDFExporter.run_latex.<locals>.log_error)rc   latex_commandlatex_countr   r2   rZ   rf   r   r   r   	run_latex   s   zPDFExporter.run_latexc                   s0   t j|d } fdd}  j|d||S )zRun bibtex one time.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)rD   warningdebugre   r   r   r   rf      s   z&PDFExporter.run_bib.<locals>.log_errorr   )r   pathsplitextrc   bib_commandri   r   r   r   run_bib   s   zPDFExporter.run_bibc              
     s|  t  j|fd|i|\}}|r%|di dr%tj|d d | _nt | _g | _t	 r}t
|] d}d|d< | jj|||d}| jd | | | |r[| | |d	 }tj|smtd
| j| jd t|d}	|	 }
W d   n1 sw   Y  W d   n1 sw   Y  W d   n1 sw   Y  d	|d< |dd |dd |
|fS )zConvert from notebook node.	resourcesmetadatarm   notebookz.texZoutput_extension)notebook_namezBuilding PDFr-   
zPDF successfully createdr:   NZoutputsattachments)superfrom_notebook_noder   r   rm   abspathrL   getcwdZ_captured_outputsr   r
   chdirwriterwriterD   rE   rj   rp   isfiler   joinrW   rM   readpop)r   nbrq   r+   latextdrt   Ztex_fileZpdf_filefZpdf_data	__class__r   r   rx      s8   




 zPDFExporter.from_notebook_node)N)F)r   r   r   r   Zexport_from_notebookr   tagrh   r   rg   ro   r   rO   r   rL   r   r|   Zoutput_mimetyperW   r	   r.   r0   rc   r   rj   rp   rx   __classcell__r   r   r   r   r#   2   s2    




I

r#   )r   
__future__r   r   rA   rH   rF   tempfiler   Z	traitletsr   r   r   r   r   r	   Znbconvert.utilsr
   r   r   IOErrorr   r"   r#   r   r   r   r   <module>   s     