o
    >Di                     @  s   d Z ddlmZ ddlZddlmZmZmZmZ ddl	m
Z
 G dd dejZG dd	 d	ejZG d
d dejZG dd dZdd ZdS )a  
Footnotes extension
~~~~~~~~~~~~~~~~~~~

Enable footnotes parsing and renderering in Marko.

Usage::

    from marko import Markdown

    text = 'Foo[^1]\n\n[^1]: This is a footnote.\n'
    markdown = Markdown(extensions=['footnote'])
    print(markdown(text))

    )annotationsN)HTMLRendererblockhelpersinline)MarkdownRendererc                      s   e Zd Z fddZ  ZS )Documentc                   s   t    i | _d S Nsuper__init__	footnotesself	__class__ K/var/www/Datamplify/venv/lib/python3.10/site-packages/marko/ext/footnote.pyr         

zDocument.__init__)__name__
__module____qualname__r   __classcell__r   r   r   r   r      s    r   c                   @  s:   e Zd ZedZdZdd Zedd Z	edd Z
d	S )
FootnoteDefz' {,3}\[\^([^\]]+)\]:[^\n\S]*(?=\S| {4})   c                 C  s,   t |d| _t| | _d| _d S )N   z {1,4})r   normalize_labelgrouplabelreescape_prefix_second_prefixr   matchr   r   r   r   #   s   
zFootnoteDef.__init__c                 C  s   | | jS r	   )	expect_repattern)clssourcer   r   r   r$   (   s   zFootnoteDef.matchc                 C  sT   | |j }|| |j||_W d    n1 sw   Y  ||jj|j< |S r	   )r$   under_stateparserparse_sourcechildrenrootr   r   )r'   r(   stater   r   r   parse,   s   
zFootnoteDef.parseN)r   r   r   r   compiler&   priorityr   classmethodr$   r/   r   r   r   r   r      s    

r   c                      s6   e Zd ZedZdZdd Ze fddZ	  Z
S )FootnoteRefz\[\^([^\]]+)\]r   c                 C  s   t |d| _d S )Nr   )r   r   r   r   r#   r   r   r   r   9   s   zFootnoteRef.__init__c                #  s>    t  j||dD ]}t|d}||jjv r|V  q	d S )N)r(   r   )r   findr   r   r   r-   r   )r'   textr(   r$   r   r   r   r   r4   <   s   zFootnoteRef.find)r   r   r   r   r0   r&   r1   r   r2   r4   r   r   r   r   r   r3   5   s    
r3   c                      s   e Zd Z fddZeedd Zee	dd Zeedd Z
e
e	dd Z
d	d
 Zeee	fdd Z  ZS )FootnoteRendererMixinc                   s   t    g | _d S r	   r
   r   r   r   r   r   E   r   zFootnoteRendererMixin.__init__c                 C  sB   |j | jvr| j|j  | j|j d }dj| |j |dS )Nr   zM<sup class="footnote-ref" id="fnref-{lab}"><a href="#fn-{lab}">{id}</a></sup>)labid)r   r   appendindexformat
escape_url)r   elementidxr   r   r   render_footnote_refI   s   z)FootnoteRendererMixin.render_footnote_refc                 C  s   d|j  dS )N[^])r   r   r=   r   r   r   r?   U   s   c                 C  s   dS )N r   rB   r   r   r   render_footnote_defY   s   z)FootnoteRendererMixin.render_footnote_defc                 C  s   d|j  d| | S )Nr@   z]: )r   render_childrenrB   r   r   r   rD   ]   s   c                 C  s`   |  | }d|j d}|drtd| d|}n| d| d}d| |j|S )Nz<a href="#fnref-z" class="footnote">&#8617;</a>z</p>z</p>$z<p>z</p>
z<li id="fn-{}">
{}</li>
)rE   rstripr   endswithr   subr;   r<   )r   r=   r,   backr   r   r   _render_footnote_defa   s   
z*FootnoteRendererMixin._render_footnote_defc                   sb     |} fdd jD }|rt ts|S d fdd|D }d| d}g  _|| S )Nc                   s   g | ]} j j| qS r   )	root_noder   ).0r   r   r   r   
<listcomp>o   s    z9FootnoteRendererMixin.render_document.<locals>.<listcomp>rC   c                 3  s    | ]}  |V  qd S r	   )rJ   )rL   itemr   r   r   	<genexpr>r   s    z8FootnoteRendererMixin.render_document.<locals>.<genexpr>z<div class="footnotes">
<ol>
z</ol>
</div>
)rE   r   
isinstancer   join)r   r=   r5   itemsr,   r   r   r   r   render_documentl   s   
z%FootnoteRendererMixin.render_document)r   r   r   r   r   render_dispatchr   r?   dispatchr   rD   rJ   rS   r   r   r   r   r   r6   D   s    



r6   c                   C  s   t jtttgtgdS )N)elementsrenderer_mixins)r   MarkoExtensionr   r   r3   r6   r   r   r   r   make_extensionx   s   rY   )__doc__
__future__r   r   markor   r   r   r   marko.md_rendererr   r   BlockElementr   InlineElementr3   r6   rY   r   r   r   r   <module>   s   4