
    $gFA                       d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 dd	lmZ dd
lmZ ddlmZ ddlZddlZddlmc mZ ej        dz   ej        z   Zej        dz   ej        z   Z ej        d          Z G d de          Z G d de          Z G d de          Z G d de
          Z G d de
          Z G d de          Z d Z!dS )z
Adds footnote handling to Python-Markdown.

See the [documentation](https://Python-Markdown.github.io/extensions/footnotes)
for details.
    )annotations   )	Extension   )BlockProcessor)InlineProcessor)Treeprocessor)Postprocessor)util)OrderedDictNzz1337820767766393qqqq3936677670287331zzz(fnref)(\d+)c                  l     e Zd ZdZ fdZd ZddZdddZddZddZ	ddZ
ddZdd dZd!dZ xZS )"FootnoteExtensionz Footnote Extension. c                    ddgddgddgddgd	d
gddgd| _         	  t                      j        di | d| _        i | _        t                      | _        |                                  dS )z Setup configs. z///Footnotes Go Here///z1The text string that marks where the footnotes goFz9Avoid name collisions across multiple calls to `reset()`.z&#8617;zCThe text string that links from the footnote to the reader's place.{}zCThe text string that links from the reader's place to the footnote.z$Jump back to footnote %d in the textznThe text string used for the title HTML attribute of the backlink. %d will be replaced by the footnote number.:zFootnote separator.)PLACE_MARKER
UNIQUE_IDSBACKLINK_TEXTSUPERSCRIPT_TEXTBACKLINK_TITLE	SEPARATORr   N )configsuper__init__unique_prefix
found_refsset	used_refsreset)selfkwargs	__class__s     ]/var/www/html/netbox-4.1.3/venv/lib/python3.11/site-packages/markdown/extensions/footnotes.pyr   zFootnoteExtension.__init__)   s    
 *+^ R ` [! 7> *%
 
, 	/""6""" *,#&55

    c                    |                     |            |j        | _        || _        |j        j                            t          |           dd           d}|j                            t          ||           dd           |j                            t          |           dd           |j                            t          |           dd           |j                            t          |           dd           d	S )
z Add pieces to Markdown. footnote   z\[\^([^\]]*)\]   2   zfootnote-duplicate      N)registerExtensionparsermdblockprocessorsregisterFootnoteBlockProcessorinlinePatternsFootnoteInlineProcessortreeprocessorsFootnoteTreeprocessorFootnotePostTreeprocessorpostprocessorsFootnotePostprocessor)r#   r1   FOOTNOTE_REs      r&   extendMarkdownz FootnoteExtension.extendMarkdownL   s    
T"""i
	!**+A$+G+GUWXXX (
""#:;#M#Mz[^___ 	""#8#>#>
BOOO 	""#<T#B#BDXZ\]]] 	""#8#>#>
BOOOOOr'   returnNonec                    t                      | _        | xj        dz  c_        i | _        t	                      | _        dS )z> Clear footnotes on reset, and prepare for distinct document. r   N)r   	footnotesr   r   r    r!   r#   s    r&   r"   zFootnoteExtension.resete   s7    0;ar'   F	referencestrfoundboolc                $   |s|S |}|| j         v r|                    |                                 d          \  }}t                              |          }|rRd|                    d          t          |                    d                    dz   |                                 |fz  }nd|d|                                 |fz  }|| j         v | j                             |           || j        v r| j        |xx         dz  cc<   n
d| j        |<   |S )z1 Get a unique reference if there are duplicates. r   %s%d%s%sr   )	r!   splitget_separator	RE_REF_IDmatchgroupintaddr   )r#   rC   rE   original_refrefrestms          r&   
unique_refzFootnoteExtension.unique_refl   s&    	 4>))!(:(:(<(<a@@IC$$A N&!''!**c!''!**ooa6GI[I[I]I]_c)dd		&#q$2D2D2F2F)MM	 4>)) 	9%%%4?**OL)))Q.)))),-DOL)r'   rootetree.Element0tuple[etree.Element, etree.Element, bool] | Nonec                ,      fd |          }|S )z@ Return ElementTree Element that contains Footnote placeholder. c                0   | D ]}|j         r8|j                                                 d                    dk    r|| dfc S |j        r8|j                                                d                    dk    r|| dfc S  |          }||c S d S )Nr   TF)textfind	getConfigtail)elementchild	child_resfinderr#   s      r&   rb   z:FootnoteExtension.findFootnotesPlaceholder.<locals>.finder   s      	% 	%: 4zt~~n'E'EFFKK$gt3333: 5zt~~n'E'EFFKK$gu4444"F5MM	($$$$ )4r'   r   )r#   rU   resrb   s   `  @r&   findFootnotesPlaceholderz*FootnoteExtension.findFootnotesPlaceholder   s6    	 	 	 	 	 	 fTll
r'   idr[   c                    || j         |<   dS )z' Store a footnote for later retrieval. NrA   )r#   re   r[   s      r&   setFootnotezFootnoteExtension.setFootnote   s    !rr'   c                ,    |                      d          S )z Get the footnote separator. r   )r]   rB   s    r&   rJ   zFootnoteExtension.get_separator   s    ~~k***r'   c                    |                      d          rd|                                 | j        |fz  S d                    |                                 |          S )z Return footnote link id. r   z	fn%s%d-%szfn{}{})r]   rJ   r   format)r#   re   s     r&   makeFootnoteIdz FootnoteExtension.makeFootnoteId   sW    >>,'' 	=$"4"4"6"68JB!OOO??4#5#5#7#7<<<r'   c                
   |                      d          r3|                     d|                                 | j        |fz  |          S |                     d                    |                                 |          |          S )z Return footnote back-link id. r   zfnref%s%d-%sz	fnref{}{})r]   rT   rJ   r   rk   )r#   re   rE   s      r&   makeFootnoteRefIdz#FootnoteExtension.makeFootnoteRefId   sy    >>,'' 	X??>T5G5G5I5I4K]_a4b#bdijjj??;#5#5d6H6H6J6JB#O#OQVWWWr'   etree.Element | Nonec                   t          | j                                                  sdS t          j        d          }|                    dd           t          j        |d           t          j        |d          }t          j        d          }|                     d                              dd	          }t          | j                                        d
          D ]\  }}t          j        |d          }|                    d| 
                    |                     | j                            || j        |                    t          |          D ],}	|                    |	           |                    |	           -t          j        d          }
|
                    dd|                     |          z              |
                    dd           |
                    d|                    |                     t"          |
_        t'          |          rh|d         }|j        dk    r+|j        t*          z   |_        |                    |
           ~t          j        |d          }|                    |
           |S )z/ Return `div` of footnotes as `etree` Element. Ndivclassr)   hrolr   z%dr   r   )startlire   ahref#footnote-backreftitlerZ   p)listrA   keysetreeElementr    
SubElementr]   replace	enumeraterl   r0   
parseChunkappendremovern   rk   FN_BACKLINK_TEXTr[   lentagNBSP_PLACEHOLDER)r#   rU   rq   rt   surrogate_parentbacklink_titleindexre   rv   elbacklinknoder|   s                r&   makeFootnotesDivz"FootnoteExtension.makeFootnotesDiv   sS    DN''))** 	4mE""$$$d###c4(( =// (899AA$MM"4>#6#6#8#8BBB 	' 	'IE2!"d++BFF4,,R00111 K""#3T^B5GHHH+,, , ,		" ''++++}S))HLLt'='=b'A'A!ABBBLL"4555LL%%e,,   -HM2ww '"v8s?? $	,< <DIKK))))(S11AHHX&&&
r'   )r>   r?   )F)rC   rD   rE   rF   r>   rD   )rU   rV   r>   rW   )re   rD   r[   rD   r>   r?   )r>   rD   )re   rD   r>   rD   )re   rD   rE   rF   r>   rD   )rU   rV   r>   ro   )__name__
__module____qualname____doc__r   r=   r"   rT   rd   rh   rJ   rl   rn   r   __classcell__r%   s   @r&   r   r   &   s        ! ! ! ! !FP P P2       *   (" " " "+ + + += = = =X X X X X* * * * * * * *r'   r   c                  p     e Zd ZdZ ej        dej                  Zd fdZddZ	ddZ
ddZddZ xZS )r4   z7 Find all footnote references and store for later use. z!^[ ]{0,3}\[\^([^\]]*)\]:[ ]*(.*)$rA   r   c                b    t                                          |j                   || _        d S N)r   r   r0   rA   )r#   rA   r%   s     r&   r   zFootnoteBlockProcessor.__init__   s)    )***"r'   parentrV   blockrD   r>   rF   c                    dS )NTr   )r#   r   r   s      r&   testzFootnoteBlockProcessor.test   s    tr'   blocks	list[str]c                   |                     d          }| j                            |          }|r?|                    d          }|                    d          g}||                                d                             d          }| j                            |          }|r|d|                                                             d          }	d                    |d         | 	                    |	          g                              d          |d<   |
                    d||                                d                    nnd                    |d         | 	                    |          g                              d          |d<   |                    |                     |                     d                    |          }
| j                            ||
                                           |d|                                                                         rC|
                    d|d|                                                             d                     dS |
                    d|           dS )	z- Find, set, and remove footnote definitions. r   r   r   N
z

TF)popREsearchrM   endlstripru   rstripjoindetabinsertstripextenddetectTabbedrA   rh   )r#   r   r   r   rS   re   	fn_blockstherestm2beforer)   s              r&   runzFootnoteBlockProcessor.run   s   

1GNN5!! 	BI AEEGGHHo,,T22G((B < !"((**-44T::#yy)A,

68J8J)KLLSSTXYY	!a!56666  $yy)A,

78K8K)LMMSSTXYY	!   !2!26!:!:;;;{{9--HN&&r8??+<+<===ZaggiiZ &&(( Aaz		z!2!9!9$!?!?@@@4aur'   c                   g }|r|d                              d          r|                    d          }| j                            |          }|r|d|                                                             d          }|                    |                     |                     |                    d||                                d                    n-|                    |                     |                     nn||S )z Find indented text and remove indent before further processing.

        Returns:
            A list of blocks with indentation removed.
        r       Nr   )	
startswithr   r   r   ru   r   r   r   r   )r#   r   r   r   rS   r   s         r&   r   z#FootnoteBlockProcessor.detectTabbed  s     	 	ay##E** 

1GNN5)) 8 #:AGGII:.55d;;F$$TZZ%7%7888MM!U17799::%6777 $$TZZ%6%67777 '  	( r'   c                    |                     d          }t          |          D ]'\  }}|                    d          r|dd         ||<   (d                    |          S )z Remove one level of indent from a block.

        Preserve lazily indented blocks by only removing indent from indented lines.
        r   r      N)rI   r   r   r   )r#   r   linesilines        r&   r   zFootnoteBlockProcessor.detab%  sh    
 D!! '' 	$ 	$GAtu%% $8ayyr'   rA   r   )r   rV   r   rD   r>   rF   )r   rV   r   r   r>   rF   )r   r   r>   r   )r   rD   r>   rD   )r   r   r   r   recompile	MULTILINEr   r   r   r   r   r   r   r   s   @r&   r4   r4      s        AA	8",	G	GB# # # # # #   " " " "H   :	  	  	  	  	  	  	  	 r'   r4   c                  ,     e Zd ZdZd fdZddZ xZS )r6   zC `InlineProcessor` for footnote markers in a document's body text. patternrD   rA   r   c                X    t                                          |           || _        d S r   )r   r   rA   )r#   r   rA   r%   s      r&   r   z FootnoteInlineProcessor.__init__4  s&    !!!"r'   rS   re.Match[str]datar>   3tuple[etree.Element | None, int | None, int | None]c                   |                     d          }|| j        j                                        v r<t          j        d          }t          j        |d          }|                    d| j                            |d                     |                    dd| j                            |          z              |                    d	d
           | j        	                    d          
                    t          | j        j                                                                      |          dz             |_        ||                    d          |                    d          fS dS )Nr   suprw   re   T)rE   rx   ry   rr   zfootnote-refr   r   )NNN)rM   rA   r~   r   r   r   r    rn   rl   r]   rk   r}   r   r[   ru   r   )r#   rS   r   re   r   rw   s         r&   handleMatchz#FootnoteInlineProcessor.handleMatch8  s4   WWQZZ)..0000-&&C c**AGGD$.::2T:JJKKKEE&# = =b A AABBBEE'>***^--.@AAHHT^-224455;;B??!C AF 

AEE!HH,,##r'   )r   rD   rA   r   )rS   r   r   rD   r>   r   )r   r   r   r   r   r   r   r   s   @r&   r6   r6   1  sW        MM# # # # # #$ $ $ $ $ $ $ $r'   r6   c                  :    e Zd ZdZddZddZddZddZddZdS )r9   z% Amend footnote div with duplicates. rA   r   c                    || _         d S r   rg   r#   rA   s     r&   r   z"FootnotePostTreeprocessor.__init__K      "r'   rv   rV   
duplicatesrN   r>   r?   c                D   |                     d          D ]}|j                            dd          dk    r|j        d                             | j                                        d          \  }}g }t          d|dz             D ]c}t          j        |          }d||| j                                        |fz  |j        d<   |	                    |           | xj
        dz  c_
        dt          |          d	         }	|D ]}|		                    |            d
S 
d
S )zF Adjust current `li` and add the duplicates: `fnref2`, `fnref3`, etc. rw   rr    rz   rx   r   r   rH   rZ   N)iterattribgetrI   rA   rJ   rangecopydeepcopyr   offsetr}   )
r#   rv   r   linkrQ   rR   linksr   sib_linkr   s
             r&   add_duplicatesz(FootnotePostTreeprocessor.add_duplicatesN  s,   GGCLL 	 	D{w++/AAA K/55dn6R6R6T6TVWXX	T "1j1n55 % %E#}T22H.8CHdHdHfHfhl;m.mHOF+LL***KK1$KKK"XXb\! $ $DIIdOOOO B	 	r'   c                0   |j                             dd                              | j                                        d          \  }}d                    || j                                        |          }| j        j                            |d          S )z3 Get the number of duplicate refs of the footnote. re   r   r   z	{}ref{}{}r   )r   r   rI   rA   rJ   rk   r   )r#   rv   fnrR   link_ids        r&   get_num_duplicatesz,FootnotePostTreeprocessor.get_num_duplicatesb  sz    9==r**001M1M1O1OQRSSD$$R)E)E)G)GNN~(,,Wa888r'   r   c                    t          |          D ]3}|                     |          }|dk    r|                     ||           4dS )z= Find duplicate footnotes and format and add the duplicates. r   N)r}   r   r   )r#   r   rv   counts       r&   handle_duplicatesz+FootnotePostTreeprocessor.handle_duplicatesh  sV    v,, 	/ 	/B ++B//Eqyy##B...	/ 	/r'   rU   c                    d| _         |                    d          D ]N}|j                            dd          dk    r-|                    d          D ]}|                     |            OdS )z= Crawl the footnote div and add missing duplicate footnotes. r   rq   rr   r   r)   rt   N)r   r   r   r   r   )r#   rU   rq   rt   s       r&   r   zFootnotePostTreeprocessor.runq  s|    99U## 	 	Cz~~gr**j88 ((4..  B**2...	 	r'   Nr   )rv   rV   r   rN   r>   r?   )rv   rV   r>   rN   )r   rV   r>   r?   rU   rV   r>   r?   )	r   r   r   r   r   r   r   r   r   r   r'   r&   r9   r9   H  s~        //# # # #   (9 9 9 9/ / / /	 	 	 	 	 	r'   r9   c                  "    e Zd ZdZddZdd	Zd
S )r8   z3 Build and append footnote div to end of document. rA   r   c                    || _         d S r   rg   r   s     r&   r   zFootnoteTreeprocessor.__init__  r   r'   rU   rV   r>   r?   c                   | j                             |          }|| j                             |          }|ry|\  }}}t          |                              |          }|r-|                    |           |                    ||           d S |                    |dz   |           d |_        d S |                    |           d S d S )Nr   )	rA   r   rd   r}   r   r   r   r^   r   )r#   rU   footnotesDivresultr`   r   isTextinds           r&   r   zFootnoteTreeprocessor.run  s    ~66t<<#^<<TBBF 
*(.%vv6ll((// &MM%(((MM#|44444MM#'<888!%EJJJL))))) $#r'   Nr   r   r   r   r   r   r   r   r   r'   r&   r8   r8   }  sB        ==# # # #* * * * * *r'   r8   c                  "    e Zd ZdZd
dZddZd	S )r;   z* Replace placeholders with html entities. rA   r   c                    || _         d S r   rg   r   s     r&   r   zFootnotePostprocessor.__init__  r   r'   r[   rD   r>   c                    |                     t          | j                            d                    }|                     t          d          S )Nr   z&#160;)r   r   rA   r]   r   )r#   r[   s     r&   r   zFootnotePostprocessor.run  sA    ||dn66GG
 
 ||,h777r'   Nr   )r[   rD   r>   rD   r   r   r'   r&   r;   r;     sB        44# # # #8 8 8 8 8 8r'   r;   c                     t          di | S )z/ Return an instance of the `FootnoteExtension` r   )r   )r$   s    r&   makeExtensionr     s    &&v&&&r'   )"r   
__future__r   r   r   r2   r   inlinepatternsr   r7   r	   r:   r
   r   collectionsr   r   r   xml.etree.ElementTreer   ElementTreeSTXETXr   r   r   rK   r   r4   r6   r9   r8   r;   r   r   r'   r&   <module>r      s1    # " " " " "       , , , , , , , , , , , , * * * * * * * * * * * *       # # # # # # 				  % % % % % % % % %844tx? 844tx? BJ''	o o o o o	 o o odV  V  V  V  V ^ V  V  V r$ $ $ $ $o $ $ $.2 2 2 2 2 2 2 2j* * * * *M * * *.	8 	8 	8 	8 	8M 	8 	8 	8' ' ' ' 'r'   