o
    ¼>hœG  ã                   @  sz  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mZ dd
lZdd
lZdd
lZddlmZ dd
lm  mZ ddlmZmZmZmZ er]ddlmZ dAdBdd„ZdCdd„Z e !d¡Z"dDdd„Z#e
dƒdEd"d#„ƒZ$e
d$ƒdFdGd*d+„ƒZ%dHd,d-„Z&dHd.d/„Z'dHd0d1„Z(dId2d3„Z)dJd4d5„Z*dKd7d8„Z+d9d:„ Z,G d;d<„ d<eƒZ-G d=d>„ d>eƒZ.d?d@„ Z/d
S )Lz‹
Add table of contents support to Python-Markdown.

See the [documentation](https://Python-Markdown.github.io/extensions/toc)
for details.
é    )Úannotationsé   )Ú	Extensioné   )ÚTreeprocessor)ÚparseBoolValueÚAMP_SUBSTITUTEÚ
deprecatedÚHTML_PLACEHOLDER_REÚAtomicString)ÚUnescapeTreeprocessor)ÚRE_AMPN)Údeepcopy)ÚTYPE_CHECKINGÚAnyÚIteratorÚ
MutableSet)ÚMarkdownFÚvalueÚstrÚ	separatorÚunicodeÚboolÚreturnc                 C  sL   |st  d| ¡} |  dd¡ d¡} t dd| ¡ ¡  ¡ } t 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   © r'   úw/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/markdown/extensions/toc.pyÚslugify&   s
   r)   c                 C  s   t | |ddS )zP Slugify a string, to make it URL friendly while preserving Unicode characters. T)r   )r)   )r   r   r'   r'   r(   Úslugify_unicode0   s   r*   z^(.*)_([0-9]+)$ÚidÚidsúMutableSet[str]c                 C  sb   | |v s| s*t  | ¡}|rd| d¡t| d¡ƒd f } nd| df } | |v s| r| | ¡ | 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,   Úmr'   r'   r(   Úunique8   s   
"û
r4   z0Use `render_inner_html` and `striptags` instead.Úelúetree.Elementc                 C  sF   g }|   ¡ D ]}t|tƒr| t |¡¡ q| |¡ qd |¡ ¡ S )zGet title name.r   )ÚitertextÚ
isinstancer   ÚappendÚhtmlÚunescapeÚjoinr$   )r5   ÚtextÚcr'   r'   r(   Úget_nameD   s   
r?   zIUse `run_postprocessors`, `render_inner_html` and/or `striptags` instead.Tr=   Úmdr   Ústrip_entitiesc                   s   d‡ ‡fdd„}t  || ¡S )	zN Extract raw HTML from stash, reduce to plain text and swap with placeholder. r3   úre.Match[str]r   r   c              	     s`   zˆ j jt|  d¡ƒ }W n ttfy   |  d¡ Y S w t dd|¡}ˆr.t dd|¡}|S )z& Substitute raw html with plain text. r   r   z	(<[^>]+>)r   z(&[\#a-zA-Z0-9]+;))Ú	htmlStashÚrawHtmlBlocksr1   r0   Ú
IndexErrorÚ	TypeErrorr"   r#   )r3   ÚrawÚres©r@   rA   r'   r(   Ú	_html_subT   s   ÿz#stashedHTML2text.<locals>._html_subN)r3   rB   r   r   )r
   r#   )r=   r@   rA   rJ   r'   rI   r(   ÚstashedHTML2textQ   s   rK   c                 C  s   t ƒ }| | ¡S )z+ Unescape Markdown backslash escaped text. )r   r;   )r=   r>   r'   r'   r(   r;   c   s   
r;   c                 C  sê   |   d¡ }dkr6|   d|¡ }dkr6| d|… › | |d d… › } |   d¡ }dkr6|   d|¡ }dks|   d¡ }dkrl|   d|¡ }dkrl| d|… › | |d d… › } |   d¡ }dkrl|   d|¡ }dksId	 |  ¡ ¡} | S )
zL Strip HTML tags and return plain text. Note: HTML entities are unaffected. z<!--éÿÿÿÿz-->Né   ú<ú>r   ú )Úfindr<   Úsplit)r=   ÚstartÚendr'   r'   r(   Ú
strip_tagsi   s   & &ÿ& &ÿrU   c                 C  s@   d| v r
t  d| ¡} d| v r|  dd¡} d| v r|  dd¡} | S )z Escape character data. ú&z&amp;rN   z&lt;rO   z&gt;)r   r#   Úreplace)r=   r'   r'   r(   Úescape_cdataw   s   rX   c                 C  s   |j D ]}| | ¡} q|  ¡ S )z4 Run postprocessors from Markdown instance on text. )ÚpostprocessorsÚrunr$   )r=   r@   Úppr'   r'   r(   Úrun_postprocessorsƒ   s   
r\   c                 C  s@   t | | ¡ƒ}| d¡d }| d¡}|||…  ¡ }t||ƒS )z< Fully render inner html of an `etree` element as a string. rO   r   rN   )r;   Ú
serializerÚindexÚrindexr$   r\   )r5   r@   r=   rS   rT   r'   r'   r(   Úrender_inner_htmlŠ   s
   

r`   Úrootc                 C  s´   t |  d¡dƒdu r| S t| ƒ} |  d¡D ]B}d}t|ƒD ]-}|jdkr<| dd¡ d¡r<|jp1d› |› }| 	|¡ q|rJ|jpBd› |› |_d}q|rW|j
pQd› |› |_
q| S )zL Remove footnote references from a copy of the element, if any are present. ÚsupNz	.//sup/..r   r+   Úfnref)ÚnextÚiterr   ÚfindallÚreversedÚtagÚgetÚ
startswithÚtailÚremover=   )ra   ÚparentÚ
carry_textÚchildr'   r'   r(   Úremove_fnrefs—   s"   €€rp   c           	      C  s  g }t | ƒr†|  d¡}g |d< |d g}| |¡ g }| r†|  d¡}|d }g |d< ||d k r]| ¡  d}t|ƒD ]}||d krG|d7 }q: |rX|d| … }|d| … }| |¡ ||d krq|rk|d d n| |¡ n|d  |¡ | |¡ | |¡ |}| s|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ÚlevelrL   r   N)ÚlenÚpopr9   rg   )	Útoc_listÚordered_listÚlastÚlevelsÚparentsÚtÚcurrent_levelÚto_popÚpr'   r'   r(   Únest_toc_tokens®   s>   







Ü&r~   c                      sh   e Zd ZdZd#‡ fdd„Zd$dd„Zd%dd„Zd&dd„Zd'dd„Zd'dd„Z	d(dd„Z
d)d!d"„Z‡  ZS )*ÚTocTreeprocessorz& Step through document and build TOC. r@   r   Úconfigúdict[str, Any]c                   s  t ƒ  |¡ |d | _|d | _t|d ƒd | _|d | _|d | _|d | _|d | _	t
|d	 ƒ| _|d
 | _t
|d dƒ| _| jd u rK|d | _|d | _|d | _t
|d dƒ| _t d¡| _t|d tƒr‚d|d v r‚dd„ |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                 S  s   g | ]}t |ƒ‘qS r'   )r1   )Ú.0Úxr'   r'   r(   Ú
<listcomp>  s    z-TocTreeprocessor.__init__.<locals>.<listcomp>)ÚsuperÚ__init__r‚   rƒ   r1   Ú
base_levelr)   Úsepr…   r†   r   Úuse_anchorsrˆ   Úuse_permalinksrŠ   r‹   rŒ   r"   ÚcompileÚ
header_rgxr8   r   rR   Útoc_topÚ
toc_bottom)Úselfr@   r€   ©Ú	__class__r'   r(   r“   ò   s*   










$zTocTreeprocessor.__init__Únoder6   r   ú-Iterator[tuple[etree.Element, etree.Element]]c                 c  sB    |D ]}| j  |j¡s|jdvr||fV  |  |¡E dH  qdS )z? Iterator wrapper to get allowed parent and child all at once. )ÚpreÚcodeN)r™   r/   rh   Ú
iterparent)rœ   rŸ   ro   r'   r'   r(   r£     s   €
€ýzTocTreeprocessor.iterparentra   ÚelemÚNonec                 C  s|   |   |¡D ]6\}}d | ¡ ¡ ¡ }|sq|jr;|j ¡ | jkr;t|ƒdkr;tt|ƒƒD ]}|| |kr:|||<  nq,qdS )z Replace marker with elem. r   r   N)r£   r<   r7   r$   r=   r‚   rs   Úrange)rœ   ra   r¤   r}   r>   r=   Úir'   r'   r(   Úreplace_marker  s   "
þ€ñzTocTreeprocessor.replace_markerc                 C  s.   t |jd ƒ| j }|dkrd}d| |_dS )z. Adjust header level according to base level. rL   é   zh%dN)r1   rh   r”   )rœ   r¤   rr   r'   r'   r(   Ú	set_level+  s   zTocTreeprocessor.set_levelr>   Úelem_idr   c                 C  sr   t  d¡}|j|_d| |jd< | j|jd< d|_|D ]}| |¡ qt|ƒr2| |d ¡ t|ƒs'| |¡ d S )NÚaú#ÚhrefÚclassr   r   )ÚetreeÚElementr=   Úattribrˆ   r9   rs   rl   )rœ   r>   r«   Úanchorr¤   r'   r'   r(   Ú
add_anchor2  s   
ÿzTocTreeprocessor.add_anchorc                 C  s‚   t  d¡}| jdu rdt n| j|_d| |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¯   rƒ   r   r   )r°   r±   r—   r   r=   r²   rŠ   r‹   rŒ   rk   Úinsertr9   )rœ   r>   r«   r‰   r'   r'   r(   Úadd_permalink>  s   

ÿþzTocTreeprocessor.add_permalinkru   Úlistc                   s|   t  d¡}| j|jd< | jr!t  |d¡}| jr| j|jd< | j|_d‡ fd	d
„‰ ˆ ||ƒ d| jj	v r<| jj	d  
|¡ |S )z' Return a string div given a toc list. Údivr¯   Úspanru   r·   rm   r6   r   c                   sl   t  |d¡}| D ]+}t  |d¡}t  |d¡}| dd¡|_d| dd¡ |jd< |d	 r3ˆ |d	 |ƒ q|S )
NÚulÚlir¬   Únamer   r­   r+   r®   rq   )r°   Ú
SubElementri   r=   r²   )ru   rm   rº   Úitemr»   Úlink©Úbuild_etree_ulr'   r(   rÁ   Z  s   €z6TocTreeprocessor.build_toc_div.<locals>.build_etree_ulÚprettifyN)ru   r·   rm   r6   r   r6   )r°   r±   r…   r²   rƒ   r½   r†   r=   r@   ÚtreeprocessorsrZ   )rœ   ru   r¸   Úheaderr'   rÀ   r(   Úbuild_toc_divN  s   

zTocTreeprocessor.build_toc_divÚdocc              	   C  sÀ  t ƒ }| ¡ D ]}d|jv r| |jd ¡ qg }| ¡ D ]”}t|jtƒr±| j |j¡r±|  	|¡ t
t|ƒ| jƒ}t|ƒ}d|jvrRt|  t |¡| j¡|ƒ|jd< d}d|jv rntt|jd ƒ| jƒ}tt|ƒƒ}|jd= t|jd ƒ| jkr—t|jd ƒ| jkr—| t|jd ƒt|jd ƒ|||dœ¡ | jr£|  ||jd ¡ | jdvr±|  ||jd ¡ qt|ƒ}|  |¡}| jrÄ|   ||¡ | j !|¡}	| jj"D ]}
|
 #|	¡}	qÎ|| j_$|	| j_%d S )Nr+   r   údata-toc-labelrL   )rr   r+   r¼   r:   rÇ   )FN)&Úsetre   r²   r2   r8   rh   r   r™   r/   rª   r`   rp   r@   rU   r4   r)   r:   r;   r•   r\   rX   r1   rš   r›   r9   r–   r´   r—   r¶   r~   rÅ   r‚   r¨   r]   rY   rZ   Ú
toc_tokensÚtoc)rœ   rÆ   Úused_idsr5   rÉ   Ú	innerhtmlr¼   Údata_toc_labelr¸   rÊ   r[   r'   r'   r(   rZ   m  sP   
€

 
(û
€
zTocTreeprocessor.run)r@   r   r€   r   )rŸ   r6   r   r    )ra   r6   r¤   r6   r   r¥   )r¤   r6   r   r¥   )r>   r6   r«   r   r   r¥   )ru   r·   r   r6   )rÆ   r6   r   r¥   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r“   r£   r¨   rª   r´   r¶   rÅ   rZ   Ú__classcell__r'   r'   r   r(   r   ï   s    





r   c                      s2   e Zd ZeZ‡ fdd„Zdd„ Zd	dd„Z‡  ZS )
ÚTocExtensionc                   st   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`.rŽ   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).)r‚   rƒ   r†   r…   r‡   rˆ   r‰   rŠ   r‹   rŒ   r„   r)   r   r   r'   )r)   r€   r’   r“   )rœ   Úkwargsr   r'   r(   r“   ¦  s@   þÿÿÿÿÿÿÿÿþÿþÙ/zTocExtension.__init__c                 C  s<   |  | ¡ || _|  ¡  |  ||  ¡ ¡}|j |dd¡ dS )z% Add TOC tree processor to Markdown. rÊ   é   N)ÚregisterExtensionr@   ÚresetÚTreeProcessorClassÚ
getConfigsrÃ   Úregister)rœ   r@   Útocextr'   r'   r(   ÚextendMarkdownÚ  s
   
zTocExtension.extendMarkdownr   r¥   c                 C  s   d| j _g | j _d S )Nr   )r@   rÊ   rÉ   )rœ   r'   r'   r(   rÛ   â  s   zTocExtension.reset)r   r¥   )	rÎ   rÏ   rÐ   r   rÜ   r“   rà   rÛ   rÒ   r'   r'   r   r(   rÓ   ¢  s
    4rÓ   c                  K  s   t di | ¤ŽS )Nr'   )rÓ   )rØ   r'   r'   r(   ÚmakeExtensionç  s   rá   )F)r   r   r   r   r   r   r   r   )r   r   r   r   r   r   )r+   r   r,   r-   r   r   )r5   r6   r   r   )T)r=   r   r@   r   rA   r   r   r   )r=   r   r   r   )r=   r   r@   r   r   r   )r5   r6   r@   r   r   r   )ra   r6   r   r6   )0rÑ   Ú
__future__r   r   r   rÃ   r   Úutilr   r   r	   r
   r   r   Úserializersr   r"   r:   r   Úcopyr   Úxml.etree.ElementTreer°   ÚElementTreeÚtypingr   r   r   r   Úmarkdownr   r)   r*   r˜   r.   r4   r?   rK   r;   rU   rX   r\   r`   rp   r~   r   rÓ   rá   r'   r'   r'   r(   Ú<module>   sD   









A 4E