U
    Oe	                     @   sT   d Z dZddlmZ ddlmZ G dd deZG dd deZG d	d
 d
eZdS )z
Miscellaneous transforms.
ZreStructuredText    )nodes)	Transformc                   @   s   e Zd ZdZdZdd ZdS )CallBacka  
    Inserts a callback into a document.  The callback is called when the
    transform is applied, which is determined by its priority.

    For use with `nodes.pending` elements.  Requires a ``details['callback']``
    entry, a bound method or function which takes one parameter: the pending
    node.  Other data can be stored in the ``details`` attribute or in the
    object hosting the callback method.
    i  c                 C   s$   | j }|jd | |j| d S )Ncallback)	startnodedetailsparentremove)selfpending r   </tmp/pip-unpacked-wheel-rro3auwd/docutils/transforms/misc.pyapply   s    zCallBack.applyN__name__
__module____qualname____doc__default_priorityr   r   r   r   r   r      s   
r   c                   @   s   e Zd ZdZdZdd ZdS )ClassAttributez|
    Move the "class" attribute specified in the "pending" node into the
    immediately following non-comment element.
       c                 C   s   | j }|j}|}|rt||d t|D ]L}|| }t|tjs,t|tjrRq,|d  |j	d 7  < |j
|  d S |}|j}q| jjjd|j	d  t|j|j|jd}|| d S )N   classesclassz,No suitable element following "%s" directive	directiveline)r   r   rangeindexlen
isinstancer   Z	InvisibleZsystem_messager   r	   documentreportererrorZliteral_blockZ	rawsourcer   Zreplace_self)r
   r   r   childr   elementr#   r   r   r   r   ,   s.    
zClassAttribute.applyNr   r   r   r   r   r   #   s   r   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )Transitionsa  
    Move transitions at the end of sections up the tree.  Complain
    on transitions after a title, at the beginning or end of the
    document, and after another transition.

    For example, transform this::

        <section>
            ...
            <transition>
        <section>
            ...

    into this::

        <section>
            ...
        <transition>
        <section>
            ...
    i>  c                 C   s"   | j tjD ]}| | qd S )N)r!   findallr   
transitionvisit_transition)r
   noder   r   r   r   `   s    zTransitions.applyc                 C   s  |j |}d }|dksLt|j d tjr|dksLt|j d tjr|dkrt|j tjslt|j tjslt| jj	j
d|j|jd}n.t|j |d  tjr| jj	j
d|j|jd}|r|j || |d7 }|t|j k st|t|j d krd S |}|t|j d kr`|j }|j d krR| jj	j
d|jd}|j |j |d | d S |j |}q|j | |j |d | d S )	Nr   r      z4Document or section may not begin with a transition.)sourcer   zZAt least one body element must separate transitions; adjacent transitions are not allowed.z'Document may not end with a transition.r   )r   r   r    r   titleZsubtitler!   sectionAssertionErrorr"   r#   r,   r   r(   insertr   r	   )r
   r*   r   r#   Zsiblingr   r   r   r)   d   sX      zTransitions.visit_transitionN)r   r   r   r   r   r   r)   r   r   r   r   r&   F   s   r&   N)	r   Z__docformat__Zdocutilsr   Zdocutils.transformsr   r   r   r&   r   r   r   r   <module>   s   #