
    /VhFA                       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 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+)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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| _         	 t        |   di | d| _        i | _        t               | _        | 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.{}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     M/home/dcms/DCMS/lib/python3.12/site-packages/markdown/extensions/footnotes.pyr   zFootnoteExtension.__init__)   s    
 *+^ R ` [! 7> *%
, 	/"6" *,#&5

    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#   r1   FOOTNOTE_REs      r&   extendMarkdownz FootnoteExtension.extendMarkdownL   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.resete   s,    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_refl   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&   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r?   )r#   idrT   s      r&   setFootnotezFootnoteExtension.setFootnote   s    !rr'   c                $    | j                  d      S )z Get the footnote separator. r   )rV   r@   s    r&   rD   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{}{})rV   rD   r   format)r#   ra   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{}{})rV   rP   rD   r   re   )r#   ra   rK   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 `etree` Element. Ndivclassr)   hrolr   z%dr   r   )startlira   ahref#footnote-backreftitlerS   p)listr?   keysetreeElementr    
SubElementrV   replace	enumeraterf   r0   
parseChunkappendremoverh   re   FN_BACKLINK_TEXTrT   lentagNBSP_PLACEHOLDER)r#   r\   rj   rm   surrogate_parentbacklink_titleindexra   ro   elbacklinknoderu   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'   )returnNone)F)rJ   strrK   boolr   r   )r\   etree.Elementr   z0tuple[etree.Element, etree.Element, bool] | None)ra   r   rT   r   r   r   )r   r   )ra   r   r   r   )ra   r   rK   r   r   r   )r\   r   r   zetree.Element | None)__name__
__module____qualname____doc__r   r=   r"   rP   r^   rb   rD   rf   rh   r   __classcell__r%   s   @r&   r   r   &   sK    !FP2*!	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 )r4   z7 Find all footnote references and store for later use. z!^[ ]{0,3}\[\^([^\]]*)\]:[ ]*(.*)$c                F    t         |   |j                         || _        y N)r   r   r0   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searchrG   endlstriprn   rstripjoindetabinsertstripextenddetectTabbedr?   rb   )r#   r   blocksr   rO   ra   	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   rn   r   r~   r   r   )r#   r   r   r   rO   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)rC   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   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&   r4   r4      s7    A	8",,	GB#"H:	 r'   r4   c                  ,     e Zd ZdZd fdZddZ xZS )r6   zC `InlineProcessor` 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__4  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   suprp   ra   T)rK   rq   rr   rk   zfootnote-refr   r   )NNN)rG   r?   rw   rx   ry   rz   r    rh   rf   rV   re   rv   r   rT   rn   r   )r#   rO   datara   r   rp   s         r&   handleMatchz#FootnoteInlineProcessor.handleMatch8  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   )rO   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&   r6   r6   1  s    M#$r'   r6   c                  8    e Zd ZdZddZd	dZd
dZddZddZy)r9   z% Amend footnote div with duplicates. c                    || _         y r   r`   r#   r?   s     r&   r   z"FootnotePostTreeprocessor.__init__K  	    "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. rp   rk    rs   rq   r   r   rB   rS   N)iterattribgetrC   r?   rD   rangecopydeepcopyr~   offsetrv   )
r#   ro   
duplicateslinkrM   rN   linksr   sib_linkr   s
             r&   add_duplicatesz(FootnotePostTreeprocessor.add_duplicatesN  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. ra   r   r   z	{}ref{}{}r   )r   r   rC   r?   rD   re   r   )r#   ro   fnrN   link_ids        r&   get_num_duplicatesz,FootnotePostTreeprocessor.get_num_duplicatesb  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)rv   r   r   )r#   r   ro   counts       r&   handle_duplicatesz+FootnotePostTreeprocessor.handle_duplicatesh  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   rj   rk   r   r)   rm   N)r   r   r   r   r   )r#   r\   rj   rm   s       r&   r   zFootnotePostTreeprocessor.runq  sb    99U# 	Czz~~gr*j8 ((4. B**2.		r'   Nr   )ro   r   r   rH   r   r   )ro   r   r   rH   )r   r   r   r   r\   r   r   r   )	r   r   r   r   r   r   r   r   r   r   r'   r&   r9   r9   H  s    /#(9/	r'   r9   c                       e Zd ZdZddZddZy)r8   z3 Build and append footnote div to end of document. c                    || _         y r   r`   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   )	r?   r   r^   rv   r   r   r   rW   r~   )r#   r\   footnotesDivresultrY   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&   r8   r8   }  s    =#*r'   r8   c                       e Zd ZdZddZd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;)r{   r   r?   rV   r   )r#   rT   s     r&   r   zFootnotePostprocessor.run  s8    ||dnn66G
 ||,h77r'   Nr   )rT   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   r2   r   inlinepatternsr   r7   r	   r:   r
   r   collectionsr   r   r   xml.etree.ElementTreerx   ElementTreeSTXETXr   r   r   rE   r   r4   r6   r9   r8   r;   r   r   r'   r&   <module>r      s    #  , , * *  # 	  % %8844txx? 8844txx? BJJ'	o	 odV ^ V r$o $.2 2j*M *.	8M 	8'r'   