o
    ñ+óeFA  ã                   @  s  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  mZ ejd ej Zejd ej Ze d¡ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ de
ƒZG dd„ de
ƒZG dd„ deƒZ d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)ÚOrderedDictNZzz1337820767766393qqZqq3936677670287331zzz(fnref)(\d+)c                      s|   e Zd ZdZ‡ fdd„Zdd„ Zd#dd	„Zd$d%dd„Zd&dd„Zd'dd„Z	d(dd„Z
d)dd„Zd$d*dd„Zd+d!d"„Z‡  ZS ),ÚFootnoteExtensionz Footnote Extension. c                   s`   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 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© )ZconfigÚsuperÚ__init__Úunique_prefixÚ
found_refsÚsetÚ	used_refsÚreset)ÚselfÚkwargs©Ú	__class__r   ú=lib/python3.10/site-packages/markdown/extensions/footnotes.pyr   )   s(   ÿÿÿÿþÿîzFootnoteExtension.__init__c                 C  sˆ   |  | ¡ |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)ZregisterExtensionÚparserÚmdÚblockprocessorsÚregisterÚFootnoteBlockProcessorZinlinePatternsÚFootnoteInlineProcessorÚtreeprocessorsÚFootnoteTreeprocessorÚFootnotePostTreeprocessorÚpostprocessorsÚFootnotePostprocessor)r   r)   ZFOOTNOTE_REr   r   r!   ÚextendMarkdownL   s   
z FootnoteExtension.extendMarkdownÚreturnÚNonec                 C  s(   t ƒ | _|  jd7  _i | _tƒ | _dS )z> Clear footnotes on reset, and prepare for distinct document. r   N)r   Ú	footnotesr   r   r   r   ©r   r   r   r!   r   e   s   zFootnoteExtension.resetFÚ	referenceÚstrÚfoundÚboolc                 C  sº   |s|S |}|| j v r@| |  ¡ d¡\}}t |¡}|r1d| d¡t| d¡ƒd |  ¡ |f }n
d|d|  ¡ |f }|| j v s| j  |¡ || jv rV| j|  d7  < |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   r8   r:   Zoriginal_refÚrefÚrestÚmr   r   r!   Ú
unique_refl   s    

*
ú

ÿzFootnoteExtension.unique_refÚrootúetree.Elementú0tuple[etree.Element, etree.Element, bool] | Nonec                   s   ‡ ‡fdd„‰ ˆ |ƒ}|S )z@ Return ElementTree Element that contains Footnote placeholder. c                   sz   | D ]8}|j r|j  ˆ d¡¡dkr|| df  S |jr.|j ˆ d¡¡dkr.|| df  S ˆ |ƒ}|d ur:|  S qd S )Nr   éÿÿÿÿTF)ÚtextÚfindÚ	getConfigÚtail)ÚelementÚchildZ	child_res©Úfinderr   r   r!   rS   …   s   ÿz:FootnoteExtension.findFootnotesPlaceholder.<locals>.finderr   )r   rH   Úresr   rR   r!   ÚfindFootnotesPlaceholder   s   z*FootnoteExtension.findFootnotesPlaceholderÚidrL   c                 C  s   || j |< dS )z' Store a footnote for later retrieval. N©r6   )r   rV   rL   r   r   r!   ÚsetFootnote•   ó   zFootnoteExtension.setFootnotec                 C  s
   |   d¡S )z Get the footnote separator. r   )rN   r7   r   r   r!   r>   ™   s   
zFootnoteExtension.get_separatorc                 C  s.   |   d¡rd|  ¡ | j|f S d |  ¡ |¡S )z Return footnote link id. r   z	fn%s%d-%szfn{}{})rN   r>   r   Úformat)r   rV   r   r   r!   ÚmakeFootnoteId   s   
z FootnoteExtension.makeFootnoteIdc                 C  s>   |   d¡r|  d|  ¡ | j|f |¡S |  d |  ¡ |¡|¡S )z Return footnote back-link id. r   zfnref%s%d-%sz	fnref{}{})rN   rG   r>   r   rZ   )r   rV   r:   r   r   r!   ÚmakeFootnoteRefId¤   s   
z#FootnoteExtension.makeFootnoteRefIdúetree.Element | Nonec                 C  s\  t | j ¡ ƒs	dS t d¡}| dd¡ t |d¡ t |d¡}t d¡}|  d¡ dd	¡}t	| j ¡ d
dD ]t\}}t |d¡}| d|  
|¡¡ | j || j| ¡ t |ƒD ]}	| |	¡ | |	¡ qXt d¡}
|
 dd|  |¡ ¡ |
 dd¡ |
 d| |¡¡ t|
_t|ƒr«|d }|jdkr |jt |_| |
¡ q7t |d¡}| |
¡ q7|S )z/ Return `div` of footnotes as `etree` Element. NÚdivÚclassr"   ÚhrÚolr   z%dr   r   )ÚstartÚlirV   ÚaÚhrefú#úfootnote-backrefÚtitlerK   Úp)Úlistr6   ÚkeysÚetreeÚElementr   Ú
SubElementrN   ÚreplaceÚ	enumerater[   r(   Z
parseChunkÚappendÚremover\   rZ   ÚFN_BACKLINK_TEXTrL   ÚlenÚtagÚNBSP_PLACEHOLDER)r   rH   r^   ra   Zsurrogate_parentZbacklink_titleÚindexrV   rc   ÚelZbacklinkZnoderi   r   r   r!   ÚmakeFootnotesDiv«   s@   



þ

€z"FootnoteExtension.makeFootnotesDiv)r4   r5   )F)r8   r9   r:   r;   r4   r9   )rH   rI   r4   rJ   )rV   r9   rL   r9   r4   r5   )r4   r9   )rV   r9   r4   r9   )rV   r9   r:   r;   r4   r9   )rH   rI   r4   r]   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r3   r   rG   rU   rX   r>   r[   r\   ry   Ú__classcell__r   r   r   r!   r   &   s    #




r   c                      sX   e Zd ZdZe dej¡Zd‡ fdd„Zddd„Z	ddd„Z
ddd„Zddd„Z‡  ZS )r,   z7 Find all footnote references and store for later use. z!^[ ]{0,3}\[\^([^\]]*)\]:[ ]*(.*)$r6   r   c                   s   t ƒ  |j¡ || _d S ©N)r   r   r(   r6   ©r   r6   r   r   r!   r   Ý   s   
zFootnoteBlockProcessor.__init__ÚparentrI   Úblockr9   r4   r;   c                 C  s   dS )NTr   )r   r   r‚   r   r   r!   Útestá   s   zFootnoteBlockProcessor.testÚblocksú	list[str]c                 C  s<  |  d¡}| j |¡}|r–| d¡}| d¡g}|| ¡ d…  d¡}| j |¡}|rT|d| ¡ …  d¡}	d |d |  	|	¡g¡ d¡|d< | 
d|| ¡ d… ¡ nd |d |  	|¡g¡ d¡|d< | |  |¡¡ d |¡}
| j ||
 ¡ ¡ |d| ¡ …  ¡ r”| 
d|d| ¡ …  d¡¡ dS | 
d|¡ dS )	z- Find, set, and remove footnote definitions. r   r   r   NÚ
z

TF)ÚpopÚREÚsearchrA   ÚendÚlstriprb   ÚrstripÚjoinÚdetabÚinsertÚstripÚextendÚdetectTabbedr6   rX   )r   r   r„   r‚   rF   rV   Ú	fn_blocksZtherestZm2Úbeforer"   r   r   r!   Úrunä   s(   

""
zFootnoteBlockProcessor.runc                 C  s”   g }|rH|d   d¡rC| d¡}| j |¡}|r:|d| ¡ …  d¡}| |  |¡¡ | d|| ¡ d… ¡ 	 |S | |  |¡¡ n	 |S |s|S )z’ Find indented text and remove indent before further processing.

        Returns:
            A list of blocks with indentation removed.
        r   ú    Nr†   )	Ú
startswithr‡   rˆ   r‰   rb   rŒ   rq   rŽ   r   )r   r„   r“   r‚   rF   r”   r   r   r!   r’     s    
üìz#FootnoteBlockProcessor.detectTabbedc                 C  s@   |  d¡}t|ƒD ]\}}| d¡r|dd… ||< q	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)r=   rp   r—   r   )r   r‚   ÚlinesÚiÚliner   r   r!   rŽ   %  s   

€
zFootnoteBlockProcessor.detab©r6   r   )r   rI   r‚   r9   r4   r;   )r   rI   r„   r…   r4   r;   )r„   r…   r4   r…   )r‚   r9   r4   r9   )rz   r{   r|   r}   ÚreÚcompileÚ	MULTILINErˆ   r   rƒ   r•   r’   rŽ   r~   r   r   r   r!   r,   Ø   s    


$r,   c                      s,   e Zd ZdZd‡ fdd„Zddd„Z‡  ZS )r-   zC `InlineProcessor` for footnote markers in a document's body text. Úpatternr9   r6   r   c                   s   t ƒ  |¡ || _d S r   )r   r   r6   )r   r    r6   r   r   r!   r   4  s   
z FootnoteInlineProcessor.__init__rF   úre.Match[str]Údatar4   ú3tuple[etree.Element | None, int | None, int | None]c                 C  s°   |  d¡}|| jj ¡ v rVt d¡}t |d¡}| d| jj|dd¡ | dd| j |¡ ¡ | d	d
¡ | j 	d¡ 
t| jj ¡ ƒ |¡d ¡|_|| d¡| d¡fS dS )Nr   Úsuprd   rV   T)r:   re   rf   r_   zfootnote-refr   r   )NNN)rA   r6   rk   rl   rm   rn   r   r\   r[   rN   rZ   rj   rw   rL   rb   rŠ   )r   rF   r¢   rV   r¤   rd   r   r   r!   ÚhandleMatch8  s   

ÿz#FootnoteInlineProcessor.handleMatch)r    r9   r6   r   )rF   r¡   r¢   r9   r4   r£   )rz   r{   r|   r}   r   r¥   r~   r   r   r   r!   r-   1  s    r-   c                   @  sB   e Zd ZdZddd„Zddd„Zddd„Zddd„Zddd„ZdS )r0   z% Amend footnote div with duplicates. r6   r   c                 C  ó
   || _ d S r   rW   r€   r   r   r!   r   K  ó   
z"FootnotePostTreeprocessor.__init__rc   rI   Ú
duplicatesrB   r4   r5   c           
      C  s¾   |  d¡D ]W}|j dd¡dkr\|jd  | j ¡ d¡\}}g }td|d ƒD ]!}t |¡}d||| j ¡ |f |jd< | 	|¡ |  j
d7  _
q't|ƒd	 }	|D ]}|	 	|¡ qQ d
S qd
S )zF Adjust current `li` and add the duplicates: `fnref2`, `fnref3`, etc. rd   r_   Ú rg   re   r   r   r<   rK   N)ÚiterÚattribÚgetr=   r6   r>   ÚrangeÚcopyZdeepcopyrq   Úoffsetrj   )
r   rc   r¨   ÚlinkrD   rE   Zlinksrw   Zsib_linkrx   r   r   r!   Úadd_duplicatesN  s   

òþz(FootnotePostTreeprocessor.add_duplicatesc                 C  sD   |j  dd¡ | j ¡ d¡\}}d || j ¡ |¡}| jj |d¡S )z3 Get the number of duplicate refs of the footnote. rV   r©   r   z	{}ref{}{}r   )r«   r¬   r=   r6   r>   rZ   r   )r   rc   ÚfnrE   Zlink_idr   r   r!   Úget_num_duplicatesb  s    z,FootnotePostTreeprocessor.get_num_duplicatesr   c                 C  s0   t |ƒD ]}|  |¡}|dkr|  ||¡ qdS )z= Find duplicate footnotes and format and add the duplicates. r   N)rj   r³   r±   )r   r   rc   Úcountr   r   r!   Úhandle_duplicatesh  s   
€ûz+FootnotePostTreeprocessor.handle_duplicatesrH   c                 C  sF   d| _ | d¡D ]}|j dd¡dkr | d¡D ]}|  |¡  qdS )z= Crawl the footnote div and add missing duplicate footnotes. r   r^   r_   r©   r"   ra   N)r¯   rª   r«   r¬   rµ   )r   rH   r^   ra   r   r   r!   r•   q  s   
€úzFootnotePostTreeprocessor.runNrœ   )rc   rI   r¨   rB   r4   r5   )rc   rI   r4   rB   )r   rI   r4   r5   ©rH   rI   r4   r5   )	rz   r{   r|   r}   r   r±   r³   rµ   r•   r   r   r   r!   r0   H  s    



	r0   c                   @  s$   e Zd ZdZddd„Zdd
d„ZdS )r/   z3 Build and append footnote div to end of document. r6   r   c                 C  r¦   r   rW   r€   r   r   r!   r   €  r§   zFootnoteTreeprocessor.__init__rH   rI   r4   r5   c                 C  s†   | j  |¡}|d urA| j  |¡}|r:|\}}}t|ƒ |¡}|r-| |¡ | ||¡ d S | |d |¡ d |_d S | |¡ d S d S )Nr   )	r6   ry   rU   rj   rw   rr   r   rO   rq   )r   rH   ZfootnotesDivÚresultrQ   r   ZisTextZindr   r   r!   r•   ƒ  s   


ôzFootnoteTreeprocessor.runNrœ   r¶   ©rz   r{   r|   r}   r   r•   r   r   r   r!   r/   }  s    
r/   c                   @  s$   e Zd ZdZddd„Zdd	d
„ZdS )r2   z* Replace placeholders with html entities. r6   r   c                 C  r¦   r   rW   r€   r   r   r!   r   –  r§   zFootnotePostprocessor.__init__rL   r9   r4   c                 C  s    |  t| j d¡¡}|  td¡S )Nr   z&#160;)ro   rs   r6   rN   rv   )r   rL   r   r   r!   r•   ™  s   ÿzFootnotePostprocessor.runNrœ   )rL   r9   r4   r9   r¸   r   r   r   r!   r2   ”  s    
r2   c                  K  s   t di | ¤ŽS )z/ Return an instance of the `FootnoteExtension` Nr   )r   )r   r   r   r!   ÚmakeExtension   rY   r¹   )"r}   Z
__future__r   r©   r   r*   r   Zinlinepatternsr   r.   r   r1   r	   r
   Úcollectionsr   r   r®   Zxml.etree.ElementTreerl   ZElementTreeZSTXZETXrs   rv   rž   r?   r   r,   r-   r0   r/   r2   r¹   r   r   r   r!   Ú<module>   s.   
 3Y5