
    -Be=                        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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y)a7  
Footnotes Extension for Python-Markdown
=======================================

Adds footnote handling to Python-Markdown.

See <https://Python-Markdown.github.io/extensions/footnotes>
for documentation.

Copyright The Python Markdown Project

License: [BSD](https://opensource.org/licenses/bsd-license.php)

   )	Extension   )BlockProcessor)InlineProcessor)Treeprocessor)Postprocessor)util    )OrderedDictNzz1337820767766393qqqq3936677670287331zzz(fnref)(\d+)c                   \     e Zd ZdZ fdZd Zd ZddZd Zd Z	d Z
d	 Zdd
Z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        |   di | d| _        i | _        t               | _        | j                          y)z Setup configs. z///Footnotes Go Here///z1The text string that marks where the footnotes goFz7Avoid 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     =lib/python3.12/site-packages/markdown/extensions/footnotes.pyr   zFootnoteExtension.__init__#   s    
 +DF ./ *+ $% 8$%
 &(-
2 	"6" 

    c                    |j                  |        |j                  | _        || _        |j                  j                  j	                  t        |       dd       d}|j                  j	                  t        ||       dd       |j                  j	                  t        |       dd       |j                  j	                  t        |       dd       |j                  j	                  t        |       dd       y	)
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"   r0   FOOTNOTE_REs      r%   extendMarkdownz FootnoteExtension.extendMarkdownH   s    
T"ii
		!!**+A$+GUWX (
""#:;#Mz[^_ 	""#8#>
BO 	""#<T#BDXZ\] 	""#8#>
BOr&   c                 x    t               | _        | xj                  dz  c_        i | _        t	               | _        y)z> Clear footnotes on reset, and prepare for distinct document. r   N)r   	footnotesr   r   r   r    r"   s    r%   r!   zFootnoteExtension.reseta   s,    $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_refh   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find	getConfigtail)elementchild	child_resfinderr"   s      r%   rZ   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resrZ   s   `  @r%   findFootnotesPlaceholderz*FootnoteExtension.findFootnotesPlaceholder}   s    	 Tl
r&   c                 "    || j                   |<   y)z' Store a footnote for later retrieval. Nr>   )r"   idrS   s      r%   setFootnotezFootnoteExtension.setFootnote   s    !rr&   c                 $    | j                  d      S )z Get the footnote separator. r   )rU   r?   s    r%   rC   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{}{})rU   rC   r   format)r"   r`   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{}{})rU   rO   rC   r   rd   )r"   r`   rJ   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      }| j                  d      j                  d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!                  |             t"        |
_        t'        |      s|d   }|j(                  dk(  r+|j$                  t*        z   |_        |j                  |
       \t        j                  |d      }|j                  |
        |S )z( Return div of footnotes as et Element. Ndivclassr(   hrolr   z%dr   r   )startlir`   ahref#footnote-backreftitlerR   p)listr>   keysetreeElementr   
SubElementrU   replace	enumeratere   r/   
parseChunkappendremoverg   rd   FN_BACKLINK_TEXTrS   lentagNBSP_PLACEHOLDER)r"   r[   ri   rl   surrogate_parentbacklink_titleindexr`   rn   elbacklinknodert   s                r%   makeFootnotesDivz"FootnoteExtension.makeFootnotesDiv   s    DNN'')*mmE"$d#c4( ==/ (89AA$M"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%%e, -HM2w"v88s? $		,< <DIKK)((S1AHHX&5	'6 
r&   )F)__name__
__module____qualname____doc__r   r<   r!   rO   r]   ra   rC   re   rg   r   __classcell__r$   s   @r%   r   r       s;    #JP2*$"+=X*r&   r   c                   t     e Zd ZdZ ej
                  dej                        Z fdZd Z	d Z
d Zd Z xZS )r3   z7 Find all footnote references and store for later use. z!^[ ]{0,3}\[\^([^\]]*)\]:[ ]*(.*)$c                 F    t         |   |j                         || _        y N)r   r   r/   r>   )r"   r>   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searchrF   endlstriprm   rstripjoindetabinsertstripextenddetectTabbedr>   ra   )r"   r   blocksr   rN   r`   	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 proccesing.

        Returns: a list of blocks with indentation removed.
        r
       Nr   )	
startswithr   r   r   rm   r   r}   r   r   )r"   r   r   r   rN   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)rB   r{   r   r   )r"   r   linesilines        r%   r   zFootnoteBlockProcessor.detab  sV    
 D! ' 	$GAtu%8a	$ yyr&   )r   r   r   r   recompile	MULTILINEr   r   r   r   r   r   r   r   s   @r%   r3   r3      s7    A	8",,	GB#"H8	 r&   r3   c                   (     e Zd ZdZ fdZd Z xZS )r5   z? InlinePattern for footnote markers in a document's body text. c                 2    t         |   |       || _        y r   )r   r   r>   )r"   patternr>   r$   s      r%   r   z FootnoteInlineProcessor.__init__-  s    !"r&   c                    |j                  d      }|| j                  j                  j                         v r't        j                  d      }t        j
                  |d      }|j                  d| j                  j                  |d             |j                  dd| j                  j                  |      z          |j                  d	d
       | j                  j                  d      j                  t        | j                  j                  j                               j                  |      dz         |_        ||j                  d      |j                  d      fS y)Nr   supro   r`   T)rJ   rp   rq   rj   zfootnote-refr   r
   )NNN)rF   r>   rv   rw   rx   ry   r   rg   re   rU   rd   ru   r   rS   rm   r   )r"   rN   datar`   r   ro   s         r%   handleMatchz#FootnoteInlineProcessor.handleMatch1  s   WWQZ))..00--&C  c*AGGD$..::2T:JKEE&# = =b AABEE'>*^^--.@AHHT^^--2245;;B?!CAF 
AEE!H,,#r&   )r   r   r   r   r   r   r   r   s   @r%   r5   r5   *  s    I#$r&   r5   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)r8   z% Amend footnote div with duplicates. c                     || _         y r   r_   r"   r>   s     r%   r   z"FootnotePostTreeprocessor.__init__D  	    "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
)z@ Adjust current li and add the duplicates: fnref2, fnref3, etc. ro   rj    rr   rp   r   r   rA   rR   N)iterattribgetrB   r>   rC   rangecopydeepcopyr}   offsetru   )
r"   rn   
duplicateslinkrL   rM   linksr   sib_linkr   s
             r%   add_duplicatesz(FootnotePostTreeprocessor.add_duplicatesG  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. r`   r   r   z	{}ref{}{}r
   )r   r   rB   r>   rC   rd   r   )r"   rn   fnrM   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)ru   r   r   )r"   r   rn   counts       r%   handle_duplicatesz+FootnotePostTreeprocessor.handle_duplicatesa  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
   ri   rj   r   r(   rl   N)r   r   r   r   r   )r"   r[   ri   rl   s       r%   r   zFootnotePostTreeprocessor.runj  sb    99U# 	Czz~~gr*j8 ((4. B**2.		r&   N)	r   r   r   r   r   r   r   r   r   r   r&   r%   r8   r8   A  s    /#(9/	r&   r8   c                       e Zd ZdZd Zd Zy)r7   z3 Build and append footnote div to end of document. c                     || _         y r   r_   r   s     r%   r   zFootnoteTreeprocessor.__init__y  r   r&   c                 b   | j                   j                  |      }|| j                   j                  |      }|rc|\  }}}t        |      j	                  |      }|r$|j                  |       |j                  ||       y |j                  |dz   |       d |_        y |j                  |       y y )Nr   )	r>   r   r]   ru   r   r~   r   rV   r}   )r"   r[   footnotesDivresultrX   r   isTextinds           r%   r   zFootnoteTreeprocessor.run|  s    ~~66t<#^^<<TBF(.%vv6l((/MM%(MM#|4MM#'<8!%EJL) $r&   Nr   r   r   r   r   r   r   r&   r%   r7   r7   v  s    =#*r&   r7   c                       e Zd ZdZd Zd Zy)r:   z* Replace placeholders with html entities. c                     || _         y r   r_   r   s     r%   r   zFootnotePostprocessor.__init__  r   r&   c                     |j                  t        | j                  j                  d            }|j                  t        d      S )Nr   z&#160;)rz   r   r>   rU   r   )r"   rS   s     r%   r   zFootnotePostprocessor.run  s8    ||dnn66G
 ||,h77r&   Nr   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   r   r   r1   r   inlinepatternsr   r6   r   r9   r   r	   collectionsr   r   r   xml.etree.ElementTreerw   ElementTreeSTXETXr   r   r   rD   r   r3   r5   r8   r7   r:   r   r   r&   r%   <module>r      s     , , * *  # 	  % %8844txx? 8844txx? BJJ'	o	 odU ^ U p$o $.2 2j*M *.	8M 	8'r&   