
    gi                        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ej                  Z G d d	ej                  Z G d
 dej                  Z G d d          Z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                       e Zd Z fdZ xZS )Documentc                V    t                                                       i | _        d S Nsuper__init__	footnotesself	__class__s    j/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/marko/ext/footnote.pyr   zDocument.__init__   $        )__name__
__module____qualname__r   __classcell__r   s   @r   r
   r
      s8                r   r
   c                  d    e Zd Z ej        d          ZdZd Zed             Z	ed             Z
dS )FootnoteDefz' {,3}\[\^([^\]]+)\]:[^\n\S]*(?=\S| {4})   c                    t          j        |                    d                    | _        t	          j        |                                          | _        d| _        d S )N   z {1,4})r   normalize_labelgrouplabelreescape_prefix_second_prefixr   matchs     r   r   zFootnoteDef.__init__#   sE    ,U[[^^<<
y//'r   c                6    |                     | j                  S r   )	expect_repattern)clssources     r   r)   zFootnoteDef.match(   s    ,,,r   c                     | |j                   }|                    |          5  |j                            |          |_        d d d            n# 1 swxY w Y   ||j        j        |j        <   |S r   )r)   under_stateparserparse_sourcechildrenrootr   r#   )r-   r.   states      r   parsezFootnoteDef.parse,   s    FL!!&& 	@ 	@#]77??EN	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@-2ek*s    AAAN)r   r   r   r$   compiler,   priorityr   classmethodr)   r6    r   r   r   r      sp        bjCDDGH( ( (
 - - [-   [  r   r   c                  X     e Zd Z ej        d          ZdZd Ze fd            Z	 xZ
S )FootnoteRefz\[\^([^\]]+)\]r   c                ^    t          j        |                    d                    | _        d S )Nr    )r   r!   r"   r#   r(   s     r   r   zFootnoteRef.__init__9   s"    ,U[[^^<<


r   c             #     K   t                                          ||          D ];}t          j        |                    d                    }||j        j        v r|V  <d S )N)r.   r    )r   findr   r!   r"   r4   r   )r-   textr.   r)   r#   r   s        r   r?   zFootnoteRef.find<   sh      WW\\$v\66 	 	E+EKKNN;;E---	 	r   )r   r   r   r$   r7   r,   r8   r   r9   r?   r   r   s   @r   r<   r<   5   sj        bj*++GH= = =     [    r   r<   c                  6    e Zd Z fdZ ej        e          d             Ze                    e	          d             Z ej        e          d             Z
e
                    e	          d             Z
d Z ej        ee	f          d             Z xZS )FootnoteRendererMixinc                V    t                                                       g | _        d S r   r   r   s    r   r   zFootnoteRendererMixin.__init__E   r   r   c                    |j         | j        vr| j                            |j                    | j                            |j                   dz   }d                    |                     |j                   |          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idxs      r   render_footnote_refz)FootnoteRendererMixin.render_footnote_refI   sv    =..N!!'-000n""7=11A5117OOGM22s 28 2 2	
r   c                    d|j          dS )N[^])r#   r   rK   s     r   rM   z)FootnoteRendererMixin.render_footnote_refU   s    $GM$$$$r   c                    dS )N r:   rQ   s     r   render_footnote_defz)FootnoteRendererMixin.render_footnote_defY   s    rr   c                B    d|j          d|                     |           S )NrO   z]: )r#   render_childrenrQ   s     r   rT   z)FootnoteRendererMixin.render_footnote_def]   s(    EGMEEd&:&:7&C&CEEEr   c                0   |                      |                                          }d|j         d}|                    d          rt	          j        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>
)rV   rstripr#   endswithr$   subrI   rJ   )r   rK   r3   backs       r   _render_footnote_defz*FootnoteRendererMixin._render_footnote_defa   s    ''007799O'-OOOV$$ 	4vh4x@@HH"33t333H+22OOGM**H
 
 	
r   c                                           |          } fd j        D             }|rt           t                    s|S d                     fd|D                       }d| d}g  _        ||z   S )Nc                4    g | ]}j         j        |         S r:   )	root_noder   ).0r#   r   s     r   
<listcomp>z9FootnoteRendererMixin.render_document.<locals>.<listcomp>o   s#    MMMU)%0MMMr   rS   c              3  B   K   | ]}                     |          V  d S r   )r\   )r`   itemr   s     r   	<genexpr>z8FootnoteRendererMixin.render_document.<locals>.<genexpr>r   s1      MMt444T::MMMMMMr   z<div class="footnotes">
<ol>
z</ol>
</div>
)rV   r   
isinstancer   join)r   rK   r@   itemsr3   r   s   `     r   render_documentz%FootnoteRendererMixin.render_documentl   s    ##G,,MMMMdnMMM 	Jt\:: 	K77MMMMuMMMMMOhOOO	ir   )r   r   r   r   r   render_dispatchr   rM   dispatchr   rT   r\   rh   r   r   s   @r   rB   rB   D   s!            W\**	
 	
 +*	
 !!"233% % 43% W\**  +* !!"233F F 43F	
 	
 	
 Wl,<=>>    ?>         r   rB   c                 ^    t          j        t          t          t          gt
          g          S )N)elementsrenderer_mixins)r   MarkoExtensionr
   r   r<   rB   r:   r   r   make_extensionro   x   s,    !K5./   r   )__doc__
__future__r   r$   markor   r   r   r   marko.md_rendererr   r
   BlockElementr   InlineElementr<   rB   ro   r:   r   r   <module>rv      s*    # " " " " " 				 6 6 6 6 6 6 6 6 6 6 6 6 . . . . . .    u~       %$   ,    &&   1  1  1  1  1  1  1  1 h    r   