o
    i                     @  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   R/var/www/edux/Edux_v2/venv/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value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)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   build_directory.)argskwzapplication/pdffile_extensionc                 C     dS )N.pdfr   r   r   r   r   _file_extension_defaultO      z#PDFExporter._file_extension_defaulttemplate_extensionc                 C  r0   )Nz.tex.j2r   r   r   r   r   _template_extension_defaultS   r3   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cr6   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win32	TEXINPUTS	BIBINPUTS	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   command_listr7   countlog_functionraise_on_failurecommandcmdlinkmsgr=   rF   rG   nullrC   _poutout_strr   r6   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   )rL   criticalre   rl   r   r   r   	log_error   s   z(PDFExporter.run_latex.<locals>.log_error)rn   latex_commandlatex_countr   r7   rd   rq   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)rL   warningdebugrp   r   r   r   rq      s   z&PDFExporter.run_bib.<locals>.log_errorr   )r   pathsplitextrn   bib_commandrt   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metadatarx   notebookz.texoutput_extension)notebook_namezBuilding PDFr1   
zPDF successfully createdrB   Noutputsattachments)superfrom_notebook_noder   r   rx   abspathrT   getcwd_captured_outputsr   r
   chdirwriterwriterL   rM   ru   r{   isfiler   joinr_   rU   readpop)r   nbr|   r.   latextdr   tex_filepdf_filefpdf_data	__class__r   r   r      s8   




 zPDFExporter.from_notebook_node)N)F)r   r   r   r   export_from_notebookr   tagrs   r   rr   rz   r   rW   r   rT   r   r   output_mimetyper_   r	   r2   r5   rn   r   ru   r{   r   __classcell__r   r   r   r   r$   2   s2    




I

r$   )r   
__future__r   r   rI   rP   rN   tempfiler   	traitletsr   r   r   r   r   r	   nbconvert.utilsr
   r   r   IOErrorr   r#   r$   r   r   r   r   <module>   s     