
    c=                        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dS )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                      j        di | d| _        i | _        t                      | _        |                                  dS )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.11/site-packages/markdown/extensions/footnotes.pyr   zFootnoteExtension.__init__#   s    
 +DF ./ *+ $% 8$%
 &(-
 
2 	""6""" 

    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"   r0   FOOTNOTE_REs      r%   extendMarkdownz FootnoteExtension.extendMarkdownH   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&   c                     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.reseta   s7    $ar&   Fc                 $   |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"   	referencefoundoriginal_refrefrestms          r%   
unique_refzFootnoteExtension.unique_refh   s.    	 4>) 	N!(:(:(<(<a@@IC$$A N&!''!**c!''!**ooa6GI[I[I]I]_c)dd		&#q$2D2D2F2F)MM	 4>) 	N 	9%%%4?* 	.OL)))Q.)))),-DOL)r&   c                 ,      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%   rZ   z:FootnoteExtension.findFootnotesPlaceholder.<locals>.finder   s      	% 	%: 4zt~~n'E'EFFK 4$gt3333: 5zt~~n'E'EFFK 5$gu4444"F5MM	 %$$$$%4r&   r   )r"   rootresrZ   s   `  @r%   findFootnotesPlaceholderz*FootnoteExtension.findFootnotesPlaceholder}   s6    	 	 	 	 	 	 fTll
r&   c                     || j         |<   dS )z' Store a footnote for later retrieval. Nr>   )r"   idrS   s      r%   setFootnotezFootnoteExtension.setFootnote   s    !rr&   c                 ,    |                      d          S )z Get the footnote separator. r   )rU   r?   s    r%   rC   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{}{})rU   rC   r   format)r"   r`   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{}{})rU   rO   rC   r   rd   )r"   r`   rJ   s      r%   makeFootnoteRefIdz#FootnoteExtension.makeFootnoteRefId   sy    >>,'' 	X??>T5G5G5I5I4K]_a4b#bdijjj??;#5#5d6H6H6J6JB#O#OQVWWWr&   c                    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 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   sV    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&   )F)__name__
__module____qualname____doc__r   r<   r!   rO   r]   ra   rC   re   rg   r   __classcell__r$   s   @r%   r   r       s        # # # # #JP P P2     *  $" " "+ + += = =X X X X* * * * * * *r&   r   c                   f     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                 b    t                                          |j                   || _        d S N)r   r   r/   r>   )r"   r>   r$   s     r%   r   zFootnoteBlockProcessor.__init__   s)    )***"r&   c                     dS )NTr   )r"   parentblocks      r%   testzFootnoteBlockProcessor.test   s    tr&   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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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 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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)rB   r{   r   r   )r"   r   linesilines        r%   r   zFootnoteBlockProcessor.detab  sh    
 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      s        AA	8",	G	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                 X    t                                          |           || _        d S r   )r   r   r>   )r"   patternr>   r$   s      r%   r   z FootnoteInlineProcessor.__init__-  s&    !!!"r&   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   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  s6   WWQZZ)..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   r   r   r   r   r   r   r   s   @r%   r5   r5   *  sM        II# # # # #$ $ $ $ $ $ $r&   r5   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )r8   z% Amend footnote div with duplicates. c                     || _         d S r   r_   r"   r>   s     r%   r   z"FootnotePostTreeprocessor.__init__D      "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 )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-   GGCLL 	 	D{w++/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	 	r&   c                 0   |j                             dd                              | j                                        d          \  }}d                    || 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[  sz    9==r**001M1M1O1OQRSSD$$R)E)E)G)GNN~(,,Wa888r&   c                     t          |          D ]3}|                     |          }|dk    r|                     ||           4dS )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  sY    v,, 	/ 	/B ++B//Eqy /##B...	/ 	/r&   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
   ri   rj   r   r(   rl   N)r   r   r   r   r   )r"   r[   ri   rl   s       r%   r   zFootnotePostTreeprocessor.runj  s~    99U## 	 	Cz~~gr**j8  ((4..  B**2...	 	r&   N)	r   r   r   r   r   r   r   r   r   r   r&   r%   r8   r8   A  se        //# # #  (9 9 9/ / /	 	 	 	 	r&   r8   c                       e Zd ZdZd Zd ZdS )r7   z3 Build and append footnote div to end of document. c                     || _         d S r   r_   r   s     r%   r   zFootnoteTreeprocessor.__init__y  r   r&   c                    | j                             |          }|| j                             |          }|ry|\  }}}t          |                              |          }|r-|                    |           |                    ||           d S |                    |dz   |           d |_        d S |                    |           d S d S )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BBF 
*(.%vv6ll((// &MM%(((MM#|44444MM#'<888!%EJJJL)))))	* 	*r&   Nr   r   r   r   r   r   r   r&   r%   r7   r7   v  s8        ==# # #* * * * *r&   r7   c                       e Zd ZdZd Zd ZdS )r:   z* Replace placeholders with html entities. c                     || _         d S r   r_   r   s     r%   r   zFootnotePostprocessor.__init__  r   r&   c                     |                     t          | j                            d                    }|                     t          d          S )Nr   z&#160;)rz   r   r>   rU   r   )r"   rS   s     r%   r   zFootnotePostprocessor.run  sA    ||dn66GG
 
 ||,h777r&   Nr   r   r&   r%   r:   r:     s8        44# # #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   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          , , , , , , , , , , , , * * * * * * * * * * * *       # # # # # # 				  % % % % % % % % %844tx? 844tx? BJ''	o o o o o	 o o odU  U  U  U  U ^ U  U  U p$ $ $ $ $o $ $ $.2 2 2 2 2 2 2 2j* * * * *M * * *.	8 	8 	8 	8 	8M 	8 	8 	8' ' ' ' 'r&   