a
    ö£BeP  ã                   @   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||v rx|j d¡}|s:g }||v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   ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/mistune/plugins/footnotes.pyÚparse_inline_footnote   s     

ý
r   )r   r   c                 C   sJ   |j  d¡}|si }t| d¡ƒ}||v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,|r˜t|ƒ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   ó    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&    
ýr:   )Ú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   )r:   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    þ
rC   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>)r0   )Úrendererr   r   r<   Úhtmlr   r   r   Úrender_footnote_ref^   s    rG   )r   c                 C   s   d| d S )Nz!<section class="footnotes">
<ol>
z</ol>
</section>
r   )rE   r   r   r   r   Úrender_footnotesd   s    rH   )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-rD   z</li>
)r0   Úrstrip)rE   r   r   r   r<   Úbackr   r   r   Úrender_footnote_itemh   s    rL   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_linkrF   r
   r(   r   N)r   ÚregisterÚINLINE_FOOTNOTEr   r   ÚREF_FOOTNOTEr   Zafter_render_hooksr   rC   rE   ÚNAMErG   rL   rH   )r?   r   r   r   r   o   s"    üü)r.   Úcorer   Úutilr   Úhelpersr   Ú__all__r/   r4   rQ   rP   ÚMatchr   r   r0   Úintr:   rC   rG   rH   rL   r   r   r   r   r   Ú<module>   s(   
ÿÿÿ