ó
_y]c           @   sµ   d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z e j d ƒ Z	 e j d ƒ Z
 d e f d „  ƒ  YZ d e d	 „ Z d d
 „ Z d d d „ Z d „  Z d S(   sG    Defines classes and functions for working with Qt's rich text system.
iÿÿÿÿN(   t   QtGui(   t	   py3compats   <p[^/>]*>\s*</p>s   <img src="(?P<name>[\d]+)" />t   HtmlExporterc           B   s    e  Z d  Z d „  Z d „  Z RS(   ss    A stateful HTML exporter for a Q(Plain)TextEdit.

    This class is designed for convenient user interaction.
    c         C   sI   t  | t j t j f ƒ s! t ‚ | |  _ d |  _ d |  _ d |  _	 d S(   sA    Creates an HtmlExporter for the given Q(Plain)TextEdit.
        s   ipython.htmlN(
   t
   isinstanceR    t   QPlainTextEditt	   QTextEditt   AssertionErrort   controlt   filenamet   Nonet	   image_tagt
   inline_png(   t   selfR   (    (    s2   lib/python2.7/site-packages/qtconsole/rich_text.pyt   __init__#   s
    !			c            s  |  j  j ƒ  } t j | d ƒ } | j t j j ƒ d d g } | j | ƒ |  j rª | j |  j ƒ t	 j
 j |  j ƒ \ } } | j ƒ  d k rª | j | d ƒ qª n  | j ƒ  r| j ƒ  d |  _ | j ƒ  } t j |  j  j ƒ  j ƒ  ƒ } | j d ƒ rt } n“|  j ‰  ˆ  d k rt j | ƒ rt j | ƒ } | j d ƒ t j | ƒ }	 d	 }
 d
 } t j d ƒ } | j d ƒ t j  d ƒ } | j d ƒ t j  d ƒ } | j d ƒ t j! t j! j" | j# ƒ  |
 ƒ } | j$ | ƒ | j% | t j! j& ƒ | j% | t j! j' ƒ |	 j( d ƒ |	 j) | ƒ |	 j) | ƒ | j* |	 ƒ | j+ ƒ  | j ƒ  } | j, ƒ  | d k ‰  | j- ƒ  rˆ  |  _ qn  ‡  f d †  } y | | |  j |  j. ƒ SWqt/ k
 r} d |  j t0 | ƒ }
 t j! j1 | d |
 t j! j2 t j! j2 ƒ } qXn  d S(   sÆ    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.
        s
   Save as...s$   HTML with PNG figures (*.html *.htm)s-   XHTML with inline SVG figures (*.xhtml *.xml)s   .xmls   .xhtmliÿÿÿÿi    t   XHTMLs   Exporting HTML with PNGssL   Would you like inline PNGs (single large html file) or external image files?s   &Don't ask againt   Ds   &Inlinet   Is	   &Externalt   Ec            s   t  |  | | ˆ  ƒ S(   N(   t   export_html(   t   ht   ft   i(   t   inline(    s2   lib/python2.7/site-packages/qtconsole/rich_text.pyt   <lambda>j   t    s   Error exporting HTML to %s
t   Error(   s   .xmls   .xhtmlN(3   R   t   windowR    t   QFileDialogt   setAcceptModet
   AcceptSavet   setNameFiltersR   t
   selectFilet   ost   patht   splitextt   lowert   selectNameFiltert   exec_t   selectedFilest   selectedNameFilterR   t   cast_unicodet   documentt   toHtmlt
   startswitht   export_xhtmlR   R	   t   IMG_REt   searcht   QDialogt   setWindowTitlet   QVBoxLayoutt	   QCheckBoxt   setShortcutt   QPushButtont   QMessageBoxt   Questiont   windowTitlet   setInformativeTextt	   addButtont   NoRolet   YesRolet
   setSpacingt	   addWidgett	   setLayoutt   showt   hidet
   checkStateR
   t	   Exceptiont   strt   warningt   Ok(   R   t   parentt   dialogt   filterst   roott   extt   choicet   htmlt   exportert   layoutt   msgt   infot   checkboxt   ibt   ebt   boxt   replyt   e(    (   R   s2   lib/python2.7/site-packages/qtconsole/rich_text.pyt   export,   sj    				

(   t   __name__t
   __module__t   __doc__R   RW   (    (    (    s2   lib/python2.7/site-packages/qtconsole/rich_text.pyR      s   		c            sÁ   ˆ  d k r t ‰  n  | r$ d ‰ nG t j j | ƒ \ } } | d ‰ t j j ˆ ƒ rk t d ˆ ƒ ‚ n  t j | d d d ƒ8 } t	 |  ƒ }  | j
 t j ‡  ‡ f d †  |  ƒ ƒ Wd QXd S(   s:   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.
    t   _filess"   %s exists, but is not a directory.t   wt   encodings   utf-8c            s   ˆ  |  d ˆ d d ƒS(   NR!   t   formatt   png(    (   t   x(   R
   R!   (    s2   lib/python2.7/site-packages/qtconsole/rich_text.pyR   š   R   N(   R	   t   default_image_tagR    R!   R"   t   isfilet   OSErrort   iot   opent   fix_htmlt   writeR-   t   sub(   RL   R   R
   R   RI   RJ   R   (    (   R
   R!   s2   lib/python2.7/site-packages/qtconsole/rich_text.pyR   z   s    		
c            s¡   ˆ  d
 k r t ‰  n  t j | d d d ƒn } |  j d ƒ } | d k sW t d ƒ ‚ d |  | d }  t |  ƒ }  | j t j	 ‡  f d	 †  |  ƒ ƒ Wd
 QXd
 S(   sp   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.
    R\   R]   s   utf-8s   <html>iÿÿÿÿs#   Invalid HTML string: no <html> tag.u,   <html xmlns="http://www.w3.org/1999/xhtml">
i   c            s   ˆ  |  d d  d d ƒS(   NR!   R^   t   svg(   R	   (   R`   (   R
   (    s2   lib/python2.7/site-packages/qtconsole/rich_text.pyR   ¸   R   N(
   R	   Ra   Rd   Re   t   findR   Rf   Rg   R-   Rh   (   RL   R   R
   R   t   offset(    (   R
   s2   lib/python2.7/site-packages/qtconsole/rich_text.pyR,   ž   s    	R_   c         C   s   d S(   sô   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.
    u    (    (   t   matchR!   R^   (    (    s2   lib/python2.7/site-packages/qtconsole/rich_text.pyRa   ¼   s    c         C   sY   |  j  d ƒ } | d k r@ |  | d  d d |  | d }  n  t j t d |  ƒ }  |  S(   s¶    Transforms a Qt-generated HTML string into a standards-compliant one.

    Parameters
    ----------
    html : unicode,
        A Python unicode string containing the Qt HTML.
    s   <head>iÿÿÿÿi   s!   
<meta http-equiv="Content-Type" s&   content="text/html; charset=utf-8" />
s   <br/>(   Rj   t   reRh   t
   EMPTY_P_RE(   RL   Rk   (    (    s2   lib/python2.7/site-packages/qtconsole/rich_text.pyRf   Ô   s    (   RZ   Rd   R    Rm   t   qtconsole.qtR    t   ipython_genutilsR   t   compileRn   R-   t   objectR   R	   t   TrueR   R,   Ra   Rf   (    (    (    s2   lib/python2.7/site-packages/qtconsole/rich_text.pyt   <module>   s   ]$