
    j;                    @   d Z ddlm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
lmZmZmZmZ erddlmZ d*d+dZd,dZ ej        d          Zd-dZd.dZ d/d0d"Z!d1d#Z"d$ Z# G d% d&e          Z$ G d' d(e          Z%d) Z&d	S )2z
Add table of contents support to Python-Markdown.

See the [documentation](https://Python-Markdown.github.io/extensions/toc)
for details.
    )annotations   )	Extension   )Treeprocessor)code_escapeparseBoolValueAMP_SUBSTITUTEHTML_PLACEHOLDER_REAtomicString)UnescapeTreeprocessorN)TYPE_CHECKINGAnyIterator
MutableSet)MarkdownFvaluestr	separatorunicodeboolreturnc                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)r   r   r   s      a/lsinfo/ai/hellotax_ai/base_platform/venv/lib/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]+)$ididsMutableSet[str]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)r,   r-   ms      r'   uniquer6   6   s    
))2)R   	#AGGAJJAGGAJJ(9::BBB7"B ))2) GGBKKKIr)   eletree.Elementc                &   g }|                                  D ]T}t          |t                    r(|                    t	          j        |                     ?|                    |           Ud                    |                                          S )zGet title name.r   )itertext
isinstancer   appendhtmlunescapejoinr$   )r7   textcs      r'   get_namerB   B   s~     D[[]]  a&& 	KKa(())))KKNNNN774==   r)   Tr@   mdr   strip_entitiesc                >    dfd}t          j        ||           S )zN Extract raw HTML from stash, reduce to plain text and swap with placeholder. r5   re.Match[str]r   r   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rawHtmlBlocksr3   r2   
IndexError	TypeErrorr"   r#   )r5   rawresrC   rD   s      r'   	_html_subz#stashedHTML2text.<locals>._html_subP   s    	,,S__=CCI& 	 	 	771::	 f\2s++ 	9&.C88C
s   26 &AA)r5   rF   r   r   )r   r#   )r@   rC   rD   rN   s    `` r'   stashedHTML2textrO   N   s<    
 
 
 
 
 
 
 "9d333r)   c                H    t                      }|                    |           S )z Unescape escaped text. )r   r>   )r@   rA   s     r'   r>   r>   _   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 )aV  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popr<   reversed)	toc_listordered_listlastlevelsparentstcurrent_levelto_popps	            r'   nest_toc_tokensra   e   s    L
8}} -||AZw-D!!!  $	QAgJMAjM vbz))

 !'**  A$'
22! 0#HfWH-F%hwh/G m,,, r
**,3 #Z(("FF1IIII
 Z ''***t$$$m,,,DI  $	L r)   c                  \     e Zd ZdZd fdZddZddZddZddZddZ	d dZ
d!dZ xZS )"TocTreeprocessorz& Step through document and build TOC. rC   r   configdict[str, Any]c                \   t                                          |           |d         | _        |d         | _        t	          |d                   dz
  | _        |d         | _        |d         | _        |d         | _        |d         | _	        t          |d	                   | _        |d
         | _        t          |d         d          | _        | j        |d         | _        |d         | _        |d         | _        t          |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title_class
anchorlinkanchorlink_class	permalinkFpermalink_classpermalink_titlepermalink_leadingz[Hh][123456]	toc_depth-c                ,    g | ]}t          |          S  )r3   ).0xs     r'   
<listcomp>z-TocTreeprocessor.__init__.<locals>.<listcomp>   s    ,\,\,\SVV,\,\,\r)   )super__init__rg   rh   r3   
base_levelr(   seprj   rk   r	   use_anchorsrm   use_permalinksro   rp   rq   r"   compile
header_rgxr;   r   splittoc_top
toc_bottom)selfrC   rd   	__class__s      r'   rz   zTocTreeprocessor.__init__   s   !(+ /
f[122Q6i(+&, &} 5!/|0D!E!E%+,>%?,VK-@%HH&"("5D$*+<$=$*+<$=.<VDW=XZ_.`.`*^44f[)3// 	7C6+;N4N4N,\,\VK=P=V=VWZ=[=[,\,\,\)DL$///DL!&"566DOOOr)   noder8   r   -Iterator[tuple[etree.Element, etree.Element]]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)r   r1   tag
iterparent)r   r   childs      r'   r   zTocTreeprocessor.iterparent   s|        	2 	2E?((33 2	8X8XEk!!!??5111111111	2 	2r)   rootelemNonec                   |                      |          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   r?   r:   r$   r@   rg   rU   range)r   r   r   r`   rA   r@   is          r'   replace_markerzTocTreeprocessor.replace_marker   s    ood++ 	 	FQ771::<<((..00D  v !&,,..DK77CFFaKKs1vv  Atqyy#! !	 	r)   c                n    t          |j        d                   | j        z   }|dk    rd}d|z  |_        dS )z. Adjust header level according to base level. rT      zh%dN)r3   r   r{   )r   r   rS   s      r'   	set_levelzTocTreeprocessor.set_level   s9    DHRL!!DO3199E5=r)   rA   elem_idr   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Elementr@   attribrm   r<   rU   remove)r   rA   r   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<   | j        r+|j        |_	        d|_        |
                    d	|           d S |                    |           d S )
Nr   Tz%spara;r   r   r   rh   r   r   )r   r   r~   r
   r@   r   ro   rp   rq   tailinsertr<   )r   rA   r   rn   s       r'   add_permalinkzTocTreeprocessor.add_permalink   s    M#&&	!0D88 $n44#2 	 $'=	 $($8	! 	=(,(<IW%! 	 VINAFHHQ	"""""HHYr)   rX   listc                T   t          j        d          }| j        |j        d<   | j        r7t          j        |d          }| j        r| j        |j        d<   | j        |_        dfd	 ||           d
| j        j	        v r%| j        j	        d
         
                    |           |S )z' Return a string div given a toc list. divr   spanrX   r   parentr8   r   c                <   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   rR   )r   
SubElementgetr@   r   )rX   r   r   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)rX   r   r   r8   r   r8   )r   r   rj   r   rh   r   rk   r@   rC   treeprocessorsrun)r   rX   r   headerr   s       @r'   build_toc_divzTocTreeprocessor.build_toc_div  s    mE"""n
7 : 	%%c622F :)-)9g&*FK
	 
	 
	 
	 
	 
	 	x%%%///G":.223777
r)   docc                   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    r|                    t#          |j        d                   |j        d         t          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,   rT   zdata-toc-labelF)rD   )rS   r,   r   )FN)$setiterr   r4   r;   r   r   r   r1   r   rB   r>   rO   rC   r6   r(   r|   r3   r   r   r<   r   r   r}   r   r~   r   ra   r   rg   r   
serializerpostprocessorsr   
toc_tokenstoc)
r   r   used_idsr7   r   r@   	innertextr   r   pps
             r'   r   zTocTreeprocessor.run$  s   55((** 	. 	.Bry  RYt_---
((** 	< 	<B"&#&& <4?+@+@+H+H <r"""|| ry(( ()9$)H)H I II&,T\\)TX-N-NPX&Y&YBIdOrvbz??dl22s26":$/7Y7Y%%!$RVBZ io ()9'	6F(M(MNN GE* * * ! !' '    $ry00	"23# 9OOB	$888&m;;&&r29T?;;;$Z00
  ,,; 	*S))) g  %%'( 	 	B&&++CC'r)   )rC   r   rd   re   )r   r8   r   r   )r   r8   r   r8   r   r   )r   r8   r   r   )rA   r8   r   r   r   r   )rX   r   r   r8   )r   r8   r   r   )__name__
__module____qualname____doc__rz   r   r   r   r   r   r   r   __classcell__r   s   @r'   rc   rc      s        007 7 7 7 7 72	2 	2 	2 	2   *! ! ! !
 
 
 
           >/ / / / / / / /r)   rc   c                  0     e Zd ZeZ fdZd Zd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d	dgddgt           dgddgddgd| _        	  t                      j        di | d S )Nz[TOC]zeText to find and replace with Table of Contents. Set to an empty string to disable. Default: `[TOC]`.r   z;Title to insert into TOC `<div>`. Default: an empty string.toctitlez2CSS class used for the title. Default: `toctitle`.r   z4CSS class(es) used for the link. Default: `toclink`.Fz7True if header should be a self link. Default: `False`.toclinkz5CSS class(es) used for the link. Defaults: `toclink`.r   zPTrue or link text if a Sphinx-style permalink should be added. Default: `False`.
headerlinkz7CSS class(es) used for the link. Default: `headerlink`.zPermanent linkz<Title attribute of the permalink. Default: `Permanent link`.z\True if permalinks should be placed at start of the header, rather than end. Default: False.1z%Base level for headers. Default: `1`.zFFunction to generate anchors based on header text. Default: `slugify`.rs   zWord separator. Default: `-`.r   a'  Define the range of section levels to include in the Table of Contents. A single integer (b) defines the bottom section level (<h1>..<hb>) only. A string consisting of two digits separated by a hyphen in between (`2-5`) defines the top (t) and the bottom (b) (<ht>..<hb>). Default: `6` (bottom).)rg   rh   rk   rj   rl   rm   rn   ro   rp   rq   ri   r(   r   rr   ru   )r(   rd   ry   rz   )r   kwargsr   s     r'   rz   zTocExtension.__init__Z  s     $ Q P M P R! e W  !"`  n" FGa >?)O.
 .
^ 	/""6"""""r)   c                    |                     |            || _        |                                  |                     ||                                           }|j                            |dd           dS )z% Add TOC tree processor to Markdown. r      N)registerExtensionrC   resetTreeProcessorClass
getConfigsr   register)r   rC   tocexts      r'   extendMarkdownzTocExtension.extendMarkdown  sj    
T"""

((T__->->??
""65!44444r)   r   r   c                6    d| j         _        g | j         _        d S )Nr   )rC   r   r   )r   s    r'   r   zTocExtension.reset  s    r)   )r   r   )	r   r   r   rc   r   rz   r   r   r   r   s   @r'   r   r   V  sb        )2# 2# 2# 2# 2#h5 5 5               r)   r   c                     t          di | S )Nru   )r   )r   s    r'   makeExtensionr     s    !!&!!!r)   )F)r   r   r   r   r   r   r   r   )r   r   r   r   r   r   )r,   r   r-   r.   r   r   )r7   r8   r   r   )T)r@   r   rC   r   rD   r   r   r   )r@   r   r   r   )'r   
__future__r   r   r   r   r   utilr   r	   r
   r   r   r   r"   r=   r   xml.etree.ElementTreer   ElementTreetypingr   r   r   r   markdownr   r(   r+   r   r0   r6   rB   rO   r>   ra   rc   r   r   ru   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 B3 3 3 3
 RZ*++
	 	 	 		! 	! 	! 	!4 4 4 4 4"   > > >Bm m m m m} m m m`B  B  B  B  B 9 B  B  B J" " " " "r)   