
    :Mc%                         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dS )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dS )LatexFailedzRException for failed latex run

    Captured latex output is in error.output.
    c                     || _         d S Noutput)selfr   s     7lib/python3.11/site-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                 .    |                                  }|S r   )r   )r   us     r   __str__zLatexFailed.__str__   s    r   N)__name__
__module____qualname____doc__r   r   r    r   r   r   r      sN         
  O O O    r   r   c                 `    |sdS |t           j        z   |                    | d          z   || <   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envdicts      r   prepend_to_env_search_pathr+   #   s;    
  rz)GKK,D,DDGGr   c                       e Zd ZdZdZ edd                              d          Z eg dd	                              d          Z	 ed
dgd                              d          Z
 edd                              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                     dS )N.pdfr"   r   s    r   _file_extension_defaultz#PDFExporter._file_extension_defaultK   s    vr   template_extensionc                     dS )Nz.tex.j2r"   r   s    r   _template_extension_defaultz'PDFExporter._template_extension_defaultO   s    yr   Nc           
      @   fd|D             }t          j        |d                   }|,d}t          d                    |d         |                    |dk    rdnd	}	| j                            d
|d         ||	|           t          j        dk    }
|
rt          j	        |          }t          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        rv| j        rd}n|                    dd          } |||           | j                            |           |r  |d                    ||                     ddd           dS 	 ddd           n# 1 swxY w 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                 <    g | ]}|                                S ))filename)format).0crA   s     r   
<listcomp>z+PDFExporter.run_command.<locals>.<listcomp>i   s'    EEE1188X8..EEEr   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}.)	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)shutilwhichOSErrorrB   loginfosysplatform
subprocesslist2cmdliner%   environcopyr+   	texinputsopendevnullverbosePIPErangePopenSTDOUTcommunicate
returncodedecode_captured_outputappend)r   command_listrA   countlog_functionraise_on_failurerU   cmdrG   rI   rR   rS   nullrO   _pouts     `              r   run_commandzPDFExporter.run_commandS   s   , FEEEEEE l<?++ 	[D#Vl1oDVII    1*1',l1oueWUUU' 	7 -g66Gjoo";DDD";DDD";DDD"*d## 	!t,0LBZ__dF5\\ ! !$!%,   Q< !| = !!jj)<< L#...)00555' ..JQQ(/ R    
 !7	! 	! 	! 	! 	! 	! 	! 	!!!	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!8 ts   &CHHHHc                 T      fd}                       j        | j        ||          S )z#Run xelatex self.latex_count times.c                 N    j                             d| d         | |           d S )Nz%s failed: %s
%sr   )rY   criticalrU   rv   r   s     r   	log_errorz(PDFExporter.run_latex.<locals>.log_error   s*    H171:wLLLLLr   )rw   latex_commandlatex_countr   rA   rq   r|   s   `   r   	run_latexzPDFExporter.run_latex   sM    	M 	M 	M 	M 	M $*:IGW
 
 	
r   c                      t           j                            |          d         } fd}                      j        |d||          S )zRun bibtex one time.r   c                     j                             d| d                    j                             d| d         | |           d S )Nz<%s had problems, most likely because there were no citationsr   z%s output: %s
%s)rY   warningdebugr{   s     r   r|   z&PDFExporter.run_bib.<locals>.log_error   sP    HNPWXYPZ   HNN.
GSIIIIIr   r   )r%   pathsplitextrw   bib_commandr   s   `   r   run_bibzPDFExporter.run_bib   s]    7##H--a0	J 	J 	J 	J 	J  0(AyJZ[[[r   c                     t                      j        |fd|i|\  }}|r=|                    di                               d          r|d         d         | _        nt	          j                    | _        g | _        t                      5 }t          j	        |          5  d}d|d<   | j
                            |||          }| j                            d           |                     |           |                     |          r|                     |           |d	z   }t          j                            |          s't%          d
                    | j                            | j                            d           t+          |d          5 }	|	                                }
d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d	|d<   |                    dd            |
|fS )N	resourcesmetadatar   notebookz.texoutput_extension)notebook_namezBuilding PDFr:   
zPDF successfully createdrN   outputs)superfrom_notebook_noder'   ra   r%   getcwd_captured_outputsr   r   chdirwriterwriterY   rZ   r   r   r   isfiler   joinrl   rb   readpop)r   nbr   r7   latextdr   tex_filepdf_filefpdf_data	__class__s              r   r   zPDFExporter.from_notebook_node   s   55775bTTITQSTTy 	)z266::6BB 	)&z26:DNNY[[DN!#!! 	$R):)@)D)D 	$ 	$&M,2I(){((	(WWHHMM.)))NN8$$$||H%% )x((($v-H7>>(++ D!$))D,A"B"BCCCHMM4555h%% $6688$ $ $ $ $ $ $ $ $ $ $ $ $ $ $	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$$ )/	$% 	i&&&""s[   G$"C3GF6*G6F::G=F:>GG$G	G$G	G$$G(+G(r   )F)r   r   r    r!   export_from_notebookr   tagr~   r   r}   r   r   rd   r   ra   r   r   output_mimetyperl   r	   r;   r>   rw   r   r   r   r   __classcell__)r   s   @r   r-   r-   .   s         +'!"HIIIMMUYMZZKD+++2X  	cc  $,/6YZZZ^^ _  K d5QRRRVV^bVccGMNNNIX5BDUWZC[\\\F'OtvvW   W!""  #"I I I IV 4? 
 
 
 

\ 
\ 
\ 
\ #  #  #  #  #  #  #  #  #  #r   r-   )r!   r%   rV   r]   r[   tempfiler   	traitletsr   r   r   r   r   r	   utilsr   r   r   IOErrorr   r+   r-   r"   r   r   <module>r      s    
 
			      



 ' ' ' ' ' ' E E E E E E E E E E E E E E E E % % % % % %                '   "E E Ef# f# f# f# f#- f# f# f# f# f#r   