U
    [Sh                      @   s^   d Z dZddl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j	Z
dS )z'
Transforms related to document parts.
reStructuredText    N)nodes)	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   L/root/rtd-docs/venv/lib/python3.8/site-packages/docutils/transforms/parts.pyapply   s    

zSectNum.applyr   r   c           	      C   s   |d7 }|rd}n| j }|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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   /   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|krL| jjj}t|tjsRt|tjsR|j}q*n| j}| jjd d | _	d|krx|d | _
n| jjj| _
|r| jjj| | jj| j n4| |}t|r| j| n| jjj| jj d S )NZuse_latex_tocFlocalidsr   	backlinks)getattrr   r   r   r   r   r!   r   r"   toc_idr3   Ztoc_backlinks
attributesupdater   build_contentslenZreplace_self)r   Ztoc_by_writerr   r   contentsr   r   r   r   W   s(    



zContents.applyr   c                 C   s4  |d7 }dd |D }g }| j jdtj}|D ]}|d }|d}| |}	tjd|	d|d	 d i}
| jj	|
d
d}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 }|| q0|r,t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"   ).0sectr   r   r   
<listcomp>u   s      z+Contents.build_contents.<locals>.<listcomp>r   r   r    r
   Zrefidr2   z	toc-entry)Zsuggested_prefix)entrytopr>   r?   r   zauto-toc)r
   r
   )r
   )r   r   r   r   r   copy_and_filterr   	referencer   Zset_idZ	paragraphZ	list_itemr3   Z	next_noder5   r8   appendZbullet_list)r   r'   levelsectionsentriesr   r"   r*   r    Z	entrytextrA   Zref_idr>   itemZsubsectsr:   r   r   r   r8   s   sB    






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'   visitorr   r   r   r@      s    

zContents.copy_and_filterN)r   )r+   r,   r-   r.   r/   r   r8   r@   r   r   r   r   r0   E   s
   
!r0   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 )rG   c                 C   s
   |   jS N)Zget_tree_copychildrenr   r   r   r   rH      s    zContentsFilter.get_entry_textc                 C   s
   t jd S rJ   r   SkipNoder   r'   r   r   r   visit_citation_reference   s    z'ContentsFilter.visit_citation_referencec                 C   s
   t jd S rJ   rL   rN   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   rB   r   TextrM   rN   r   r   r   visit_image   s    
zContentsFilter.visit_imagec                 C   s
   t jd S rJ   )r   ZSkipDeparturerN   r   r   r    ignore_node_but_process_children   s    z/ContentsFilter.ignore_node_but_process_childrenN)r+   r,   r-   rH   rO   rP   rS   rT   Zvisit_problematicZvisit_referenceZvisit_targetr   r   r   r   rG      s   rG   )r.   __docformat__r   Zdocutilsr   Zdocutils.transformsr   r   r0   ZTreeCopyVisitorrG   r   r   r   r   <module>   s   4V