ó
§]c           @   sY   d  Z  d d l Z d d l Z d d l m Z d   Z e j d  Z e d    Z d S(   sH   Preprocessor for merging consecutive stream outputs for easier handling.i˙˙˙˙N(   t
   get_loggerc            s"   t  j      f d    } | S(   s   
    Wrap a function to be executed on all cells of a notebook
    
    The wrapped function should have these parameters:
    
    cell : NotebookNode cell
        Notebook cell being processed
    resources : dictionary
        Additional resources used in the conversion process.  Allows
        preprocessors to pass variables into the Jinja engine.
    index : int
        Index of the cell being processed
    c            s_   t    j d   j  x< t |  j  D]+ \ } }   | | |  \ |  j | <} q& W|  | f S(   Ns   Applying preprocessor: %s(   R    t   debugt   __name__t	   enumeratet   cells(   t   nbt	   resourcest   indext   cell(   t   function(    sF   lib/python2.7/site-packages/nbconvert/preprocessors/coalescestreams.pyt   wrappedfunc   s
    	#(   t	   functoolst   wraps(   R	   R
   (    (   R	   sF   lib/python2.7/site-packages/nbconvert/preprocessors/coalescestreams.pyt   cell_preprocessor
   s    s   .*\r(?=[^\n])c         C   s˙   |  j  d g   } | s" |  | f S| d } | g } xj | d D]^ } | j d k r | j d k r | j | j k r | j | j 7_ q@ | j |  | } q@ WxG | D]? } | j d k rİ d | j k rİ t j d | j  | _ qİ qİ W| |  _ |  | f S(   s½  
    Merge consecutive sequences of stream output into single stream
    to prevent extra newlines inserted at flush calls
    
    Parameters
    ----------
    cell : NotebookNode cell
        Notebook cell being processed
    resources : dictionary
        Additional resources used in the conversion process.  Allows
        transformers to pass variables into the Jinja engine.
    index : int
        Index of the cell being processed
    t   outputsi    i   t   streams   t    (   t   gett   output_typet   namet   textt   appendt   cr_patt   subR   (   R   R   R   R   t   lastt   new_outputst   output(    (    sF   lib/python2.7/site-packages/nbconvert/preprocessors/coalescestreams.pyt   coalesce_streams$   s"    

	
	(	   t   __doc__t   reR   t   traitlets.logR    R   t   compileR   R   (    (    (    sF   lib/python2.7/site-packages/nbconvert/preprocessors/coalescestreams.pyt   <module>   s   	