o
    N=&b                      @   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.
Z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settingsZsectnum_xformsysmaxsizeupdate_section_numbersZsectnum_depthZsectnum_startZsectnum_prefixZsectnum_suffixself r   =usr/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 )NZuse_latex_tocFlocalidsr   	backlinks)getattrr   r   r   r   r   r    r   r!   toc_idr2   Ztoc_backlinksZ
attributesupdater   build_contentslenZreplace_self)r   Z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	   Zrefidr1   z	toc-entry)Zsuggested_prefix)entrytopr<   r=   r   zauto-toc)r   r   r   r   r   copy_and_filterr   	referencer   Zset_idZ	paragraphZ	list_itemr2   Z	next_noder4   r6   appendZbullet_list)r   r&   levelsectionsentriesr   r!   r)   r   Z	entrytextr?   Zref_idr<   itemZsubsectsr8   r   r   r   r6   s   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&   visitorr   r   r   r>      s   

zContents.copy_and_filterN)r   )r*   r+   r,   r-   r.   r   r6   r>   r   r   r   r   r/   E   s    
!r/   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 )rE   c                 C   s
   |   jS N)Zget_tree_copychildrenr   r   r   r   rF      s   
zContentsFilter.get_entry_textc                 C      t jrH   r   SkipNoder   r&   r   r   r   visit_citation_reference      z'ContentsFilter.visit_citation_referencec                 C   rJ   rH   rK   rM   r   r   r   visit_footnote_reference   rO   z'ContentsFilter.visit_footnote_referencec                 C   s&   | dr| jt|d  tj)NZalt)hasattrr   r@   r   TextrL   rM   r   r   r   visit_image   s   
zContentsFilter.visit_imagec                 C   rJ   rH   )r   ZSkipDeparturerM   r   r   r    ignore_node_but_process_children   rO   z/ContentsFilter.ignore_node_but_process_childrenN)r*   r+   r,   rF   rN   rP   rS   rT   Zvisit_problematicZvisit_referenceZvisit_targetr   r   r   r   rE      s    rE   )r-   Z__docformat__r   docutilsr   Zdocutils.transformsr   r   r/   ZTreeCopyVisitorrE   r   r   r   r   <module>   s   4V