
    
8d!                         d Z ddlZddlZddlZddlmZ  ej        d          Z ej        d          Z G d de	          Z
dd	Zdd
ZddZd ZdS )zG Defines classes and functions for working with Qt's rich text system.
    N)	QtWidgetsz<p[^/>]*>\s*</p>z<img src="(?P<name>[\d]+)" />c                       e Zd ZdZd Zd ZdS )HtmlExporterzs A stateful HTML exporter for a Q(Plain)TextEdit.

    This class is designed for convenient user interaction.
    c                     t          |t          j        t          j        f          sJ || _        d| _        d| _        d| _        dS )zA Creates an HtmlExporter for the given Q(Plain)TextEdit.
        zipython.htmlN)
isinstancer   QPlainTextEdit	QTextEditcontrolfilename	image_tag
inline_png)selfr
   s     3lib/python3.11/site-packages/qtconsole/rich_text.py__init__zHtmlExporter.__init__!   sE     'I$<i>Q#RSSSSS&    c                 `   | j                                         }t          j        |d          }|                    t          j        j                   ddg}|                    |           | j        rr|                    | j                   t          j
                            | j                  \  }}|                                dv r|                    |d                    |                                r3|                                d         | _        |                                }| j                                                                         }|                    d          r	t(          }n-| j        t,                              |          rt          j        |          }|                    d           t          j        |          }	d	}
d
}t          j        d          }|                    d           t          j        d          }|                    d           t          j        d          }|                    d           t          j        t          j        j        |                                 |
          }|!                    |           |"                    |t          j        j#                   |"                    |t          j        j$                   |	%                    d           |	&                    |           |	&                    |           |'                    |	           |(                                 |                                }|)                                 |dk    |*                                r| _        fd}	  ||| j        | j+                  S # tX          $ re}d| j        z  t[          |          z   }
t          j        .                    |d|
t          j        j/        t          j        j/                  }Y d}~nd}~ww xY wdS )z Displays a dialog for exporting HTML generated by Qt's rich text
        system.

        Returns
        -------
        The name of the file that was saved, or None if no file was saved.
        z
Save as...z$HTML with PNG figures (*.html *.htm)z-XHTML with inline SVG figures (*.xhtml *.xml))z.xmlz.xhtmlr   XHTMLNzExporting HTML with PNGszLWould you like inline PNGs (single large html file) or external image files?z&Don't ask againDz&InlineIz	&ExternalEc                 (    t          | ||          S N)export_html)hfiinlines      r   <lambda>z%HtmlExporter.export.<locals>.<lambda>h   s    ;q!Q+G+G r   zError exporting HTML to %s
Error)0r
   windowr   QFileDialogsetAcceptMode
AcceptSavesetNameFiltersr   
selectFileospathsplitextlowerselectNameFilterexec_selectedFilesselectedNameFilterdocumenttoHtml
startswithexport_xhtmlr   IMG_REsearchQDialogsetWindowTitleQVBoxLayout	QCheckBoxsetShortcutQPushButtonQMessageBoxQuestionwindowTitlesetInformativeText	addButtonNoRoleYesRole
setSpacing	addWidget	setLayoutshowhide
checkStater   	ExceptionstrwarningOk)r   parentdialogfiltersrootextchoicehtmlexporterlayoutmsginfocheckboxibebboxreplyer   s                     @r   exportzHtmlExporter.export*   s    $$&&&v|<<Y2=>>>2;
 	g&&&= 	5dm,,,w''66HDyy{{000''444<<>> 0	H"002215DM..00F<((**1133D   )) "H' >fmmD&9&9>&.v66F)),777&26::F4C9D(23EFFH((---".y99BNN3'''".{;;BNN3'''#/	0E0N,2,>,>,@,@#G GC**4000MM"i&;&BCCCMM"i&;&CDDD%%a((($$S)))$$X...$$V,,,KKMMMIIKKEKKMMM#qjF**,, 1*0GGGGHxdmT^DDD H H H4t}Ds1vvM!-55fgs),i.C.FH HH
 ts   %N< <
P+AP&&P+N)__name__
__module____qualname____doc__r   r]    r   r   r   r      sA         
  H H H H Hr   r   Tc                    t           |rdnXt          j                            |          \  }}|dz   t          j                                      rt          dz            t          j        |dd          5 }t          |           } |	                    t                              fd|                      ddd           dS # 1 swxY w Y   dS )a:   Export the contents of the ConsoleWidget as HTML.

    Parameters
    ----------
    html : unicode,
        A Python unicode string containing the Qt HTML to export.

    filename : str
        The file to be saved.

    image_tag : callable, optional (default None)
        Used to convert images. See ``default_image_tag()`` for information.

    inline : bool, optional [default True]
        If True, include images as inline PNGs.  Otherwise, include them as
        links to external PNG files, mimicking web browsers' "Web Page,
        Complete" behavior.
    N_filesz"%s exists, but is not a directory.wutf-8encodingc                       | d          S )Npngr(   formatrb   )xr   r(   s    r   r   zexport_html.<locals>.<lambda>       YYq%N%N%N r   )default_image_tagr'   r(   r)   isfileOSErrorioopenfix_htmlwriter3   sub)rR   r   r   r   rO   rP   r   r(   s     `    @r   r   r   x   s)   & %	 G7##H--Sh7>>$ 	G>EFFF	3	1	1	1 "Q~~	

NNNNN! ! 	" 	" 	"" " " " " " " " " " " " " " " " " "s    ACCCc                 d   t           t          j        |dd          5 }|                     d          }|dk    s
J d            d| |d	z   d         z   } t	          |           } |                    t                              fd
|                      ddd           dS # 1 swxY w Y   dS )ap   Export the contents of the ConsoleWidget as XHTML with inline SVGs.

    Parameters
    ----------
    html : unicode,
        A Python unicode string containing the Qt HTML to export.

    filename : str
        The file to be saved.

    image_tag : callable, optional (default None)
        Used to convert images. See ``default_image_tag()`` for information.
    Nre   rf   rg   z<html>r   z#Invalid HTML string: no <html> tag.z,<html xmlns="http://www.w3.org/1999/xhtml">
   c                       | d d          S )Nsvgrk   rb   )rm   r   s    r   r   zexport_xhtml.<locals>.<lambda>   rn   r   )ro   rr   rs   findrt   ru   r3   rv   )rR   r   r   r   offsets     `  r   r2   r2      s
    %		3	1	1	1 
"Q 8$${{{A{{{?VAXYY  ~~	

NNNN! ! 	" 	" 	"
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
" 
"s   A6B%%B),B)rj   c                     dS )a   Return (X)HTML mark-up for the image-tag given by match.

    This default implementation merely removes the image, and exists mostly
    for documentation purposes. More information than is present in the Qt
    HTML is required to supply the images.

    Parameters
    ----------
    match : re.SRE_Match
        A match to an HTML image tag as exported by Qt, with match.group("Name")
        containing the matched image ID.

    path : string|None, optional [default None]
        If not None, specifies a path to which supporting files may be written
        (e.g., for linked images).  If None, all images are to be included
        inline.

    format : "png"|"svg", optional [default "png"]
        Format for returned or referenced images.
     rb   )matchr(   rl   s      r   ro   ro      s	    * 2r   c                     |                      d          }|dk    r!| d|dz            dz   dz   | |dz   d         z   } t          j        t          d|           } | S )z Transforms a Qt-generated HTML string into a standards-compliant one.

    Parameters
    ----------
    html : unicode,
        A Python unicode string containing the Qt HTML.
    z<head>r   Nrx   z!
<meta http-equiv="Content-Type" z&content="text/html; charset=utf-8" />
z<br/>)r{   rerv   
EMPTY_P_RE)rR   r|   s     r   rt   rt      so     YYx  F{{YfQhY459: VAXYY  6*gt,,DKr   )NTr   )Nrj   )ra   rr   r'   r   qtpyr   compiler   r3   objectr   r   r2   ro   rt   rb   r   r   <module>r      s    
 
			 				 				       RZ+,,
 
4	5	5W W W W W6 W W Wz!" !" !" !"H" " " "<   0    r   