
    c7                         d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
 ddlmZ ddlZddlZddlZddlmc mZ dd
Zd Z ej        d          Zd Zd ZddZd Zd Z G d de          Z G d de          Zd ZdS )an  
Table of Contents Extension for Python-Markdown
===============================================

See <https://Python-Markdown.github.io/extensions/toc>
for documentation.

Oringinal code Copyright 2008 [Jack Miller](https://codezen.org/)

All changes Copyright 2008-2014 The Python Markdown Project

License: [BSD](https://opensource.org/licenses/bsd-license.php)

   )	Extension   )Treeprocessor)code_escapeparseBoolValueAMP_SUBSTITUTEHTML_PLACEHOLDER_REAtomicString)UnescapeTreeprocessor    NFc                 H   |s>t          j        d|           } |                     dd                              d          } t	          j        dd|                                                                           } t	          j        d                    |          ||           S )z, Slugify a string, to make it URL friendly. NFKDasciiignorez[^\w\s-] z[{}\s]+)	unicodedata	normalizeencodedecoderesubstriplowerformat)value	separatorunicodes      7lib/python3.11/site-packages/markdown/extensions/toc.pyslugifyr      s     @%fe44Wh//66w??F;E**002288::E6*##I..	5AAA    c                 &    t          | |d          S )zP Slugify a string, to make it URL friendly while preserving Unicode characters. T)r   )r   )r   r   s     r   slugify_unicoder"   $   s    5)T2222r    z^(.*)_([0-9]+)$c                 
   | |v s| sgt                               |           }|r>d|                    d          t          |                    d                    dz   fz  } nd| dfz  } | |v e| g|                    |            | S )z@ Ensure id is unique in set of ids. Append '_1', '_2'... if not z%s_%dr   r   )
IDCOUNT_REmatchgroupintadd)ididsms      r   uniquer,   ,   s    
) #2 #R   	#AGGAJJAGGAJJ(9::BBB7"B ) #2 # GGBKKKIr    c                 &   g }|                                  D ]T}t          |t                    r(|                    t	          j        |                     ?|                    |           Ud                    |                                          S )zGet title name.r   )itertext
isinstancer
   appendhtmlunescapejoinr   )eltextcs      r   get_namer7   8   s~     D[[]]  a&& 	KKa(())))KKNNNN774==   r    Tc                 <    fd}t          j        ||           S )zN Extract raw HTML from stash, reduce to plain text and swap with placeholder. c                 $   	 j         j        t          |                     d                             }n,# t          t
          f$ r |                     d          cY S w xY wt          j        dd|          }rt          j        dd|          }|S )z& Substitute raw html with plain text. r   r   z	(<[^>]+>)r   z(&[\#a-zA-Z0-9]+;))	htmlStashrawHtmlBlocksr'   r&   
IndexError	TypeErrorr   r   )r+   rawresmdstrip_entitiess      r   	_html_subz#stashedHTML2text.<locals>._html_subF   s    	,,S__=CCI& 	 	 	771::	 f\2s++ 	9&.C88C
s   26 &AA)r	   r   )r5   r@   rA   rB   s    `` r   stashedHTML2textrC   D   s7    
 
 
 
 
 
 "9d333r    c                 H    t                      }|                    |           S )z Unescape escaped text. )r   r2   )r5   r6   s     r   r2   r2   U   s    A::dr    c                    g }t          |           rN|                     d          }g |d<   |d         g}|                    |           g }| r|                     d          }|d         }g |d<   ||d         k     rg|                                 d}t          |          D ]}||d         k    r|dz  } |r|d|          }|d|          }|                    |           ||d         k    r&|r|d         d         n|                    |           nE|d                             |           |                    |           |                    |           |}| |S )a<  Given an unsorted list with errors and skips, return a nested one.
    [{'level': 1}, {'level': 2}]
    =>
    [{'level': 1, 'children': [{'level': 2, 'children': []}]}]

    A wrong list is also converted:
    [{'level': 2}, {'level': 1}]
    =>
    [{'level': 2, 'children': []}, {'level': 1, 'children': []}]
    r   childrenlevelr   N)lenpopr0   reversed)	toc_listordered_listlastlevelsparentstcurrent_levelto_popps	            r   nest_toc_tokensrU   [   s    L
8}} -||AZw-D!!!  $	QAgJMAjM vbz) -

 !'**  A$'
2 ! 0#HfWH-F%hwh/G m,,, r
* 	-,3 #Z(("FF1IIII
 Z ''***t$$$m,,,DI  $	L r    c                   H     e Zd Z fdZd Zd Zd Zd Zd Zd Z	d Z
 xZS )	TocTreeprocessorc                    t                                          |           |d         | _        |d         | _        t	          |d                   dz
  | _        |d         | _        |d         | _        |d         | _        t          |d                   | _
        |d	         | _        t          |d
         d          | _        | j        |d
         | _        |d         | _        |d         | _        t          j        d          | _        t%          |d         t&                    r>d|d         v r4d |d                             d          D             \  | _        | _        d S d| _        t	          |d                   | _        d S )Nmarkertitle	baselevelr   r   r   	toc_class
anchorlinkanchorlink_class	permalinkFpermalink_classpermalink_titlez[Hh][123456]	toc_depth-c                 ,    g | ]}t          |          S  )r'   ).0xs     r   
<listcomp>z-TocTreeprocessor.__init__.<locals>.<listcomp>   s    ,\,\,\SVV,\,\,\r    )super__init__rY   rZ   r'   
base_levelr   sepr\   r   use_anchorsr^   use_permalinksr`   ra   r   compile
header_rgxr/   strsplittoc_top
toc_bottom)selfr@   config	__class__s      r   rj   zTocTreeprocessor.__init__   si   X&G_
f[122Q6i(+&,)&*>?? &'9 :,VK-@%HH 	6"("5D%&78%&78*^44f[)3// 	7C6+;N4N 	7,\,\VK=P=V=VWZ=[=[,\,\,\)DL$///DL!&"566DOOOr    c              #      K   |D ]K}| j                             |j                  s*|j        dvr!||fV  |                     |          E d{V  LdS )z? Iterator wrapper to get allowed parent and child all at once. )precodeN)rp   r%   tag
iterparent)ru   nodechilds      r   r|   zTocTreeprocessor.iterparent   s~        	2 	2E?((33 2	8X 2Ek!!!??5111111111	2 	2r    c                    |                      |          D ]\  }}d                    |                                                                          }|sA|j        rg|j                                        | j        k    rEt          |          dk    r2t          t          |                    D ]}||         |k    r|||<    ndS )z Replace marker with elem. r   r   N)r|   r3   r.   r   r5   rY   rI   range)ru   rootelemrT   r6   r5   is          r   replace_markerzTocTreeprocessor.replace_marker   s    ood++ 	 	FQ771::<<((..00D  v !&,,..DK7 CFFaK s1vv  Atqy #!	 	r    c                 n    t          |j        d                   | j        z   }|dk    rd}d|z  |_        dS )z. Adjust header level according to base level. rH      zh%dN)r'   r{   rk   )ru   r   rG   s      r   	set_levelzTocTreeprocessor.set_level   s<    DHRL!!DO319 	E5=r    c                 \   t          j        d          }|j        |_        d|z   |j        d<   | j        |j        d<   d|_        |D ]}|                    |           t          |          r*|                    |d                    t          |          *|                    |           d S )Na#hrefclassr   r   )etreeElementr5   attribr^   r0   rI   remove)ru   r6   elem_idanchorr   s        r   
add_anchorzTocTreeprocessor.add_anchor   s    s##f #gf!%!6g 	  	 DMM$!ff 	HHQqTNNN !ff 		r    c                     t          j        d          }| j        du r
dt          z  n| j        |_        d|z   |j        d<   | j        |j        d<   | j        r| j        |j        d<   |                    |           d S )Nr   Tz%spara;r   r   r   rZ   )	r   r   rn   r   r5   r   r`   ra   r0   )ru   r6   r   r_   s       r   add_permalinkzTocTreeprocessor.add_permalink   s    M#&&	!0D83)n44#2 	 $'=	 $($8	! 	=(,(<IW%	r    c                 :   t          j        d          }| j        |j        d<   | j        r+t          j        |d          }d|j        d<   | j        |_        fd ||           d| j        j        v r%| j        j        d         	                    |           |S )z' Return a string div given a toc list. divr   spantoctitlec                 <   t          j        |d          }| D ]}t          j        |d          }t          j        |d          }|                    dd          |_        d|                    dd          z   |j        d<   |d	         r |d	         |           |S )
Nullir   namer   r   r)   r   rF   )r   
SubElementgetr5   r   )rL   parentr   itemr   linkbuild_etree_uls         r   r   z6TocTreeprocessor.build_toc_div.<locals>.build_etree_ul   s    !&$//B  9 9%b$//'C00 HHVR00	&)DHHT2,>,>&>F#
# 9"N4
#3R888Ir    prettify)
r   r   r\   r   rZ   r   r5   r@   treeprocessorsrun)ru   rL   r   headerr   s       @r   build_toc_divzTocTreeprocessor.build_toc_div   s    mE"""n
7 : 	%%c622F%/FM'"*FK
	 
	 
	 
	 
	 	x%%%// 	8G":.223777
r    c                    t                      }|                                D ]+}d|j        v r |                    |j        d                    ,g }|                                D ]}t	          |j        t                    r| j                            |j                  r| 	                    |           t          |          }d|j        vrSt          t          || j                            }t          |                     || j                  |          |j        d<   t#          |j        d                   | j        k    rt#          |j        d                   | j        k    rw|                    t#          |j        d                   |j        d         t          t+          |j                            d|                    | j        d          d           d|j        v r|j        d= | j        r!|                     ||j        d                    | j        dvr!|                     ||j        d                    t7          |          }|                     |          }| j        r|                     ||           | j                            |          }| j        j         D ]}	|	!                    |          }|| j        _"        || j        _#        d S )Nr)   rH   zdata-toc-labelF)rA   )rG   r)   r   )FN)$setiterr   r(   r/   r{   rq   rp   r%   r   r7   r2   rC   r@   r,   r   rl   r'   rs   rt   r0   r   r   rm   r   rn   r   rU   r   rY   r   
serializerpostprocessorsr   
toc_tokenstoc)
ru   docused_idsr4   r   r5   	innertextr   r   pps
             r   r   zTocTreeprocessor.run  s   55((** 	. 	.Bry  .RYt_---
((** 	< 	<B"&#&& <4?+@+@+H+H <r"""|| ry( Z ()9$)H)H I II&,T\\)TX-N-NPX&Y&YBIdOrvbz??dl2 s26":$/7Y %%!$RVBZ io 0'	6F(M(MNN GE! ! !' '    $ry0 4	"23# 9OOB	$888&m; <&&r29T?;;;$Z00
  ,,; 	*S))) g  %%'( 	 	B&&++CC'r    )__name__
__module____qualname__rj   r|   r   r   r   r   r   r   __classcell__rw   s   @r   rW   rW      s        7 7 7 7 7.	2 	2 	2  *! ! !
 
 
	 	 	  </ / / / / / /r    rW   c                   .     e Zd ZeZ fdZd Zd Z xZS )TocExtensionc                     ddgddgddgddgd	dgd
dgddgddgddgt           dgddgddgd| _         t                      j        di | d S )Nz[TOC]zhText to find and replace with Table of Contents - Set to an empty string to disable. Defaults to "[TOC]"r   z<Title to insert into TOC <div> - Defaults to an empty stringr   z6CSS class(es) used for the link. Defaults to "toclink"Fz8True if header should be a self link - Defaults to Falsetoclinkr   zQTrue or link text if a Sphinx-style permalink should be added - Defaults to False
headerlinkz9CSS class(es) used for the link. Defaults to "headerlink"zPermanent linkz?Title attribute of the permalink - Defaults to 'Permanent link'1zBase level for headers.zdFunction to generate anchors based on header text - Defaults to the headerid ext's slugify function.rc   z Word separator. Defaults to "-".r   a%  Define the range of section levels to include inthe Table of Contents. A single integer (b) definesthe bottom section level (<h1>..<hb>) only.A string consisting of two digits separated by a hyphenin between ("2-5"), define the top (t) and thebottom (b) (<ht>..<hb>). Defaults to `6` (bottom).)rY   rZ   r\   r]   r^   r_   r`   ra   r[   r   r   rb   re   )r   rv   ri   rj   )ru   kwargsrw   s     r   rj   zTocExtension.__init__D  s    PQ 45  23 !/0 "+"9!: 9: !-!; < !1!? @ 89KL ABOP=%
 %
N 	""6"""""r    c                     |                     |            || _        |                                  |                     ||                                           }|j                            |dd           d S )Nr      )registerExtensionr@   resetTreeProcessorClass
getConfigsr   register)ru   r@   tocexts      r   extendMarkdownzTocExtension.extendMarkdownn  sl    
T"""

((T__->->?? 	""65!44444r    c                 6    d| j         _        g | j         _        d S )Nr   )r@   r   r   )ru   s    r   r   zTocExtension.resetz  s    r    )	r   r   r   rW   r   rj   r   r   r   r   s   @r   r   r   @  s]        )(# (# (# (# (#T
5 
5 
5             r    r   c                      t          di | S )Nre   )r   )r   s    r   makeExtensionr     s    !!&!!!r    )F)T)__doc__r   r   r   r   utilr   r   r   r	   r
   r   r   r1   r   xml.etree.ElementTreer   ElementTreer   r"   ro   r$   r,   r7   rC   r2   rU   rW   r   r   re   r    r   <module>r      s          * * * * * * a a a a a a a a a a a a a a 2 2 2 2 2 2 				      % % % % % % % % %B B B B3 3 3
 RZ*++
	 	 		! 	! 	!4 4 4 4"  < < <~c c c c c} c c cL<  <  <  <  < 9 <  <  < ~" " " " "r    