B
    ç<[E  ã               @   sn   d Z dZddlZddlZddlmZ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jƒZdS )z'
Transforms related to document parts.
ZreStructuredTexté    N)ÚnodesÚutils)ÚTransformErrorÚ	Transformc               @   s&   e Zd ZdZdZdd„ Zd
dd„Zd	S )ÚSectNumaS  
    Automatically assigns numbers to the titles of document sections.

    It is possible to limit the maximum section level for which the numbers
    are added.  For those sections that are auto-numbered, the "autonum"
    attribute is set, informing the contents table generator that a different
    form of the TOC should be used.
    iÆ  c             C   s¶   | j j dd ¡| _| j j dd¡| _| j j dd¡| _| j j dd¡| _| j j | j ¡ | j	j
jr‚| jd krttj| _|  | j	¡ n0| j| j	j
_| j| j	j
_| j| j	j
_| j| j	j
_d S )NÚdepthÚstarté   ÚprefixÚ Úsuffix)Ú	startnodeÚdetailsÚgetÚmaxdepthÚ
startvaluer
   r   ÚparentÚremoveÚdocumentÚsettingsZsectnum_xformÚsysÚmaxsizeÚupdate_section_numbersZsectnum_depthZsectnum_startZsectnum_prefixZsectnum_suffix)Úself© r   ú8lib/python3.7/site-packages/docutils/transforms/parts.pyÚapply    s    

zSectNum.applyr   r   c       	      C   s¨   |d7 }|rd}n| j }xŠ|D ]‚}t|tjƒr|t|ƒf }|d }tjd| jd |¡ | j d dgd}| 	d|¡ d|d< || j
k r˜|  |||¡ |d7 }qW d S )	Nr	   r   r   Ú.u   Â Â Â Úsectnum)ÚclassesÚauto)r   Ú
isinstancer   ÚsectionÚstrÚ	generatedr
   Újoinr   Úinsertr   r   )	r   Únoder
   r   r   ZchildZnumbersÚtitler$   r   r   r   r   0   s"    


zSectNum.update_section_numbersN)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Údefault_priorityr   r   r   r   r   r   r      s   	r   c               @   s.   e Zd ZdZdZdd„ Zddd„Zdd	„ Zd
S )ÚContentsaè  
    This transform generates a table of contents from the entire document tree
    or from a single branch.  It locates "section" elements and builds them
    into a nested bullet list, which is placed within a "topic" created by the
    contents directive.  A title is either explicitly specified, taken from
    the appropriate language module, or omitted (local table of contents).
    The depth may be specified.  Two-way references between the table of
    contents and section titles are generated (requires Writer support).

    This transform requires a startnode, which contains generation
    options and provides the location for the generated table of contents (the
    startnode is replaced by the table of contents "topic").
    iÐ  c             C   sþ   y| j jj}W n tk
r&   d}Y nX | jj}d|krh| jjj}x*t|tj	ƒsdt|tj ƒsd|j}qDW n| j }| jjd d | _
d|kr”|d | _n| j jj| _|rÆ| jjj |¡ | jj | j¡ n4|  |¡}t|ƒræ| j |¡ n| jjj | jj¡ d S )NFZlocalÚidsr   Ú	backlinks)r   r   Zuse_latex_tocÚAttributeErrorr   r   r   r!   r   r"   Útoc_idr0   Ztoc_backlinksZ
attributesÚupdater   Úbuild_contentsÚlenZreplace_self)r   Ztoc_by_writerr   r   Úcontentsr   r   r   r   X   s,    


zContents.applyr   c             C   s8  |d7 }dd„ |D ƒ}g }d}| j j dtj¡}xÒ|D ]Ê}|d }| d¡}	|  |¡}
tjd|
žd|d	 d iŽ}| j 	|¡}t 
dd|¡}t d|¡}| jd
krÚ| tj¡d krÚ| jdkrÆ||d< n| jdkrÚ| j|d< ||k rö|  ||¡}||7 }| |¡ q6W |r0tjd|žŽ }|	r,|d  d¡ |S g S d S )Nr	   c             S   s   g | ]}t |tjƒr|‘qS r   )r!   r   r"   )Ú.0Zsectr   r   r   ú
<listcomp>x   s    z+Contents.build_contents.<locals>.<listcomp>r   r   r    r   Zrefidr/   )ÚentryÚtopr9   r:   r   zauto-toc)r   r   )r   )r   r   r   r   r   Úcopy_and_filterr   Ú	referencer   Zset_idZ	paragraphZ	list_itemr0   Z	next_noder2   r4   ÚappendZbullet_list)r   r'   ÚlevelZsectionsÚentriesZautonumr   r"   r(   r    Z	entrytextr<   Zref_idr9   ÚitemZsubsectsr6   r   r   r   r4   v   s<    







zContents.build_contentsc             C   s   t | jƒ}| |¡ | ¡ S )z@Return a copy of a title, with references, images, etc. removed.)ÚContentsFilterr   Z	walkaboutÚget_entry_text)r   r'   Zvisitorr   r   r   r;   —   s    

zContents.copy_and_filterN)r   )r)   r*   r+   r,   r-   r   r4   r;   r   r   r   r   r.   F   s
   
!r.   c               @   sD   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZeZeZ	eZ
eZdS )rA   c             C   s
   |   ¡ jS )N)Zget_tree_copyZchildren)r   r   r   r   rB       s    zContentsFilter.get_entry_textc             C   s
   t j‚d S )N)r   ÚSkipNode)r   r'   r   r   r   Úvisit_citation_reference£   s    z'ContentsFilter.visit_citation_referencec             C   s
   t j‚d S )N)r   rC   )r   r'   r   r   r   Úvisit_footnote_reference¦   s    z'ContentsFilter.visit_footnote_referencec             C   s*   |  d¡r | j t |d ¡¡ tj‚d S )NZalt)Úhasattrr   r=   r   ZTextrC   )r   r'   r   r   r   Úvisit_image©   s    
zContentsFilter.visit_imagec             C   s
   t j‚d S )N)r   ZSkipDeparture)r   r'   r   r   r   Ú ignore_node_but_process_children®   s    z/ContentsFilter.ignore_node_but_process_childrenN)r)   r*   r+   rB   rD   rE   rG   rH   Zvisit_interpretedZvisit_problematicZvisit_referenceZvisit_targetr   r   r   r   rA   ž   s   rA   )r,   Z__docformat__Úrer   Zdocutilsr   r   Zdocutils.transformsr   r   r   r.   ZTreeCopyVisitorrA   r   r   r   r   Ú<module>   s   4X