o
    „¿Ñg?  ã                   @  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)ÚOrderedDictNÚzz1337820767766393qqÚqq3936677670287331zzz(fnref)(\d+)c                      st   e Zd ZdZ‡ fdd„Zdd„ Zddd	„Zdddd„Zdd„ Zddd„Z	dd„ Z
dd„ Zdddd„Z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© )ÚconfigÚsuperÚ__init__Úunique_prefixÚ
found_refsÚsetÚ	used_refsÚreset)ÚselfÚkwargs©Ú	__class__r   ú]/var/www/html/bloggers_ems/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¡ |j t
| ƒdd¡ |j t| ƒdd¡ d	S )
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    r-   Ú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Ú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    Ú	referencer>   Úoriginal_refÚrefÚrestÚmr   r   r$   Ú
unique_refl   s    

*
ú

ÿzFootnoteExtension.unique_refc                   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ÚchildÚ	child_res©Úfinderr    r   r$   rW   ƒ   s   ÿz:FootnoteExtension.findFootnotesPlaceholder.<locals>.finderr   )r    ÚrootÚresr   rV   r$   ÚfindFootnotesPlaceholder   s   z*FootnoteExtension.findFootnotesPlaceholderc                 C  s   || j |< dS )z' Store a footnote for later retrieval. N©r<   )r    ÚidrO   r   r   r$   ÚsetFootnote“   ó   zFootnoteExtension.setFootnotec                 C  s
   |   d¡S )z Get the footnote separator. r   )rQ   r=   r   r   r$   rB   —   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{}{})rQ   rB   r   Úformat)r    r\   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{}{})rQ   rM   rB   r   r_   )r    r\   r>   r   r   r$   ÚmakeFootnoteRefId¢   s   
z#FootnoteExtension.makeFootnoteRefIdc                 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Úlir\   ÚaÚhrefú#úfootnote-backrefÚtitlerN   Úp)Úlistr<   ÚkeysÚetreeÚElementr   Ú
SubElementrQ   ÚreplaceÚ	enumerater`   r,   Ú
parseChunkÚappendÚremovera   r_   ÚFN_BACKLINK_TEXTrO   ÚlenÚtagÚNBSP_PLACEHOLDER)r    rX   rb   re   Úsurrogate_parentÚbacklink_titleÚindexr\   rg   ÚelÚbacklinkÚnoderm   r   r   r$   ÚmakeFootnotesDiv©   s@   



þ

€z"FootnoteExtension.makeFootnotesDiv©r:   r;   )F)r>   r?   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r9   r   rM   rZ   r]   rB   r`   ra   r‚   Ú__classcell__r   r   r"   r$   r   &   s    #

r   c                      sP   e Zd ZdZe dej¡Z‡ fdd„Zdd„ Z	dd„ Z
ddd„Zdd„ Z‡  ZS )r0   z7 Find all footnote references and store for later use. z!^[ ]{0,3}\[\^([^\]]*)\]:[ ]*(.*)$c                   s   t ƒ  |j¡ || _d S ©N)r   r   r,   r<   ©r    r<   r"   r   r$   r   Û   s   
zFootnoteBlockProcessor.__init__c                 C  s   dS )NTr   )r    ÚparentÚblockr   r   r$   Útestß   s   zFootnoteBlockProcessor.testc                 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ÚsearchrE   ÚendÚlstriprf   ÚrstripÚjoinÚdetabÚinsertÚstripÚextendÚdetectTabbedr<   r]   )r    r‹   ÚblocksrŒ   rL   r\   Ú	fn_blocksÚtherestÚm2Úbeforer%   r   r   r$   Úrunâ   s(   

""
zFootnoteBlockProcessor.runr:   ú	list[str]c                 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‘   rf   r”   rv   r–   r—   )r    r›   rœ   rŒ   rL   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)rA   rt   r£   r•   )r    rŒ   ÚlinesÚiÚliner   r   r$   r–   #  s   

€
zFootnoteBlockProcessor.detab)r:   r¡   )r„   r…   r†   r‡   ÚreÚcompileÚ	MULTILINEr   r   r   r    rš   r–   rˆ   r   r   r"   r$   r0   Ö   s    
$r0   c                      s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )r2   zC `InlineProcessor` for footnote markers in a document's body text. c                   s   t ƒ  |¡ || _d S r‰   )r   r   r<   )r    Úpatternr<   r"   r   r$   r   2  s   
z FootnoteInlineProcessor.__init__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   Úsuprh   r\   T)r>   ri   rj   rc   zfootnote-refr   r   )NNN)rE   r<   ro   rp   rq   rr   r   ra   r`   rQ   r_   rn   r~   rO   rf   r’   )r    rL   Údatar\   r¬   rh   r   r   r$   ÚhandleMatch6  s   

ÿz#FootnoteInlineProcessor.handleMatch)r„   r…   r†   r‡   r   r®   rˆ   r   r   r"   r$   r2   /  s    r2   c                   @  s<   e Zd ZdZdd„ Zddd„Zdd	„ Zdd
d„Zdd„ ZdS )r5   z% Amend footnote div with duplicates. c                 C  ó
   || _ d S r‰   r[   rŠ   r   r   r$   r   I  ó   
z"FootnotePostTreeprocessor.__init__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. rh   rc   Ú rk   ri   r   r   r@   rN   N)ÚiterÚattribÚgetrA   r<   rB   ÚrangeÚcopyÚdeepcopyrv   Úoffsetrn   )
r    rg   Ú
duplicatesÚlinkrJ   rK   Úlinksr~   Úsib_linkr   r   r   r$   Úadd_duplicatesL  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. r\   r±   r   z	{}ref{}{}r   )r³   r´   rA   r<   rB   r_   r   )r    rg   ÚfnrK   Úlink_idr   r   r$   Úget_num_duplicates`  s    z,FootnotePostTreeprocessor.get_num_duplicatesc                 C  s0   t |ƒD ]}|  |¡}|dkr|  ||¡ qdS )z= Find duplicate footnotes and format and add the duplicates. r   N)rn   rÀ   r½   )r    r‹   rg   Úcountr   r   r$   Úhandle_duplicatesf  s   
€ûz+FootnotePostTreeprocessor.handle_duplicatesc                 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   rb   rc   r±   r%   re   N)r¸   r²   r³   r´   rÂ   )r    rX   rb   re   r   r   r$   r    o  s   
€úzFootnotePostTreeprocessor.runNrƒ   )	r„   r…   r†   r‡   r   r½   rÀ   rÂ   r    r   r   r   r$   r5   F  s    

	r5   c                   @  ó    e Zd ZdZdd„ Zdd„ ZdS )r4   z3 Build and append footnote div to end of document. c                 C  r¯   r‰   r[   rŠ   r   r   r$   r   ~  r°   zFootnoteTreeprocessor.__init__c                 C  s†   | j  |¡}|d urA| j  |¡}|r:|\}}}t|ƒ |¡}|r-| |¡ | ||¡ d S | |d |¡ d |_d S | |¡ d S d S )Nr   )	r<   r‚   rZ   rn   r~   rw   r—   rR   rv   )r    rX   ÚfootnotesDivÚresultrT   r‹   ÚisTextÚindr   r   r$   r      s   


ôzFootnoteTreeprocessor.runN©r„   r…   r†   r‡   r   r    r   r   r   r$   r4   {  s    r4   c                   @  rÃ   )r7   z* Replace placeholders with html entities. c                 C  r¯   r‰   r[   rŠ   r   r   r$   r   ”  r°   zFootnotePostprocessor.__init__c                 C  s    |  t| j d¡¡}|  td¡S )Nr   z&#160;)rs   rx   r<   rQ   r{   )r    rO   r   r   r$   r    —  s   ÿzFootnotePostprocessor.runNrÈ   r   r   r   r$   r7   ’  s    r7   c                  K  s   t di | ¤ŽS )z/ Return an instance of the `FootnoteExtension` Nr   )r   )r!   r   r   r$   ÚmakeExtensionž  r^   rÉ   )"r‡   Ú
__future__r   r±   r   r.   r   Úinlinepatternsr   r3   r   r6   r	   r
   Úcollectionsr   r¨   r¶   Úxml.etree.ElementTreerp   ÚElementTreeÚSTXÚETXrx   r{   r©   rC   r   r0   r2   r5   r4   r7   rÉ   r   r   r   r$   Ú<module>   s.   
 1Y5