B
    !eK                 @   sX   d Z dZddlmZ ddlmZ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)	TransformTransformErrorc               @   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   7lib/python3.7/site-packages/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}|}xz|rxpt||d t|D ]J}|| }t|tjs0t|tjrVq0|d  |j	d 7  < |j
| d S W |}|j}qW | jjjd|j	d  t|j|j|jd}|| d S )N   classesclassz,No suitable element following "%s" directiveZ	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	   Zchildr   elementr!   r   r   r   r   ,   s(    


zClassAttribute.applyN)r   r   r   r   r   r   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&   x | j tjD ]}| | qW d S )N)r   Ztraverser   
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 |}xh|t|j d krb|j }|j d krT| jj	j
d|jd}|j |j |d | d S |j |}qW |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   ZsectionAssertionErrorr    r!   r(   r   r$   insertr   r
   )r   r&   r   r!   Zsiblingr   r   r   r%   d   sD    
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   r   <module>   s   #