a
    ö£Be’  ã                   @   s`   d dl Z d dlmZmZ e  d¡ZG dd„ dƒZG dd„ dƒZG dd	„ d	ƒZG d
d„ de	ƒZ
dS )é    N)ÚDictÚAnyz\n|$c                   @   sž   e Zd ZdZddd„Zedœdd„Zedœdd	„Zd
d„ Ze	dœdd„Z
dd„ Zeeef dœdd„Zeeef dœdd„Zedœdd„Zdd„ Zdd„ ZdS )Ú
BlockStatez3The state to save block parser's cursor and tokens.Nc                 C   s@   d| _ g | _d| _d| _d| _|| _|r2|j| _n
di i| _d S )NÚ r   TZ	ref_links)ÚsrcÚtokensÚcursorÚ
cursor_maxZ
list_tightÚparentÚenv)Úselfr
   © r   úO/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/mistune/core.pyÚ__init__	   s    
zBlockState.__init__)r   c                 C   s   |   | ¡}| |¡ |S ©N)Ú	__class__Úprocess)r   r   Úchildr   r   r   Úchild_state   s    

zBlockState.child_statec                 C   s   || _ t|ƒ| _d S r   )r   Úlenr	   )r   r   r   r   r   r       s    zBlockState.processc                 C   s   t  | j| j¡}| ¡ S r   )Ú	_LINE_ENDÚsearchr   r   Úend)r   Úmr   r   r   Úfind_line_end$   s    zBlockState.find_line_end)Úend_posc                 C   s   | j | j|… S r   )r   r   )r   r   r   r   r   Úget_text(   s    zBlockState.get_textc                 C   s   | j r| j d S d S )Néÿÿÿÿ)r   ©r   r   r   r   Ú
last_token+   s    zBlockState.last_token©Útokenc                 C   s   | j  t| j ƒd |¡ dS ©z#Insert token before the last token.é   N©r   Úinsertr   ©r   r!   r   r   r   Úprepend_token/   s    zBlockState.prepend_tokenc                 C   s   | j  |¡ dS ©z#Add token to the end of token list.N©r   Úappendr&   r   r   r   Úappend_token3   s    zBlockState.append_token)Útextc                 C   s@   |   ¡ }|r*|d dkr*|d  |7  < n| j d|dœ¡ d S )NÚtypeÚ	paragraphr,   )r-   r,   )r   r   r*   )r   r,   r   r   r   r   Úadd_paragraph7   s    zBlockState.add_paragraphc                 C   s>   |   ¡ }|r:|d dkr:|  ¡ }|d  |  |¡7  < |S d S )Nr-   r.   r,   )r   r   r   )r   r   Úposr   r   r   Úappend_paragraph>   s
    zBlockState.append_paragraphc                 C   s"   d}| j }|r|d7 }|j }q
|S )Nr   r#   )r
   )r   Údr
   r   r   r   ÚdepthE   s    zBlockState.depth)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústrr   r   r   Úintr   r   r   r   r'   r+   r/   r1   r3   r   r   r   r   r      s   
r   c                   @   sZ   e Zd ZdZeeef dœdd„Zeeef dœdd„Zeeef dœdd	„Z	d
d„ Z
dS )ÚInlineStatez)The state to save inline parser's tokens.)r   c                 C   s.   || _ d| _g | _d| _d| _d| _d| _d S )Nr   F)r   r   r   Úin_imageÚin_linkÚin_emphasisÚ	in_strong)r   r   r   r   r   r   P   s    zInlineState.__init__r    c                 C   s   | j  t| j ƒd |¡ dS r"   r$   r&   r   r   r   r'   Y   s    zInlineState.prepend_tokenc                 C   s   | j  |¡ dS r(   r)   r&   r   r   r   r+   ]   s    zInlineState.append_tokenc                 C   s0   |   | j¡}| j|_| j|_| j|_| j|_|S )zCreate a copy of current state.)r   r   r;   r<   r=   r>   )r   Ústater   r   r   Úcopya   s    zInlineState.copyN)r4   r5   r6   r7   r   r8   r   r   r'   r+   r@   r   r   r   r   r:   N   s
   	r:   c                   @   s^   e Zd ZejZeZi Zg Z	dd„ Z
ddd„Zdedœdd„Zd	d
„ Zeddd„ƒZdd„ ZdS )ÚParserc                 C   s(   | j  ¡ | _t| jƒ| _i | _i | _d S r   )ÚSPECIFICATIONr@   ÚspecificationÚlistÚDEFAULT_RULESÚrulesÚ_methodsÚ_Parser__scr   r   r   r   r   r   s    zParser.__init__Nc                    sf   |d u rd}ˆ j }n
d |¡}ˆ j |¡}|r2|S d ‡ fdd„|D ƒ¡}t |ˆ j¡}|ˆ j|< |S )Nú$ú|c                 3   s    | ]}d |ˆ j | f V  qdS )z
(?P<%s>%s)N)rC   )Ú.0Úkr   r   r   Ú	<genexpr>„   ó    z$Parser.compile_sc.<locals>.<genexpr>)rF   ÚjoinrH   ÚgetÚreÚcompileÚsc_flag)r   rF   ÚkeyÚscÚregexr   r   r   Ú
compile_scy   s    

zParser.compile_sc©Únamec                    sB   ‡ ‡fdd„ˆj |< |r"|ˆj|< |ˆjvr>ˆjˆj||d� dS )a3  Register a new rule to parse the token. This method is usually used to
        create a new plugin.

        :param name: name of the new grammar
        :param pattern: regex pattern in string
        :param func: the parsing function
        :param before: insert this rule before a built-in rule
        c                    s   ˆ ˆ| |ƒS r   r   )r   r?   ©Úfuncr   r   r   Ú<lambda>’   rN   z!Parser.register.<locals>.<lambda>)ÚbeforeN)rG   rC   rF   Úinsert_rule)r   rY   Úpatternr[   r]   r   rZ   r   Úregister‰   s
    	

zParser.registerc                 C   s   t dƒ‚d S )Nz.This plugin is not compatible with mistune v3.)ÚDeprecationWarning)r   rY   r_   r[   r   r   r   Úregister_rule˜   s    zParser.register_rulec                 C   sL   |r>z|   |¡}|  ||¡ W qH ty:   |  |¡ Y qH0 n
|  |¡ d S r   )Úindexr%   Ú
ValueErrorr*   )rF   rY   r]   rc   r   r   r   r^   ›   s    
zParser.insert_rulec                 C   s   | j |j }|||ƒS r   )rG   Ú	lastgroup)r   r   r?   r[   r   r   r   Úparse_method¦   s    zParser.parse_method)N)N)N)r4   r5   r6   rQ   ÚMrS   r   Z	state_clsrB   rE   r   rW   r8   r`   rb   Ústaticmethodr^   rf   r   r   r   r   rA   k   s   

rA   c                   @   sN   e Zd ZdZdd„ Zedœdd„Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚBaseRendererÚbasec                 C   s
   i | _ d S r   ©Ú_BaseRenderer__methodsr   r   r   r   r   ®   s    zBaseRenderer.__init__rX   c                    s   ‡ ‡fdd„ˆj |< dS )zèRegister a render method for the named token. For example::

            def render_wiki(renderer, key, title):
                return f'<a href="/wiki/{key}">{title}</a>'

            renderer.register('wiki', render_wiki)
        c                     s   ˆ ˆg| ¢R i |¤ŽS r   r   )ÚargÚkwargs©Úmethodr   r   r   r\   º   rN   z'BaseRenderer.register.<locals>.<lambda>Nrk   ©r   rY   rp   r   ro   r   r`   ±   s    	zBaseRenderer.registerc                 C   sH   zt  | |¡W S  tyB   | j |¡}|s:td |¡ƒ‚| Y S 0 d S )NzNo renderer "{!r}")ÚobjectÚ__getattribute__ÚAttributeErrorrl   rP   Úformatrq   r   r   r   Ú_get_method¼   s    zBaseRenderer._get_methodc                 C   s   |   |d ¡}|||ƒS )Nr-   )rv   )r   r!   r?   r[   r   r   r   Úrender_tokenÅ   s    zBaseRenderer.render_tokenc                 c   s   |D ]}|   ||¡V  qd S r   )rw   )r   r   r?   Útokr   r   r   Úiter_tokensÉ   s    zBaseRenderer.iter_tokensc                 C   s   d  |  ||¡¡S )Nr   )rO   ry   ©r   r   r?   r   r   r   Úrender_tokensÍ   s    zBaseRenderer.render_tokensc                 C   s   |   ||¡S r   )r{   rz   r   r   r   Ú__call__Ð   s    zBaseRenderer.__call__N)r4   r5   r6   ÚNAMEr   r8   r`   rv   rw   ry   r{   r|   r   r   r   r   ri   «   s   	ri   )rQ   Útypingr   r   rR   r   r   r:   rA   rr   ri   r   r   r   r   Ú<module>   s   
G@