o
    i                     @   s   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 ddl	m
Z
mZmZmZ ddlmZ ddlmZ d	Zd
ZejdkrSzddlZW n eyR   ddlZY nw G dd deZdS )zcModule containing a preprocessor that converts outputs in the notebook from
one format to another.
    N)which)TemporaryDirectory)ListUnicodeUniondefault)FormatSafeDict   )ConvertFiguresPreprocessorz:/Applications/Inkscape.app/Contents/Resources/bin/inkscapez2/Applications/Inkscape.app/Contents/MacOS/inkscapewin32c                   @   s   e Zd ZdZeddd Zeddd Zedd	jd
dZ	eddd Z
ee e gdd	jd
dZeddd Zedd	jd
dZeddd Zdd ZdS )SVG2PDFPreprocessorzD
    Converts all of the outputs in a notebook from SVG to PDF.
    from_formatc                 C      dS )Nzimage/svg+xml selfr   r   Z/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/nbconvert/preprocessors/svg2pdf.py_from_format_default'      z(SVG2PDFPreprocessor._from_format_default	to_formatc                 C   r   )Nzapplication/pdfr   r   r   r   r   _to_format_default+   r   z&SVG2PDFPreprocessor._to_format_defaultzeThe version of inkscape being used.

        This affects how the conversion command is run.
        )helpT)configinkscape_versionc                 C   sP   t j| jdgt jt jd}| \}}|jdkrd}t||ddd S )Nz	--version)stdoutstderrr   z,Unable to find inkscape executable --versionutf-8 r	   )	
subprocessPopeninkscapePIPEcommunicate
returncodeRuntimeErrordecodesplit)r   poutput_msgr   r   r   _inkscape_version_default6   s   
z-SVG2PDFPreprocessor._inkscape_version_defaulta  
        The command to use for converting SVG to PDF

        This traitlet is a template, which will be formatted with the keys
        to_filename and from_filename.

        The conversion call must read the SVG from {from_filename},
        and write a PDF to {to_filename}.

        It could be a List (recommended) or a String. If string, it will
        be passed to a shell for execution.
        commandc                 C   sR   | j dd }| jg}t|dk r|d |d n|d |d |S )N.r   r	   z--without-guiz--export-pdf={to_filename}z--export-filename={to_filename}z{from_filename})r   r&   r    intappend)r   major_versionr,   r   r   r   _command_defaultT   s   


z$SVG2PDFPreprocessor._command_defaultz"The path to Inkscape, if necessaryr    c                 C   s   t d}|d ur
|S tjdkrtjtrtS tjtrtS tjdkrKt	d tj
}zt|d}t|dd }W |S  tyJ   d}t|d w dS )Nr    darwinr   z)SOFTWARE\Classes\inkscape.svg\DefaultIcon r   zInkscape executable not found)r   sysplatformospathisfileINKSCAPE_APP_v1INKSCAPE_APPwinregConnectRegistryHKEY_LOCAL_MACHINEOpenKeyQueryValueExFileNotFoundError)r   inkscape_path	wr_handlerkeyr    r*   r   r   r   _inkscape_defaultf   s&   


z%SVG2PDFPreprocessor._inkscape_defaultc           	   	      s2  t  }tj|d}t|ddd}|| W d   n1 s"w   Y  tj|d}||d t| jtrD fdd	| jD }n| jj	  }t
j|t|td
 tj|rt|d}t| dW  d   W  d   S 1 s{w   Y  nd}t|W d   dS 1 sw   Y  dS )zN
        Convert a single SVG figure to PDF.  Returns converted data.
        z
figure.svgwutf8)encodingNz
figure.pdf)from_filenameto_filenamec                    s    g | ]}| td i  qS )r   )
format_mapr   ).0stemplate_varsr   r   
<listcomp>   s     z6SVG2PDFPreprocessor.convert_figure.<locals>.<listcomp>)shellrbr   z%Inkscape svg to pdf conversion failed)r   r6   r7   joinopenwrite
isinstancer,   listformatr   callstrr8   base64encodebytesreadr%   	TypeError)	r   data_formatdatatmpdirinput_filenamefoutput_filenamefull_cmdr*   r   rM   r   convert_figure}   s*   
"z"SVG2PDFPreprocessor.convert_figureN)__name__
__module____qualname____doc__r   r   r   r   tagr   r+   r   r   r,   r1   r    rD   re   r   r   r   r   r   "   s2    





r   )ri   rZ   r6   r   r4   shutilr   tempfiler   	traitletsr   r   r   r   nbconvert.utils.ior   convertfiguresr
   r:   r9   r5   r;   ImportError_winregr   r   r   r   r   <module>   s&    
