o
    ^i                     @   s  d dl Z d dlmZmZmZmZmZmZ ddlm	Z	 ddl
mZ ddlmZ erBddlmZ ddlmZmZ dd	lmZ dd
lmZ dgZe dZde d Zde d Zdddee dddefddZdddee de	defddZdddedede	deeef f
dd Zd!d"d#eeeeeef  f de	deeeeeef  f fd$d%Z d&d'dededefd(d)Z!d&d'd*edefd+d,Z"d&d'd*edededef
d-d.Z#d0d/dZ$dS )1    N)TYPE_CHECKINGAnyDictListMatchUnion   )
BlockState)
LINK_LABEL)unikey)BlockParser)BaseRendererInlineState)InlineParser)Markdown	footnotesz\n{2,}z/^(?P<footnote_lead> {0,4})\[\^(?P<footnote_key>zW)]:[ \t\n](?P<footnote_text>[^\n]*(?:\n+|$)(?:(?P=footnote_lead) {1,4}(?! )[^\n]*\n+)*)z\[\^(?P<footnote_key>z)\]inliner   mstater   returnc                 C   s   t |d}|jd}|r?||v r?|jd}|sg }||vr+|| ||jd< |d|d||d id | S |d|d	d
 | S )Nfootnote_keyref_footnotesr   footnote_refindex   )typerawattrstextr   )r   r   )r   groupenvgetappendappend_tokenr   end)r   r   r   keyrefnotes r(   T/home/ubuntu/hpml_nyu/venv/lib/python3.10/site-packages/mistune/plugins/footnotes.pyparse_inline_footnote   s   

 r*   blockr   c                 C   sJ   |j d}|s
i }t|d}||vr!|d||< ||j d< | S )Nr   r   footnote_text)r    r!   r   r   r$   )r+   r   r   r&   r%   r(   r(   r)   parse_ref_footnote-   s   
r-   r%   r   c                 C   s   |j d}|std|| }| }d }|dd  D ]}|r" nq|rVt|t|  }tjdt| d tj	d}	|	
d| }t }
|
| | |
 |
j}n
| }d|d	g}d
|||ddS )Nr   zMissing 'ref_footnotes'.r   z^ {z,})flags 	paragraph)r   r   footnote_item)r%   r   )r   childrenr   )r    r!   
ValueError
splitlineslenlstriprecompilestrMsubstripr	   processparsetokens)r+   r%   r   r   r&   r   linessecond_linespacespatternfooter_stater2   r(   r(   r)   parse_footnote_item9   s*   

rE   mdr   resultc                    sV   j d}|s
|S  fddt|D }tdd|dg_ }|| S )Nr   c                    s$   g | ]\}}t  j||d  qS )r   )rE   r+   ).0ikrF   r   r(   r)   
<listcomp>[   s   $ z%md_footnotes_hook.<locals>.<listcomp>)parent)r   r2   )r    r!   	enumerater	   r?   render_state)rF   rG   r   r'   r2   outputr(   rK   r)   md_footnotes_hookT   s   

rQ   rendererr   c                 C   s,   t |}d| d }|d | d | d S )Nz$<sup class="footnote-ref" id="fnref-">z<a href="#fn-z
</a></sup>)r9   )rR   r%   r   rI   htmlr(   r(   r)   render_footnote_refb   s   rU   r   c                 C   s   d| d S )Nz!<section class="footnotes">
<ol>
z</ol>
</section>
r(   )rR   r   r(   r(   r)   render_footnotesh   s   rV   c                 C   s\   t |}d| d }| }|dr|d d | d }n|d | }d| d | d S )	Nz<a href="#fnref-z" class="footnote">&#8617;</a>z</p>
z<li id="fn-rS   z</li>
)r9   rstripendswith)rR   r   r%   r   rI   backr(   r(   r)   render_footnote_iteml   s   
r\   c                 C   s|   | j jdttdd | jjdttdd | jt	 | j
r:| j
jdkr<| j
dt | j
dt | j
d	t d
S d
S d
S )a  A mistune plugin to support footnotes, spec defined at
    https://michelf.ca/projects/php-markdown/extra/#footnotes

    Here is an example:

    .. code-block:: text

        That's some text with a footnote.[^1]

        [^1]: And that's the footnote.

    It will be converted into HTML:

    .. code-block:: html

        <p>That's some text with a footnote.<sup class="footnote-ref" id="fnref-1"><a href="#fn-1">1</a></sup></p>
        <section class="footnotes">
        <ol>
        <li id="fn-1"><p>And that's the footnote.<a href="#fnref-1" class="footnote">&#8617;</a></p></li>
        </ol>
        </section>

    :param md: Markdown instance
    footnotelink)beforeref_footnoteref_linkrT   r   r1   r   N)r   registerINLINE_FOOTNOTEr*   r+   REF_FOOTNOTEr-   after_render_hooksr"   rQ   rR   NAMErU   r\   rV   )rF   r(   r(   r)   r   w   s$   )rF   r   r   N)%r7   typingr   r   r   r   r   r   corer	   helpersr
   utilr   block_parserr   r   r   inline_parserr   markdownr   __all__r8   _PARAGRAPH_SPLITrd   rc   r9   intr*   r-   rE   rQ   rU   rV   r\   r   r(   r(   r(   r)   <module>   sF     
&
