ó
¡V]c           @   s  d  Z  d d l m Z y d d l m Z Wn- e e f k
 rY d e f d „  ƒ  YZ n Xy6 d d l Z d d l	 m
 Z
 d d l m Z e Z Wn e e f k
 r¯ 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   ApplicationError(   t   DistributionNotFoundt   ResourceErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s;   lib/python2.7/site-packages/docutils/utils/code_analyzer.pyR      s   N(   t   get_lexer_by_name(   t   _get_ttype_classt   tokent   textt    t
   LexerErrorc           B   s   e  Z RS(    (   R   R   (    (    (    s;   lib/python2.7/site-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
 t f 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	   R   (   t   codet   languaget
   tokennamest   Nonet   lexert   with_pygmentsR
   R   t   pygmentst   utilt   ClassNotFoundR   (   t   selfR   R   R   (    (    s;   lib/python2.7/site-packages/docutils/utils/code_analyzer.pyt   __init__5   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;   lib/python2.7/site-packages/docutils/utils/code_analyzer.pyt   mergeP   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;   lib/python2.7/site-packages/docutils/utils/code_analyzer.pyt   __iter__b   s    %(   R   R   t   __doc__R   R!   R,   (    (    (    s;   lib/python2.7/site-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;   lib/python2.7/site-packages/docutils/utils/code_analyzer.pyR      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/   R1   R   R'   (   R   t   linenoR   R    t   linest   line(    (    s;   lib/python2.7/site-packages/docutils/utils/code_analyzer.pyR,   …   s    	
(   R   R   R-   R   R,   (    (    (    s;   lib/python2.7/site-packages/docutils/utils/code_analyzer.pyR.   r   s   	(   R-   t   docutilsR    t   pkg_resourcesR   R   t   ImportErrort   RuntimeErrorR   t   pygments.lexersR   t   pygments.formatters.htmlR   t   TrueR   t   SyntaxErrort   FalseR(   R
   t   objectR   R.   (    (    (    s;   lib/python2.7/site-packages/docutils/utils/code_analyzer.pyt   <module>   s$   

	Q