a
    ø£Be  ã                   @  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   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/nbconvert/exporters/pdf.pyÚ__init__   s    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__   s    zLatexFailed.__unicode__c                 C  s   |   ¡ }|S )zString representation.)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 j | | d¡ || < dS )zŽAdd 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U 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ƒ Zded< 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 )*ÚPDFExporterzÐWriter 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	List[str]Ú_captured_outputZfile_extensionc                 C  s   dS )Nú.pdfr   r   r   r   r   Ú_file_extension_defaultP   s    z#PDFExporter._file_extension_defaultZtemplate_extensionc                 C  s   dS )Nz.tex.j2r   r   r   r   r   Ú_template_extension_defaultT   s    z'PDFExporter._template_extension_defaultNc              
     s   ‡ fdd„|D ƒ}t  |d ¡}|du rRd}|d › d|d › d|› d�}	t|	ƒ‚|d	kr^d
nd}
| j d|d ||
|¡ tjdk}|r’t |¡}t	j
 ¡ }td| j|ƒ td| j|ƒ td| j|ƒ tt	jdƒ�¸}| jsàtjnd}t|ƒD ]Ž}tj||tj|||d�}| ¡ \}}|jrì| j�r(d}n| dd¡}|||ƒ | j |¡ |�rhd|› d|› �}	||	ƒ‚ W d  ƒ dS qìW d  ƒ n1 �s’0    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Úcr0   r   r   Ú
<listcomp>p   ó    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Údecoder,   Úappend)r   Zcommand_listr1   ÚcountZlog_functionÚraise_on_failureÚcommandÚcmdÚlinkÚmsgr8   r>   r?   Únullr;   Ú_ÚpÚoutZout_strr   r0   r   Úrun_commandX   sV    
ÿþÿ


ú
4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©rZ   ra   r   r   r   Ú	log_error¦   s    z(PDFExporter.run_latex.<locals>.log_error)rb   Úlatex_commandÚlatex_count©r   r1   rY   re   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Údebugrd   r   r   r   re   ±   s    ÿz&PDFExporter.run_bib.<locals>.log_errorr   )r   ÚpathÚsplitextrb   Úbib_commandrh   r   r   r   Úrun_bib­   s    zPDFExporter.run_bibc              
     s‚  t ƒ j|fd|i|¤Ž\}}|rJ| di ¡ d¡rJtj |d d ¡| _n
t ¡ | _g | _t	ƒ �è}t
 |¡�¼ d}d|d< | jj|||d�}| j d¡ |  |¡ |  |¡r¶|  |¡ |d	 }tj |¡sÚtd
 | j¡ƒ‚| j d¡ t|dƒ�}	|	 ¡ }
W d  ƒ n1 �s0    Y  W d  ƒ n1 �s00    Y  W d  ƒ n1 �sP0    Y  d	|d< | dd¡ | dd¡ |
|fS )zConvert from notebook node.Ú	resourcesÚmetadatarl   Únotebookz.texZoutput_extension)Únotebook_namezBuilding PDFr-   Ú
zPDF successfully createdr:   NÚoutputsÚattachments)ÚsuperÚfrom_notebook_noder   r   rl   ÚabspathrL   ÚgetcwdZ_captured_outputsr   r
   ÚchdirÚwriterÚwriterD   rE   ri   ro   Úisfiler   Újoinr,   rM   ÚreadÚpop)r   Únbrp   r+   ÚlatexÚtdrs   Ztex_fileZpdf_fileÚfZpdf_data©Ú	__class__r   r   rx   ¹   s.    



hzPDFExporter.from_notebook_node)N)F)N)r   r   r   r   Zexport_from_notebookr   Útagrg   r   rf   rn   r   rO   r   rL   r   r|   Zoutput_mimetyper,   Ú__annotations__r	   r.   r/   rb   r   ri   ro   rx   Ú__classcell__r   r   r†   r   r#   3   s2   
ÿþÿ


 ÿ
K

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    