o
    "5p`  ã                   @   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.
Ú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rB| jd u r:tj| _|  | j	¡ d S | 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ÚsettingsÚsectnum_xformÚsysÚmaxsizeÚupdate_section_numbersÚsectnum_depthÚsectnum_startÚsectnum_prefixÚsectnum_suffix©Úself© r!   ú;/usr/lib/python3/dist-packages/docutils/transforms/parts.pyÚapply    s   

zSectNum.applyr!   r   c           	      C   s¤   |d7 }|r	d}n| j }|D ]A}t|tjƒrO|t|ƒf }|d }tjd| jd |¡ | j d dgd�}| 	d|¡ d|d< || j
k rK|  |||¡ |d7 }qd S )	Nr
   r   r   Ú.u   Â Â Â Úsectnum)ÚclassesÚauto)r   Ú
isinstancer   ÚsectionÚstrÚ	generatedr   Újoinr   Úinsertr   r   )	r    Únoder   r   r%   ÚchildÚ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ü   t | jjddƒ}| jj}d|v r1| jjj}t|tjƒs0t|tjƒs0|j}t|tjƒs0t|tjƒr!n| j}| jjd d | _	d|v rG|d | _
n| jjj| _
|ra| jjj |¡ | jj | j¡ d S |  |¡}t|ƒrr| j |¡ d S | jjj | jj¡ d S )NÚuse_latex_tocFÚlocalÚidsr   Ú	backlinks)Úgetattrr   r   r   r   r   r(   r   r)   Útoc_idr;   Útoc_backlinksÚ
attributesÚupdater   Úbuild_contentsÚlenÚreplace_self)r    Útoc_by_writerr   r   Úcontentsr!   r!   r"   r#   X   s0   

ÿý
ÿ€
zContents.applyr   c                 C   s:  |d7 }dd„ |D ƒ}g }d}| j j dtj¡}|D ]j}|d }| d¡}	|  |¡}
tjddg|
¢R d|d	 d iŽ}| jj	|d
d�}t 
dd|¡}t d|¡}| jdv rq| tj¡d u rq| jdkrg||d< n
| jdkrq| j|d< ||k r|  ||¡}||7 }| |¡ q|r›tjdg|¢R Ž }|	r™|d  d¡ |S g S )Nr
   c                 S   s   g | ]
}t |tjƒr|‘qS r!   )r(   r   r)   )Ú.0Úsectr!   r!   r"   Ú
<listcomp>v   s    z+Contents.build_contents.<locals>.<listcomp>r   r   r'   r   Úrefidr:   z	toc-entry)Úsuggested_prefix)ÚentryÚtoprK   rL   r&   zauto-toc)r   r   r   r   r   Úcopy_and_filterr   Ú	referencer   Úset_idÚ	paragraphÚ	list_itemr;   Ú	next_noder=   rA   ÚappendÚbullet_list)r    r.   ÚlevelÚsectionsÚentriesÚautonumr   r)   r1   r'   Ú	entrytextrN   Úref_idrK   ÚitemÚsubsectsrE   r!   r!   r"   rA   t   sB   


ÿÿ




zContents.build_contentsc                 C   s   t | jƒ}| |¡ | ¡ S )z@Return a copy of a title, with references, images, etc. removed.)ÚContentsFilterr   Ú	walkaboutÚget_entry_text)r    r.   Úvisitorr!   r!   r"   rM   –   s   

zContents.copy_and_filterN)r   )r2   r3   r4   r5   r6   r#   rA   rM   r!   r!   r!   r"   r7   F   s    
"r7   c                   @   s@   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
dS )r]   c                 C   s
   |   ¡ jS ©N)Úget_tree_copyÚchildrenr   r!   r!   r"   r_   Ÿ   s   
zContentsFilter.get_entry_textc                 C   ó   t j‚ra   ©r   ÚSkipNode©r    r.   r!   r!   r"   Úvisit_citation_reference¢   ó   z'ContentsFilter.visit_citation_referencec                 C   rd   ra   re   rg   r!   r!   r"   Úvisit_footnote_reference¥   ri   z'ContentsFilter.visit_footnote_referencec                 C   s&   |  d¡r| j t |d ¡¡ tj‚)NÚalt)Úhasattrr   rS   r   ÚTextrf   rg   r!   r!   r"   Úvisit_image¨   s   
zContentsFilter.visit_imagec                 C   rd   ra   )r   ÚSkipDeparturerg   r!   r!   r"   Ú ignore_node_but_process_children­   ri   z/ContentsFilter.ignore_node_but_process_childrenN)r2   r3   r4   r_   rh   rj   rn   rp   Úvisit_problematicÚvisit_referenceÚvisit_targetr!   r!   r!   r"   r]   �   s    r]   )r5   Ú__docformat__Úrer   Údocutilsr   r   Údocutils.transformsr   r   r   r7   ÚTreeCopyVisitorr]   r!   r!   r!   r"   Ú<module>   s   4W