U
    Sh                     @   s   d dl Z d dlmZmZmZmZ d dlmZ d dlm	Z	 d dl
mZ deeeeeegef  eeeedd	ddZee eegef eeeeee	gdf dddZeedddZeee edddZdS )    N)CallableListOptionalSet)
MarkdownIt	StateCore)Token      F   ¶T)	md	min_level	max_level	slug_func	permalinkpermalinkSymbolpermalinkBeforepermalinkSpacereturnc           	   
   C   s8   t t||d }| jjdt||p&t|||| dS )u  Plugin for adding header anchors, based on
    `markdown-it-anchor <https://github.com/valeriangalliat/markdown-it-anchor>`__

    .. code-block:: md

        # Title String

    renders as:

    .. code-block:: html

        <h1 id="title-string">Title String <a class="header-anchor" href="#title-string">¶</a></h1>

    :param min_level: minimum header level to apply anchors
    :param max_level: maximum header level to apply anchors
    :param slug_func: function to convert title text to id slug.
    :param permalink: Add a permalink next to the title
    :param permalinkSymbol: the symbol to show
    :param permalinkBefore: Add the permalink before the title, otherwise after
    :param permalinkSpace: Add a space between the permalink and the title

    Note, the default slug function aims to mimic the GitHub Markdown format, see:

    - https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb
    - https://gist.github.com/asabaylus/3071099

    r
   anchorN)listrangecoreZrulerpush_make_anchors_funcslugify)	r   r   r   r   r   r   r   r   selected_levels r   P/root/rtd-docs/venv/lib/python3.8/site-packages/mdit_py_plugins/anchors/index.pyanchors_plugin	   s    %r    )r   r   r   r   r   r   r   c                    s"   t d d fdd}|S )N)stater   c           
   	      s2  t  }t| jD ]\}}|jdkr&qt|jd }|kr>q| j|d  }|jd k	sZtddd |jD }t	||}|
d|  rtddd}|
d	d
 |
dd|  |tddddtdddg}	r|	rtdddddgng  |j |_q|jr"tdddddgng |	  qd S )NZheading_openr
    c                 s   s   | ]}|j d kr|jV  qdS ))textZcode_inlineN)typecontent).0childr   r   r   	<genexpr>N   s   
z;_make_anchors_func.<locals>._anchor_func.<locals>.<genexpr>id	link_openaclasszheader-anchorhref#Z
html_blockr   )r%   Z
link_closer#    )set	enumeratetokensr$   inttagchildrenAssertionErrorjoinunique_slugZattrSetr	   extend)
r!   slugsidxtokenlevelZinline_tokentitleslugr*   Zlink_tokensr   r   r   r   r   r   r   r   _anchor_funcD   sP    



z(_make_anchors_func.<locals>._anchor_funcr   )r   r   r   r   r   r   rB   r   rA   r   r   <   s    /r   )r?   r   c                 C   s   t dd|   ddS )Nz[^\w\u4e00-\u9fff\- ]r"   r0   -)resubstriplowerreplace)r?   r   r   r   r   v   s    r   )r@   r;   r   c                 C   s6   | }d}||kr(|  d| }|d7 }q| | |S )Nr
   rC   )add)r@   r;   Zuniqir   r   r   r9   z   s    

r9   )r
   r   NFr   FT)rD   typingr   r   r   r   Zmarkdown_itr   Zmarkdown_it.rules_corer   Zmarkdown_it.tokenr	   r4   strboolr    r   r   r9   r   r   r   r   <module>   s@          4: