ó
_y]c           @   sÙ   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
 m Z m Z m Z d  d l m Z m Z d  d l m Z d d	 „ Z e e
 _ d
 e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   QtGui(   t   qstring_length(   t   PY3t   string_types(   t   HtmlFormatter(   t
   RegexLexert
   _TokenTypet   Textt   Error(   t   PythonLexert   Python3Lexer(   t   get_style_by_namet   rootc         c   sE  d } |  j  } t |  d ƒ r0 t |  j ƒ } n t | ƒ } | | d } xåxÞ| D]`\ } } }	 | | | ƒ }
 |
 rT | d k	 rÏ t | ƒ t k r­ | | |
 j ƒ  f VqÏ x | |  |
 ƒ D] } | Vq½ Wn  |
 j ƒ  } |	 d k	 r°t	 |	 t
 ƒ rPx¦ |	 D]L } | d k r| j ƒ  qý | d k r<| j | d ƒ qý | j | ƒ qý WnO t	 |	 t ƒ ri| |	 3n6 |	 d k r‰| j | d ƒ n t sŸt d |	 ƒ ‚ | | d } n  PqT qT Wya | | d k rü| d 7} d	 g } | d	 } | t d
 f VwM n  | t | | f V| d 7} WqM t k
 r-PqM XqM Wt | ƒ |  _ d S(   s©   Split ``text`` into (tokentype, text) pairs.

        Monkeypatched to store the final stack on the object itself.

        The `text` parameter this gets passed is only the current line, so to
        highlight things like multiline strings correctly, we need to retrieve
        the state from the previous line (this is done in PygmentsHighlighter,
        below), and use it to continue processing the current line.
    i    t   _saved_state_stackiÿÿÿÿs   #pops   #pushs   wrong state def: %rs   
i   R   u   
N(   t   _tokenst   hasattrt   listR   t   Nonet   typeR   t   groupt   endt
   isinstancet   tuplet   popt   appendt   intt   Falset   AssertionErrorR   R   t
   IndexError(   t   selft   textt   stackt   post	   tokendefst
   statestackt   statetokenst   rexmatcht   actiont	   new_statet   mt   itemt   state(    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyt   get_tokens_unprocessed   sV    
	

	
	t   PygmentsBlockUserDatac           B   s&   e  Z d  Z d Z d „  Z d „  Z RS(   s:    Storage for the user data associated with each line.
    R   c         K   sA   x* | j  ƒ  D] \ } } t |  | | ƒ q Wt j j |  ƒ d  S(   N(   t   itemst   setattrR    t   QTextBlockUserDatat   __init__(   R   t   kwdst   keyt   value(    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyR/   W   s    c         C   sF   d g } d j  g  | D] } d | t |  | ƒ f ^ q ƒ } d | S(   Nt   syntax_stacks   , s   %s=%rs   PygmentsBlockUserData(%s)(   t   joint   getattr(   R   t   attrst   attrR0   (    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyt   __repr__\   s    		,(   R   (   t   __name__t
   __module__t   __doc__R3   R/   R8   (    (    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyR+   Q   s   	t   PygmentsHighlighterc           B   sk   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z RS(   s4    Syntax highlighter that uses Pygments for parsing. c         C   s   t  t |  ƒ j | ƒ |  j ƒ  |  _ t d t ƒ |  _ |  j d ƒ | d  k	 r\ | |  _
 n! t rq t ƒ  |  _
 n t ƒ  |  _
 d  S(   Nt   nowrapt   default(   t   superR<   R/   t   documentt	   _documentR   t   Truet
   _formattert	   set_styleR   t   _lexerR   R
   R	   (   R   t   parentt   lexer(    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyR/   j   s    c         C   sù   |  j  ƒ  j ƒ  j ƒ  } | d k	 r6 | j |  j _ n t |  j d ƒ rT |  j ` n  d } xR |  j j | ƒ D]> \ } } t	 | ƒ } |  j
 | | |  j | ƒ ƒ | | 7} qm Wt |  j d ƒ rõ t d |  j j ƒ } |  j  ƒ  j | ƒ |  j ` n  d S(   s$    Highlight a block of text.
        R   i    R3   N(   t   currentBlockt   previoust   userDataR   R3   RE   R   R   t
   get_tokensR   t	   setFormatt   _get_formatR+   t   setUserData(   R   t   stringt	   prev_datat   indext   tokenR   t   lengtht   data(    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyt   highlightBlockx   s    c         C   s5   t  | t ƒ r t | ƒ } n  | |  _ |  j ƒ  d S(   s9    Sets the style to the specified Pygments style.
        N(   R   R   R   t   _stylet   _clear_caches(   R   t   style(    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyRD   “   s    	c         C   s'   |  j  j | ƒ d |  _ |  j ƒ  d S(   s    Sets a CSS stylesheet. The classes in the stylesheet should
        correspond to those generated by:

            pygmentize -S <style> -f html

        Note that 'set_style' and 'set_style_sheet' completely override each
        other, i.e. they cannot be used in conjunction.
        N(   RA   t   setDefaultStyleSheetR   RV   RW   (   R   t
   stylesheet(    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyt   set_style_sheet›   s    		c         C   s   i  |  _  i  |  _ d S(   s/    Clear caches for brushes and formats.
        N(   t   _brushest   _formats(   R   (    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyRW   ¬   s    	c         C   sg   | |  j  k r |  j  | S|  j d k rA |  j | |  j ƒ } n |  j | |  j ƒ } | |  j  | <| S(   s6    Returns a QTextCharFormat for token or None.
        N(   R]   RV   R   t   _get_format_from_documentRA   t   _get_format_from_style(   R   RR   t   result(    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyRM   ²   s    c         C   sM   t  |  j j | d f g ƒ ƒ \ } } |  j j | ƒ t j |  j ƒ j ƒ  S(   s0    Returns a QTextCharFormat for token by
        u   dummy(   t   nextRC   t   _format_linesRA   t   setHtmlR    t   QTextCursort
   charFormat(   R   RR   R@   t   codet   html(    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyR^   À   s    'c         C   sO  t  j ƒ  } x<| j | ƒ j ƒ  D]%\ } } | r" | d k rY | j |  j | ƒ ƒ qG| d k r~ | j |  j | ƒ ƒ qG| d k r  | j t  j j	 ƒ qG| d k r¼ | j
 t ƒ qG| d k rÞ | j t  j j ƒ qG| d k r | j t  j j ƒ qG| d k r"| j t  j j ƒ qG| d k rG| j t  j j ƒ qGq" q" W| S(	   sJ    Returns a QTextCharFormat for token by reading a Pygments style.
        t   colort   bgcolort   boldt   italict	   underlinet   sanst   romant   mono(   R    t   QTextCharFormatt   style_for_tokenR,   t   setForegroundt
   _get_brusht   setBackgroundt   setFontWeightt   QFontt   Boldt   setFontItalicRB   t   setUnderlineStylet   SingleUnderlinet   setFontStyleHintt	   SansSerift   Timest
   TypeWriter(   R   RR   RX   R`   R1   R2   (    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyR_   Ç   s*    "c         C   sP   |  j  j | ƒ } | d k rL |  j | ƒ } t j | ƒ } | |  j  | <n  | S(   s(    Returns a brush for the color.
        N(   R\   t   getR   t
   _get_colorR    t   QBrush(   R   Rh   R`   t   qcolor(    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyRs   à   s    c         C   sY   t  j ƒ  } | j t | d  d d ƒt | d d !d d ƒt | d d !d d ƒƒ | S(   s>    Returns a QColor built from a Pygments color string.
        i   t   basei   i   i   (   R    t   QColort   setRgbR   (   R   Rh   R‚   (    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyR€   ê   s
    N(   R9   R:   R;   R   R/   RU   RD   R[   RW   RM   R^   R_   Rs   R€   (    (    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyR<   c   s   								
N(   R   (   t   qtconsole.qtR    t   qtconsole.qstringhelpersR   t   ipython_genutils.py3compatR   R   t   pygments.formatters.htmlR   t   pygments.lexerR   R   R   R   t   pygments.lexersR	   R
   t   pygments.stylesR   R*   R.   R+   t   QSyntaxHighlighterR<   (    (    (    s=   lib/python2.7/site-packages/qtconsole/pygments_highlighter.pyt   <module>   s   "@	