U
    \> 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d	„Zd#ddddœdd„Zdddœdd„Zddddœdd„Z	ddœdd„Z
dddœdd„Zd$ddddœdd„Zdd dœd!d"„Z‡  ZS )%ÚFootnoteExtensionz Footnote Extension. c                   sZ   ddgddgddgddgd	d
gddgdœ| _ tƒ jf |Ž 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__© úA/tmp/pip-unpacked-wheel-wj0og6ym/markdown/extensions/footnotes.pyr   )   s0     ÿ ÿ ÿ ÿþ ÿî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ÚNone)Úreturnc                 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ÚstrÚbool)Ú	referenceÚfoundr6   c                 C  s°   |s|S |}|| j krx| |  ¡ d¡\}}t |¡}|rbd| d¡t| d¡ƒd |  ¡ |f }qd|d|  ¡ |f }q| j  |¡ || jkr¢| j|  d7  < n
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   r;   r<   Zoriginal_refÚrefÚrestÚmr!   r!   r"   Ú
unique_refl   s    

*

zFootnoteExtension.unique_refúetree.Elementz0tuple[etree.Element, etree.Element, bool] | None©Úrootr6   c                   s   ‡ ‡fdd„‰ ˆ |ƒ}|S )z@ Return ElementTree Element that contains Footnote placeholder. c                   sz   | D ]p}|j r2|j  ˆ d¡¡dkr2|| df  S |jr\|j ˆ d¡¡dkr\|| df  S ˆ |ƒ}|d k	r|  S qd S )Nr   éÿÿÿÿTF)ÚtextÚfindÚ	getConfigÚtail)ÚelementÚchildZ	child_res©Úfinderr   r!   r"   rT   …   s    
z:FootnoteExtension.findFootnotesPlaceholder.<locals>.finderr!   )r   rK   Úresr!   rS   r"   ÚfindFootnotesPlaceholder   s    z*FootnoteExtension.findFootnotesPlaceholder)ÚidrM   r6   c                 C  s   || j |< dS )z' Store a footnote for later retrieval. N©r7   )r   rW   rM   r!   r!   r"   ÚsetFootnote•   s    zFootnoteExtension.setFootnotec                 C  s
   |   d¡S )z Get the footnote separator. r   )rO   r8   r!   r!   r"   r?   ™   s    zFootnoteExtension.get_separator)rW   r6   c                 C  s2   |   d¡rd|  ¡ | j|f S d |  ¡ |¡S dS )z Return footnote link id. r   z	fn%s%d-%szfn{}{}N)rO   r?   r   Úformat)r   rW   r!   r!   r"   ÚmakeFootnoteId   s    
z FootnoteExtension.makeFootnoteId)rW   r<   r6   c                 C  sB   |   d¡r&|  d|  ¡ | j|f |¡S |  d |  ¡ |¡|¡S dS )z Return footnote back-link id. r   zfnref%s%d-%sz	fnref{}{}N)rO   rH   r?   r   rZ   )r   rW   r<   r!   r!   r"   ÚmakeFootnoteRefId¤   s    
z#FootnoteExtension.makeFootnoteRefIdz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 |d¡}| d|  
|¡¡ | j || j| ¡ t |ƒD ]}	| |	¡ | |	¡ q°t d¡}
|
 dd|  |¡ ¡ |
 dd¡ |
 d| |¡¡ t|
_t|ƒrn|d }|jdkrB|jt |_| |
¡ qnt |d¡}| |
¡ qn|S )z/ Return `div` of footnotes as `etree` Element. NÚdivÚclassr#   ÚhrÚolr   z%dr   r   )ÚstartÚlirW   ÚaÚhrefú#úfootnote-backrefÚtitlerL   Úp)Úlistr7   ÚkeysÚetreeÚElementr   Ú
SubElementrO   ÚreplaceÚ	enumerater[   r)   Z
parseChunkÚappendÚremover\   rZ   ÚFN_BACKLINK_TEXTrM   ÚlenÚtagÚNBSP_PLACEHOLDER)r   rK   r]   r`   Zsurrogate_parentZbacklink_titleÚindexrW   rb   ÚelZbacklinkÚnoderh   r!   r!   r"   ÚmakeFootnotesDiv«   s>    



þz"FootnoteExtension.makeFootnotesDiv)F)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r4   r   rH   rV   rY   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dœ‡ fdd„Zddd	d
œdd„Z	ddd	dœdd„Z
dddœdd„Zdddœdd„Z‡  ZS )r-   z7 Find all footnote references and store for later use. z!^[ ]{0,3}\[\^([^\]]*)\]:[ ]*(.*)$r   rX   c                   s   t ƒ  |j¡ || _d S ©N)r   r   r)   r7   ©r   r7   r   r!   r"   r   Ý   s    zFootnoteBlockProcessor.__init__rI   r9   r:   )ÚparentÚblockr6   c                 C  s   dS )NTr!   )r   r   r‚   r!   r!   r"   Útestá   s    zFootnoteBlockProcessor.testz	list[str])r   Úblocksr6   c                 C  s@  |  d¡}| j |¡}|r0| d¡}| d¡g}|| ¡ d…  d¡}| j |¡}|rª|d| ¡ …  d¡}	d |d |  	|	¡g¡ d¡|d< | 
d|| ¡ d… ¡ n2d |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ÚsearchrB   ÚendÚlstripra   ÚrstripÚjoinÚdetabÚinsertÚstripÚextendÚdetectTabbedr7   rY   )r   r   r„   r‚   rG   rW   Ú	fn_blocksZtherestÚm2Úbeforer#   r!   r!   r"   Úrunä   s(    

""
zFootnoteBlockProcessor.run)r„   r6   c                 C  sŒ   g }|rˆ|d   d¡rˆ| d¡}| j |¡}|rr|d| ¡ …  d¡}| |  |¡¡ | d|| ¡ d… ¡ qˆq†| |  |¡¡ qqˆq|S )z’ Find indented text and remove indent before further processing.

        Returns:
            A list of blocks with indentation removed.
        r   ú    Nr…   )	Ú
startswithr†   r‡   rˆ   ra   r‹   rp   r   rŽ   )r   r„   r’   r‚   rG   r”   r!   r!   r"   r‘     s    
z#FootnoteBlockProcessor.detectTabbed)r‚   r6   c                 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>   ro   r—   rŒ   )r   r‚   ÚlinesÚiÚliner!   r!   r"   r   %  s
    

zFootnoteBlockProcessor.detab)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ddœ‡ fdd„Zdddd	œd
d„Z‡  ZS )r.   zC `InlineProcessor` for footnote markers in a document's body text. r9   r   )Úpatternr7   c                   s   t ƒ  |¡ || _d S r   )r   r   r7   )r   rŸ   r7   r   r!   r"   r   4  s    z FootnoteInlineProcessor.__init__zre.Match[str]z3tuple[etree.Element | None, int | None, int | None])rG   Údatar6   c                 C  s´   |  d¡}|| jj ¡ kr¬t 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 d S )Nr   Úsuprc   rW   T)r<   rd   re   r^   zfootnote-refr   r   )NNN)rB   r7   rj   rk   rl   rm   r   r\   r[   rO   rZ   ri   rv   rM   ra   r‰   )r   rG   r    rW   r¡   rc   r!   r!   r"   ÚhandleMatch8  s    

ÿz#FootnoteInlineProcessor.handleMatch)rz   r{   r|   r}   r   r¢   r~   r!   r!   r   r"   r.   1  s   r.   c                   @  s`   e Zd ZdZddœdd„Zdddd	œd
d„Zdddœdd„Zdddœdd„Zdddœdd„ZdS )r1   z% Amend footnote div with duplicates. r   rX   c                 C  s
   || _ d S r   rX   r€   r!   r!   r"   r   K  s    z"FootnotePostTreeprocessor.__init__rI   rC   r5   )rb   Ú
duplicatesr6   c           
      C  s¼   |  d¡D ]¬}|j dd¡dkr
|jd  | j ¡ d¡\}}g }td|d ƒD ]B}t |¡}d||| j ¡ |f |jd< | 	|¡ |  j
d7  _
qNt|ƒd	 }	|D ]}|	 	|¡ q¢ q¸q
d
S )zF Adjust current `li` and add the duplicates: `fnref2`, `fnref3`, etc. rc   r^   Ú rf   rd   r   r   r=   rL   N)ÚiterÚattribÚgetr>   r7   r?   ÚrangeÚcopyÚdeepcopyrp   Úoffsetri   )
r   rb   r£   ÚlinkrE   rF   Úlinksrv   Zsib_linkrw   r!   r!   r"   Úadd_duplicatesN  s    

z(FootnotePostTreeprocessor.add_duplicates)rb   r6   c                 C  sD   |j  dd¡ | j ¡ d¡\}}d || j ¡ |¡}| jj |d¡S )z3 Get the number of duplicate refs of the footnote. rW   r¤   r   z	{}ref{}{}r   )r¦   r§   r>   r7   r?   rZ   r   )r   rb   ÚfnrF   Zlink_idr!   r!   r"   Úget_num_duplicatesb  s     z,FootnotePostTreeprocessor.get_num_duplicates)r   r6   c                 C  s0   t |ƒD ]"}|  |¡}|dkr|  ||¡ qdS )z= Find duplicate footnotes and format and add the duplicates. r   N)ri   r°   r®   )r   r   rb   Úcountr!   r!   r"   Úhandle_duplicatesh  s    
z+FootnotePostTreeprocessor.handle_duplicatesrJ   c                 C  sJ   d| _ | d¡D ]4}|j dd¡dkr| d¡D ]}|  |¡  qq0qdS )z= Crawl the footnote div and add missing duplicate footnotes. r   r]   r^   r¤   r#   r`   N)r«   r¥   r¦   r§   r²   )r   rK   r]   r`   r!   r!   r"   r•   q  s    
zFootnotePostTreeprocessor.runN)	rz   r{   r|   r}   r   r®   r°   r²   r•   r!   r!   r!   r"   r1   H  s   	r1   c                   @  s.   e Zd ZdZddœdd„Zdddœd	d
„ZdS )r0   z3 Build and append footnote div to end of document. r   rX   c                 C  s
   || _ d S r   rX   r€   r!   r!   r"   r   €  s    zFootnoteTreeprocessor.__init__rI   r5   rJ   c                 C  s~   | j  |¡}|d k	rz| j  |¡}|rp|\}}}t|ƒ |¡}|rX| |¡ | ||¡ qz| |d |¡ d |_n
| |¡ d S )Nr   )	r7   ry   rV   ri   rv   rq   rŽ   rP   rp   )r   rK   ZfootnotesDivÚresultrR   r   ZisTextÚindr!   r!   r"   r•   ƒ  s    

zFootnoteTreeprocessor.runN©rz   r{   r|   r}   r   r•   r!   r!   r!   r"   r0   }  s   r0   c                   @  s.   e Zd ZdZddœdd„Zdddœdd	„Zd
S )r3   z* Replace placeholders with html entities. r   rX   c                 C  s
   || _ d S r   rX   r€   r!   r!   r"   r   –  s    zFootnotePostprocessor.__init__r9   )rM   r6   c                 C  s    |  t| j d¡¡}|  td¡S )Nr   z&#160;)rn   rr   r7   rO   ru   )r   rM   r!   r!   r"   r•   ™  s
     
ÿzFootnotePostprocessor.runNrµ   r!   r!   r!   r"   r3   ”  s   r3   c                  K  s
   t f | ŽS )z/ Return an instance of the `FootnoteExtension` )r   )r   r!   r!   r"   ÚmakeExtension   s    r¶   )"r}   Ú
__future__r   r¤   r   r+   r   Zinlinepatternsr   r/   r   r2   r	   r
   Úcollectionsr   rœ   r©   Úxml.etree.ElementTreerk   ÚElementTreeZSTXZETXrr   ru   r   r@   r   r-   r.   r1   r0   r3   r¶   r!   r!   r!   r"   Ú<module>   s,   
 3Y5