
    UgFA                       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 " 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+)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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.U l          [        TU ]  " S0 UD6  SU l        0 U l        [        5       U l        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.{}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     ?/usr/lib/python3/dist-packages/markdown/extensions/footnotes.pyr   FootnoteExtension.__init__)   s    
 *+^ R ` [! 7> *%
, 	/"6" *,#&5

    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                  R	                  [        U 5      SS5        UR                  R	                  [        U 5      SS5        g	)
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$   r3   FOOTNOTE_REs      r'   extendMarkdown FootnoteExtension.extendMarkdownL   s    
T"ii
		!!**+A$+GUWX (
""#:;#Mz[^_ 	""#8#>
BO 	""#<T#BDXZ\] 	""#8#>
BOr)   c                x    [        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)r   	footnotesr   r    r!   r"   r$   s    r'   r#   FootnoteExtension.resete   s,    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_refl   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find	getConfigtail)elementchild	child_resfinderr$   s      r'   r`   :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resr`   s   `  @r'   findFootnotesPlaceholder*FootnoteExtension.findFootnotesPlaceholder   s    	 Tl
r)   c                     X R                   U'   g)z&Store a footnote for later retrieval. NrB   )r$   idrY   s      r'   setFootnoteFootnoteExtension.setFootnote   s    !rr)   c                $    U R                  S5      $ )zGet the footnote separator. r   )r[   rC   s    r'   rH   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[   rH   r   format)r$   rh   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[   rT   rH   r   rn   )r$   rh   rO   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                  S5      R                  SS	5      n[        U R                  R                  5       S
S9 GH  u  pg[        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!                  U5      5        ["        U
l        ['        U5      (       d  GM%  US   nUR(                  S:X  a,  UR$                  [*        -   Ul        UR                  U
5        GMf  [        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   z%dr   r   )startlirh   ahref#footnote-backreftitlerX   p)listrB   keysetreeElementr!   
SubElementr[   replace	enumeratero   r2   
parseChunkappendremoverr   rn   FN_BACKLINK_TEXTrY   lentagNBSP_PLACEHOLDER)r$   rb   ru   rx   surrogate_parentbacklink_titleindexrh   rz   elbacklinknoder   s                r'   makeFootnotesDiv"FootnoteExtension.makeFootnotesDiv   s    DNN'')**mmE"$d#c4( ==/ (89AA$M"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%%e, -HM2ww"v88s? $		,< <DIKK)((S1AHHX&5 C6 
r)   )r   rB   r    r3   r2   r   r"   )returnNone)F)rN   strrO   boolr   r   )rb   etree.Elementr   z0tuple[etree.Element, etree.Element, bool] | None)rh   r   rY   r   r   r   )r   r   )rh   r   r   r   )rh   r   rO   r   r   r   )rb   r   r   zetree.Element | None)__name__
__module____qualname____firstlineno____doc__r   r?   r#   rT   rd   ri   rH   ro   rr   r   __static_attributes____classcell__r&   s   @r'   r   r   &   sP    !FP2*!	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$ )r6      z6Find all footnote references and store for later use. z!^[ ]{0,3}\[\^([^\]]*)\]:[ ]*(.*)$c                D   > [         TU ]  UR                  5        Xl        g N)r   r   r2   rB   )r$   rB   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searchrK   endlstripry   rstripjoindetabinsertstripextenddetectTabbedrB   ri   )r$   r   blocksr   rS   rh   	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   ry   r   r   r   r   )r$   r   r   r   rS   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)rG   r   r   r   )r$   r   linesilines        r'   r   FootnoteBlockProcessor.detab%  sP    
 D! 'GAu%%8 ( yyr)   rg   rB   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'   r6   r6      s:    A	8",,	GB#"H:	  	 r)   r6   c                  :   ^  \ rS rSrSrSU 4S jjrSS jrSrU =r$ )r8   i1  zB`InlineProcessor` for footnote markers in a document's body text. c                0   > [         TU ]  U5        X l        g r   )r   r   rB   )r$   patternrB   r&   s      r'   r    FootnoteInlineProcessor.__init__4  s    !"r)   c                   UR                  S5      nX0R                  R                  R                  5       ;   Ga%  [        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 R                  R                  R                  5       5      R                  U5      S-   5      Ul        XAR                  S5      UR                  S5      4$ g)Nr   supr{   rh   T)rO   r|   r}   rv   zfootnote-refr   r   )NNN)rK   rB   r   r   r   r   r!   rr   ro   r[   rn   r   r   rY   ry   r   )r$   rS   datarh   r   r{   s         r'   handleMatch#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)   rg   )r   r   rB   r   )rS   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'   r8   r8   1  s    M#$ $r)   r8   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;   iH  z$Amend footnote div with duplicates. c                    Xl         g r   rg   r$   rB   s     r'   r   "FootnotePostTreeprocessor.__init__K      "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{   rv    r~   r|   r   r   rF   rX   N)iterattribgetrG   rB   rH   rangecopydeepcopyr   offsetr   )
r$   rz   
duplicateslinkrQ   rR   linksr   sib_linkr   s
             r'   add_duplicates(FootnotePostTreeprocessor.add_duplicatesN  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. rh   r   r   z	{}ref{}{}r   )r   r   rG   rB   rH   rn   r    )r$   rz   fnrR   link_ids        r'   get_num_duplicates,FootnotePostTreeprocessor.get_num_duplicatesb  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   rz   counts       r'   handle_duplicates+FootnotePostTreeprocessor.handle_duplicatesh  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   ru   rv   r   r+   rx   N)r   r   r   r   r   )r$   rb   ru   rx   s       r'   r   FootnotePostTreeprocessor.runq  sX    99U#Czz~~gr*j8 ((4.B**2. )	 $r)   )rB   r   Nr   )rz   r   r   rL   r   r   )rz   r   r   rL   )r   r   r   r   rb   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r)   r'   r;   r;   H  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   rg   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   )	rB   r   rd   r   r   r   r   r\   r   )r$   rb   footnotesDivresultr^   r   isTextinds           r'   r   FootnoteTreeprocessor.run  s    ~~66t<#^^<<TBF(.%v6l((/MM%(MM#4MM#'<8!%EJL) $r)   rg   Nr   r   r   r   r   r   r   r   r   r   r   r)   r'   r:   r:   }  s    =#*r)   r:   c                  ,    \ rS rSrSrSS jrSS jrSrg)	r=   i  z)Replace placeholders with html entities. c                    Xl         g r   rg   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   rB   r[   r   )r$   rY   s     r'   r   FootnotePostprocessor.run  s8    ||dnn66G
 ||,h77r)   rg   Nr   )rY   r   r   r   r  r   r)   r'   r=   r=     s    4#8r)   r=   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   r4   r   inlinepatternsr   r9   r	   r<   r
   r   collectionsr   r   r   xml.etree.ElementTreer   ElementTreeSTXETXr   r   r   rI   r   r6   r8   r;   r:   r=   r  r   r)   r'   <module>r     s    #  , , * *  # 	  % %8844txx? 8844txx? JJ'	o	 odV ^ V r$o $.2 2j*M *.	8M 	8'r)   