o
    Ni                      @   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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   M/home/ubuntu/.local/lib/python3.10/site-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   /   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_idr9   toc_backlinks
attributesupdater   build_contentslenreplace_self)r   toc_by_writerr   r   contentsr   r   r    r!   W   s0   



zContents.applyr   c                 C   s6  |d7 }dd |D }g }| 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 ro|tjd u ro| jdkre||d< n
| jdkro| j|d< ||k r}| ||}||7 }|| q|rt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>u   s    z+Contents.build_contents.<locals>.<listcomp>r   r   r%   r
   refidr8   z	toc-entry)suggested_prefix)entrytoprI   rJ   r$   zauto-toc)r   r   r   r   r   copy_and_filterr   	referencer   set_id	paragraph	list_itemr9   	next_noder;   r?   appendbullet_list)r   r,   levelsectionsentriesr   r'   r/   r%   	entrytextrL   ref_idrI   itemsubsectsrC   r   r   r    r?   s   s@   







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    rK      s   

zContents.copy_and_filterN)r   )r0   r1   r2   r3   r4   r!   r?   rK   r   r   r   r    r5   E   s    
!r5   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 )rZ   c                 C   s
   |   jS N)get_tree_copychildrenr   r   r   r    r\      s   
zContentsFilter.get_entry_textc                 C      t jr^   r   SkipNoder   r,   r   r   r    visit_citation_reference      z'ContentsFilter.visit_citation_referencec                 C   ra   r^   rb   rd   r   r   r    visit_footnote_reference   rf   z'ContentsFilter.visit_footnote_referencec                 C   s&   | dr| jt|d  tj)Nalt)hasattrr   rQ   r   Textrc   rd   r   r   r    visit_image   s   
zContentsFilter.visit_imagec                 C   ra   r^   )r   SkipDeparturerd   r   r   r     ignore_node_but_process_children   rf   z/ContentsFilter.ignore_node_but_process_childrenN)r0   r1   r2   r\   re   rg   rk   rm   visit_problematicvisit_referencevisit_targetr   r   r   r    rZ      s    rZ   )r3   __docformat__r   docutilsr   docutils.transformsr   r   r5   TreeCopyVisitorrZ   r   r   r   r    <module>   s   4V