U
    mufP                     @   s   d dl Z ddl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e jdddZe jedddZeeedddZeedddZeedddZedddZeeeddd Zd!d ZdS )"    N   )
BlockState)unikey)
LINK_LABEL	footnotesz\n{2,}z/^(?P<footnote_lead> {0,3})\[\^(?P<footnote_key>zU)]:[ \t](?P<footnote_text>[^\n]*(?:\n+|$)(?:(?P=footnote_lead) {1,3}(?! )[^\n]*\n+)*)z\[\^(?P<footnote_key>z)\])mc                 C   s   t |d}|jd}|rx||krx|jd}|s:g }||krV|| ||jd< |d|d||d id n|d|d	d
 | S )Nfootnote_keyref_footnotesr   footnote_refindex   )typerawattrstextr   )r   r   )r   groupenvgetappendZappend_tokenr   end)inliner   statekeyrefnotes r   =/tmp/pip-unpacked-wheel-8p1ovdot/mistune/plugins/footnotes.pyparse_inline_footnote   s     


r   )r   r   c                 C   sJ   |j d}|si }t|d}||krB|d||< ||j d< | S )Nr	   r   Zfootnote_text)r   r   r   r   r   )blockr   r   r   r   r   r   r   parse_ref_footnote)   s    
r   )r   r   r   c                 C   s   |j d}|| }| }d }|dd  D ]}|r, q:q,|rt|t|  }tjdt| d tjd}	|		d|
 }t|}
dd |
D }n|
 }d	|d
g}d|||ddS )Nr	   r   z^ {z,})flags c                 S   s   g | ]}d |dqS )	paragraphr   r   r   ).0sr   r   r   
<listcomp>D   s     z'parse_footnote_item.<locals>.<listcomp>r"   r#   footnote_itemr   r   )r   childrenr   )r   r   
splitlineslenlstriprecompilestrMsubstrip_PARAGRAPH_SPLITsplit)r   r   r   r   r   r   linesZsecond_linespacespatternitemsr)   r   r   r   parse_footnote_item5   s&    
r9   )resultr   c                    sR   j d}|s|S  fddt|D }t d|dg_ }|| S )Nr   c                    s$   g | ]\}}t  j||d  qS )r   )r9   r   )r$   ikmdr   r   r   r&   T   s   z%md_footnotes_hook.<locals>.<listcomp>)r   r)   )r   r   	enumerater   tokensZrender_state)r>   r:   r   r   r)   outputr   r=   r   md_footnotes_hookO   s    
rB   r(   c                 C   s,   t |}d| d }|d | d | d S )Nz$<sup class="footnote-ref" id="fnref-">z<a href="#fn-z
</a></sup>)r/   )rendererr   r   r;   htmlr   r   r   render_footnote_ref^   s    rF   )r   c                 C   s   d| d S )Nz!<section class="footnotes">
<ol>
z</ol>
</section>
r   )rD   r   r   r   r   render_footnotesd   s    rG   )r   r   r   c                 C   s@   t |}d| d }| d d | d }d| d | d S )Nz<a href="#fnref-z" class="footnote">&#8617;</a>z</p>z<li id="fn-rC   z</li>
)r/   rstrip)rD   r   r   r   r;   backr   r   r   render_footnote_itemh   s    rK   c                 C   st   | j jdttdd | jjdttdd | jt	 | j
rp| j
jdkrp| j
dt | j
dt | j
d	t 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
    Zfootnotelink)beforeZref_footnoteZref_linkrE   r
   r'   r   N)r   registerINLINE_FOOTNOTEr   r   REF_FOOTNOTEr   Zafter_render_hooksr   rB   rD   NAMErF   rK   rG   )r>   r   r   r   r   o   s"    )r-   corer   utilr   Zhelpersr   __all__r.   r3   rP   rO   Matchr   r   r/   intr9   rB   rF   rG   rK   r   r   r   r   r   <module>   s(   
