
    $gG                       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c mZ ddlmZmZmZmZ erddlmZ d4d5dZd6dZ  ej!        d          Z"d7dZ# e
d          d8d            Z$ e
d           d9d:d&            Z%d;d'Z&d;d(Z'd;d)Z(d<d*Z)d=d+Z*d>d-Z+d. Z, G d/ d0e          Z- G d1 d2e          Z.d3 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)parseBoolValueAMP_SUBSTITUTE
deprecatedHTML_PLACEHOLDER_REAtomicString)UnescapeTreeprocessor)RE_AMPN)deepcopy)TYPE_CHECKINGAnyIterator
MutableSet)MarkdownFvaluestr	separatorunicodeboolreturnc                H   |s>t          j        d|           } |                     dd                              d          } t	          j        dd|                                                                           } t	          j        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)r   r   r   s      W/var/www/html/netbox-4.1.3/venv/lib/python3.11/site-packages/markdown/extensions/toc.pyslugifyr*   &   s     @%fe44Wh//66w??F;E**002288::E6*##I..	5AAA    c                &    t          | |d          S )zP Slugify a string, to make it URL friendly while preserving Unicode characters. T)r   )r*   )r   r   s     r)   slugify_unicoder-   0   s    5)T2222r+   z^(.*)_([0-9]+)$ididsMutableSet[str]c                
   | |v s| sgt                               |           }|r>d|                    d          t          |                    d                    dz   fz  } nd| dfz  } | |v e| g|                    |            | 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/   ms      r)   uniquer8   8   s    
))2)R   	#AGGAJJAGGAJJ(9::BBB7"B ))2) GGBKKKIr+   z0Use `render_inner_html` and `striptags` instead.eletree.Elementc                &   g }|                                  D ]T}t          |t                    r(|                    t	          j        |                     ?|                    |           Ud                    |                                          S )zGet title name.r   )itertext
isinstancer   appendhtmlunescapejoinr&   )r9   textcs      r)   get_namerD   D   s~     D[[]]  a&& 	KKa(())))KKNNNN774==   r+   zIUse `run_postprocessors`, `render_inner_html` and/or `striptags` instead.TrB   mdr   strip_entitiesc                >    dfd}t          j        ||           S )zN Extract raw HTML from stash, reduce to plain text and swap with placeholder. r7   re.Match[str]r   r   c                $   	 j         j        t          |                     d                             }n,# t          t
          f$ r |                     d          cY S w xY wt          j        dd|          }rt          j        dd|          }|S )z& Substitute raw html with plain text. r   r   z	(<[^>]+>)r   z(&[\#a-zA-Z0-9]+;))	htmlStashrawHtmlBlocksr5   r4   
IndexError	TypeErrorr$   r%   )r7   rawresrE   rF   s      r)   	_html_subz#stashedHTML2text.<locals>._html_subT   s    	,,S__=CCI& 	 	 	771::	 f\2s++ 	9&.C88C
s   26 &AA)r7   rH   r   r   )r   r%   )rB   rE   rF   rP   s    `` r)   stashedHTML2textrQ   Q   s<    
 
 
 
 
 
 
 "9d333r+   c                H    t                      }|                    |           S )z+ Unescape Markdown backslash escaped text. )r   r@   )rB   rC   s     r)   r@   r@   c   s    A::dr+   c                p   |                      d          x}dk    rl|                      d|          x}dk    rP| d|          | |dz   d          } |                      d          x}dk    r|                      d|          x}dk    P|                      d          x}dk    rl|                      d|          x}dk    rP| d|          | |dz   d          } |                      d          x}dk    r|                      d|          x}dk    Pd	                    |                                           } | S )
zL Strip HTML tags and return plain text. Note: HTML entities are unaffected. z<!--z-->N   <>r    )findrA   split)rB   startends      r)   
strip_tagsr]   i   sV    IIf%%%5"
,
,5%9P9P2P#UW1W1Wvv,0S1WXX00 IIf%%%5"
,
,5%9P9P2P#UW1W1W IIcNN"5r
)
)diiU6K6K/KsPR.R.Rvv,0S1WXX00 IIcNN"5r
)
)diiU6K6K/KsPR.R.R 88DJJLL!!DKr+   c                    d| v rt          j        d|           } d| v r|                     dd          } d| v r|                     dd          } | S )z Escape character data. &z&amp;rV   z&lt;rW   z&gt;)r   r%   replace)rB   s    r)   escape_cdatara   w   sY    
d{{z'4((
d{{||C((
d{{||C((Kr+   c                h    |j         D ]}|                    |           } |                                 S )z4 Run postprocessors from Markdown instance on text. )postprocessorsrunr&   )rB   rE   pps      r)   run_postprocessorsrf      s3      vvd||::<<r+   c                    t          |                    |                     }|                    d          dz   }|                    d          }|||                                         }t          ||          S )z< Fully render inner html of an `etree` element as a string. rW   r   rV   )r@   
serializerindexrindexr&   rf   )r9   rE   rB   r[   r\   s        r)   render_inner_htmlrk      sm     BMM"%%&&D JJsOOaE
++c

Cc	?  ""DdB'''r+   rootc                   t          |                     d          d          | S t          |           } |                     d          D ]}d}t	          |          D ]o}|j        dk    rL|                    dd                              d          r#|j        pd | }|	                    |           Y|r|j        pd | |_        d}p|r|j
        pd | |_
        | S )zL Remove footnote references from a copy of the element, if any are present. supNz	.//sup/..r   r.   fnref)nextiterr   findallreversedtagget
startswithtailremoverB   )rl   parent
carry_textchilds       r)   remove_fnrefsr|      s    DIIed##+D>>D,,{++ = =
f%% 	  	 EyE!!eiib&9&9&D&DW&M&M! %
 0b>*>>
e$$$$   %
 0b>*>>

 	=#[.B<
<<FKKr+   c                   g }t          |           rN|                     d          }g |d<   |d         g}|                    |           g }| r|                     d          }|d         }g |d<   ||d         k     rg|                                 d}t          |          D ]}||d         k    r|dz  } |r|d|          }|d|          }|                    |           ||d         k    r&|r|d         d         n|                    |           nE|d                             |           |                    |           |                    |           |}| |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levelrT   r   N)lenpopr>   rs   )	toc_listordered_listlastlevelsparentstcurrent_levelto_popps	            r)   nest_toc_tokensr      s    L
8}} -||AZw-D!!!  $	QAgJMAjM vbz))

 !'**  A$'
22! 0#HfWH-F%hwh/G m,,, r
**,3 #Z(("FF1IIII
 Z ''***t$$$m,,,DI  $	L r+   c                  \     e Zd ZdZd fdZddZddZddZddZddZ	d dZ
d!dZ xZS )"TocTreeprocessorz& Step through document and build TOC. rE   r   configdict[str, Any]c                \   t                                          |           |d         | _        |d         | _        t	          |d                   dz
  | _        |d         | _        |d         | _        |d         | _        |d         | _	        t          |d	                   | _        |d
         | _        t          |d         d          | _        | j        |d         | _        |d         | _        |d         | _        t          |d         d          | _        t#          j        d          | _        t)          |d         t*                    r>d|d         v r4d |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                ,    g | ]}t          |          S  )r5   ).0xs     r)   
<listcomp>z-TocTreeprocessor.__init__.<locals>.<listcomp>  s    ,\,\,\SVV,\,\,\r+   )super__init__r   r   r5   
base_levelr*   sepr   r   r   use_anchorsr   use_permalinksr   r   r   r$   compile
header_rgxr=   r   rZ   toc_top
toc_bottom)selfrE   r   	__class__s      r)   r   zTocTreeprocessor.__init__   s   !(+ /
f[122Q6i(+&, &} 5!/|0D!E!E%+,>%?,VK-@%HH&"("5D$*+<$=$*+<$=.<VDW=XZ_.`.`*^44f[)3// 	7C6+;N4N4N,\,\VK=P=V=VWZ=[=[,\,\,\)DL$///DL!&"566DOOOr+   noder:   r   -Iterator[tuple[etree.Element, etree.Element]]c              #     K   |D ]K}| j                             |j                  s*|j        dvr!||fV  |                     |          E d{V  LdS )z? Iterator wrapper to get allowed parent and child all at once. )precodeN)r   r3   rt   
iterparent)r   r   r{   s      r)   r   zTocTreeprocessor.iterparent  s|        	2 	2E?((33 2	8X8XEk!!!??5111111111	2 	2r+   rl   elemNonec                   |                      |          D ]\  }}d                    |                                                                          }|sA|j        rg|j                                        | j        k    rEt          |          dk    r2t          t          |                    D ]}||         |k    r|||<    ndS )z Replace marker with elem. r   r   N)r   rA   r<   r&   rB   r   r   range)r   rl   r   r   rC   rB   is          r)   replace_markerzTocTreeprocessor.replace_marker  s    ood++ 	 	FQ771::<<((..00D  v !&,,..DK77CFFaKKs1vv  Atqyy#! !	 	r+   c                n    t          |j        d                   | j        z   }|dk    rd}d|z  |_        dS )z. Adjust header level according to base level. rT      zh%dN)r5   rt   r   )r   r   r   s      r)   	set_levelzTocTreeprocessor.set_level+  s9    DHRL!!DO3199E5=r+   rC   elem_idr   c                \   t          j        d          }|j        |_        d|z   |j        d<   | j        |j        d<   d|_        |D ]}|                    |           t          |          r*|                    |d                    t          |          *|                    |           d S )Na#hrefclassr   r   )etreeElementrB   attribr   r>   r   rx   )r   rC   r   anchorr   s        r)   
add_anchorzTocTreeprocessor.add_anchor2  s    s##f #gf!%!6g 	  	 DMM$!ff 	HHQqTNNN !ff 		r+   c                ^   t          j        d          }| j        du r
dt          z  n| j        |_        d|z   |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   r   r   r   )r   r   r   r	   rB   r   r   r   r   rw   insertr>   )r   rC   r   r   s       r)   add_permalinkzTocTreeprocessor.add_permalink>  s    M#&&	!0D88 $n44#2 	 $'=	 $($8	! 	=(,(<IW%! 	 VINAFHHQ	"""""HHYr+   r   listc                T   t          j        d          }| j        |j        d<   | j        r7t          j        |d          }| j        r| j        |j        d<   | j        |_        dfd	 ||           d
| j        j	        v r%| j        j	        d
         
                    |           |S )z' Return a string div given a toc list. divr   spanr   r   ry   r:   r   c                <   t          j        |d          }| D ]}t          j        |d          }t          j        |d          }|                    dd          |_        d|                    dd          z   |j        d<   |d	         r |d	         |           |S )
Nullir   namer   r   r.   r   r~   )r   
SubElementru   rB   r   )r   ry   r   itemr   linkbuild_etree_uls         r)   r   z6TocTreeprocessor.build_toc_div.<locals>.build_etree_ulZ  s    !&$//B  9 9%b$//'C00 HHVR00	&)DHHT2,>,>&>F#
# 9"N4
#3R888Ir+   prettify)r   r   ry   r:   r   r:   )r   r   r   r   r   r   r   rB   rE   treeprocessorsrd   )r   r   r   headerr   s       @r)   build_toc_divzTocTreeprocessor.build_toc_divN  s    mE"""n
7 : 	%%c622F :)-)9g&*FK
	 
	 
	 
	 
	 
	 	x%%%///G":.223777
r+   docc           	        t                      }|                                D ]+}d|j        v r |                    |j        d                    ,g }|                                D ]}t	          |j        t                    r| j                            |j                  r| 	                    |           t          t          |          | j                  }t          |          }d|j        vrCt          |                     t!          j        |          | j                  |          |j        d<   d}d|j        v rQt'          t#          |j        d                   | j                  }t)          t          |                    }|j        d= t+          |j        d                   | j        k    rat+          |j        d                   | j        k    r>|                    t+          |j        d                   |j        d         |||d           | j        r!|                     ||j        d                    | j        dvr!|                     ||j        d                    t;          |          }|                     |          }| j        r|                      ||           | j        !                    |          }	| j        j"        D ]}
|
#                    |	          }	|| j        _$        |	| j        _%        d S )Nr.   r   data-toc-labelrT   )r   r.   r   r?   r   )FN)&setrq   r   r6   r=   rt   r   r   r3   r   rk   r|   rE   r]   r8   r*   r?   r@   r   rf   ra   r5   r   r   r>   r   r   r   r   r   r   r   r   rh   rc   rd   
toc_tokenstoc)r   r   used_idsr9   r   	innerhtmlr   data_toc_labelr   r   re   s              r)   rd   zTocTreeprocessor.runm  s   55((** 	. 	.Bry  RYt_---
((** 	< 	<B"&#&& <4?+@+@+H+H <r"""-mB.?.?II	!),, ry((&,T\\$-:M:Mtx-X-XZb&c&cBIdO!##ry00%7K[A\8]8]_c_f%g%gN'
>(B(BCCD	"23rvbz??dl22s26":$/7Y7Y%%!$RVBZ io $ )*8' '    # 9OOB	$888&m;;&&r29T?;;;$Z00
  ,,; 	*S))) g  %%'( 	 	B&&++CC'r+   )rE   r   r   r   )r   r:   r   r   )rl   r:   r   r:   r   r   )r   r:   r   r   )rC   r:   r   r   r   r   )r   r   r   r:   )r   r:   r   r   )__name__
__module____qualname____doc__r   r   r   r   r   r   r   rd   __classcell__r   s   @r)   r   r      s        007 7 7 7 7 72	2 	2 	2 	2   *! ! ! !
 
 
 
           >2 2 2 2 2 2 2 2r+   r   c                  0     e Zd ZeZ fdZd ZddZ xZS )TocExtensionc                    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).)r   r   r   r   r   r   r   r   r   r   r   r*   r   r   r   )r*   r   r   r   )r   kwargsr   s     r)   r   zTocExtension.__init__  s     $ Q P M P R! e W  !"`  n" FGa >?)O.
 .
^ 	/""6"""""r+   c                    |                     |            || _        |                                  |                     ||                                           }|j                            |dd           dS )z% Add TOC tree processor to Markdown. r      N)registerExtensionrE   resetTreeProcessorClass
getConfigsr   register)r   rE   tocexts      r)   extendMarkdownzTocExtension.extendMarkdown  sj    
T"""

((T__->->??
""65!44444r+   r   r   c                6    d| j         _        g | j         _        d S )Nr   )rE   r   r   )r   s    r)   r   zTocExtension.reset  s    r+   )r   r   )	r   r   r   r   r   r   r   r   r   r   s   @r)   r   r     sb        )2# 2# 2# 2# 2#h5 5 5               r+   r   c                     t          di | S )Nr   )r   )r   s    r)   makeExtensionr     s    !!&!!!r+   )F)r   r   r   r   r   r   r   r   )r   r   r   r   r   r   )r.   r   r/   r0   r   r   )r9   r:   r   r   )T)rB   r   rE   r   rF   r   r   r   )rB   r   r   r   )rB   r   rE   r   r   r   )r9   r:   rE   r   r   r   )rl   r:   r   r:   )0r   
__future__r   r   r   r   r   utilr   r	   r
   r   r   r   serializersr   r$   r?   r    copyr   xml.etree.ElementTreer   ElementTreetypingr   r   r   r   markdownr   r*   r-   r   r2   r8   rD   rQ   r@   r]   ra   rf   rk   r|   r   r   r   r   r   r+   r)   <module>r     s    # " " " " "       * * * * * * ` ` ` ` ` ` ` ` ` ` ` ` ` ` 2 2 2 2 2 2             				            % % % % % % % % % ; ; ; ; ; ; ; ; ; ; ; ; "!!!!!!B B B B B3 3 3 3
 RZ*++
	 	 	 	 >??	! 	! 	! @?	! WXX4 4 4 4 YX4"      	 	 	 	   
( 
( 
( 
(   .> > >Bp p p p p} p p pfB  B  B  B  B 9 B  B  B J" " " " "r+   