
    eiK                       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j2                  d      Z ej2                  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! G d de      Z"d Z#y) 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+)z(?<!!)\[\^([^\]]*)\](?!\s*:)c                  |     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
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dgd| _         	 t        |   di | d| _        i | _        t               | _        | j                  d| j                  d      j                  dd             | j                          y)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.z{}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.Tz]Order footnote labels by definition order (True) or by document order (False). Default: True.)PLACE_MARKER
UNIQUE_IDSBACKLINK_TEXTSUPERSCRIPT_TEXTBACKLINK_TITLE	SEPARATORUSE_DEFINITION_ORDERr   r   z%dN )configsuper__init__unique_prefix
found_refsset	used_refs	setConfig	getConfigreplacereset)selfkwargs	__class__s     _/var/www/html/glpi_dashboard/venv/lib/python3.12/site-packages/markdown/extensions/footnotes.pyr   zFootnoteExtension.__init__*   s    
 *+^ R ` [! 7> * !%+
6 	/"6" *,#&5 	'8H)I)Q)QRVX\)]^

    c                Z   |j                  |        |j                  | _        || _        |j                  j                  j	                  t        |       dd       d}|j                  j	                  t        ||       dd       |j                  j	                  t        |       dd       | j                  d      s&|j                  j	                  t        |       dd       |j                  j	                  t        |       d	d
       |j                  j	                  t        |       dd       y)z Add pieces to Markdown. footnote   z\[\^([^\]]*)\]   2   r   zfootnote-reorder   zfootnote-duplicate      N)registerExtensionparsermdblockprocessorsregisterFootnoteBlockProcessorinlinePatternsFootnoteInlineProcessortreeprocessorsFootnoteTreeprocessorr#   FootnoteReorderingProcessorFootnotePostTreeprocessorpostprocessorsFootnotePostprocessor)r&   r5   FOOTNOTE_REs      r)   extendMarkdownz FootnoteExtension.extendMarkdownU   s    
T"ii
		!!**+A$+GUWX (
""#:;#Mz[^_ 	""#8#>
BO
 ~~45&&'B4'HJ\^`a 	""#<T#BDXZ\] 	""#8#>
BOr*   c                    g | _         t               | _        | xj                  dz  c_        i | _        t               | _        y)z> Clear footnotes on reset, and prepare for distinct document. r   N)footnote_orderr   	footnotesr   r   r    r!   r&   s    r)   r%   zFootnoteExtension.resett   s4    )+0;ar*   c                   |s|S |}|| j                   v r|j                  | j                         d      \  }}t        j	                  |      }|rBd|j                  d      t        |j                  d            dz   | j                         |fz  }nd|d| j                         |fz  }|| j                   v r| j                   j                  |       || j                  v r| j                  |xx   dz  cc<   |S 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&   	referencefoundoriginal_refrefrestms          r)   
unique_refzFootnoteExtension.unique_ref|   s     4>>)!(:(:(<a@IC$A&!''!*c!''!*oa6GI[I[I]_c)dd	&#q$2D2D2F)MM	 4>>) 	9%4??*OOL)Q.)  -.DOOL)r*   c                &      fd |      }|S )z@ Return ElementTree Element that contains Footnote placeholder. c                0   | D ]  }|j                   r4|j                   j                  j                  d            dkD  r|| dfc S |j                  r4|j                  j                  j                  d            dkD  r|| dfc S  |      }||c S  y )Nr   TF)textfindr#   tail)elementchild	child_resfinderr&   s      r)   r`   z:FootnoteExtension.findFootnotesPlaceholder.<locals>.finder   s      	%::zzt~~n'EFK$gt33::zzt~~n'EFK$gu44"5M	($$	% r*   r   )r&   rootresr`   s   `  @r)   findFootnotesPlaceholderz*FootnoteExtension.findFootnotesPlaceholder   s    	 Tl
r*   c                "    || j                   |<   y)z' Store a footnote for later retrieval. NrE   )r&   idrZ   s      r)   setFootnotezFootnoteExtension.setFootnote   s    !rr*   c                X    || j                   vr| j                   j                  |       yy)z7 Store a footnote reference id in order of appearance. N)rD   appendr&   rf   s     r)   addFootnoteRefz FootnoteExtension.addFootnoteRef   s)    T(((&&r* )r*   c                $    | j                  d      S )z Get the footnote separator. r   )r#   rF   s    r)   rJ   zFootnoteExtension.get_separator   s    ~~k**r*   c                    | j                  d      r d| j                         | j                  |fz  S dj                  | j                         |      S )z Return footnote link id. r   z	fn%s%d-%szfn{}{})r#   rJ   r   formatrj   s     r)   makeFootnoteIdz FootnoteExtension.makeFootnoteId   sI    >>,'$"4"4"68J8JB!OOO??4#5#5#7<<r*   c                    | j                  d      r0| j                  d| j                         | j                  |fz  |      S | j                  dj	                  | j                         |      |      S )z Return footnote back-link id. r   zfnref%s%d-%sz	fnref{}{})r#   rV   rJ   r   rn   )r&   rf   rQ   s      r)   makeFootnoteRefIdz#FootnoteExtension.makeFootnoteRefId   sd    >>,'??>T5G5G5I4K]K]_a4b#bdijj??;#5#5d6H6H6JB#OQVWWr*   c                   t        | j                  j                               syt        j                  d      }|j                  dd       t        j                  |d       t        j                  |d      }t        j                  d      }t        | j                  j                         d      D ]  \  }}t        j                  |d	      }|j                  d
| j                  |             | j                  j                  || j                  |          t        |      D ]$  }|j                  |       |j                  |       & t        j                  d      }	|	j                  dd| j                  |      z          |	j                  dd       |	j                  d| j                  d      j                  |             t         |	_        t%        |      s,|d   }
|
j&                  dk(  r+|
j"                  t(        z   |
_        |
j                  |	       kt        j                  |d      }|j                  |	        |S )z/ Return `div` of footnotes as `etree` Element. Ndivclassr,   hrolr   startlirf   ahref#footnote-backreftitler   rY   p)listrE   keysetreeElementr    
SubElement	enumeratero   r4   
parseChunkri   removerq   r#   rn   FN_BACKLINK_TEXTrZ   lentagNBSP_PLACEHOLDER)r&   ra   rs   rv   surrogate_parentindexrf   ry   elbacklinknoder   s               r)   makeFootnotesDivz"FootnoteExtension.makeFootnotesDiv   s    DNN'')*mmE"$d#c4( ==/"4>>#6#6#8B 	'IE2!!"d+BFF4,,R01 KK""#3T^^B5GH+, ,		" ''+, }}S)HLLt'='=b'A!ABLL"45LL/077> -HM2w"v88s? $		,< <DIKK)((S1AHHX&5	'6 
r*   )returnNone)F)rP   strrQ   boolr   r   )ra   etree.Elementr   z0tuple[etree.Element, etree.Element, bool] | None)rf   r   rZ   r   r   r   )rf   r   r   r   )r   r   )rf   r   r   r   )rf   r   rQ   r   r   r   )ra   r   r   zetree.Element | None)__name__
__module____qualname____doc__r   rB   r%   rV   rc   rg   rk   rJ   ro   rq   r   __classcell__r(   s   @r)   r   r   '   sP    )VP>*!	9("+
+=X'r*   r   c                  ~     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 )r8   z4 Find footnote definitions and store for later use. z!^[ ]{0,3}\[\^([^\]]*)\]:[ ]*(.*)$c                F    t         |   |j                         || _        y N)r   r   r4   rE   )r&   rE   r(   s     r)   r   zFootnoteBlockProcessor.__init__   s    ))*"r*   c                     y)NTr   )r&   parentblocks      r)   testzFootnoteBlockProcessor.test   s    r*   c                   |j                  d      }| j                  j                  |      }|r|j                  d      }|j                  d      g}||j	                         d j                  d      }| j                  j                  |      }|r}|d|j                          j                  d      }	dj                  |d   | j                  |	      g      j                  d      |d<   |j                  d||j                         d        nWdj                  |d   | j                  |      g      j                  d      |d<   |j                  | j                  |             dj                  |      }
| j                  j                  ||
j                                |d|j                          j                         r2|j                  d|d|j                          j                  d             y|j                  d|       y)	z- Find, set, and remove footnote definitions. r   r   r   N
z

TF)popREsearchrM   endlstriprx   rstripjoindetabinsertstripextenddetectTabbedrE   rg   )r&   r   blocksr   rU   rf   	fn_blockstherestm2beforer,   s              r)   runzFootnoteBlockProcessor.run   s   

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

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

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

        Returns:
            A list of blocks with indentation removed.
        r       Nr   )	
startswithr   r   r   rx   r   ri   r   r   )r&   r   r   r   rU   r   s         r)   r   z#FootnoteBlockProcessor.detectTabbed  s     	ay##E*

1GGNN5) #:AGGI.55d;F$$TZZ%78MM!U1779:%67 	 $$TZZ%67 ) ( r*   c                    |j                  d      }t        |      D ]  \  }}|j                  d      s|dd ||<   ! dj                  |      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.detab8  sV    
 D! ' 	$GAtu%8a	$ yyr*   rE   r   )r   r   r   r   r   r   )r   r   r   	list[str]r   r   )r   r   r   r   )r   r   r   r   )r   r   r   r   recompile	MULTILINEr   r   r   r   r   r   r   r   s   @r)   r8   r8      s7    >	8",,	GB##J:	 r*   r8   c                  ,     e Zd ZdZd fdZddZ xZS )r:   zC `InlineProcessor` for footnote markers in a document's body text. c                2    t         |   |       || _        y r   )r   r   rE   )r&   patternrE   r(   s      r)   r   z FootnoteInlineProcessor.__init__G  s    !"r*   c                   |j                  d      }|| j                  j                  j                         v r| j                  j                  |       | j                  j	                  d      s)| j                  j
                  j                  |      dz   }n?t        | j                  j                  j                               j                  |      dz   }t        j                  d      }t        j                  |d      }|j                  d| j                  j                  |d             |j                  dd	| j                  j                  |      z          |j                  d
d       | j                  j	                  d      j                  |      |_        ||j!                  d      |j#                  d      fS y)Nr   r   suprz   rf   T)rQ   r{   r|   rt   zfootnote-refr   r   )NNN)rM   rE   r   rk   r#   rD   r   r   r   r   r   r    rq   ro   rn   rZ   rx   r   )r&   rU   datarf   footnote_numr   rz   s          r)   handleMatchz#FootnoteInlineProcessor.handleMatchK  sR   WWQZ))..00NN))"->>++,BC#~~<<BB2FJ  $DNN$<$<$A$A$CDJJ2NQRR--&C  c*AGGD$..::2T:JKEE&# = =b AABEE'>*^^--.@AHHVAF
AEE!H,,#r*   )r   r   rE   r   )rU   zre.Match[str]r   r   r   z3tuple[etree.Element | None, int | None, int | None])r   r   r   r   r   r   r   r   s   @r)   r:   r:   D  s    M#$r*   r:   c                  8    e Zd ZdZddZd	dZd
dZddZddZy)r>   z% Amend footnote div with duplicates. c                    || _         y r   re   r&   rE   s     r)   r   z"FootnotePostTreeprocessor.__init__e  	    "r*   c                2   |j                  d      D ]  }|j                  j                  dd      dk(  s$|j                  d   j                  | j                  j                         d      \  }}g }t        d|dz         D ]k  }t        j                  |      }d||| j                  j                         |fz  |j                  d<   |j                  |       | xj                  dz  c_
        m t        |      d	   }	|D ]  }|	j                  |         y
 y
)zF Adjust current `li` and add the duplicates: `fnref2`, `fnref3`, etc. rz   rt    r}   r{   r   r   rH   rY   N)iterattribgetrI   rE   rJ   rangecopydeepcopyri   offsetr   )
r&   ry   
duplicateslinkrS   rT   linksr   sib_linkr   s
             r)   add_duplicatesz(FootnotePostTreeprocessor.add_duplicatesh  s    GGCL 	D{{w+/AA KK/55dnn6R6R6TVWX	T "1j1n5 %E#}}T2H.8CHdHdHfhl;m.mHOOF+LL*KK1$K	% "Xb\! $DIIdO$!	r*   c                2   |j                   j                  dd      j                  | j                  j	                         d      \  }}dj                  || j                  j	                         |      }| j                  j                  j                  |d      S )z3 Get the number of duplicate refs of the footnote. rf   r   r   z	{}ref{}{}r   )r   r   rI   rE   rJ   rn   r   )r&   ry   fnrT   link_ids        r)   get_num_duplicatesz,FootnotePostTreeprocessor.get_num_duplicates|  ss    99==r*001M1M1OQRSD$$R)E)E)GN~~((,,Wa88r*   c                v    t        |      D ]+  }| j                  |      }|dkD  s| j                  ||       - y)z= Find duplicate footnotes and format and add the duplicates. r   N)r   r   r   )r&   r   ry   counts       r)   handle_duplicatesz+FootnotePostTreeprocessor.handle_duplicates  s>    v, 	/B ++B/Eqy##B.	/r*   c                    d| _         |j                  d      D ]J  }|j                  j                  dd      dk(  s#|j                  d      D ]  }| j	                  |        J L y)z= Crawl the footnote div and add missing duplicate footnotes. r   rs   rt   r   r,   rv   N)r   r   r   r   r   )r&   ra   rs   rv   s       r)   r   zFootnotePostTreeprocessor.run  sb    99U# 	Czz~~gr*j8 ((4. B**2.		r*   Nr   )ry   r   r   rN   r   r   )ry   r   r   rN   r   r   r   r   ra   r   r   r   )	r   r   r   r   r   r   r   r   r   r   r*   r)   r>   r>   b  s    /#(9/	r*   r>   c                       e Zd ZdZddZddZy)r<   z3 Build and append footnote div to end of document. c                    || _         y r   re   r   s     r)   r   zFootnoteTreeprocessor.__init__  r   r*   c                b   | j                   j                  |      }|| j                   j                  |      }|rc|\  }}}t        |      j	                  |      }|r$|j                  |       |j                  ||       y |j                  |dz   |       d |_        y |j                  |       y y )Nr   )	rE   r   rc   r   r   r   r   r\   ri   )r&   ra   footnotesDivresultr^   r   isTextinds           r)   r   zFootnoteTreeprocessor.run  s    ~~66t<#^^<<TBF(.%vv6l((/MM%(MM#|4MM#'<8!%EJL) $r*   Nr   r   r   r   r   r   r   r   r   r*   r)   r<   r<     s    =#*r*   r<   c                  (    e Zd ZdZddZddZddZy)	r=   z* Reorder list items in the footnotes div. c                    || _         y r   re   r   s     r)   r   z$FootnoteReorderingProcessor.__init__  r   r*   c                L   | j                   j                   sy | j                   j                  t        | j                   j                   j                               k7  rI|j	                  d      D ]4  }|j
                  j                  dd      dk(  s#| j                  |        y  y y )Nrs   rt   r   r,   )rE   rD   r   r   r   r   r   reorder_footnotes)r&   ra   rs   s      r)   r   zFootnoteReorderingProcessor.run  s    ~~''>>((D1I1I1N1N1P,QQyy' ::>>'2.*<**3/ Rr*   c                    |j                  d      }|j                  |       |j                  d      }d
 fd}t        ||      }t	        j
                  |d      }t        |d      D ]a  \  }}|j                  d      }|j                  d j                  j                  d	      j                  |             |j                  |       c y )Nrv   ry   c                J   | j                   j                  dd      j                  j                  j	                         d      d   }|j                  j
                  v r%j                  j
                  j                  |      S t        j                  j                        S )Nrf   r   r   rY   )r   r   rI   rE   rJ   rD   r   r   )ry   rf   r&   s     r)   order_by_idzBFootnoteReorderingProcessor.reorder_footnotes.<locals>.order_by_id  s    tR(..t~~/K/K/MqQRTUB 666 --33B7 112r*   )keyr   rw   z.//a[@class="footnote-backref"]r~   r   )r   rN   )r[   r   findallsortedr   r   r   r    rE   r#   rn   ri   )	r&   r   old_listitemsr   new_listr   itemr   s	   `        r)   r   z-FootnoteReorderingProcessor.reorder_footnotes  s    ;;t$h  &	 u+.##FD1$U!4 	"KE4yy!BCHLL$..":":;K"L"S"STY"Z[OOD!	"r*   Nr   r   r   )r   r   r   r   r   r   r   r   r*   r)   r=   r=     s    4#"r*   r=   c                       e Zd ZdZddZddZy)r@   z* Replace placeholders with html entities. c                    || _         y r   re   r   s     r)   r   zFootnotePostprocessor.__init__  r   r*   c                    |j                  t        | j                  j                  d            }|j                  t        d      S )Nr   z&#160;)r$   r   rE   r#   r   )r&   rZ   s     r)   r   zFootnotePostprocessor.run  s8    ||dnn66G
 ||,h77r*   Nr   )rZ   r   r   r   r   r   r*   r)   r@   r@     s    4#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   r6   r   inlinepatternsr   r;   r	   r?   r
   r   collectionsr   r   r   xml.etree.ElementTreer   ElementTreeSTXETXr   r   r   rK   RE_REFERENCEr   r8   r:   r>   r<   r=   r@   r  r   r*   r)   <module>r     s    #  , , * *  # 	  % %8844txx? 8844txx? BJJ'	rzz9:@	 @FW ^ W t$o $<2 2j*M *.#"- #"L	8M 	8'r*   