
    ̆h{K                       S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SS	KJr  SS
KJr  SSKJr  SSKrSSKrSSKJs  Jr  \R*                  S-   \R,                  -   r\R*                  S-   \R,                  -   r\R2                  " S5      r\R2                  " S5      r " S S\5      r " S S\5      r " S S\5      r " S S\
5      r " S S\
5      r  " S S\
5      r! " S S\5      r"S r#g) 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                     ^  \ rS rSrSrU 4S jrS rSS jrSSS jjr    SS jr	SS jr
SS	 jrSS
 jrSS jrSSS jjrSS jrSrU =r$ )FootnoteExtension'   zFootnote Extension. c                  > SS/SS/SS/SS/S	S
/SS/SS/S.U l          [        TU ]  " S0 UD6  SU l        0 U l        [        5       U l        U R                  SU R                  S5      R                  SS5      5        U R                  5         g)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     U/opt/services/DDDS/venv/lib/python3.13/site-packages/markdown/extensions/footnotes.pyr   FootnoteExtension.__init__*   s    
 *+^ R ` [! 7> * f%+
2 	/"6" *,#&5 	'8H)I)Q)QRVX\)]^

    c                `   UR                  U 5        UR                  U l        Xl        UR                  R                  R	                  [        U 5      SS5        SnUR                  R	                  [        X 5      SS5        UR                  R	                  [        U 5      SS5        U R                  S5      (       d&  UR                  R	                  [        U 5      SS5        UR                  R	                  [        U 5      S	S
5        UR                  R	                  [        U 5      SS5        g)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'   r7   FOOTNOTE_REs      r*   extendMarkdown FootnoteExtension.extendMarkdownS   s    
T"ii
		!!**+A$+GUWX (
""#:;#Mz[^_ 	""#8#>
BO
 ~~455&&'B4'HJ\^`a 	""#<T#BDXZ\] 	""#8#>
BOr,   c                    / U l         [        5       U l        U =R                  S-  sl        0 U l        [        5       U l        g)z=Clear footnotes on reset, and prepare for distinct document. r   N)footnote_orderr   	footnotesr   r    r!   r"   r'   s    r*   r&   FootnoteExtension.resetr   s4    )+0;ar,   c                .   U(       d  U$ UnXR                   ;   a  UR                  U R                  5       S5      u  pE[        R	                  U5      nU(       aB  SUR                  S5      [        UR                  S5      5      S-   U R                  5       U4-  nOSUSU R                  5       U4-  nXR                   ;   a  M  U R                   R                  U5        X0R                  ;   a  U R                  U==   S-  ss'   U$ SU R                  U'   U$ )z0Get 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_refFootnoteExtension.unique_refz   s     >>)!(:(:(<a@IC$A&!''!*c!''!*oa6GI[I[I]_c)dd	&#q$2D2D2F)MM	 >>) 	9%??*OOL)Q.)  -.DOOL)r,   c                (   ^ ^ UU 4S jmT" U5      nU$ )z?Return ElementTree Element that contains Footnote placeholder. c                J  > U  H  nUR                   (       a4  UR                   R                  TR                  S5      5      S:  a  XS4s  $ UR                  (       a4  UR                  R                  TR                  S5      5      S:  a  XS4s  $ T" U5      nUc  M  Us  $    g )Nr   TF)textfindr$   tail)elementchild	child_resfinderr'   s      r*   re   :FootnoteExtension.findFootnotesPlaceholder.<locals>.finder   s     ::zzt~~n'EFK$t33::zzt~~n'EFK$u44"5M	($$ ! r,   r   )r'   rootresre   s   `  @r*   findFootnotesPlaceholder*FootnoteExtension.findFootnotesPlaceholder   s    	 Tl
r,   c                     X R                   U'   g)z&Store a footnote for later retrieval. NrH   )r'   idr_   s      r*   setFootnoteFootnoteExtension.setFootnote   s    !rr,   c                Z    XR                   ;  a  U R                   R                  U5        gg)z6Store a footnote reference id in order of appearance. N)rG   appendr'   rm   s     r*   addFootnoteRef FootnoteExtension.addFootnoteRef   s'    (((&&r* )r,   c                $    U R                  S5      $ )zGet the footnote separator. r   )r$   rI   s    r*   rN   FootnoteExtension.get_separator   s    ~~k**r,   c                    U R                  S5      (       a   SU R                  5       U R                  U4-  $ SR                  U R                  5       U5      $ )zReturn footnote link id. r   z	fn%s%d-%szfn{}{})r$   rN   r   formatrr   s     r*   makeFootnoteId FootnoteExtension.makeFootnoteId   sL    >>,''$"4"4"68J8JB!OOO??4#5#5#7<<r,   c                    U R                  S5      (       a0  U R                  SU R                  5       U R                  U4-  U5      $ U R                  SR	                  U R                  5       U5      U5      $ )zReturn footnote back-link id. r   zfnref%s%d-%sz	fnref{}{})r$   rZ   rN   r   rx   )r'   rm   rU   s      r*   makeFootnoteRefId#FootnoteExtension.makeFootnoteRefId   sg    >>,''??>T5G5G5I4K]K]_a4b#bdijj??;#5#5d6H6H6JB#OQVWWr,   c                   [        U R                  R                  5       5      (       d  g[        R                  " S5      nUR                  SS5        [        R                  " US5        [        R                  " US5      n[        R                  " S5      n[        U R                  R                  5       SS9 GH  u  pV[        R                  " US	5      nUR                  S
U R                  U5      5        U R                  R                  X@R                  U   5        [        U5       H%  nUR                  U5        UR                  U5        M'     [        R                  " S5      n	U	R                  SSU R                  U5      -   5        U	R                  SS5        U	R                  SU R                  S5      R                  U5      5        [         U	l        [%        U5      (       d  GM4  US   n
U
R&                  S:X  a,  U
R"                  [(        -   U
l        U
R                  U	5        GMu  [        R                  " US5      nUR                  U	5        GM     U$ )z.Return `div` of footnotes as `etree` Element. Ndivclassr.   hrolr   startlirm   ahref#footnote-backreftitler   r^   p)listrH   keysetreeElementr!   
SubElement	enumeratery   r6   
parseChunkrq   remover|   r$   rx   FN_BACKLINK_TEXTr_   lentagNBSP_PLACEHOLDER)r'   rg   r   r   surrogate_parentindexrm   r   elbacklinknoder   s               r*   makeFootnotesDiv"FootnoteExtension.makeFootnotesDiv   s    DNN'')**mmE"$d#c4( ==/"4>>#6#6#8BIE!!"d+BFF4,,R01 KK""#3^^B5GH+,		" ''+ - }}S)HLLt'='=b'A!ABLL"45LL/077> -HM2ww"v88s? $		,< <DIKK)((S1AHHX&5 C6 
r,   )r   rG   rH   r    r7   r6   r   r"   )returnNone)F)rT   strrU   boolr   r   )rg   etree.Elementr   z0tuple[etree.Element, etree.Element, bool] | None)rm   r   r_   r   r   r   )rm   r   r   r   )r   r   )rm   r   r   r   )rm   r   rU   r   r   r   )rg   r   r   zetree.Element | None)__name__
__module____qualname____firstlineno____doc__r   rD   r&   rZ   ri   rn   rs   rN   ry   r|   r   __static_attributes____classcell__r)   s   @r*   r   r   '   sU    'RP>*!	9("+
+=X' 'r,   r   c                     ^  \ rS rSrSr\R                  " S\R                  5      rS
U 4S jjr	SS jr
SS jrSS jrSS jrS	rU =r$ )r:      z3Find footnote definitions and store for later use. z!^[ ]{0,3}\[\^([^\]]*)\]:[ ]*(.*)$c                D   > [         TU ]  UR                  5        Xl        g N)r   r   r6   rH   )r'   rH   r)   s     r*   r   FootnoteBlockProcessor.__init__   s    ))*"r,   c                    g)NTr   )r'   parentblocks      r*   testFootnoteBlockProcessor.test   s    r,   c                (   UR                  S5      nU R                  R                  U5      nU(       Ga  UR                  S5      nUR                  S5      /nX4R	                  5       S R                  S5      nU R                  R                  U5      nU(       a|  USUR                  5        R                  S5      n	SR                  US   U R                  U	5      /5      R                  S5      US'   UR                  SXxR                  5       S 5        OWSR                  US   U R                  U5      /5      R                  S5      US'   UR                  U R                  U5      5        SR                  U5      n
U R                  R                  XZR                  5       5        USUR                  5        R                  5       (       a2  UR                  SUSUR                  5        R                  S5      5        gUR                  SU5        g)	z,Find, set, and remove footnote definitions. r   r   r   N
z

TF)popREsearchrQ   endlstripr   rstripjoindetabinsertstripextenddetectTabbedrH   rn   )r'   r   blocksr   rY   rm   	fn_blockstherestm2beforer.   s              r*   runFootnoteBlockProcessor.run   s   

1GGNN5!BI EEGHo,,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&&r??+<=Zaggi &&((az	!2!9!9$!?@ar,   c                   / nU(       a  US   R                  S5      (       a  UR                  S5      nU R                  R                  U5      nU(       ag  USUR	                  5        R                  S5      nUR                  U R                  U5      5        UR                  SX4R	                  5       S 5         U$ UR                  U R                  U5      5        O U$ U(       a  M  U$ )zyFind indented text and remove indent before further processing.

Returns:
    A list of blocks with indentation removed.
r       Nr   )	
startswithr   r   r   r   r   rq   r   r   )r'   r   r   r   rY   r   s         r*   r   #FootnoteBlockProcessor.detectTabbed  s     	ay##E**

1GGNN5) #:AGGI.55d;F$$TZZ%78MM!U779:%67 	 $$TZZ%67 ) f( r,   c                    UR                  S5      n[        U5       H$  u  p4UR                  S5      (       d  M  USS X#'   M&     SR                  U5      $ )zwRemove one level of indent from a block.

Preserve lazily indented blocks by only removing indent from indented lines.
r   r      N)rM   r   r   r   )r'   r   linesilines        r*   r   FootnoteBlockProcessor.detab6  sP    
 D! 'GAu%%8 ( yyr,   rl   rH   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   r   recompile	MULTILINEr   r   r   r   r   r   r   r   r   s   @r*   r:   r:      s:    >	8",,	GB##J:	  	 r,   r:   c                  :   ^  \ rS rSrSrSU 4S jjrSS jrSrU =r$ )r<   iB  zB`InlineProcessor` for footnote markers in a document's body text. c                0   > [         TU ]  U5        X l        g r   )r   r   rH   )r'   patternrH   r)   s      r*   r    FootnoteInlineProcessor.__init__E  s    !"r,   c                   UR                  S5      nX0R                  R                  R                  5       ;   Ga  U R                  R                  U5        U R                  R	                  S5      (       d)  U R                  R
                  R                  U5      S-   nO?[        U R                  R                  R                  5       5      R                  U5      S-   n[        R                  " S5      n[        R                  " US5      nUR                  SU R                  R                  USS95        UR                  SS	U R                  R                  U5      -   5        UR                  S
S5        U R                  R	                  S5      R                  U5      Ul        XQR!                  S5      UR#                  S5      4$ g)Nr   r   supr   rm   T)rU   r   r   r   zfootnote-refr   r   )NNN)rQ   rH   r   rs   r$   rG   r   r   r   r   r   r!   r|   ry   rx   r_   r   r   )r'   rY   datarm   footnote_numr   r   s          r*   handleMatch#FootnoteInlineProcessor.handleMatchI  sQ   WWQZ))..00NN))"->>++,BC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,   rl   )r   r   rH   r   )rY   zre.Match[str]r   r   r   z3tuple[etree.Element | None, int | None, int | None])	r   r   r   r   r   r   r   r   r   r   s   @r*   r<   r<   B  s    M#$ $r,   r<   c                  J    \ rS rSrSrS
S jrSS jrSS jrSS jrSS jr	Sr
g	)r@   i`  z$Amend footnote div with duplicates. c                    Xl         g r   rl   r'   rH   s     r*   r   "FootnotePostTreeprocessor.__init__c      "r,   c                >   UR                  S5       GH  nUR                  R                  SS5      S:X  d  M&  UR                  S   R                  U R                  R                  5       S5      u  pE/ n[        SUS-   5       Hl  n[        R                  " U5      nSXGU R                  R                  5       U4-  UR                  S'   UR                  U5        U =R                  S-  sl
        Mn     [        U5      S	   n	U H  nU	R                  U5        M       g
   g
)zEAdjust current `li` and add the duplicates: `fnref2`, `fnref3`, etc. r   r    r   r   r   r   rL   r^   N)iterattribgetrM   rH   rN   rangecopydeepcopyrq   offsetr   )
r'   r   
duplicateslinkrW   rX   linksr   sib_linkr   s
             r*   add_duplicates(FootnotePostTreeprocessor.add_duplicatesf  s    GGCLD{{w+/AA KK/55dnn6R6R6TVWX	 "1j1n5E#}}T2H.8CHdHdHfhl;m.mHOOF+LL*KK1$K	 6 "Xb\!DIIdO "! !r,   c                .   UR                   R                  SS5      R                  U R                  R	                  5       S5      u  p#SR                  X R                  R	                  5       U5      nU R                  R                  R                  US5      $ )z2Get the number of duplicate refs of the footnote. rm   r   r   z	{}ref{}{}r   )r   r   rM   rH   rN   rx   r    )r'   r   fnrX   link_ids        r*   get_num_duplicates,FootnotePostTreeprocessor.get_num_duplicatesz  so    99==r*001M1M1OQRS$$R)E)E)GN~~((,,Wa88r,   c                |    [        U5       H-  nU R                  U5      nUS:  d  M  U R                  X#5        M/     g)z<Find duplicate footnotes and format and add the duplicates. r   N)r   r   r   )r'   r   r   counts       r*   handle_duplicates+FootnotePostTreeprocessor.handle_duplicates  s7    v,B ++B/Eqy##B. r,   c                    SU l         UR                  S5       HO  nUR                  R                  SS5      S:X  d  M%  UR                  S5       H  nU R	                  U5          MM     MQ     g)z<Crawl the footnote div and add missing duplicate footnotes. r   r   r   r   r.   r   N)r   r   r   r   r   )r'   rg   r   r   s       r*   r   FootnotePostTreeprocessor.run  sX    99U#Czz~~gr*j8 ((4.B**2. )	 $r,   )rH   r   Nr   )r   r   r   rR   r   r   )r   r   r   rR   r   r   r   r   rg   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r,   r*   r@   r@   `  s    /#(9/	r,   r@   c                  ,    \ rS rSrSrSS jrSS jrSrg)	r>   i  z2Build and append footnote div to end of document. c                    Xl         g r   rl   r   s     r*   r   FootnoteTreeprocessor.__init__  r   r,   c                t   U R                   R                  U5      nUb  U R                   R                  U5      nU(       af  Uu  pEn[        U5      R	                  U5      nU(       a#  UR                  U5        UR                  Xr5        g UR                  US-   U5        S Ul        g UR                  U5        g g )Nr   )	rH   r   ri   r   r   r   r   ra   rq   )r'   rg   footnotesDivresultrc   r   isTextinds           r*   r   FootnoteTreeprocessor.run  s    ~~66t<#^^<<TBF(.%v6l((/MM%(MM#4MM#'<8!%EJL) $r,   rl   Nr   r  r   r   r   r   r   r   r   r   r   r,   r*   r>   r>     s    =#*r,   r>   c                  6    \ rS rSrSrSS jrS	S jrS
S jrSrg)r?   i  z)Reorder list items in the footnotes div. c                    Xl         g r   rl   r   s     r*   r   $FootnoteReorderingProcessor.__init__  r   r,   c                ^   U R                   R                   (       d  g U R                   R                  [        U R                   R                   R                  5       5      :w  aL  UR	                  S5       H6  nUR
                  R                  SS5      S:X  d  M%  U R                  U5          g    g g )Nr   r   r   r.   )rH   rG   r   r   r   r   r   reorder_footnotes)r'   rg   r   s      r*   r   FootnoteReorderingProcessor.run  s}    ~~''>>((D1I1I1N1N1P,QQyy'::>>'2.*<**3/ ( Rr,   c                  ^  UR                  S5      nUR                  U5        UR                  S5      nS
U 4S jjn[        X4S9n[        R
                  " US5      n[        USS9 Ha  u  pgUR                  S5      nUR                  ST R                  R                  S	5      R                  U5      5        UR                  U5        Mc     g )Nr   r   c                N  > U R                   R                  SS5      R                  TR                  R	                  5       S5      S   nUTR                  R
                  ;   a%  TR                  R
                  R                  U5      $ [        TR                  R                  5      $ )Nrm   r   r   r^   )r   r   rM   rH   rN   rG   r   r   )r   rm   r'   s     r*   order_by_idBFootnoteReorderingProcessor.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   r   z.//a[@class="footnote-backref"]r   r   )r   rR   )r`   r   findallsortedr   r   r   r!   rH   r$   rx   rq   )	r'   r   old_listitemsr  new_listr   itemr   s	   `        r*   r  -FootnoteReorderingProcessor.reorder_footnotes  s    ;;t$h  &	 u.##FD1$U!4KEyy!BCHLL$..":":;K"L"S"STY"Z[OOD! 5r,   rl   Nr   r  r  )	r   r   r   r   r   r   r   r  r   r   r,   r*   r?   r?     s    4#"r,   r?   c                  ,    \ rS rSrSrSS jrSS jrSrg)	rB   i  z)Replace placeholders with html entities. c                    Xl         g r   rl   r   s     r*   r   FootnotePostprocessor.__init__  r   r,   c                    UR                  [        U R                  R                  S5      5      nUR                  [        S5      $ )Nr   z&#160;)r%   r   rH   r$   r   )r'   r_   s     r*   r   FootnotePostprocessor.run  s8    ||dnn66G
 ||,h77r,   rl   Nr   )r_   r   r   r   r  r   r,   r*   rB   rB     s    4#8r,   rB   c                     [        S0 U D6$ )z.Return an instance of the `FootnoteExtension` r   )r   )r(   s    r*   makeExtensionr'    s    &v&&r,   )$r   
__future__r   r   r   r8   r   inlinepatternsr   r=   r	   rA   r
   r   collectionsr   r   r   xml.etree.ElementTreer   ElementTreeSTXETXr   r   r   rO   RE_REFERENCEr   r:   r<   r@   r>   r?   rB   r'  r   r,   r*   <module>r0     s    #  , , * *  # 	  % %8844txx? 8844txx? JJ'	zz9:~	 ~BW ^ W t$o $<2 2j*M *.#"- #"L	8M 	8'r,   