o
    go9                     @  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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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 )&z
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)UnescapeTreeprocessorNFc                 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)value	separatorunicode r   W/var/www/html/bloggers_ems/venv/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_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)ididsmr   r   r   unique2   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_name>   s   
r3   Tstrip_entitiesboolc                   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]+;))	htmlStashrawHtmlBlocksr$   r#   
IndexError	TypeErrorr   r   )r(   rawresmdr4   r   r   	_html_subL   s   z#stashedHTML2text.<locals>._html_sub)r
   r   )r1   r=   r4   r>   r   r<   r   stashedHTML2textJ   s   r?   c                 C  s   t  }|| S )z Unescape escaped text. )r   r.   )r1   r2   r   r   r   r.   [   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 )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pr   r   r   nest_toc_tokensa   s>   







&rO   c                      s`   e Zd ZdZ fddZdd Zddd	Zdd
dZdddZdddZ	dd Z
dd Z  ZS )TocTreeprocessorz& Step through document and build TOC. 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rd|d v rd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   )r$   ).0xr   r   r   
<listcomp>   s    z-TocTreeprocessor.__init__.<locals>.<listcomp>)super__init__rQ   rR   r$   
base_levelr   seprT   rU   r   use_anchorsrW   use_permalinksrY   rZ   r[   r   compile
header_rgxr+   strsplittoc_top
toc_bottom)selfr=   config	__class__r   r   rb      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)rh   r"   tag
iterparent)rm   nodechildr   r   r   rt      s   
zTocTreeprocessor.iterparentreturn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)rt   r/   r*   r   r1   rQ   rC   range)rm   rootelemrN   r2   r1   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. rB      zh%dN)r$   rs   rc   )rm   r{   rA   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Elementr1   attribrW   r,   rC   remove)rm   r2   elem_idanchorr{   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< | jr:|j|_	d|_|
d	| d S || d S )
Nr   Tz%spara;r   r   r   rR   r   r   )r   r   rf   r	   r1   r   rY   rZ   r[   tailinsertr,   )rm   r2   r   rX   r   r   r   add_permalink   s   

zTocTreeprocessor.add_permalinkc                   sz   t d}| j|jd< | jr!t |d}| jr| j|jd< | j|_ fdd  || d| jj	v r;| jj	d 
| |S )z' Return a string div given a toc list. divr   spanc                   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   r@   )r   
SubElementgetr1   r   )rF   parentr   itemr   linkbuild_etree_ulr   r   r     s   z6TocTreeprocessor.build_toc_div.<locals>.build_etree_ulprettify)r   r   rT   r   rR   r   rU   r1   r=   treeprocessorsrun)rm   rF   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rt|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&   rB   zdata-toc-labelF)r4   )rA   r&   r   )FN)$setiterr   r%   r+   rs   ri   rh   r"   r   r3   r.   r?   r=   r)   r   rd   r$   rk   rl   r,   r   r   re   r   rf   r   rO   r   rQ   r}   
serializerpostprocessorsr   
toc_tokenstoc)
rm   docused_idsr0   r   r1   	innertextr   r   ppr   r   r   r      sL   


(



zTocTreeprocessor.runrw   rx   )__name__
__module____qualname____doc__rb   rt   r}   r   r   r   r   r   __classcell__r   r   ro   r   rP      s    



rP   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).)rQ   rR   rU   rT   rV   rW   rX   rY   rZ   r[   rS   r   r   r\   r   )r   rn   ra   rb   )rm   kwargsro   r   r   rb   V  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)rm   r=   tocextr   r   r   extendMarkdown  s
   
zTocExtension.extendMarkdownrw   rx   c                 C  s   d| j _g | j _d S )Nr   )r=   r   r   )rm   r   r   r   r     s   zTocExtension.resetr   )	r   r   r   rP   r   rb   r   r   r   r   r   ro   r   r   R  s
    4r   c                  K  s   t di | S )Nr   )r   )r   r   r   r   makeExtension  s   r   )F)T)r4   r5   ) r   
__future__r   r   r   r   r   utilr   r   r	   r
   r   r   r   r-   r   xml.etree.ElementTreer   ElementTreer   r    rg   r!   r)   r3   r?   r.   rO   rP   r   r   r   r   r   r   <module>   s,   


A 1E