a
    !DfG                     @  s  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rddlmZ d?dddddddZddddddZ e!dZ"ddddddZ#e
dddddd Z$e
d!d@dd#ddd$d%d&Z%ddd'd(d)Z&ddd'd*d+Z'ddd'd,d-Z(dd#dd.d/d0Z)dd#dd1d2d3Z*ddd4d5d6Z+d7d8 Z,G d9d: d:eZ-G d;d< d<eZ.d=d> Z/d
S )Az
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strbool)value	separatorunicodereturnc                 C  sL   |s"t d| } | ddd} tdd|   } td||| S )z, Slugify a string, to make it URL friendly. ZNFKDasciiignorez[^\w\s-] z[{}\s]+)	unicodedata	normalizeencodedecoderesubstriplowerformat)r   r   r    r&   d/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/markdown/extensions/toc.pyslugify&   s
    r(   )r   r   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]+)$zMutableSet[str])ididsr   c                 C  sX   | |v s| sJt | }|r<d|dt|dd f } q d| df } q ||  | 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    
"
r2   z0Use `render_inner_html` and `striptags` instead.etree.Element)elr   c                 C  sF   g }|   D ]*}t|tr,|t| q|| qd| S )zGet title name.r   )itertext
isinstancer   appendhtmlunescapejoinr#   )r4   textcr&   r&   r'   get_nameD   s    
r=   zIUse `run_postprocessors`, `render_inner_html` and/or `striptags` instead.Tr   )r;   mdstrip_entitiesr   c                   s"   ddd fdd}t || S )zN Extract raw HTML from stash, reduce to plain text and swap with placeholder. zre.Match[str]r   )r1   r   c              	     s`   z j jt| d }W n  ttfy:   | d Y S 0 tdd|}r\tdd|}|S )z& Substitute raw html with plain text. r   r   z	(<[^>]+>)r   z(&[\#a-zA-Z0-9]+;))Z	htmlStashZrawHtmlBlocksr/   r.   
IndexError	TypeErrorr!   r"   )r1   rawresr>   r?   r&   r'   	_html_subT   s    z#stashedHTML2text.<locals>._html_sub)r
   r"   )r;   r>   r?   rE   r&   rD   r'   stashedHTML2textQ   s    rF   )r;   r   c                 C  s   t  }|| S )z+ Unescape Markdown backslash escaped text. )r   r9   )r;   r<   r&   r&   r'   r9   c   s    r9   c                 C  s   |  d }dkrH|  d| }dkrH| d|  | |d d  } q |  d }dkr|  d| }dkr| d|  | |d d  } qHd	|  } | 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    &"&"rP   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;rI   z&lt;rJ   z&gt;)r   r"   replace)r;   r&   r&   r'   escape_cdataw   s    rS   )r;   r>   r   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    
rW   )r4   r>   r   c                 C  s@   t || }|dd }|d}|||  }t||S )z< Fully render inner html of an `etree` element as a string. rJ   r   rI   )r9   
serializerindexrindexr#   rW   )r4   r>   r;   rN   rO   r&   r&   r'   render_inner_html   s
    
r[   )rootr   c                 C  s   t | dddu r| S t| } | dD ]}d}t|D ]Z}|jdkrx|dddrx|jpbd | }|	| q:|r:|jpd | |_d}q:|r*|j
pd | |_
q*| S )zL Remove footnote references from a copy of the element, if any are present. supNz	.//sup/..r   r*   Zfnref)nextiterr   findallreversedtagget
startswithtailremover;   )r\   parentZ
carry_textchildr&   r&   r'   remove_fnrefs   s    ri   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r|d7 }qx qqx|r|d|  }|d|  }|| ||d kr|r|d d n|| n"|d | || || |}q8|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levelrG   r   N)lenpopr7   ra   )	toc_listZordered_listlastlevelsparentstZcurrent_levelZto_poppr&   r&   r'   nest_toc_tokens   sB    








rt   c                      s   e Zd ZdZddd fddZddd	d
dZddddddZdddddZddddddZddddddZ	dddddZ
dddddZ  ZS ) TocTreeprocessorz& Step through document and build TOC. r   zdict[str, Any])r>   configc                   s  t  | |d | _|d | _t|d d | _|d | _|d | _|d | _|d | _	t
|d	 | _|d
 | _t
|d d| _| jd u r|d | _|d | _|d | _t
|d d| _td| _t|d trd|d v rdd |d dD \| _| _n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&   )r/   ).0xr&   r&   r'   
<listcomp>      z-TocTreeprocessor.__init__.<locals>.<listcomp>)super__init__rw   rx   r/   
base_levelr(   seprz   r{   r   use_anchorsr}   use_permalinksr   r   r   r!   compile
header_rgxr6   r   rM   toc_top
toc_bottom)selfr>   rv   	__class__r&   r'   r      s*    










"zTocTreeprocessor.__init__r3   z-Iterator[tuple[etree.Element, etree.Element]])noder   c                 c  s@   |D ]6}| 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-   rb   
iterparent)r   r   rh   r&   r&   r'   r     s    
zTocTreeprocessor.iterparentNone)r\   elemr   c                 C  s|   |  |D ]l\}}d|  }|s*q
|jr
|j | jkr
t|dkr
tt|D ]}|| |krX|||<  q
qXq
dS )z Replace marker with elem. r   r   N)r   r:   r5   r#   r;   rw   rl   range)r   r\   r   rs   r<   r;   ir&   r&   r'   replace_marker  s    
"zTocTreeprocessor.replace_marker)r   r   c                 C  s.   t |jd | j }|dkr d}d| |_dS )z. Adjust header level according to base level. rG      zh%dN)r/   rb   r   )r   r   rk   r&   r&   r'   	set_level+  s    zTocTreeprocessor.set_levelr   )r<   elem_idr   c                 C  sl   t d}|j|_d| |jd< | j|jd< d|_|D ]}|| q6t|r^||d  qF|| d S )Na#hrefclassr   r   )etreeElementr;   attribr}   r7   rl   rf   )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rP| j|jd< | jrr|j|_	d|_|
d	| n
|| d S )
Nr   Tz%spara;r   r   r   rx   r   r   )r   r   r   r   r;   r   r   r   r   re   insertr7   )r   r<   r   r~   r&   r&   r'   add_permalink>  s    

zTocTreeprocessor.add_permalinklist)rn   r   c                   s   t d}| j|jd< | jrBt |d}| jr:| j|jd< | j|_dddd fdd  || d	| jj	v r| jj	d	 
| |S )
z' Return a string div given a toc list. divr   spanr   r3   )rn   rg   r   c                   sl   t |d}| D ]V}t |d}t |d}|dd|_d|dd |jd< |d	 r |d	 | q|S )
Nullir   namer   r   r*   r   rj   )r   
SubElementrc   r;   r   )rn   rg   r   itemr   linkbuild_etree_ulr&   r'   r   Z  s    z6TocTreeprocessor.build_toc_div.<locals>.build_etree_ulZprettify)r   r   rz   r   rx   r   r{   r;   r>   treeprocessorsrU   )r   rn   r   headerr&   r   r'   build_toc_divN  s    

zTocTreeprocessor.build_toc_div)docr   c              	   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rt| t|| j||jd< d}d|jv rtt|jd | j}tt|}|jd= t|jd | jkr0t|jd | jkr0|t|jd |jd |||d | jrJ| ||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-labelrG   )rk   r*   r   r8   r   )FN)&setr_   r   r0   r6   rb   r   r   r-   r   r[   ri   r>   rP   r2   r(   r8   r9   r   rW   rS   r/   r   r   r7   r   r   r   r   rt   r   rw   r   rX   rT   rU   
toc_tokenstoc)r   r   Zused_idsr4   r   Z	innerhtmlr   Zdata_toc_labelr   r   rV   r&   r&   r'   rU   m  sL    


 
,

zTocTreeprocessor.run)__name__
__module____qualname____doc__r   r   r   r   r   r   r   rU   __classcell__r&   r&   r   r'   ru      s   ru   c                      s6   e Zd ZeZ fddZdd ZddddZ  ZS )	TocExtensionc                   sr   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f 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.Z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`.Ztoclinkz5CSS class(es) used for the link. Defaults: `toclink`.r   zPTrue or link text if a Sphinx-style permalink should be added. Default: `False`.Z
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).)rw   rx   r{   rz   r|   r}   r~   r   r   r   ry   r(   r   r   )r(   rv   r   r   )r   kwargsr   r&   r'   r     s>    1zTocExtension.__init__c                 C  s<   | |  || _|   | ||  }|j|dd dS )z% Add TOC tree processor to Markdown. r      N)ZregisterExtensionr>   resetTreeProcessorClassZ
getConfigsr   register)r   r>   Z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   ru   r   r   r   r   r   r&   r&   r   r'   r     s   4r   c                  K  s   t f i | S )N)r   )r   r&   r&   r'   makeExtension  s    r   )F)T)0r   
__future__r   r   r   r   r   utilr   r   r	   r
   r   r   Zserializersr   r!   r8   r   copyr   xml.etree.ElementTreer   ElementTreetypingr   r   r   r   Zmarkdownr   r(   r)   r   r,   r2   r=   rF   r9   rP   rS   rW   r[   ri   rt   ru   r   r   r&   r&   r&   r'   <module>   sB   

A 4E