a
    ø£Beü2  ã                   @   sH  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 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 ed	d
„ ej d¡dd… D ƒƒdk rÈddlmZ nddlmZ ddlmZ ddl m!Z! ddl"m#Z# ddl$m%Z%m&Z& ddl'm(Z( ddl)m*Z* ddl+m,Z, dd„ Z-G dd„ de,ƒZ.dS )zHTML Exporter classé    N)ÚPath)ÚAnyÚDictÚOptionalÚTuple)ÚBeautifulSoup)Újupyter_path)ÚBoolÚUnicodeÚdefaultÚvalidate)ÚConfigc                 c   s   | ]}t |ƒV  qd S )N)Úint)Ú.0Úx© r   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/nbconvert/exporters/html.pyÚ	<genexpr>   ó    r   Ú.é   )r   r   r   )Úcontextfilter)Úpass_context)Úsplit_template_path)ÚNotebookNode)ÚHighlight2HTML)ÚIPythonRendererÚMarkdownWithMath)ÚWidgetsDataTypeFilter)Úiso639_1é   )ÚTemplateExporterc              
   C   s
  t dƒ}g }d}|D ]¤}t |¡D ]”\}}}d|v r"d|v r"tt|ƒd dd��"}t | ¡ ¡d }	W d  ƒ n1 sx0    Y  |	| ks˜| |	 d¡v r"| 	|	¡ |	}
t|ƒd |	 }q"qt
|ƒd	krÚd
| › d�}t|ƒ‚t
|ƒdk�rd| › d|› d�}t|ƒ‚|
|fS )aÁ  
    Find a JupyterLab theme location by name.

    Parameters
    ----------
    theme_name : str
        The name of the labextension theme you want to find.

    Raises
    ------
    ValueError
        If the theme was not found, or if it was not specific enough.

    Returns
    -------
    theme_name: str
        Full theme name (with scope, if any)
    labextension_path : Path
        The path to the found labextension on the system.
    ZlabextensionsNzpackage.jsonÚthemeszutf-8)ÚencodingÚnameú/r   zCould not find lab theme "ú"r    z Found multiple themes matching "z": z<. Please be more specific about which theme you want to use.)r   ÚosÚwalkÚopenr   ÚjsonÚloadsÚreadÚsplitÚappendÚlenÚ
ValueError)Z
theme_nameÚpathsZmatching_themesÚ
theme_pathÚpathÚdirpathÚdirnamesÚ	filenamesZfobjZlabext_nameZfull_theme_nameÚmsgr   r   r   Úfind_lab_theme$   s(    0
ÿr8   c                	       s²  e Zd ZdZdZeddd�jdd�Zedd	d�jdd�Z	ed
dd�jdd�Z
eddd�jdd�Zeddd�Zeddd�jdd�Zeddd�jdd�Zeddd�jdd�Zeddd�jdd�Zedƒdd„ ƒZedƒdd„ ƒZeddd�jdd�Zedd d�jdd�Zedd!d�jdd�Zd"Ze‡ fd#d$„ƒZed%d&d�jdd�Zed'ƒd(d)„ ƒZed*d+„ ƒZ‡ fd,d-„Zd4e e!e"e#e$f  e$e%e#e"e#e$f f d/œ‡ fd0d1„Z&‡ fd2d3„Z'‡  Z(S )5ÚHTMLExportera'  
    Exports a basic HTML document.  This exporter assists with the export of
    HTML.  Inherit from it if you are writing your own HTML template and need
    custom preprocessors/filters.  If you don't need custom preprocessors/
    filters, just change the 'template_file' config option.
    ÚHTMLõ   Â¶z+The text used as the text for anchor links.)ÚhelpT)ÚconfigFz*If anchor links should be included or not.zGhttps://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.10/require.min.jszW
        URL to load require.js from.

        Defaults to loading from cdnjs.
        z]https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS_CHTML-full,SafezT
        URL to load Mathjax from.

        Defaults to loading from cdnjs.
        zIhttps://cdnjs.cloudflare.com/ajax/libs/mermaid/10.6.0/mermaid.esm.min.mjszV
        URL to load MermaidJS from.

        Defaults to loading from cdnjs.
        zAhttps://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.jszS
        URL to load jQuery from.

        Defaults to loading from cdnjs.
        zhttps://unpkg.com/zURL base for Jupyter widgetsÚ zFull URL for Jupyter widgetsÚ*z-Semver range for Jupyter widgets HTML managerZfile_extensionc                 C   s   dS )Nz.htmlr   ©Úselfr   r   r   Ú_file_extension_defaultš   s    z$HTMLExporter._file_extension_defaultZtemplate_namec                 C   s   dS )NZlabr   r@   r   r   r   Ú_template_name_defaultž   s    z#HTMLExporter._template_name_defaultZlightzwTemplate specific theme(e.g. the name of a JupyterLab CSS theme distributed as prebuilt extension for the lab template)z€Whether the HTML in Markdown cells and cell outputs should be sanitized.This should be set to True by nbviewer or similar tools.z;Whether or not to embed images as base64 in markdown cells.ú	text/htmlc                    s@   t dg d¢iddidœƒ}tƒ jr<tƒ j ¡ }| |¡ |}|S )NZdisplay_data_priority)
z(application/vnd.jupyter.widget-view+jsonzapplication/javascriptrD   ztext/markdownzimage/svg+xmlztext/vnd.mermaidz
text/latexz	image/pngz
image/jpegz
text/plainÚenabledT)ZNbConvertBaseZHighlightMagicsPreprocessor)r   ÚsuperÚdefault_configÚcopyÚmerge)rA   ÚcÚc2©Ú	__class__r   r   rG   µ   s    ÿñÿ
zHTMLExporter.default_configÚenz;Language code of the content, should be one of the ISO639-1Úlanguage_codec                 C   s2   | j tvr*| j d| j › d�¡ |d jS |d S )Nr&   zT" is not an ISO 639-1 language code. It has been replaced by the default value "en".ZtraitÚvalue)rO   r   ÚlogÚwarningÚdefault_value)rA   Zproposalr   r   r   Ú_valid_language_codeÔ   s    
ÿ
z!HTMLExporter._valid_language_codec                 C   s^   |  di ¡}|  di ¡}|  di ¡  di ¡  dd¡}td|| j|| j| jd�}t|d	� |¡S )
z?Markdown to HTML filter respecting the anchor_link_text settingÚcellÚattachmentsÚ	resourcesÚmetadatar3   r>   F)ÚescaperV   Úembed_imagesr3   Úanchor_link_textÚexclude_anchor_links)Úrenderer)Úgetr   rZ   r[   r\   r   Úrender)rA   ÚcontextÚsourcerU   rV   r3   r]   r   r   r   Úmarkdown2htmlÞ   s    úzHTMLExporter.markdown2htmlc                 #   s    t ƒ  ¡ E dH  d| jfV  dS )zGet the default filters.Nrb   )rF   Údefault_filtersrb   r@   rL   r   r   rc   ï   s    zHTMLExporter.default_filtersN)ÚnbrW   ÚkwÚreturnc                    s  |j  di ¡}| d| dd¡¡}| j dt|| d�¡}t| j| |d�}|  d|¡ |  d|¡ tƒ j||fi |¤Ž\}}t	|d	d
�}	d}
|	 
d¡D ]}d|jd< |
d7 }
q”|
rÄ| j d|
› d�¡ |	 
d¡D ]}d|jd< qÎ|	 
d¡D ]}d|jd< qèt|	ƒ|fS )zConvert from notebook node.Zlanguage_infoÚpygments_lexerr$   NÚhighlight_code)rg   Úparent)Znotebook_metadatari   rW   Úfilter_data_typezhtml.parser)Úfeaturesr   zimg:not([alt])z/No description has been provided for this imageZaltr    zAlternative text is missing on z
 image(s).z%.jp-Notebook div.jp-Cell-inputWrapperÚ0Ztabindexz%.jp-Notebook div.jp-OutputArea-output)rX   r^   Úfiltersr   r   Z_nb_metadataZregister_filterrF   Úfrom_notebook_noder   ÚselectÚattrsrQ   rR   Ústr)rA   rd   rW   re   ZlanginfoÚlexerrh   rj   ÚhtmlZsoupZmissing_altÚelemrL   r   r   rn   ô   s.    ÿÿ

zHTMLExporter.from_notebook_nodec                    sÀ   ‡ fdd„}dd„ }‡ fdd„}‡ fdd„}t ƒ  |¡}ˆ j|d	< ||d
< ||d< ||d< ||d< ˆ j|d< ˆ j|d< ˆ j|d< ˆ j|d< ˆ j|d< ˆ j|d< ˆ j	|d< ˆ j
|d< ˆ j|d< |S )Nc                    s&   ˆ j }d|j || ¡d  }t |¡S )Nú"<style type="text/css">
%s</style>r   ©ÚenvironmentÚloaderÚ
get_sourceÚ
markupsafeÚMarkup©r$   ÚenvÚcoder@   r   r   Úresources_include_css  s    z;HTMLExporter._init_resources.<locals>.resources_include_cssc              
   S   sô   t | ƒ\}}t|d ƒ�}| ¡ }W d   ƒ n1 s60    Y  t |¡D ]–}dt|ƒ ¡ › d�}||v rJt |¡d }t|| dƒ�J}t	 
| ¡ ¡}	|	 dd¡ d¡}
| |d	|› d
|
› d�¡}W d   ƒ qJ1 sÖ0    Y  qJd| }t |¡S )Nz	index.csszurl(ú)r   Úrbó   
r   Úasciiz	url(data:ú;base64,ru   )r8   r)   r,   r'   Úlistdirr   Úas_posixÚ	mimetypesÚ
guess_typeÚbase64Ú	b64encodeÚreplaceÚdecoderz   r{   )r$   Ú_r2   ÚfileÚdataZassetZ	local_urlÚ	mime_typeZ	assetfileZbase64_dataZ
base64_strr~   r   r   r   Úresources_include_lab_theme  s    &:zAHTMLExporter._init_resources.<locals>.resources_include_lab_themec                    s&   ˆ j }d|j || ¡d  }t |¡S )z(Get the resources include JS for a name.z<script>
%s</script>r   rv   r|   r@   r   r   Úresources_include_js3  s    z:HTMLExporter._init_resources.<locals>.resources_include_jsc                    s  ˆ j }t | ¡\}}z|j || ¡d  d¡}W n  tyÐ   t| ƒ}ˆ jD ]f}t	j
j|g|¢R Ž }t	j
 |¡rLt|dƒ�(}| ¡ }W d  ƒ  qÌW d  ƒ qL1 s¨0    Y  qLd| ›d|›�}	t|	ƒ‚Y n0 t |¡}| dd¡ d	¡}d
|› d|› �}
t |
¡S )z)Get the resources include url for a name.r   Úutf8r�   NzNo file z
 found in r‚   r   rƒ   zdata:r„   )rw   r‡   rˆ   rx   ry   ÚencodeÚUnicodeDecodeErrorr   Ztemplate_pathsr'   r3   ÚjoinÚexistsr)   r,   r0   r‰   rŠ   r‹   rŒ   rz   r{   )r$   r}   r�   r#   r�   ÚpiecesZ
searchpathÚfilenameÚfr7   Úsrcr@   r   r   Úresources_include_url9  s$    
0
z;HTMLExporter._init_resources.<locals>.resources_include_urlÚthemeZinclude_cssZinclude_lab_themeZ
include_jsZinclude_urlÚrequire_js_urlÚmathjax_urlÚmermaid_js_urlÚ
jquery_urlÚjupyter_widgets_base_urlÚwidget_renderer_urlÚhtml_manager_semver_rangeZshould_sanitize_htmlrO   )rF   Ú_init_resourcesr�   rž   rŸ   r    r¡   r¢   r£   r¤   Úsanitize_htmlrO   )rA   rW   r   r‘   r’   rœ   rL   r@   r   r¥     s(    









zHTMLExporter._init_resources)N))Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zexport_from_notebookr
   Útagr[   r	   r\   rž   rŸ   r    r¡   r¢   r£   r¤   r   rB   rC   r�   r¦   rZ   Zoutput_mimetypeÚpropertyrG   rO   r   rT   r   rb   rc   r   r   r   rq   r   r   rn   r¥   Ú__classcell__r   r   rL   r   r9   Z   s¢   ÿÿþù	þù	þ	þù	ÿþÿþ

þýýúÿþÿþ
	
 ÿþ!r9   )/rª   r‰   r*   r‡   r'   Úpathlibr   Útypingr   r   r   r   Zjinja2rz   Zbs4r   Zjupyter_core.pathsr   Z	traitletsr	   r
   r   r   Ztraitlets.configr   ÚtupleÚ__version__r-   r   r   Zjinja2.loadersr   Znbformatr   Znbconvert.filters.highlightr   Z"nbconvert.filters.markdown_mistuner   r   Z'nbconvert.filters.widgetsdatatypefilterr   Znbconvert.utils.iso639_1r   Ztemplateexporterr!   r8   r9   r   r   r   r   Ú<module>   s0   &6