o
    ³2úh{K  ã                   @  s6  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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!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)ÚOrderedDictNÚzz1337820767766393qqÚqq3936677670287331zzz(fnref)(\d+)z(?<!!)\[\^([^\]]*)\](?!\s*:)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„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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__r   úX/var/www/html/chatgem/venv/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¡ |  
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#   r1   ÚFOOTNOTE_REr   r   r'   ÚextendMarkdownS   s   

z FootnoteExtension.extendMarkdownÚreturnÚNonec                 C  s.   g | _ tƒ | _|  jd7  _i | _tƒ | _dS )z> Clear footnotes on reset, and prepare for distinct document. r   N)Úfootnote_orderr   Ú	footnotesr   r   r   r   ©r#   r   r   r'   r"   r   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#   rD   rF   Úoriginal_refÚrefÚrestÚmr   r   r'   Ú
unique_refz   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Úfindr    Útail)ÚelementÚchildÚ	child_res©Úfinderr#   r   r'   r`   “   s   ÿz:FootnoteExtension.findFootnotesPlaceholder.<locals>.finderr   )r#   rU   Úresr   r_   r'   ÚfindFootnotesPlaceholder   s   z*FootnoteExtension.findFootnotesPlaceholderÚidrY   c                 C  s   || j |< dS )z' Store a footnote for later retrieval. N©rB   )r#   rc   rY   r   r   r'   ÚsetFootnote£   ó   zFootnoteExtension.setFootnotec                 C  s   || j vr| j  |¡ dS dS )z7 Store a footnote reference id in order of appearance. N)rA   Úappend©r#   rc   r   r   r'   ÚaddFootnoteRef§   s   
ÿz FootnoteExtension.addFootnoteRefc                 C  s
   |   d¡S )z Get the footnote separator. r   )r    rC   r   r   r'   rJ   ¬   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{}{})r    rJ   r   Úformatrh   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{}{})r    rT   rJ   r   rj   )r#   rc   rF   r   r   r'   ÚmakeFootnoteRefId·   s   
z#FootnoteExtension.makeFootnoteRefIdúetree.Element | Nonec                 C  sP  t | j ¡ ƒs	dS t d¡}| dd¡ t |d¡ t |d¡}t d¡}t| j ¡ ddD ]w\}}t |d	¡}| d
|  |¡¡ | j	 
|| j| ¡ t |ƒD ]}| |¡ | |¡ qOt d¡}	|	 dd|  |¡ ¡ |	 dd¡ |	 d|  d¡ |¡¡ t|	_t|ƒr¥|d }
|
jdkrš|
jt |
_|
 |	¡ q.t |d¡}| |	¡ q.|S )z/ Return `div` of footnotes as `etree` Element. NÚdivÚclassr(   ÚhrÚolr   ©ÚstartÚlirc   ÚaÚhrefú#úfootnote-backrefÚtitler   rX   Úp)ÚlistrB   ÚkeysÚetreeÚElementr   Ú
SubElementÚ	enumeraterk   r0   Ú
parseChunkrg   Úremoverl   r    rj   ÚFN_BACKLINK_TEXTrY   ÚlenÚtagÚNBSP_PLACEHOLDER)r#   rU   rn   rq   Úsurrogate_parentÚindexrc   rt   ÚelÚbacklinkÚnoderz   r   r   r'   ÚmakeFootnotesDiv¾   s>   



þ

€z"FootnoteExtension.makeFootnotesDiv)r?   r@   )F)rD   rE   rF   rG   r?   rE   )rU   rV   r?   rW   )rc   rE   rY   rE   r?   r@   )rc   rE   r?   r@   )r?   rE   )rc   rE   r?   rE   )rc   rE   rF   rG   r?   rE   )rU   rV   r?   rm   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r>   r"   rT   rb   re   ri   rJ   rk   rl   rŒ   Ú__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 )r4   z4 Find footnote definitions and store for later use. z!^[ ]{0,3}\[\^([^\]]*)\]:[ ]*(.*)$rB   r   c                   s   t ƒ  |j¡ || _d S ©N)r   r   r0   rB   ©r#   rB   r%   r   r'   r   í   s   
zFootnoteBlockProcessor.__init__ÚparentrV   ÚblockrE   r?   rG   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ÚsearchrM   ÚendÚlstriprs   ÚrstripÚjoinÚdetabÚinsertÚstripÚextendÚdetectTabbedrB   re   )r#   r”   r—   r•   rS   rc   Ú	fn_blocksÚtherestÚ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œ   rs   rŸ   rg   r¡   r¢   )r#   r—   r¦   r•   rS   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)rI   r€   r¬   r    )r#   r•   ÚlinesÚiÚliner   r   r'   r¡   6  s   

€
zFootnoteBlockProcessor.detab©rB   r   )r”   rV   r•   rE   r?   rG   )r”   rV   r—   r˜   r?   rG   )r—   r˜   r?   r˜   )r•   rE   r?   rE   )r   rŽ   r   r   ÚreÚcompileÚ	MULTILINEr›   r   r–   rª   r¥   r¡   r‘   r   r   r%   r'   r4   è   s    


%r4   c                      s,   e Zd ZdZd‡ fdd„Zddd„Z‡  ZS )r6   zC `InlineProcessor` for footnote markers in a document's body text. ÚpatternrE   rB   r   c                   s   t ƒ  |¡ || _d S r’   )r   r   rB   )r#   rµ   rB   r%   r   r'   r   E  s   
z FootnoteInlineProcessor.__init__rS   úre.Match[str]Údatar?   ú3tuple[etree.Element | None, int | None, int | None]c                 C  sà   |  d¡}|| jj ¡ v rn| j |¡ | j d¡s#| jj |¡d }nt| jj ¡ ƒ |¡d }t 	d¡}t 
|d¡}| d| jj|dd¡ | dd	| j |¡ ¡ | d
d¡ | j d¡ |¡|_|| d¡| d¡fS dS )Nr   r   Úsupru   rc   T)rF   rv   rw   ro   zfootnote-refr   r   )NNN)rM   rB   r|   ri   r    rA   rˆ   r{   r}   r~   r   r   rl   rk   rj   rY   rs   r   )r#   rS   r·   rc   Úfootnote_numr¹   ru   r   r   r'   ÚhandleMatchI  s   

z#FootnoteInlineProcessor.handleMatch)rµ   rE   rB   r   )rS   r¶   r·   rE   r?   r¸   )r   rŽ   r   r   r   r»   r‘   r   r   r%   r'   r6   B  s    r6   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 )r:   z% Amend footnote div with duplicates. rB   r   c                 C  ó
   || _ d S r’   rd   r“   r   r   r'   r   c  ó   
z"FootnotePostTreeprocessor.__init__rt   rV   Ú
duplicatesrN   r?   r@   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. ru   ro   Ú rx   rv   r   r   rH   rX   N)ÚiterÚattribÚgetrI   rB   rJ   ÚrangeÚcopyÚdeepcopyrg   Úoffsetr{   )
r#   rt   r¾   ÚlinkrQ   rR   Úlinksrˆ   Úsib_linkr‰   r   r   r'   Úadd_duplicatesf  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. rc   r¿   r   z	{}ref{}{}r   )rÁ   rÂ   rI   rB   rJ   rj   r   )r#   rt   ÚfnrR   Úlink_idr   r   r'   Úget_num_duplicatesz  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)r{   rÍ   rÊ   )r#   r”   rt   Úcountr   r   r'   Úhandle_duplicates€  s   
€ûz+FootnotePostTreeprocessor.handle_duplicatesrU   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   rn   ro   r¿   r(   rq   N)rÆ   rÀ   rÁ   rÂ   rÏ   )r#   rU   rn   rq   r   r   r'   rª   ‰  s   
€úzFootnotePostTreeprocessor.runNr±   )rt   rV   r¾   rN   r?   r@   )rt   rV   r?   rN   ©r”   rV   r?   r@   ©rU   rV   r?   r@   )	r   rŽ   r   r   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 )r8   z3 Build and append footnote div to end of document. rB   r   c                 C  r¼   r’   rd   r“   r   r   r'   r   ˜  r½   zFootnoteTreeprocessor.__init__rU   rV   r?   r@   c                 C  s†   | j  |¡}|d urA| j  |¡}|r:|\}}}t|ƒ |¡}|r-| |¡ | ||¡ d S | |d |¡ d |_d S | |¡ d S d S )Nr   )	rB   rŒ   rb   r{   rˆ   r‚   r¢   r[   rg   )r#   rU   ÚfootnotesDivÚresultr]   r”   ÚisTextÚindr   r   r'   rª   ›  s   


ôzFootnoteTreeprocessor.runNr±   rÑ   ©r   rŽ   r   r   r   rª   r   r   r   r'   r8   •  s    
r8   c                   @  s.   e Zd ZdZddd„Zdd
d„Zddd„ZdS )r9   z* Reorder list items in the footnotes div. rB   r   c                 C  r¼   r’   rd   r“   r   r   r'   r   ¯  r½   z$FootnoteReorderingProcessor.__init__rU   rV   r?   r@   c                 C  s^   | j j sd S | j jt| j j  ¡ ƒkr+| d¡D ]}|j dd¡dkr*|  |¡  d S qd S d S )Nrn   ro   r¿   r(   )rB   rA   r{   r|   rÀ   rÁ   rÂ   Úreorder_footnotes)r#   rU   rn   r   r   r'   rª   ²  s   
þþzFootnoteReorderingProcessor.runr”   c           	        sŒ   |  d¡}| |¡ | d¡}d‡ fdd„}t||d}t |d¡}t|dd	D ]\}}|  d
¡}| dˆ j 	d¡ 
|¡¡ | |¡ q(d S )Nrq   rt   r?   rN   c                   sF   | j  dd¡ ˆ j ¡ d¡d }|ˆ jjv rˆ jj |¡S tˆ jjƒS )Nrc   r¿   r   rX   )rÁ   rÂ   rI   rB   rJ   rA   rˆ   r„   )rt   rc   rC   r   r'   Úorder_by_idÀ  s    ÿÿ
ýzBFootnoteReorderingProcessor.reorder_footnotes.<locals>.order_by_id)Úkeyr   rr   z.//a[@class="footnote-backref"]ry   r   )r?   rN   )rZ   r‚   ÚfindallÚsortedr}   r   r€   r   rB   r    rj   rg   )	r#   r”   Úold_listÚitemsrØ   Únew_listrˆ   ÚitemrŠ   r   rC   r'   r×   »  s   



ýz-FootnoteReorderingProcessor.reorder_footnotesNr±   rÑ   rÐ   )r   rŽ   r   r   r   rª   r×   r   r   r   r'   r9   ¬  s
    

	r9   c                   @  s$   e Zd ZdZddd„Zdd	d
„ZdS )r<   z* Replace placeholders with html entities. rB   r   c                 C  r¼   r’   rd   r“   r   r   r'   r   Ô  r½   zFootnotePostprocessor.__init__rY   rE   r?   c                 C  s    |  t| j d¡¡}|  td¡S )Nr   z&#160;)r!   rƒ   rB   r    r†   )r#   rY   r   r   r'   rª   ×  s   ÿzFootnotePostprocessor.runNr±   )rY   rE   r?   rE   rÖ   r   r   r   r'   r<   Ò  s    
r<   c                  K  s   t di | ¤ŽS )z/ Return an instance of the `FootnoteExtension` Nr   )r   )r$   r   r   r'   ÚmakeExtensionÞ  rf   rà   )$r   Ú
__future__r   r¿   r   r2   r   Úinlinepatternsr   r7   r   r;   r	   r
   Úcollectionsr   r²   rÄ   Úxml.etree.ElementTreer}   ÚElementTreeÚSTXÚETXrƒ   r†   r³   rK   ÚRE_REFERENCEr   r4   r6   r:   r8   r9   r<   rà   r   r   r   r'   Ú<module>   s2   

 BZ5&