U
    \> eÔ  ã                   @  s–   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 dd	lZdd	lm  mZ G d
d„ deƒZG dd„ deƒZG dd„ deƒZdd„ Zd	S )z¡
This extension adds abbreviation handling to Python-Markdown.

See the [documentation](https://Python-Markdown.github.io/extensions/abbreviations)
for details.
é    )Úannotationsé   )Ú	Extensioné   )ÚBlockProcessor)ÚInlineProcessor)ÚAtomicStringNc                   @  s   e Zd ZdZdd„ ZdS )ÚAbbrExtensionz- Abbreviation Extension for Python-Markdown. c                 C  s   |j j t|j ƒdd¡ dS )z; Insert `AbbrPreprocessor` before `ReferencePreprocessor`. Úabbré   N)ÚparserÚblockprocessorsÚregisterÚAbbrPreprocessor)ÚselfÚmd© r   ú</tmp/pip-unpacked-wheel-wj0og6ym/markdown/extensions/abbr.pyÚextendMarkdown$   s    zAbbrExtension.extendMarkdownN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r	   !   s   r	   c                   @  sR   e Zd ZdZe dej¡Zddddœdd„Zdd	dd
œdd„Z	dddœdd„Z
dS )r   z= Abbreviation Preprocessor - parse text for abbr references. z6^[*]\[(?P<abbr>[^\]]*)\][ ]?:[ ]*\n?[ ]*(?P<title>.*)$zetree.ElementÚstrÚbool)ÚparentÚblockÚreturnc                 C  s   dS )NTr   )r   r   r   r   r   r   Útest.   s    zAbbrPreprocessor.testz	list[str])r   Úblocksr   c                 C  sÒ   |  d¡}| j |¡}|rÂ| d¡ ¡ }| d¡ ¡ }| jjj t	|  
|¡|ƒd| d¡ || ¡ d…  ¡ rŒ| d|| ¡ d…  d¡¡ |d| ¡ …  ¡ r¾| d|d| ¡ …  d¡¡ dS | d|¡ d	S )
zœ
        Find and remove all Abbreviation references from the text.
        Each reference is set as a new `AbbrPattern` in the markdown instance.

        r   r
   Útitlezabbr-%sr   NÚ
TF)ÚpopÚREÚsearchÚgroupÚstripr   r   ZinlinePatternsr   ÚAbbrInlineProcessorÚ_generate_patternÚendÚinsertÚlstripÚstartÚrstrip)r   r   r   r   Úmr
   r    r   r   r   Úrun1   s"    

  ÿzAbbrPreprocessor.run)Útextr   c                 C  s8   t |ƒ}tt|ƒƒD ]}d||  ||< qdd |¡ S )zø
        Given a string, returns an regex pattern to match that string.

        'HTML' -> r'(?P<abbr>[H][T][M][L])'

        Note: we force each char as a literal match (in brackets) as we don't
        know what they will be beforehand.

        z[%s]z(?P<abbr>\b%s\b)Ú )ÚlistÚrangeÚlenÚjoin)r   r0   ÚcharsÚir   r   r   r(   J   s    
z"AbbrPreprocessor._generate_patternN)r   r   r   r   ÚreÚcompileÚ	MULTILINEr#   r   r/   r(   r   r   r   r   r   )   s
   r   c                      s:   e Zd ZdZdddœ‡ fdd„Zddddœd	d
„Z‡  ZS )r'   z Abbreviation inline pattern. r   )Úpatternr    c                   s   t ƒ  |¡ || _d S ©N)ÚsuperÚ__init__r    )r   r;   r    ©Ú	__class__r   r   r>   ]   s    zAbbrInlineProcessor.__init__zre.Match[str]ztuple[etree.Element, int, int])r.   Údatar   c                 C  s>   t  d¡}t| d¡ƒ|_| d| j¡ || d¡| d¡fS )Nr
   r    r   )	ÚetreeÚElementr   r%   r0   Úsetr    r,   r)   )r   r.   rA   r
   r   r   r   ÚhandleMatcha   s    
zAbbrInlineProcessor.handleMatch)r   r   r   r   r>   rE   Ú__classcell__r   r   r?   r   r'   Z   s   r'   c                  K  s
   t f | ŽS r<   )r	   )Úkwargsr   r   r   ÚmakeExtensionh   s    rH   )r   Ú
__future__r   r1   r   r   r   Zinlinepatternsr   Úutilr   r8   Úxml.etree.ElementTreerB   ÚElementTreer	   r   r'   rH   r   r   r   r   Ú<module>   s   1