a
    SicÅs  ã                   @   s^  d Z ddlmZ ddlmZ ddlZddlm  mZ zddl	m
Z
 W n ey`   ddlZ
Y n0 dd„ Zd	Zd
ZdZdZdZdZdZdZdZdZdZdZdZed ZdZeZeZdZdZ dZ!dZ"dZ#dZ$dd„ Z%G d d!„ d!ed!g d"¢ƒƒZ&G d#d$„ d$ƒZ'G d%d&„ d&e'ƒZ(G d'd(„ d(e'ƒZ)G d)d*„ d*e(ƒZ*G d+d,„ d,e(ƒZ+G d-d.„ d.e'ƒZ,G d/d0„ d0e(ƒZ-G d1d2„ d2e,ƒZ.G d3d4„ d4e-ƒZ/G d5d6„ d6e(ƒZ0G d7d8„ d8e,ƒZ1G d9d:„ d:e-ƒZ2G d;d<„ d<e(ƒZ3G d=d>„ d>e(ƒZ4G d?d@„ d@e4ƒZ5G dAdB„ dBe(ƒZ6G dCdD„ dDe6ƒZ7G dEdF„ dFe6ƒZ8G dGdH„ dHe8ƒZ9G dIdJ„ dJe8ƒZ:G dKdL„ dLe:ƒZ;G dMdN„ dNe(ƒZ<G dOdP„ dPe(ƒZ=dS )Qa]	  
Python Markdown

A Python implementation of John Gruber's Markdown.

Documentation: https://python-markdown.github.io/
GitHub: https://github.com/Python-Markdown/markdown/
PyPI: https://pypi.org/project/Markdown/

Started by Manfred Stienstra (http://www.dwerg.net/).
Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org).
Currently maintained by Waylan Limberg (https://github.com/waylan),
Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser).

Copyright 2007-2018 The Python Markdown Project (v. 1.7 and later)
Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
Copyright 2004 Manfred Stienstra (the original version)

License: BSD (see LICENSE.md for details).

INLINE PATTERNS
=============================================================================

Inline patterns such as *emphasis* are handled by means of auxiliary
objects, one per pattern.  Pattern objects must be instances of classes
that extend markdown.Pattern.  Each pattern object uses a single regular
expression and needs support the following methods:

    pattern.getCompiledRegExp() # returns a regular expression

    pattern.handleMatch(m) # takes a match object and returns
                           # an ElementTree element or just plain text

All of python markdown's built-in patterns subclass from Pattern,
but you can add additional patterns that don't.

Also note that all the regular expressions used by inline must
capture the whole block.  For this reason, they all start with
'^(.*)' and end with '(.*)!'.  In case with built-in expression
Pattern takes care of adding the "^(.*)" and "(.*)!".

Finally, the order in which regular expressions are applied is very
important - e.g. if we first replace http://.../ links with <a> tags
and _then_ try to replace inline html, we would end up with a mess.
So, we apply the expressions in the following order:

* escape and backticks have to go before everything else, so
  that we can preempt any markdown patterns by escaping them.

* then we handle auto-links (must be done before inline html)

* then we handle inline HTML.  At this point we will simply
  replace all inline HTML strings with a placeholder and add
  the actual HTML to a hash.

* then inline images (must be done before links)

* then bracketed links, first regular then reference-style

* finally we apply strong and emphasis
é   )Úutilé    )Ú
namedtupleN)Úentitiesc                 K   sD  t  ¡ }| ttƒdd¡ | tt| ƒdd¡ | tt| ƒdd¡ | t	t
| ƒdd¡ | tt| ƒd	d
¡ | tt| ƒdd¡ | tt| ƒdd¡ | tt| ƒdd¡ | tt| ƒdd¡ | tt| ƒdd¡ | ttdƒdd¡ | tt| ƒdd¡ | tt| ƒdd¡ | ttƒdd¡ | tdƒdd ¡ | td!ƒd"d#¡ |S )$z8 Build the default set of inline patterns for Markdown. Úbacktické¾   Úescapeé´   Ú	referenceéª   Úlinké    Z
image_linké–   Zimage_referenceéŒ   Zshort_referenceé‚   Zshort_image_refé}   Zautolinkéx   Zautomailén   ÚbrZ	linebreakéd   ÚhtmléZ   ÚentityéP   Z
not_strongéF   z\*Z	em_strongé<   Ú_Z
em_strong2é2   )r   ÚRegistryÚregisterÚBacktickInlineProcessorÚBACKTICK_REÚEscapeInlineProcessorÚ	ESCAPE_REÚReferenceInlineProcessorÚREFERENCE_REÚLinkInlineProcessorÚLINK_REÚImageInlineProcessorÚIMAGE_LINK_REÚImageReferenceInlineProcessorÚIMAGE_REFERENCE_REÚShortReferenceInlineProcessorÚ"ShortImageReferenceInlineProcessorÚAutolinkInlineProcessorÚAUTOLINK_REÚAutomailInlineProcessorÚAUTOMAIL_REÚSubstituteTagInlineProcessorÚLINE_BREAK_REÚHtmlInlineProcessorÚHTML_REÚ	ENTITY_REÚSimpleTextInlineProcessorÚNOT_STRONG_REÚAsteriskProcessorÚUnderscoreProcessor)ÚmdÚkwargsÚinlinePatterns© r>   úS/var/www/html/django/DPS/env/lib/python3.9/site-packages/markdown/inlinepatterns.pyÚbuild_inlinepatternsI   s0    ÿÿÿr@   z(?<!\!)z;(?:(?<!\\)((?:\\{2})+)(?=`+)|(?<!\\)(`+)(.+?)(?<!`)\2(?!`))z\\(.)z(\*)([^\*]+)\1z(\*{2})(.+?)\1z%(?<!\w)(_{2})(?!_)(.+?)(?<!_)\1(?!\w)z"(?<!\w)(_)(?!_)(.+?)(?<!_)\1(?!\w)z7(?<!\w)(\_)\1(?!\1)(.+?)(?<!\w)\1(?!\1)(.+?)\1{3}(?!\w)z(\*)\1{2}(.+?)\1(.*?)\1{2}z(_)\1{2}(.+?)\1(.*?)\1{2}z(\*)\1{2}(.+?)\1{2}(.*?)\1z(_)\1{2}(.+?)\1{2}(.*?)\1z&(\*)\1(?!\1)([^*]+?)\1(?!\1)(.+?)\1{3}z\[z\!\[z((^|\s)(\*|_)(\s|$))z+<((?:[Ff]|[Hh][Tt])[Tt][Pp][Ss]?://[^<>]*)>z<([^<> !]+@[^@<> ]+)>z;(<(\/?[a-zA-Z][^<>@ ]*( [^<>]*)?|!--(?:(?!<!--|-->).)*--)>)z-(&(?:\#[0-9]+|\#x[0-9a-fA-F]+|[a-zA-Z0-9]+);)z  \nc                 C   s<   |   d¡r|  d¡s(|   d¡r4|  d¡r4| dd… S | S dS )z#Remove quotes from around a string.ú"ú'r   éÿÿÿÿN)Ú
startswithÚendswith)Ústringr>   r>   r?   Údequote¯   s    ÿÿrG   c                   @   s   e Zd ZdZdS )ÚEmStrongItemzEmphasis/strong pattern item.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r>   r>   r>   r?   rH   ¸   s   rH   )ÚpatternÚbuilderÚtagsc                   @   s@   e Zd ZdZeƒ Zddd„Zdd„ Zdd„ Zd	d
„ Z	dd„ Z
dS )ÚPatternz*Base class that inline patterns subclass. Nc                 C   s*   || _ t d| tjtjB ¡| _|| _dS )ú–
        Create an instant of an inline pattern.

        Keyword arguments:

        * pattern: A regular expression that matches a pattern

        z^(.*?)%s(.*)$N)rM   ÚreÚcompileÚDOTALLÚUNICODEÚcompiled_rer;   ©ÚselfrM   r;   r>   r>   r?   Ú__init__Ç   s
    	

ÿzPattern.__init__c                 C   s   | j S )z' Return a compiled regular expression. )rV   ©rX   r>   r>   r?   ÚgetCompiledRegExpÖ   s    zPattern.getCompiledRegExpc                 C   s   dS )zÌReturn a ElementTree element from the given match.

        Subclasses should override this method.

        Keyword arguments:

        * m: A re match object containing a match of the pattern.

        Nr>   ©rX   Úmr>   r>   r?   ÚhandleMatchÚ   s    
zPattern.handleMatchc                 C   s   | j jS )z+ Return class name, to define pattern type )Ú	__class__rI   rZ   r>   r>   r?   Útypeæ   s    zPattern.typec                    sD   z| j jd j‰ W n ty(   | Y S 0 ‡ fdd„}tj ||¡S )ú> Return unescaped text given text with an inline placeholder. Úinlinec                    s<   |   d¡}|ˆ v r8ˆ  |¡}t|tƒr*|S d | ¡ ¡S d S )Nr   Ú )ÚgroupÚgetÚ
isinstanceÚstrÚjoinÚitertext©r]   ÚidÚvalue©Ústashr>   r?   Ú	get_stashñ   s    


z#Pattern.unescape.<locals>.get_stash©r;   ÚtreeprocessorsÚstashed_nodesÚKeyErrorr   ÚINLINE_PLACEHOLDER_REÚsub©rX   Útextro   r>   rm   r?   Úunescapeê   s    
	zPattern.unescape)N)rI   rJ   rK   rL   ÚtupleÚANCESTOR_EXCLUDESrY   r[   r^   r`   rx   r>   r>   r>   r?   rP   Â   s   
rP   c                   @   s"   e Zd ZdZddd„Zdd„ ZdS )ÚInlineProcessorzž
    Base class that inline patterns subclass.

    This is the newer style inline processor that uses a more
    efficient and flexible search approach.
    Nc                 C   s,   || _ t |tjtjB ¡| _d| _|| _dS )rQ   FN)rM   rR   rS   rT   rU   rV   Z	safe_moder;   rW   r>   r>   r?   rY     s    	zInlineProcessor.__init__c                 C   s   dS )a–  Return a ElementTree element from the given match and the
        start and end index of the matched text.

        If `start` and/or `end` are returned as `None`, it will be
        assumed that the processor did not find a valid region of text.

        Subclasses should override this method.

        Keyword arguments:

        * m: A re match object containing a match of the pattern.
        * data: The buffer current under analysis

        Returns:

        * el: The ElementTree element, text or None.
        * start: The start of the region that has been matched or None.
        * end: The end of the region that has been matched or None.

        Nr>   ©rX   r]   Údatar>   r>   r?   r^     s    zInlineProcessor.handleMatch)N©rI   rJ   rK   rL   rY   r^   r>   r>   r>   r?   r{   ý   s   
r{   c                   @   s   e Zd ZdZdd„ ZdS )ÚSimpleTextPatternz0 Return a simple text of group(2) of a Pattern. c                 C   s
   |  d¡S )Né   )rd   r\   r>   r>   r?   r^   /  s    zSimpleTextPattern.handleMatchN©rI   rJ   rK   rL   r^   r>   r>   r>   r?   r   -  s   r   c                   @   s   e Zd ZdZdd„ ZdS )r7   z0 Return a simple text of group(1) of a Pattern. c                 C   s   |  d¡| d¡| d¡fS ©Nr   r   )rd   ÚstartÚendr|   r>   r>   r?   r^   5  s    z%SimpleTextInlineProcessor.handleMatchNr   r>   r>   r>   r?   r7   3  s   r7   c                   @   s   e Zd ZdZdd„ ZdS )r"   z Return an escaped character. c                 C   sX   |  d¡}|| jjv r>d tjt|ƒtj¡| d¡| 	d¡fS d | d¡| 	d¡fS d S )Nr   ú{}{}{}r   )
rd   r;   ÚESCAPED_CHARSÚformatr   ÚSTXÚordÚETXrƒ   r„   )rX   r]   r}   Úcharr>   r>   r?   r^   <  s    
(z!EscapeInlineProcessor.handleMatchNr   r>   r>   r>   r?   r"   9  s   r"   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚSimpleTagPatternz[
    Return element of type `tag` with a text attribute of group(3)
    of a Pattern.

    c                 C   s   t  | |¡ || _d S ©N)rP   rY   Útag©rX   rM   rŽ   r>   r>   r?   rY   J  s    zSimpleTagPattern.__init__c                 C   s   t  | j¡}| d¡|_|S )Né   )ÚetreeÚElementrŽ   rd   rw   )rX   r]   Úelr>   r>   r?   r^   N  s    zSimpleTagPattern.handleMatchNr~   r>   r>   r>   r?   rŒ   D  s   rŒ   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚSimpleTagInlineProcessorz[
    Return element of type `tag` with a text attribute of group(2)
    of a Pattern.

    c                 C   s   t  | |¡ || _d S r   )r{   rY   rŽ   r   r>   r>   r?   rY   Z  s    z!SimpleTagInlineProcessor.__init__c                 C   s.   t  | j¡}| d¡|_|| d¡| d¡fS )Nr€   r   )r‘   r’   rŽ   rd   rw   rƒ   r„   ©rX   r]   r}   r“   r>   r>   r?   r^   ^  s    z$SimpleTagInlineProcessor.handleMatchNr~   r>   r>   r>   r?   r”   T  s   r”   c                   @   s   e Zd ZdZdd„ ZdS )ÚSubstituteTagPatternú3 Return an element of type `tag` with no children. c                 C   s   t  | j¡S r   )r‘   r’   rŽ   r\   r>   r>   r?   r^   f  s    z SubstituteTagPattern.handleMatchNr   r>   r>   r>   r?   r–   d  s   r–   c                   @   s   e Zd ZdZdd„ ZdS )r2   r—   c                 C   s   t  | j¡| d¡| d¡fS )Nr   )r‘   r’   rŽ   rƒ   r„   r|   r>   r>   r?   r^   l  s    z(SubstituteTagInlineProcessor.handleMatchNr   r>   r>   r>   r?   r2   j  s   r2   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r    z9 Return a `<code>` element containing the matching text. c                 C   s.   t  | |¡ d tjtdƒtj¡| _d| _d S )Nr…   ú\Úcode)	r{   rY   r‡   r   rˆ   r‰   rŠ   ÚESCAPED_BSLASHrŽ   )rX   rM   r>   r>   r?   rY   r  s    z BacktickInlineProcessor.__init__c                 C   sr   |  d¡rHt | j¡}t t |  d¡ ¡ ¡¡|_|| 	d¡| 
d¡fS |  d¡ d| j¡| 	d¡| 
d¡fS d S )Nr   r   r   z\\)rd   r‘   r’   rŽ   r   ÚAtomicStringÚcode_escapeÚstriprw   rƒ   r„   Úreplacerš   r•   r>   r>   r?   r^   w  s
    
z#BacktickInlineProcessor.handleMatchNr~   r>   r>   r>   r?   r    p  s   r    c                   @   s   e Zd ZdZdd„ ZdS )ÚDoubleTagPatternúfReturn a ElementTree element nested in tag2 nested in tag1.

    Useful for strong emphasis etc.

    c                 C   sR   | j  d¡\}}t |¡}t ||¡}| d¡|_t| ¡ ƒdkrN| d¡|_	|S )Nú,r   é   é   )
rŽ   Úsplitr‘   r’   Ú
SubElementrd   rw   ÚlenÚgroupsÚtail)rX   r]   Útag1Útag2Úel1Úel2r>   r>   r?   r^   †  s    
zDoubleTagPattern.handleMatchNr   r>   r>   r>   r?   rŸ   €  s   rŸ   c                   @   s   e Zd ZdZdd„ ZdS )ÚDoubleTagInlineProcessorr    c                 C   sd   | j  d¡\}}t |¡}t ||¡}| d¡|_t| ¡ ƒdkrN| d¡|_	|| 
d¡| d¡fS )Nr¡   r€   r   r   )rŽ   r¤   r‘   r’   r¥   rd   rw   r¦   r§   r¨   rƒ   r„   )rX   r]   r}   r©   rª   r«   r¬   r>   r>   r?   r^   –  s    
z$DoubleTagInlineProcessor.handleMatchNr   r>   r>   r>   r?   r­     s   r­   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r4   z1 Store raw inline html and return a placeholder. c                 C   s4   |   | d¡¡}| jj |¡}|| d¡| d¡fS r‚   )rx   rd   r;   Ú	htmlStashÚstorerƒ   r„   )rX   r]   r}   ZrawhtmlZplace_holderr>   r>   r?   r^   ¢  s    zHtmlInlineProcessor.handleMatchc                    sF   zˆ j jd j‰W n ty(   | Y S 0 ‡ ‡fdd„}tj ||¡S )ra   rb   c                    sJ   |   d¡}ˆ |¡}|d urFzˆ j |¡W S  tyD   d|  Y S 0 d S )Nr   z\%s)rd   re   r;   Ú
serializerÚ	Exceptionrj   ©rX   rn   r>   r?   ro   ®  s    

z/HtmlInlineProcessor.unescape.<locals>.get_stashrp   rv   r>   r²   r?   rx   §  s    
	zHtmlInlineProcessor.unescapeN)rI   rJ   rK   rL   r^   rx   r>   r>   r>   r?   r4      s   r4   c                
   @   sÆ   e Zd ZdZee eejej	B ¡ddƒee e
ejej	B ¡ddƒee eejej	B ¡ddƒee eejej	B ¡ddƒee eejej	B ¡ddƒgZd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )r9   zGEmphasis processor for handling strong and em matches inside asterisks.Údoubleú	strong,emú	em,strongÚdouble2ÚsingleÚstrongÚemc                 C   s(   t  |¡}| d¡}|  ||d|¡ |S )zReturn single tag.r€   N)r‘   r’   rd   Úparse_sub_patterns)rX   r]   rŽ   Úidxr«   rw   r>   r>   r?   Úbuild_singleÅ  s    

zAsteriskProcessor.build_singlec           	      C   st   |  d¡\}}t |¡}t |¡}| d¡}|  ||d|¡ | |¡ t| ¡ ƒdkrp| d¡}|  ||||¡ |S )zReturn double tag.r¡   r€   Nr   )r¤   r‘   r’   rd   rº   Úappendr¦   r§   ©	rX   r]   rO   r»   r©   rª   r«   r¬   rw   r>   r>   r?   Úbuild_doubleÌ  s    




zAsteriskProcessor.build_doublec           	      C   sd   |  d¡\}}t |¡}t |¡}| d¡}|  ||d|¡ | d¡}| |¡ |  ||d|¡ |S )zFReturn double tags (variant 2): `<strong>text <em>text</em></strong>`.r¡   r€   Nr   )r¤   r‘   r’   rd   rº   r½   r¾   r>   r>   r?   Úbuild_double2Ú  s    




zAsteriskProcessor.build_double2c                 C   sü   d}d}t |ƒ}||k rÒ| j ||¡rÈd}t| jƒD ]„\}	}
|	|krFq4|
j ||¡}|r4||| d¡… }|r„|dur~||_n||_|  	||
j
|
j|	¡}| |¡ |}| d¡ }}d}q4|sÐ|d7 }q|d7 }q||d… }|rø|durò||_n||_dS )a”  
        Parses sub patterns.

        `data` (`str`):
            text to evaluate.

        `parent` (`etree.Element`):
            Parent to attach text and sub elements to.

        `last` (`etree.Element`):
            Last appended child to parent. Can also be None if parent has no children.

        `idx` (`int`):
            Current pattern index that was used to evaluate the parent.

        r   FNTr   )r¦   rV   ÚmatchÚ	enumerateÚPATTERNSrM   rƒ   r¨   rw   Úbuild_elementrN   rO   r½   r„   )rX   r}   ÚparentÚlastr»   ÚoffsetÚposÚlengthÚmatchedÚindexÚitemr]   rw   r“   r>   r>   r?   rº   ç  s:    


z$AsteriskProcessor.parse_sub_patternsc                 C   s>   |dkr|   |||¡S |dkr,|  |||¡S |  |||¡S dS )zElement builder.r¶   r³   N)rÀ   r¿   r¼   )rX   r]   rN   rO   rË   r>   r>   r?   rÄ   '  s
    zAsteriskProcessor.build_elementc           	      C   sn   d}d}d}t | jƒD ]L\}}|j || d¡¡}|r| d¡}| d¡}|  ||j|j|¡} qdq|||fS )zParse patterns.Nr   )	rÂ   rÃ   rM   rÁ   rƒ   r„   rÄ   rN   rO   )	rX   r]   r}   r“   rƒ   r„   rË   rÌ   Úm1r>   r>   r?   r^   1  s    

zAsteriskProcessor.handleMatchN)rI   rJ   rK   rL   rH   rR   rS   ÚEM_STRONG_RErT   rU   ÚSTRONG_EM_REÚSTRONG_EM3_REÚ	STRONG_REÚEMPHASIS_RErÃ   r¼   r¿   rÀ   rº   rÄ   r^   r>   r>   r>   r?   r9   º  s   û@
r9   c                
   @   s–   e Zd ZdZee eejej	B ¡ddƒee e
ejej	B ¡ddƒee eejej	B ¡ddƒee eejej	B ¡ddƒee eejej	B ¡ddƒgZd	S )
r:   zIEmphasis processor for handling strong and em matches inside underscores.r³   r´   rµ   r¶   r·   r¸   r¹   N)rI   rJ   rK   rL   rH   rR   rS   ÚEM_STRONG2_RErT   rU   ÚSTRONG_EM2_REÚSMART_STRONG_EM_REÚSMART_STRONG_REÚSMART_EMPHASIS_RErÃ   r>   r>   r>   r?   r:   B  s   ûr:   c                   @   sF   e Zd ZdZe dejejB ¡Ze d¡Z	dd„ Z
dd„ Zdd	„ Zd
S )r&   z- Return a link element from the given match. z2\(\s*(?:(<[^<>]*>)\s*(?:('[^']*'|"[^"]*")\s*)?\))?z\sc           	      C   s|   |   || d¡¡\}}}|s dS |  ||¡\}}}}|s<dS t d¡}||_| d|¡ |d url| d|¡ || d¡|fS )Nr   ©NNNÚaÚhrefÚtitle)ÚgetTextr„   ÚgetLinkr‘   r’   rw   Úsetrƒ   )	rX   r]   r}   rw   rË   ÚhandledrÚ   rÛ   r“   r>   r>   r?   r^   S  s    
zLinkInlineProcessor.handleMatchc                 C   sô  d}d}d}| j j||d}|rn| d¡rn| d¡dd…  ¡ }| d¡r\| d¡dd… }| d¡}d	}nF|r´d}d}| ¡ }	|	}d}
d}d}d}d}d}d}d}d}t|t|ƒƒD ]Â}|| }|d
krð|sÞ|d7 }n|dkrî|d8 }nâ|dkr^|dkr||ks"|dkr(||kr(d}n4|s8|d8 }n$|dkrÒ|d8 }|dkrÒ|d }
nt|dv rÒ|sˆd	}|}d}|d }|}nJ||kr¦|s¦|d }|}n,||krº|d }n|rÒ||krÒ|d }|d7 }|dkrr|dkr ||kr ||	|d … }d |||d … ¡}nL|dkr\||kr\||	|d … }d |||d … ¡}n||	|d … } q€|dkrº|}qº|dkr¬|dkr¬||	|
d … }|
}d}|dk}|durÚ| j 	dt
|  | ¡ ¡ƒ¡}|  |¡ ¡ }||||fS )z?Parse data between `()` of `[Text]()` allowing recursive `()`. rc   NF©rÈ   r   rC   r€   r   Tú(ú))rB   rA   ú )ÚRE_LINKrÁ   rd   r   r„   Úranger¦   rh   ÚRE_TITLE_CLEANru   rG   rx   )rX   r}   rË   rÚ   rÛ   rß   r]   Úbracket_countZbacktrack_countÚstart_indexZlast_bracketÚquoteZstart_quoteZ
exit_quoteZignore_matchesZ	alt_quoteZstart_alt_quoteZexit_alt_quoterÆ   rÈ   Úcr>   r>   r?   rÝ   g  s–    




(








zLinkInlineProcessor.getLinkc                 C   sx   d}g }t |t|ƒƒD ]L}|| }|dkr4|d8 }n|dkrD|d7 }|d7 }|dkrX qd| |¡ qd |¡||dkfS )zsParse the content between `[]` of the start of an image or link
        resolving nested square brackets.

        r   ú]ú[r   rc   )rå   r¦   r½   rh   )rX   r}   rË   rç   rw   rÈ   rê   r>   r>   r?   rÜ   Û  s    
zLinkInlineProcessor.getTextN)rI   rJ   rK   rL   rR   rS   rT   rU   rä   ræ   r^   rÝ   rÜ   r>   r>   r>   r?   r&   N  s   
tr&   c                   @   s   e Zd ZdZdd„ ZdS )r(   z, Return a img element from the given match. c           	      C   sˆ   |   || d¡¡\}}}|s dS |  ||¡\}}}}|s<dS t d¡}| d|¡ |d urf| d|¡ | d|  |¡¡ || d¡|fS )Nr   rØ   ÚimgÚsrcrÛ   Úalt)rÜ   r„   rÝ   r‘   r’   rÞ   rx   rƒ   )	rX   r]   r}   rw   rË   rß   rî   rÛ   r“   r>   r>   r?   r^   ò  s    
z ImageInlineProcessor.handleMatchNr   r>   r>   r>   r?   r(   ï  s   r(   c                   @   sJ   e Zd ZdZe dej¡Ze dejej	B ¡Z
dd„ Zdd„ Zdd	„ Zd
S )r$   z6 Match to a stored reference and return link element. z\s+z\s?\[([^\]]*)\]c           
      C   s   |   || d¡¡\}}}|s dS |  |||¡\}}}|s<dS | j d|¡}|| jjvrfd | d¡|fS | jj| \}}	|  ||	|¡| d¡|fS )Nr   rØ   rã   )	rÜ   r„   ÚevalIdÚNEWLINE_CLEANUP_REru   r;   Ú
referencesrƒ   ÚmakeTag)
rX   r]   r}   rw   rË   rß   rk   r„   rÚ   rÛ   r>   r>   r?   r^     s    z$ReferenceInlineProcessor.handleMatchc                 C   sL   | j j||d}|sd|dfS | d¡ ¡ }| d¡}|sB| ¡ }||dfS )zV
        Evaluate the id portion of [ref][id].

        If [ref][] use [ref].
        rà   NFr   r   T)rä   rÁ   rd   Úlowerr„   )rX   r}   rË   rw   r]   rk   r„   r>   r>   r?   rð     s    

zReferenceInlineProcessor.evalIdc                 C   s0   t  d¡}| d|¡ |r&| d|¡ ||_|S )NrÙ   rÚ   rÛ   )r‘   r’   rÞ   rw   ©rX   rÚ   rÛ   rw   r“   r>   r>   r?   ró   .  s    
z ReferenceInlineProcessor.makeTagN)rI   rJ   rK   rL   rR   rS   Ú	MULTILINErñ   rT   rU   rä   r^   rð   ró   r>   r>   r>   r?   r$     s   r$   c                   @   s   e Zd ZdZdd„ ZdS )r,   z#Short form of reference: [google]. c                 C   s   |  ¡ |dfS ©zEvaluate the id from of [ref]  T©rô   ©rX   r}   rË   rw   r>   r>   r?   rð   ;  s    z$ShortReferenceInlineProcessor.evalIdN©rI   rJ   rK   rL   rð   r>   r>   r>   r?   r,   9  s   r,   c                   @   s   e Zd ZdZdd„ ZdS )r*   z5 Match to a stored reference and return img element. c                 C   s<   t  d¡}| d|¡ |r&| d|¡ | d|  |¡¡ |S )Nrí   rî   rÛ   rï   )r‘   r’   rÞ   rx   rõ   r>   r>   r?   ró   C  s    
z%ImageReferenceInlineProcessor.makeTagN)rI   rJ   rK   rL   ró   r>   r>   r>   r?   r*   A  s   r*   c                   @   s   e Zd ZdZdd„ ZdS )r-   z( Short form of inage reference: ![ref]. c                 C   s   |  ¡ |dfS r÷   rø   rù   r>   r>   r?   rð   N  s    z)ShortImageReferenceInlineProcessor.evalIdNrú   r>   r>   r>   r?   r-   L  s   r-   c                   @   s   e Zd ZdZdd„ ZdS )r.   zC Return a link Element given an autolink (`<http://example/com>`). c                 C   sJ   t  d¡}| d|  | d¡¡¡ t | d¡¡|_|| d¡| 	d¡fS )NrÙ   rÚ   r   r   )
r‘   r’   rÞ   rx   rd   r   r›   rw   rƒ   r„   r•   r>   r>   r?   r^   V  s    
z#AutolinkInlineProcessor.handleMatchNr   r>   r>   r>   r?   r.   T  s   r.   c                   @   s   e Zd ZdZdd„ ZdS )r0   zT
    Return a mailto link Element given an automail link (`<foo@example.com>`).
    c                    sž   t  d¡}|  | d¡¡}| d¡r4|tdƒd … }dd„ ‰ ‡ fdd„|D ƒ}t d |¡¡|_	d| }d d	d„ |D ƒ¡}| 
d
|¡ || d¡| d¡fS )NrÙ   r   zmailto:c                 S   s0   t j | ¡}|rd tj|¡S dtj| f S dS )z=Return entity definition by code, or the code if not defined.z{}{};z%s#%d;N)r   Úcodepoint2namere   r‡   r   ÚAMP_SUBSTITUTE)r™   r   r>   r>   r?   rû   g  s    z;AutomailInlineProcessor.handleMatch.<locals>.codepoint2namec                    s   g | ]}ˆ t |ƒƒ‘qS r>   )r‰   ©Ú.0Úletter©rû   r>   r?   Ú
<listcomp>o  ó    z7AutomailInlineProcessor.handleMatch.<locals>.<listcomp>rc   c                 S   s   g | ]}t jd t|ƒ  ‘qS )z#%d;)r   rü   r‰   rý   r>   r>   r?   r  s  s   ÿÿrÚ   r   )r‘   r’   rx   rd   rD   r¦   r   r›   rh   rw   rÞ   rƒ   r„   )rX   r]   r}   r“   ÚemailÚlettersÚmailtor>   r   r?   r^   a  s    


ÿz#AutomailInlineProcessor.handleMatchNr   r>   r>   r>   r?   r0   ]  s   r0   )>rL   rc   r   Úcollectionsr   rR   Úxml.etree.ElementTreer‘   ÚElementTreer   r   ÚImportErrorÚhtmlentitydefsr@   ZNOIMGr!   r#   rÒ   rÑ   rÖ   r×   rÕ   rÎ   rÓ   rÏ   rÔ   rÐ   r'   r)   r%   r+   r8   r/   r1   r5   r6   r3   rG   rH   rP   r{   r   r7   r"   rŒ   r”   r–   r2   r    rŸ   r­   r4   r9   r:   r&   r(   r$   r,   r*   r-   r.   r0   r>   r>   r>   r?   Ú<module>   sv   >!	
;0 	 "3	