ó
=˜ðNc           @   s»   d  Z  d d l m Z y6 d d l Z d d l m Z d d l m Z e Z	 Wn e
 k
 re e Z	 n Xd d d g Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s@   Lexical analysis of formal languages (i.e. code) using Pygments.iÿÿÿÿ(   t   ApplicationErrorN(   t   get_lexer_by_name(   t   _get_ttype_classt   tokent   textt    t
   LexerErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s@   /usr/lib/python2.7/dist-packages/docutils/utils/code_analyzer.pyR      s   t   Lexerc           B   s,   e  Z d  Z d d „ Z d „  Z d „  Z RS(   s¡  Parse `code` lines and yield "classified" tokens.

    Arguments

      code       -- string of source code to parse,
      language   -- formal language the code is written in,
      tokennames -- either 'long', 'short', or '' (see below).

    Merge subsequent tokens of the same token-type.

    Iterating over an instance yields the tokens as ``(tokentype, value)``
    tuples. The value of `tokennames` configures the naming of the tokentype:

      'long':  downcased full token type name,
      'short': short name defined by pygments.token.STANDARD_TYPES
               (= class argument used in pygments html output),
      'none':      skip lexical analysis.
    t   shortc         C   s™   | |  _  | |  _ | |  _ d |  _ | d k s< | d k r@ d St sU t d ƒ ‚ n  y t |  j ƒ |  _ Wn' t j	 j
 k
 r” t d | ƒ ‚ n Xd S(   sE   
        Set up a lexical analyzer for `code` in `language`.
        R   R   t   noneNs0   Cannot analyze code. Pygments package not found.s6   Cannot analyze code. No Pygments lexer found for "%s".(   R   s   text(   t   codet   languaget
   tokennamest   Nonet   lexert   with_pygmentsR   R   t   pygmentst   utilt   ClassNotFound(   t   selfR   R   R   (    (    s@   /usr/lib/python2.7/dist-packages/docutils/utils/code_analyzer.pyt   __init__0   s    				c         c   sš   t  | ƒ } | j ƒ  \ } } xE | D]= \ } } | | k rJ | | 7} q% | | f V| | } } q% W| j d ƒ r‚ | d  } n  | r– | | f Vn  d S(   sr   Merge subsequent tokens of same token-type.

           Also strip the final newline (added by pygments).
        s   
iÿÿÿÿN(   t   itert   nextt   endswith(   R   t   tokenst   lasttypet   lastvalt   ttypet   value(    (    s@   /usr/lib/python2.7/dist-packages/docutils/utils/code_analyzer.pyt   mergeH   s    c         c   sÉ   |  j  d k r! g  |  j f Vd St j |  j |  j  ƒ } x‰ |  j | ƒ D]x \ } } |  j d k r‚ t | ƒ j ƒ  j	 d ƒ } n t
 | ƒ g } g  | D] } | t k r˜ | ^ q˜ } | | f VqI Wd S(   s7   Parse self.code and yield "classified" tokens.
        Nt   longt   .(   R   R   R   R   t   lexR   R   t   strt   lowert   splitR   t   unstyled_tokens(   R   R   t	   tokentypeR   t   classest   cls(    (    s@   /usr/lib/python2.7/dist-packages/docutils/utils/code_analyzer.pyt   __iter__Z   s    %(   R   R   t   __doc__R   R   R*   (    (    (    s@   /usr/lib/python2.7/dist-packages/docutils/utils/code_analyzer.pyR	      s   	t   NumberLinesc           B   s    e  Z d  Z d „  Z d „  Z RS(   st  Insert linenumber-tokens at the start of every code line.

    Arguments

       tokens    -- iterable of ``(classes, value)`` tuples
       startline -- first line number
       endline   -- last line number

    Iterating over an instance yields the tokens with a
    ``(['ln'], '<the line number>')`` token added for every code line.
    Multi-line tokens are splitted.c         C   s/   | |  _  | |  _ d t t | ƒ ƒ |  _ d  S(   Ns   %%%dd (   R   t	   startlinet   lenR#   t   fmt_str(   R   R   R-   t   endline(    (    s@   /usr/lib/python2.7/dist-packages/docutils/utils/code_analyzer.pyR   w   s    		c         c   s   |  j  } d g |  j | f Vxx |  j D]m \ } } | j d ƒ } x@ | d  D]4 } | | d f V| d 7} d g |  j | f VqN W| | d f Vq( Wd  S(   Nt   lns   
iÿÿÿÿi   (   R-   R/   R   R%   (   R   t   linenoR   R   t   linest   line(    (    s@   /usr/lib/python2.7/dist-packages/docutils/utils/code_analyzer.pyR*   }   s    	
(   R   R   R+   R   R*   (    (    (    s@   /usr/lib/python2.7/dist-packages/docutils/utils/code_analyzer.pyR,   j   s   	(   R+   t   docutilsR    R   t   pygments.lexersR   t   pygments.formatters.htmlR   t   TrueR   t   ImportErrort   FalseR&   R   t   objectR	   R,   (    (    (    s@   /usr/lib/python2.7/dist-packages/docutils/utils/code_analyzer.pyt   <module>   s   

	N