
    !Sh{K                       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 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! 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+)z(?<!!)\[\^([^\]]*)\](?!\s*:)c                  t     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                Z   ddg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|                     d          	                    dd                     | 
                                 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.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.zMWhether to order footnotes by footnote content rather than by footnote label.)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     c/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/markdown/extensions/footnotes.pyr   zFootnoteExtension.__init__*   s    
 *+^ R ` [! 7> * f%+
 
2 	/""6""" *,#&55 	'8H)I)I)Q)QRVX\)])]^^^

    c                |   |                     |            |j        | _        || _        |j        j                            t          |           dd           d}|j                            t          ||           dd           |j                            t          |           dd           | 
                    d          s)|j                            t          |           dd           |j                            t          |           d	d
           |j                            t          |           dd           dS )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.extendMarkdownS   s3   
T"""i
	!**+A$+G+GUWXXX (
""#:;#M#Mz[^___ 	""#8#>#>
BOOO
 ~~455 	b&&'B4'H'HJ\^`aaa 	""#<T#B#BDXZ\]]] 	""#8#>#>
BOOOOOr*   returnNonec                    g | _         t                      | _        | xj        dz  c_        i | _        t                      | _        dS )z> Clear footnotes on reset, and prepare for distinct document. r   N)footnote_orderr   	footnotesr   r   r    r!   r&   s    r)   r%   zFootnoteExtension.resetr   s?    )+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&   rI   rK   original_refrefrestms          r)   
unique_refzFootnoteExtension.unique_refz   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findr#   tail)elementchild	child_resfinderr&   s      r)   rg   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&   r[   resrg   s   `  @r)   findFootnotesPlaceholderz*FootnoteExtension.findFootnotesPlaceholder   s6    	 	 	 	 	 	 fTll
r*   idra   c                    || j         |<   dS )z' Store a footnote for later retrieval. NrG   )r&   rj   ra   s      r)   setFootnotezFootnoteExtension.setFootnote   s    !rr*   c                P    || j         vr| j                             |           dS dS )z7 Store a footnote reference id in order of appearance. N)rF   appendr&   rj   s     r)   addFootnoteRefz FootnoteExtension.addFootnoteRef   s5    T(((&&r***** )(r*   c                ,    |                      d          S )z Get the footnote separator. r   )r#   rH   s    r)   rP   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#   rP   r   formatrp   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#   rZ   rP   r   rt   )r&   rj   rK   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          }t          | j                                        d          D ]\  }}t          j        |d	          }|                    d
|                     |                     | j	        
                    || j        |                    t          |          D ],}|                    |           |                    |           -t          j        d          }	|	                    dd|                     |          z              |	                    d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   startlirj   ahref#footnote-backreftitler   r`   p)listrG   keysetreeElementr    
SubElement	enumerateru   r4   
parseChunkro   removerw   r#   rt   FN_BACKLINK_TEXTra   lentagNBSP_PLACEHOLDER)r&   r[   rz   r}   surrogate_parentindexrj   r   elbacklinknoder   s               r)   makeFootnotesDivz"FootnoteExtension.makeFootnotesDiv   s<    DN''))** 	4mE""$$$d###c4(( =//"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/0077>>   -HM2ww '"v8s?? $	,< <DIKK))))(S11AHHX&&&
r*   )rC   rD   )F)rI   rJ   rK   rL   rC   rJ   )r[   r\   rC   r]   )rj   rJ   ra   rJ   rC   rD   )rj   rJ   rC   rD   )rC   rJ   )rj   rJ   rC   rJ   )rj   rJ   rK   rL   rC   rJ   )r[   r\   rC   rx   )__name__
__module____qualname____doc__r   rB   r%   rZ   ri   rm   rq   rP   ru   rw   r   __classcell__r(   s   @r)   r   r   '   s       ' ' ' ' 'RP P P>       *   (" " " "+ + + +
+ + + += = = =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 )r8   z4 Find footnote definitions and store for later use. z!^[ ]{0,3}\[\^([^\]]*)\]:[ ]*(.*)$rG   r   c                b    t                                          |j                   || _        d S N)r   r   r4   rG   )r&   rG   r(   s     r)   r   zFootnoteBlockProcessor.__init__   s)    )***"r*   parentr\   blockrJ   rC   rL   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searchrS   endlstripr   rstripjoindetabinsertstripextenddetectTabbedrG   rm   )r&   r   r   r   rY   rj   	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   r   r   ro   r   r   )r&   r   r   r   rY   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)rO   r   r   r   )r&   r   linesilines        r)   r   zFootnoteBlockProcessor.detab6  sh    
 D!! '' 	$ 	$GAtu%% $8ayyr*   rG   r   )r   r\   r   rJ   rC   rL   )r   r\   r   r   rC   rL   )r   r   rC   r   )r   rJ   rC   rJ   )r   r   r   r   recompile	MULTILINEr   r   r   r   r   r   r   r   s   @r)   r8   r8      s        >>	8",	G	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. patternrJ   rG   r   c                X    t                                          |           || _        d S r   )r   r   rG   )r&   r   rG   r(   s      r)   r   z FootnoteInlineProcessor.__init__E  s&    !!!"r*   rY   re.Match[str]datarC   3tuple[etree.Element | None, int | None, int | None]c                   |                     d          }|| j        j                                        v r| j                            |           | j                            d          s#| j        j                            |          dz   }nAt          | j        j                                                                      |          dz   }t          j	        d          }t          j
        |d          }|                    d| j                            |d                     |                    dd	| j                            |          z              |                    d
d           | j                            d                              |          |_        ||                    d          |                    d          fS dS )Nr   r   supr   rj   T)rK   r   r   r{   zfootnote-refr   r   )NNN)rS   rG   r   rq   r#   rF   r   r   r   r   r   r    rw   ru   rt   ra   r   r   )r&   rY   r   rj   footnote_numr   r   s          r)   handleMatchz#FootnoteInlineProcessor.handleMatchI  s   WWQZZ)..0000N))"--->++,BCC S#~<BB2FFJ  $DN$<$A$A$C$CDDJJ2NNQRR-&&C c**AGGD$.::2T:JJKKKEE&# = =b A AABBBEE'>***^--.@AAHHVVAF

AEE!HH,,##r*   )r   rJ   rG   r   )rY   r   r   rJ   rC   r   )r   r   r   r   r   r   r   r   s   @r)   r:   r:   B  sW        MM# # # # # #$ $ $ $ $ $ $ $r*   r:   c                  :    e Zd ZdZddZddZddZddZddZdS )r>   z% Amend footnote div with duplicates. rG   r   c                    || _         d S r   rl   r&   rG   s     r)   r   z"FootnotePostTreeprocessor.__init__c      "r*   r   r\   
duplicatesrT   rC   rD   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. r   r{    r   r   r   r   rN   r`   N)iterattribgetrO   rG   rP   rangecopydeepcopyro   offsetr   )
r&   r   r   linkrW   rX   linksr   sib_linkr   s
             r)   add_duplicatesz(FootnotePostTreeprocessor.add_duplicatesf  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. rj   r   r   z	{}ref{}{}r   )r   r   rO   rG   rP   rt   r   )r&   r   fnrX   link_ids        r)   get_num_duplicatesz,FootnotePostTreeprocessor.get_num_duplicatesz  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   r   counts       r)   handle_duplicatesz+FootnotePostTreeprocessor.handle_duplicates  sV    v,, 	/ 	/B ++B//Eqyy##B...	/ 	/r*   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   rz   r{   r   r,   r}   N)r   r   r   r   r   )r&   r[   rz   r}   s       r)   r   zFootnotePostTreeprocessor.run  s|    99U## 	 	Cz~~gr**j88 ((4..  B**2...	 	r*   Nr   )r   r\   r   rT   rC   rD   )r   r\   rC   rT   r   r\   rC   rD   r[   r\   rC   rD   )	r   r   r   r   r   r   r   r   r   r   r*   r)   r>   r>   `  s~        //# # # #   (9 9 9 9/ / / /	 	 	 	 	 	r*   r>   c                  "    e Zd ZdZddZdd	Zd
S )r<   z3 Build and append footnote div to end of document. rG   r   c                    || _         d S r   rl   r   s     r)   r   zFootnoteTreeprocessor.__init__  r   r*   r[   r\   rC   rD   c                   | j                             |          }|| j                             |          }|ry|\  }}}t          |                              |          }|r-|                    |           |                    ||           d S |                    |dz   |           d |_        d S |                    |           d S d S )Nr   )	rG   r   ri   r   r   r   r   rc   ro   )r&   r[   footnotesDivresultre   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)   r<   r<     sB        ==# # # #* * * * * *r*   r<   c                  *    e Zd ZdZddZdd	ZddZdS )r=   z* Reorder list items in the footnotes div. rG   r   c                    || _         d S r   rl   r   s     r)   r   z$FootnoteReorderingProcessor.__init__  r   r*   r[   r\   rC   rD   c                6   | j         j         sd S | j         j        t          | j         j                                                   k    rO|                    d          D ];}|j                            dd          dk    r|                     |            d S :d S d S )Nrz   r{   r   r,   )rG   rF   r   r   r   r   r   reorder_footnotes)r&   r[   rz   s      r)   r   zFootnoteReorderingProcessor.run  s    ~' 	F>(D1I1N1N1P1P,Q,QQQyy''  :>>'2..*<<**3///EE = RQ r*   r   c                    |                     d          }|                    |           |                    d          }d fd}t          ||          }t	          j        |d          }t          |d          D ]p\  }}|                     d	          }|                    d
 j        	                    d          
                    |                     |                    |           qd S )Nr}   r   rC   rT   c                (   | j                             dd                              j                                        d          d         }|j        j        v rj        j                            |          nt          j        j                  S )Nrj   r   r   r`   )r   r   rO   rG   rP   rF   r   r   )r   rj   r&   s     r)   order_by_idzBFootnoteReorderingProcessor.reorder_footnotes.<locals>.order_by_id  s~    tR((..t~/K/K/M/MqQQRTUB 666 -33B777122r*   )keyr   r~   z.//a[@class="footnote-backref"]r   r   )rC   rT   )rb   r   findallsortedr   r   r   r    rG   r#   rt   ro   )	r&   r   old_listitemsr   new_listr   itemr   s	   `        r)   r   z-FootnoteReorderingProcessor.reorder_footnotes  s   ;;t$$h  &&	 	 	 	 	 	 u+...#FD11$U!444 	" 	"KE4yy!BCCHLL$.":":;K"L"L"S"STY"Z"Z[[[OOD!!!!	" 	"r*   Nr   r   r   )r   r   r   r   r   r   r   r   r*   r)   r=   r=     sV        44# # # #   " " " " " "r*   r=   c                  "    e Zd ZdZd
dZddZd	S )r@   z* Replace placeholders with html entities. rG   r   c                    || _         d S r   rl   r   s     r)   r   zFootnotePostprocessor.__init__  r   r*   ra   rJ   rC   c                    |                     t          | j                            d                    }|                     t          d          S )Nr   z&#160;)r$   r   rG   r#   r   )r&   ra   s     r)   r   zFootnotePostprocessor.run  sA    ||dn66GG
 
 ||,h777r*   Nr   )ra   rJ   rC   rJ   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   r6   r   inlinepatternsr   r;   r	   r?   r
   r   collectionsr   r   r   xml.etree.ElementTreer   ElementTreeSTXETXr   r   r   rQ   RE_REFERENCEr   r8   r:   r>   r<   r=   r@   r  r   r*   r)   <module>r     sn    # " " " " "       , , , , , , , , , , , , * * * * * * * * * * * *       # # # # # # 				  % % % % % % % % %844tx? 844tx? BJ''	rz9::~ ~ ~ ~ ~	 ~ ~ ~BW  W  W  W  W ^ W  W  W t$ $ $ $ $o $ $ $<2 2 2 2 2 2 2 2j* * * * *M * * *.#" #" #" #" #"- #" #" #"L	8 	8 	8 	8 	8M 	8 	8 	8' ' ' ' 'r*   