o
    ^i                     @   s   d dl Z d dlZd dlmZmZ ddlmZ ddlmZ er:ddl	m
Z
 ddlmZmZmZ ddlmZ dd	lmZ d
gZde d Zdddee dddefddZdddeddddfddZdddededefddZd#d"d
ZdS )$    N)TYPE_CHECKINGMatch   )PREVENT_BACKSLASHescape)BlockParser)BaseRenderer
BlockStateInlineState)InlineParser)Markdownabbrz^ {0,3}\*\[(?P<abbr_key>[^\]]+)z<\]:(?P<abbr_text>(?:[ \t]*\n(?: {3,}|\t)[^\n]+)|(?:[^\n]*))$blockr   mstater
   returnc                 C   sX   |j d}|s
i }|d}|d}| ||< ||j d< |ddi | d S )N	ref_abbrsabbr_key	abbr_texttype
blank_line   )envgetgroupstripappend_tokenend)r   r   r   refkeytext r"   O/home/ubuntu/hpml_nyu/venv/lib/python3.10/site-packages/mistune/plugins/abbr.pyparse_ref_abbr   s   


r$   inliner   r!   r   c                 C   s`  |j d}|s|d|dS |jr)|jd }|d dkr)|j  |d | }|j d}|sEtdd	d
 | D }||j d< d}|t	|k r|
||}|sVn6| }||krl||| }	|d|	d |d}
|dd|
dgd||
 id | }|t	|k sM|dkr|d|d d S |t	|k r|d||d  d d S d S )Nr   r!   )r   rawr   r&   abbrs_re|c                 s   s    | ]}t |V  qd S )N)rer   ).0kr"   r"   r#   	<genexpr>1   s    zprocess_text.<locals>.<genexpr>r   r   title)r   childrenattrs)r   r   r   tokenspopr*   compilejoinkeyslensearchstartr   r   )r%   r!   r   r   lastr(   posr   end_posholelabelr"   r"   r#   process_text$   s@   



r>   rendererr	   r.   c                 C   s(   |sd| d S dt | d | d S )Nz<abbr>z</abbr>z<abbr title="z">r   )r?   r!   r.   r"   r"   r#   render_abbrL   s   r@   mdr   c                 C   sR   | j jdttdd tt| j| j_| jr%| jj	dkr'| jdt
 dS dS dS )aG  A mistune plugin to support abbreviations, spec defined at
    https://michelf.ca/projects/php-markdown/extra/#abbr

    Here is an example:

    .. code-block:: text

        The HTML specification
        is maintained by the W3C.

        *[HTML]: Hyper Text Markup Language
        *[W3C]:  World Wide Web Consortium

    It will be converted into HTML:

    .. code-block:: html

        The <abbr title="Hyper Text Markup Language">HTML</abbr> specification
        is maintained by the <abbr title="World Wide Web Consortium">W3C</abbr>.

    :param md: Markdown instance
    ref_abbr	paragraph)beforehtmlr   N)r   registerREF_ABBRr$   types
MethodTyper>   r%   r?   NAMEr@   )rA   r"   r"   r#   r   R   s
   )rA   r   r   N)r*   rH   typingr   r   helpersr   utilr   block_parserr   corer	   r
   r   inline_parserr   markdownr   __all__rG   strintr$   r>   r@   r   r"   r"   r"   r#   <module>   s"    
(