o
    Ke”6  ã                   @   sà   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  mZ d d
d„Zdd„ Ze d¡Zdd„ Zdd„ Zd!dd„Zdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZdd„ ZdS )"am  
Table of Contents Extension for Python-Markdown
===============================================

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

Original 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                 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)ÚvalueÚ	separatorÚunicode© r   úGD:\Projects\ConvertPro\env\Lib\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_unicode$   s   r   z^(.*)_([0-9]+)$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)ÚidZidsÚmr   r   r   Úunique,   s   
"û
r&   c                 C   sF   g }|   ¡ D ]}t|tƒr| t |¡¡ q| |¡ qd |¡ ¡ S )zGet title name.r   )ÚitertextÚ
isinstancer	   ÚappendÚhtmlÚunescapeÚjoinr   )ÚelÚtextÚcr   r   r   Úget_name8   s   
r0   Tc                    s   ‡ ‡fdd„}t  || ¡S )zN Extract raw HTML from stash, reduce to plain text and swap with placeholder. 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]+;))Z	htmlStashZrawHtmlBlocksr"   r!   Ú
IndexErrorÚ	TypeErrorr   r   )r%   ÚrawÚres©ÚmdÚstrip_entitiesr   r   Ú	_html_subF   s   ÿz#stashedHTML2text.<locals>._html_sub)r   r   )r.   r6   r7   r8   r   r5   r   ÚstashedHTML2textD   s   r9   c                 C   s   t ƒ }| | ¡S )z Unescape escaped text. )r
   r+   )r.   r/   r   r   r   r+   U   s   
r+   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 )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Úleveléÿÿÿÿr   N)ÚlenÚpopr)   Úreversed)	Útoc_listZordered_listÚlastZlevelsÚparentsÚtZcurrent_levelZto_popÚpr   r   r   Únest_toc_tokens[   s>   







Ü&rE   c                       sT   e Zd Z‡ fdd„Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
‡  ZS )ÚTocTreeprocessorc                    s  t ƒ  |¡ |d | _|d | _t|d ƒd | _|d | _|d | _|d | _t	|d ƒ| _
|d	 | _t	|d
 dƒ| _| jd u rF|d
 | _|d | _|d | _t d¡| _t|d tƒrud|d v rudd„ |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                 S   s   g | ]}t |ƒ‘qS r   )r"   )Ú.0Úxr   r   r   Ú
<listcomp>­   s    z-TocTreeprocessor.__init__.<locals>.<listcomp>)ÚsuperÚ__init__rG   rH   r"   Ú
base_levelr   ÚseprJ   r   Úuse_anchorsrL   Úuse_permalinksrN   rO   r   ÚcompileÚ
header_rgxr(   ÚstrÚsplitÚtoc_topÚ
toc_bottom)Úselfr6   Úconfig©Ú	__class__r   r   rV   ›   s&   









$zTocTreeprocessor.__init__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    ÚtagÚ
iterparent)ra   ÚnodeÚchildr   r   r   rh   ²   s   €
€ýzTocTreeprocessor.iterparentc                 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)rh   r,   r'   r   r.   rG   r=   Úrange)ra   ÚrootÚelemrD   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. r<   é   zh%dN)r"   rg   rW   )ra   rm   r;   r   r   r   Ú	set_levelÒ   s   zTocTreeprocessor.set_levelc                 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.   ÚattribrL   r)   r=   Úremove)ra   r/   Úelem_idÚanchorrm   r   r   r   Ú
add_anchorÙ   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< | |¡ d S )Nrr   Tz%spara;rs   rt   ru   rH   )	rv   rw   rZ   r   r.   rx   rN   rO   r)   )ra   r/   rz   rM   r   r   r   Úadd_permalinkå   s   

ÿþzTocTreeprocessor.add_permalinkc                    sr   t  d¡}| j|jd< | jrt  |d¡}d|jd< | j|_‡ fdd„‰ ˆ ||ƒ d| jjv r7| jjd  	|¡ |S )z' Return a string div given a toc list. Údivru   ÚspanZtoctitlec                    sl   t  |d¡}| D ]+}t  |d¡}t  |d¡}| dd¡|_d| dd¡ |jd< |d	 r3ˆ |d	 |ƒ q|S )
NÚulÚlirr   Únamer   rs   r$   rt   r:   )rv   Ú
SubElementÚgetr.   rx   )r@   Úparentr€   Úitemr�   Úlink©Úbuild_etree_ulr   r   r‰   û   s   €z6TocTreeprocessor.build_toc_div.<locals>.build_etree_ulZprettify)
rv   rw   rJ   rx   rH   rƒ   r.   r6   ÚtreeprocessorsÚrun)ra   r@   r~   Úheaderr   rˆ   r   Úbuild_toc_divð   s   


zTocTreeprocessor.build_toc_divc           
      C   sª  t ƒ }| ¡ D ]}d|jv r| |jd ¡ qg }| ¡ D ]‰}t|jtƒr¦| j |j¡r¦|  	|¡ t
|ƒ}d|jvrOtt|| jƒƒ}t|  || j¡|ƒ|jd< t|jd ƒ| jkrƒt|jd ƒ| jkrƒ| t|jd ƒ|jd ttt|j d|¡ƒ| jdd�ƒdœ¡ d|jv rŒ|j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<   zdata-toc-labelF)r7   )r;   r$   r‚   )FN)$ÚsetÚiterrx   r#   r(   rg   r]   r\   r    rq   r0   r+   r9   r6   r&   r   rX   r"   r_   r`   r)   r   r„   rY   r|   rZ   r}   rE   r�   rG   ro   Ú
serializerZpostprocessorsr‹   Ú
toc_tokensÚtoc)
ra   ÚdocZused_idsr-   r‘   r.   Z	innertextr~   r’   Úppr   r   r   r‹     sL   
€

(þý


€
zTocTreeprocessor.run)Ú__name__Ú
__module__Ú__qualname__rV   rh   ro   rq   r|   r}   r�   r‹   Ú__classcell__r   r   rc   r   rF   š   s    rF   c                       s0   e Zd ZeZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚTocExtensionc                    sf   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 FalseZtoclinkr   zQTrue or link text if a Sphinx-style permalink should be added - Defaults to FalseZ
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.rQ   z Word separator. Defaults to "-".rp   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).)rG   rH   rJ   rK   rL   rM   rN   rO   rI   r   r   rP   r   )r   rb   rU   rV   )ra   Úkwargsrc   r   r   rV   D  sD   ÿÿÿÿÿÿÿÿÿÿâ'zTocExtension.__init__c                 C   s<   |  | ¡ || _|  ¡  |  ||  ¡ ¡}|j |dd¡ d S )Nr’   é   )ZregisterExtensionr6   ÚresetÚTreeProcessorClassZ
getConfigsrŠ   Úregister)ra   r6   Ztocextr   r   r   ÚextendMarkdownn  s
   
zTocExtension.extendMarkdownc                 C   s   d| j _g | j _d S )Nr   )r6   r’   r‘   )ra   r   r   r   r�   u  s   zTocExtension.reset)	r•   r–   r—   rF   rž   rV   r    r�   r˜   r   r   rc   r   r™   @  s
    *r™   c                  K   s   t di | ¤ŽS )Nr   )r™   )r›   r   r   r   ÚmakeExtensionz  s   r¡   )F)T)Ú__doc__r   r   rŠ   r   Úutilr   r   r   r   r	   r
   r   r*   r   Zxml.etree.ElementTreerv   ZElementTreer   r   r[   r   r&   r0   r9   r+   rE   rF   r™   r¡   r   r   r   r   Ú<module>   s*    



? ':