o
    i                     @  sj   d Z ddlm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.
    )annotationsreStructuredTextN)nodes)	Transformc                   @  s,   e Zd ZdZdZ	 dddZddd	d
ZdS )SectNumaP  
    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 "auto"
    attribute is set, informing the contents table generator that a different
    form of the TOC should be used.
    i  returnNonec                 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"   _/var/www/html/karishye-ai-python/venv/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   0   s*   
zSectNum.update_section_numbersNr   r   )r"   r   )__name__
__module____qualname____doc__default_priorityr$   r   r"   r"   r"   r#   r      s    	
r   c                   @  s0   e Zd ZdZdZd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  r   r   c                 C  s   t | jjdd}|rd S | jj}d|v r5| jjj}t|tjs4t|tjs4|j}t|tjs4t|tjr%n| j}| jjd d | _	d|v rK|d | _
n| jjj| _
| |}t|rb| 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build_contentslenreplace_selfr   )r!   toc_by_writerr   r   contentsr"   r"   r#   r$   X   s.   



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>t   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entriesr	   r*   r2   r(   	entrytextrN   ref_idrK   itemsubsectsrE   r"   r"   r#   rA   r   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#   rM      s   

zContents.copy_and_filterNr3   )r   )r4   r5   r6   r7   r8   r$   rA   rM   r"   r"   r"   r#   r9   F   s    

!r9   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r`   r   SkipNoder!   r/   r"   r"   r#   visit_citation_reference      z'ContentsFilter.visit_citation_referencec                 C  rc   r`   rd   rf   r"   r"   r#   visit_footnote_reference   rh   z'ContentsFilter.visit_footnote_referencec                 C  s&   | dr| jt|d  tj)Nalt)hasattrr   rS   r   Textre   rf   r"   r"   r#   visit_image   s   
zContentsFilter.visit_imagec                 C  rc   r`   )r   SkipDeparturerf   r"   r"   r#    ignore_node_but_process_children   rh   z/ContentsFilter.ignore_node_but_process_childrenN)r4   r5   r6   r^   rg   ri   rm   ro   visit_problematicvisit_referencevisit_targetr"   r"   r"   r#   r\      s    r\   )r7   
__future__r   __docformat__r   docutilsr   docutils.transformsr   r   r9   TreeCopyVisitorr\   r"   r"   r"   r#   <module>   s   4T