o
    7d6                     @   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   nC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\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)id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   
r1   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/   r7   r8   r9   r   r6   r   stashedHTML2textD   s   r:   c                 C   s   t  }|| S )z Unescape escaped text. )r
   r,   )r/   r0   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levelr   N)lenpopr*   reversed)	toc_listZordered_listlastZlevelsparentstZcurrent_levelZto_poppr   r   r   nest_toc_tokens[   s>   







&rF   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__rH   rI   r"   
base_levelr   seprK   r   use_anchorsrM   use_permalinksrO   rP   r   compile
header_rgxr)   strsplittoc_top
toc_bottom)selfr7   config	__class__r   r   rW      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)rb   nodechildr   r   r   ri      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)ri   r-   r(   r   r/   rH   r>   range)rb   rootelemrE   r0   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"   rh   rX   )rb   rn   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/   attribrM   r*   r>   remove)rb   r0   elem_idanchorrn   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 )Nrs   Tz%spara;rt   ru   rv   rI   )	rw   rx   r[   r   r/   ry   rO   rP   r*   )rb   r0   r{   rN   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. divrv   spanZtoctitlec                    sl   t |d}| D ]+}t |d}t |d}|dd|_d|dd |jd< |d	 r3 |d	 | q|S )
Nullirs   namer   rt   r$   ru   r;   )rw   
SubElementgetr/   ry   )rA   parentr   itemr   linkbuild_etree_ulr   r   r      s   z6TocTreeprocessor.build_toc_div.<locals>.build_etree_ulZprettify)
rw   rx   rK   ry   rI   r   r/   r7   treeprocessorsrun)rb   rA   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$   r=   zdata-toc-labelF)r8   )r<   r$   r   )FN)$setiterry   r#   r)   rh   r^   r]   r    rr   r1   r,   r:   r7   r'   r   rY   r"   r`   ra   r*   r   r   rZ   r}   r[   r~   rF   r   rH   rp   
serializerZpostprocessorsr   
toc_tokenstoc)
rb   docZused_idsr.   r   r/   Z	innertextr   r   ppr   r   r   r     sL   


(



zTocTreeprocessor.run)__name__
__module____qualname__rW   ri   rp   rr   r}   r~   r   r   __classcell__r   r   rd   r   rG      s    rG   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.rR   z Word separator. Defaults to "-".rq   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).)rH   rI   rK   rL   rM   rN   rO   rP   rJ   r   r   rQ   r   )r   rc   rV   rW   )rb   kwargsrd   r   r   rW   D  sD   'zTocExtension.__init__c                 C   s<   | |  || _|   | ||  }|j|dd d S )Nr      )ZregisterExtensionr7   resetTreeProcessorClassZ
getConfigsr   register)rb   r7   Ztocextr   r   r   extendMarkdownn  s
   
zTocExtension.extendMarkdownc                 C   s   d| j _g | j _d S )Nr   )r7   r   r   )rb   r   r   r   r   u  s   zTocExtension.reset)	r   r   r   rG   r   rW   r   r   r   r   r   rd   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   xml.etree.ElementTreerw   ElementTreer   r   r\   r   r'   r1   r:   r,   rF   rG   r   r   r   r   r   r   <module>   s*    


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