o
    iy                     @   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,3})\[\^(?P<footnote_key>zU)]:[ \t](?P<footnote_text>[^\n]*(?:\n+|$)(?:(?P=footnote_lead) {1,3}(?! )[^\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(   ^/var/www/html/crm_dreinet/venv_linux/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|rRt|t|  }tjdt| d tj	d}	|	
d| }t|}
dd	 |
D }n
| }d
|dg}d|||ddS )Nr   zMissing 'ref_footnotes'.r   z^ {z,})flags c                 S   s   g | ]}d |dqS )	paragraphr   r   r(   ).0sr(   r(   r)   
<listcomp>J   s    z'parse_footnote_item.<locals>.<listcomp>r0   r1   footnote_item)r%   r   )r   childrenr   )r    r!   
ValueError
splitlineslenlstriprecompilestrMsubstrip_PARAGRAPH_SPLITsplit)r+   r%   r   r   r&   r   linessecond_linespacespatternitemsr6   r(   r(   r)   parse_footnote_item9   s&   
rH   mdr   resultc                    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   )rH   r+   )r2   ikrI   r   r(   r)   r4   X   s   $ z%md_footnotes_hook.<locals>.<listcomp>)r   r6   )r    r!   	enumerater	   tokensrender_state)rI   rJ   r   r'   r6   outputr(   rM   r)   md_footnotes_hookQ   s   
rR   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>)r=   )rS   r%   r   rK   htmlr(   r(   r)   render_footnote_ref_   s   rV   r   c                 C   s   d| d S )Nz!<section class="footnotes">
<ol>
z</ol>
</section>
r(   )rS   r   r(   r(   r)   render_footnotese   s   rW   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-rT   z</li>
)r=   rstrip)rS   r   r%   r   rK   backr(   r(   r)   render_footnote_itemi   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_linkrU   r   r5   r   N)r   registerINLINE_FOOTNOTEr*   r+   REF_FOOTNOTEr-   after_render_hooksr"   rR   rS   NAMErV   r[   rW   )rI   r(   r(   r)   r   p   s$   )rI   r   r   N)%r;   typingr   r   r   r   r   r   corer	   helpersr
   utilr   block_parserr   r   r   inline_parserr   markdownr   __all__r<   rA   rc   rb   r=   intr*   r-   rH   rR   rV   rW   r[   r   r(   r(   r(   r)   <module>   sF     
&
