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

    Captured latex output is in error.output.
    c                    || _         y)zInitialize the error.Noutput)selfr   s     R/var/www/cvtools/html/venv/lib/python3.12/site-packages/nbconvert/exporters/pdf.py__init__zLatexFailed.__init__   s	        c                     d| j                   z  S )zUnicode representation.z.PDF creating failed, captured latex output:
%sr   r   s    r   __unicode__zLatexFailed.__unicode__   s    @4;;NNr   c                "    | j                         S )zString representation.)r   r   s    r   __str__zLatexFailed.__str__"   s    !!r   N)__name__
__module____qualname____doc__r   r   r    r   r   r   r      s    
O"r   r   c                \    |sy|t         j                  z   |j                  | d      z   || <   y)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-    
 rzz)GKK,DDGGr   c                      e Zd ZdZdZ edd      j                  d      Z eg dd	      j                  d      Z	 ed
dgd      j                  d      Z
 edd      j                  d      Z ed      Z eddddi      ZdZ e 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.bibtexr0   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                     y)N.pdfr    r   s    r   _file_extension_defaultz#PDFExporter._file_extension_defaultO   s    r   template_extensionc                     y)Nz.tex.j2r    r   s    r   _template_extension_defaultz'PDFExporter._template_extension_defaultS   s    r   c           
     :   |D cg c]  }|j                  |       }}t        j                  |d         }|d}	|d    d|d    d|	 d}
t        |
      |dk(  rd	nd
}| j                  j                  d|d   |||       t        j                  dk(  }|rt        j                  |      }t        j                  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-                         \  }}|j.                  sL| j"                  rd}n|j1                  dd      } |||       | j2                  j5                  |       |rd| d| }
 ||
       ddd       y 	 ddd       yc c}w # 1 sw Y   yxY w)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).
        )filenamer   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 r3   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)format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_listr>   countlog_functionraise_on_failureccommandcmdlinkmsgr@   rI   rJ   nullrF   _poutout_strs                       r   run_commandzPDFExporter.run_commandW   s   , 9EE1188X8.EE ll<?+;[D?##Q?# $V1 
 #,1*',l1oueWU' --g6Gjjoo";D";D";D"**d# 	!t,0LLZ__dF5\ !$$!%,, Q<<|| #%"%**Wi"@ #.))009' /yWIN.s33 1	! 	!!	!2 c F0	!2 s   H*A5H AHHHc                `      fd} j                   j                  | j                  ||      S )z#Run xelatex self.latex_count times.c                H    j                   j                  d| d   | |       y )Nz%s failed: %s
%sr   )rP   criticalrj   rq   r   s     r   	log_errorz(PDFExporter.run_latex.<locals>.log_error   s     HH171:wLr   )rs   latex_commandlatex_countr   r>   rh   rx   s   `   r   	run_latexzPDFExporter.run_latex   s6    	M $*:*:IGW
 	
r   c                     t         j                  j                  |      d   } fd} j                   j                  |d||      S )zRun bibtex one time.r   c                    j                   j                  d| d          j                   j                  d| d   | |       y )Nz<%s had problems, most likely because there were no citationsr   z%s output: %s
%s)rP   warningdebugrw   s     r   rx   z&PDFExporter.run_bib.<locals>.log_error   s>    HHNPWXYPZ HHNN.
GSIr   r   )r#   pathsplitextrs   bib_commandr{   s   `   r   run_bibzPDFExporter.run_bib   sD    77##H-a0	J  0 0(AyJZ[[r   c                   t        |   |fd|i|\  }}|rL|j                  di       j                  d      r+t        j                  j                  |d   d         | _        nt        j                         | _        g | _        t               5 }t        j                  |      5  d}d|d<   | j                  j                  |||      }| j                  j                  d       | j!                  |       | j#                  |      r| j!                  |       |d	z   }t        j                  j%                  |      s$t'        d
j)                  | j*                              | j                  j                  d       t-        |d      5 }	|	j/                         }
ddd       ddd       ddd       d	|d<   |j1                  dd       |j1                  dd       
|fS # 1 sw Y   FxY w# 1 sw Y   JxY w# 1 sw Y   NxY w)zConvert from notebook node.	resourcesmetadatar   notebookz.texoutput_extension)notebook_namezBuilding PDFr8   
zPDF successfully createdrE   Noutputsattachments)superfrom_notebook_noder%   r#   r   abspathrX   getcwd_captured_outputsr   r   chdirwriterwriterP   rQ   r|   r   isfiler   joinrc   rY   readpop)r   nbr   r5   latextdr   tex_filepdf_filefpdf_data	__class__s              r   r   zPDFExporter.from_notebook_node   s    75bTITQSTyz26::6BWW__Yz-B6-JKDNYY[DN!#! 	$R):)@)@)D 	$&M,2I(){{((	(WHHHMM.)NN8$||H%x($v-H77>>(+!$))D,A,A"BCCHHMM45h% $668$	$ 	$$ )/	$% 	i&mT*""$ $	$ 	$ 	$ 	$s=   G6%C#G*GG*!G6G'#G**G3	/G66G?)N)F)r   r   r   r   export_from_notebookr   tagrz   r   ry   r   r   r[   r	   rX   r   r   output_mimetyperc   r
   r9   r<   rs   r   r|   r   r   __classcell__)r   s   @r   r+   r+   2   s    +!"HIMMUYMZK+2X	cc  ,/6YZ^^ _ K 5QRVV^bVcGMNI5BDUWZC[\F'OGI  !" #GR 4? 

\"# "#r   r+   )r   
__future__r   r#   rM   rT   rR   tempfiler   	traitletsr   r   r   r   r	   r
   nbconvert.utilsr   r   r   IOErrorr   r)   r+   r    r   r   <module>r      sH     # 	   
 ' E E -  "' "&Ef#- f#r   