U
    \> ep•  ã                   @  sn  d Z ddlmZ ddlmZ ddlmZmZmZm	Z	 ddl
Z
ddlm  mZ ddlmZ erjddlmZ d	d
ddœ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$d%œd&d'„Z*G d(d)„ d)e	ƒZ+G d*d+„ d+ƒZ,G d,d-„ d-e,ƒZ-G d.d/„ d/e,ƒZ.G d0d1„ d1e-ƒZ/G d2d3„ d3e-ƒZ0G d4d5„ d5e,ƒZ1G d6d7„ d7e-ƒZ2G d8d9„ d9e1ƒZ3G d:d;„ d;e2ƒZ4G d<d=„ d=e-ƒZ5G d>d?„ d?e1ƒZ6G d@dA„ dAe2ƒZ7G dBdC„ dCe-ƒZ8G dDdE„ dEe-ƒZ9G dFdG„ dGe9ƒZ:G dHdI„ dIe-ƒZ;G dJdK„ dKe;ƒZ<G dLdM„ dMe;ƒZ=G dNdO„ dOe=ƒZ>G dPdQ„ dQe=ƒZ?G dRdS„ dSe?ƒZ@G dTdU„ dUe-ƒZAG dVdW„ dWe-ƒZBdS )XaB  
In version 3.0, a new, more flexible inline processor was added, [`markdown.inlinepatterns.InlineProcessor`][].   The
original inline patterns, which inherit from [`markdown.inlinepatterns.Pattern`][] or one of its children are still
supported, though users are encouraged to migrate.

The new `InlineProcessor` provides two major enhancements to `Patterns`:

1. Inline Processors no longer need to match the entire block, so regular expressions no longer need to start with
  `r'^(.*?)'` and end with `r'(.*?)%'`. This runs faster. The returned [`Match`][re.Match] object will only contain
   what is explicitly matched in the pattern, and extension pattern groups now start with `m.group(1)`.

2.  The `handleMatch` method now takes an additional input called `data`, which is the entire block under analysis,
    not just what is matched with the specified pattern. The method now returns the element *and* the indexes relative
    to `data` that the return element is replacing (usually `m.start(0)` and `m.end(0)`).  If the boundaries are
    returned as `None`, it is assumed that the match did not take place, and nothing will be altered in `data`.

    This allows handling of more complex constructs than regular expressions can handle, e.g., matching nested
    brackets, and explicit control of the span "consumed" by the processor.

é    )Úannotationsé   )Úutil)ÚTYPE_CHECKINGÚAnyÚ
CollectionÚ
NamedTupleN)Úentities)ÚMarkdownr
   r   zutil.Registry[InlineProcessor])ÚmdÚkwargsÚreturnc                 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 )$a  
    Build the default set of inline patterns for Markdown.

    The order in which processors and/or patterns 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:

    * backticks and escaped characters have to be handled before everything else so that we can preempt any markdown
      patterns by escaping them;

    * then we handle the various types of links (auto-links must be handled 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 stash;

    * finally we apply strong, emphasis, etc.

    Ú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   Z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)r   r   ZinlinePatterns© rB   ú;/tmp/pip-unpacked-wheel-wj0og6ym/markdown/inlinepatterns.pyÚbuild_inlinepatterns5   s<      ÿ  ÿ  ÿrD   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))(\*{1,3}|_{1,3})(?=\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  \nÚstr)Ústringr   c                 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)rF   rB   rB   rC   Údequote«   s    ÿÿrL   c                   @  s*   e Zd ZU dZded< ded< ded< dS )ÚEmStrongItemzEmphasis/strong pattern item.úre.Pattern[str]ÚpatternrE   ÚbuilderÚtagsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__rB   rB   rB   rC   rM   ´   s   
rM   c                   @  s~   e Zd ZU dZeƒ Zded< ded< ded< dd	dd
œdd„Zddœdd„Zdddœdd„Z	d	dœdd„Z
d	d	dœdd„ZdS )ÚPatternaí  
    Base class that inline patterns subclass.

    Inline patterns are handled by means of `Pattern` subclasses, one per regular expression.
    Each pattern object uses a single regular expression and must support the following methods:
    [`getCompiledRegExp`][markdown.inlinepatterns.Pattern.getCompiledRegExp] and
    [`handleMatch`][markdown.inlinepatterns.Pattern.handleMatch].

    All the regular expressions used by `Pattern` subclasses must capture the whole block.  For this
    reason, they all start with `^(.*)` and end with `(.*)!`.  When passing a regular expression on
    class initialization, the `^(.*)` and `(.*)!` are added automatically and the regular expression
    is pre-compiled.

    It is strongly suggested that the newer style [`markdown.inlinepatterns.InlineProcessor`][] that
    use a more efficient and flexible search approach be used instead. However, the older style
    `Pattern` remains for backward compatibility with many existing third-party extensions.

    zCollection[str]ÚANCESTOR_EXCLUDESrN   Úcompiled_reúMarkdown | Noner   NrE   ©rO   r   c                 C  s*   || _ t d| tjtjB ¡| _|| _dS )a!  
        Create an instant of an inline pattern.

        Arguments:
            pattern: A regular expression that matches a pattern.
            md: An optional pointer to the instance of `markdown.Markdown` and is available as
                `self.md` on the class instance.


        z^(.*?)%s(.*)$N)rO   ÚreÚcompileÚDOTALLÚUNICODErY   r   ©ÚselfrO   r   rB   rB   rC   Ú__init__Ü   s
    

ÿzPattern.__init__z
re.Pattern)r   c                 C  s   | j S )z' Return a compiled regular expression. )rY   ©ra   rB   rB   rC   ÚgetCompiledRegExpí   s    zPattern.getCompiledRegExpúre.Match[str]zetree.Element | str©Úmr   c                 C  s   dS )zóReturn a ElementTree element from the given match.

        Subclasses should override this method.

        Arguments:
            m: A match object containing a match of the pattern.

        Returns: An ElementTree Element object.

        NrB   ©ra   rg   rB   rB   rC   ÚhandleMatchñ   s    zPattern.handleMatchc                 C  s   | j jS )z+ Return class name, to define pattern type )Ú	__class__rR   rc   rB   rB   rC   Útypeþ   s    zPattern.type©Útextr   c                   sF   z| j jd j‰ W n tk
r*   | Y S X ‡ fdd„}tj ||¡S )ú> Return unescaped text given text with an inline placeholder. Úinlinec                   s<   |   d¡}|ˆ kr8ˆ  |¡}t|tƒr*|S d | ¡ ¡S d S )Nr   Ú )ÚgroupÚgetÚ
isinstancerE   ÚjoinÚitertext©rg   ÚidÚvalue©ÚstashrB   rC   Ú	get_stash	  s    


z#Pattern.unescape.<locals>.get_stash©r   ÚtreeprocessorsZstashed_nodesÚKeyErrorr   ZINLINE_PLACEHOLDER_REÚsub©ra   rm   r{   rB   ry   rC   Úunescape  s    
	zPattern.unescape)N)rR   rS   rT   rU   ÚtuplerX   rV   rb   rd   ri   rk   r   rB   rB   rB   rC   rW   ¿   s   
rW   c                   @  s4   e Zd ZdZddddœdd„Zddd	d
œdd„ZdS )ÚInlineProcessorz¡
    Base class that inline processors subclass.

    This is the newer style inline processor that uses a more
    efficient and flexible search approach.

    NrE   rZ   r[   c                 C  s,   || _ t |tjtjB ¡| _d| _|| _dS )a"  
        Create an instant of an inline processor.

        Arguments:
            pattern: A regular expression that matches a pattern.
            md: An optional pointer to the instance of `markdown.Markdown` and is available as
                `self.md` on the class instance.

        FN)rO   r\   r]   r^   r_   rY   Z	safe_moder   r`   rB   rB   rC   rb     s    
zInlineProcessor.__init__re   z9tuple[etree.Element | str | None, int | None, int | None]©rg   Údatar   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.

        Arguments:
            m: A re match object containing a match of the pattern.
            data: The buffer currently 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.

        NrB   ©ra   rg   r…   rB   rB   rC   ri   /  s    zInlineProcessor.handleMatch)N©rR   rS   rT   rU   rb   ri   rB   rB   rB   rC   rƒ     s   rƒ   c                   @  s    e Zd ZdZdddœdd„ZdS )ÚSimpleTextPatternz2 Return a simple text of `group(2)` of a Pattern. re   rE   rf   c                 C  s
   |  d¡S )z< Return string content of `group(2)` of a matching pattern. é   )rq   rh   rB   rB   rC   ri   G  s    zSimpleTextPattern.handleMatchN©rR   rS   rT   rU   ri   rB   rB   rB   rC   rˆ   E  s   rˆ   c                   @  s"   e Zd ZdZddddœdd„ZdS )	r>   z2 Return a simple text of `group(1)` of a Pattern. re   rE   útuple[str, int, int]r„   c                 C  s   |  d¡| d¡| d¡fS )z< Return string content of `group(1)` of a matching pattern. r   r   )rq   ÚstartÚendr†   rB   rB   rC   ri   N  s    z%SimpleTextInlineProcessor.handleMatchNrŠ   rB   rB   rB   rC   r>   L  s   r>   c                   @  s"   e Zd ZdZddddœdd„ZdS )	r)   z Return an escaped character. re   rE   ztuple[str | None, int, int]r„   c                 C  sX   |  d¡}|| jjkr>d tjt|ƒtj¡| d¡| 	d¡fS d| d¡| 	d¡fS dS )aµ  
        If the character matched by `group(1)` of a pattern is in [`ESCAPED_CHARS`][markdown.Markdown.ESCAPED_CHARS]
        then return the integer representing the character's Unicode code point (as returned by [`ord`][]) wrapped
        in [`util.STX`][markdown.util.STX] and [`util.ETX`][markdown.util.ETX].

        If the matched character is not in [`ESCAPED_CHARS`][markdown.Markdown.ESCAPED_CHARS], then return `None`.
        r   ú{}{}{}r   N)
rq   r   ZESCAPED_CHARSÚformatr   ÚSTXÚordÚETXrŒ   r   )ra   rg   r…   ÚcharrB   rB   rC   ri   V  s    	
(z!EscapeInlineProcessor.handleMatchNrŠ   rB   rB   rB   rC   r)   S  s   r)   c                   @  s0   e Zd ZdZdddœdd„Zdddœd	d
„ZdS )ÚSimpleTagPatternz]
    Return element of type `tag` with a text attribute of `group(3)`
    of a Pattern.

    rE   ©rO   Útagc                 C  s   t  | |¡ || _dS )zµ
        Create an instant of an simple tag pattern.

        Arguments:
            pattern: A regular expression that matches a pattern.
            tag: Tag of element.

        N)rW   rb   r–   ©ra   rO   r–   rB   rB   rC   rb   l  s    	zSimpleTagPattern.__init__re   úetree.Elementrf   c                 C  s   t  | j¡}| d¡|_|S )z¤
        Return [`Element`][xml.etree.ElementTree.Element] of type `tag` with the string in `group(3)` of a
        matching pattern as the Element's text.
        é   )ÚetreeÚElementr–   rq   rm   )ra   rg   ÚelrB   rB   rC   ri   y  s    zSimpleTagPattern.handleMatchNr‡   rB   rB   rB   rC   r”   f  s   r”   c                   @  s2   e Zd ZdZdddœdd„Zddddœd	d
„ZdS )ÚSimpleTagInlineProcessorz]
    Return element of type `tag` with a text attribute of `group(2)`
    of a Pattern.

    rE   r•   c                 C  s   t  | |¡ || _dS )z·
        Create an instant of an simple tag processor.

        Arguments:
            pattern: A regular expression that matches a pattern.
            tag: Tag of element.

        N)rƒ   rb   r–   r—   rB   rB   rC   rb   ‰  s    	z!SimpleTagInlineProcessor.__init__re   útuple[etree.Element, int, int]r„   c                 C  s.   t  | j¡}| d¡|_|| d¡| d¡fS )z¤
        Return [`Element`][xml.etree.ElementTree.Element] of type `tag` with the string in `group(2)` of a
        matching pattern as the Element's text.
        r‰   r   )rš   r›   r–   rq   rm   rŒ   r   ©ra   rg   r…   rœ   rB   rB   rC   ri   –  s    z$SimpleTagInlineProcessor.handleMatchNr‡   rB   rB   rB   rC   r   ƒ  s   r   c                   @  s    e Zd ZdZdddœdd„ZdS )ÚSubstituteTagPatternú3 Return an element of type `tag` with no children. re   r˜   rf   c                 C  s   t  | j¡S )úH Return empty [`Element`][xml.etree.ElementTree.Element] of type `tag`. )rš   r›   r–   rh   rB   rB   rC   ri   ¢  s    z SubstituteTagPattern.handleMatchNrŠ   rB   rB   rB   rC   r       s   r    c                   @  s"   e Zd ZdZddddœdd„ZdS )	r9   r¡   re   rE   rž   r„   c                 C  s   t  | j¡| d¡| d¡fS )r¢   r   )rš   r›   r–   rŒ   r   r†   rB   rB   rC   ri   ©  s    z(SubstituteTagInlineProcessor.handleMatchNrŠ   rB   rB   rB   rC   r9   §  s   r9   c                   @  s0   e Zd ZdZddœdd„Zddddœd	d
„ZdS )r'   zA Return a `<code>` element containing the escaped matching text. rE   )rO   c                 C  s.   t  | |¡ d tjtdƒtj¡| _d| _d S )NrŽ   ú\Úcode)	rƒ   rb   r   r   r   r‘   r’   ÚESCAPED_BSLASHr–   )ra   rO   rB   rB   rC   rb   °  s    z BacktickInlineProcessor.__init__re   z$tuple[etree.Element | str, int, int]r„   c                 C  sr   |  d¡rHt | j¡}t t |  d¡ ¡ ¡¡|_|| 	d¡| 
d¡fS |  d¡ d| j¡| 	d¡| 
d¡fS dS )a€  
        If the match contains `group(3)` of a pattern, then return a `code`
        [`Element`][xml.etree.ElementTree.Element] which contains HTML escaped text (with
        [`code_escape`][markdown.util.code_escape]) as an [`AtomicString`][markdown.util.AtomicString].

        If the match does not contain `group(3)` then return the text of `group(1)` backslash escaped.

        r™   r   r   z\\N)rq   rš   r›   r–   r   ÚAtomicStringZcode_escapeÚstriprm   rŒ   r   Úreplacer¥   rŸ   rB   rB   rC   ri   ¶  s
    	
z#BacktickInlineProcessor.handleMatchNr‡   rB   rB   rB   rC   r'   ®  s   r'   c                   @  s    e Zd ZdZdddœdd„ZdS )ÚDoubleTagPatternúfReturn a ElementTree element nested in tag2 nested in tag1.

    Useful for strong emphasis etc.

    re   r˜   rf   c                 C  sR   | j  d¡\}}t |¡}t ||¡}| d¡|_t| ¡ ƒdkrN| d¡|_	|S )z¬
        Return [`Element`][xml.etree.ElementTree.Element] in following format:
        `<tag1><tag2>group(3)</tag2>group(4)</tag2>` where `group(4)` is optional.

        ú,r™   é   é   )
r–   Úsplitrš   r›   Ú
SubElementrq   rm   ÚlenÚgroupsÚtail)ra   rg   Útag1Útag2Úel1Úel2rB   rB   rC   ri   Í  s    
zDoubleTagPattern.handleMatchNrŠ   rB   rB   rB   rC   r©   Ç  s   r©   c                   @  s"   e Zd ZdZddddœdd„ZdS )	ÚDoubleTagInlineProcessorrª   re   rE   rž   r„   c                 C  sd   | j  d¡\}}t |¡}t ||¡}| d¡|_t| ¡ ƒdkrN| d¡|_	|| 
d¡| d¡fS )z¬
        Return [`Element`][xml.etree.ElementTree.Element] in following format:
        `<tag1><tag2>group(2)</tag2>group(3)</tag2>` where `group(3)` is optional.

        r«   r‰   r™   r   )r–   r®   rš   r›   r¯   rq   rm   r°   r±   r²   rŒ   r   )ra   rg   r…   r³   r´   rµ   r¶   rB   rB   rC   ri   â  s    
z$DoubleTagInlineProcessor.handleMatchNrŠ   rB   rB   rB   rC   r·   Ü  s   r·   c                   @  sB   e Zd ZdZddddœdd„Zdddœd	d
„Zdddœdd„ZdS )r;   z1 Store raw inline html and return a placeholder. re   rE   r‹   r„   c                 C  s:   |   |  | d¡¡¡}| jj |¡}|| d¡| d¡fS )zL Store the text of `group(1)` of a pattern and return a placeholder string. r   r   )Úbackslash_unescaper   rq   r   Z	htmlStashÚstorerŒ   r   )ra   rg   r…   ZrawhtmlZplace_holderrB   rB   rC   ri   ó  s    zHtmlInlineProcessor.handleMatchrl   c                   sP   zˆ j jd j‰W n tk
r*   | Y S X dddœ‡ ‡fdd„}tj ||¡S )rn   ro   re   rE   rf   c                   sL   |   d¡}ˆ |¡}|d k	rHzˆ j |¡W S  tk
rF   d|  Y S X d S )Nr   z\%s)rq   rr   r   Ú
serializerÚ	Exceptionrv   ©ra   rz   rB   rC   r{      s    

z/HtmlInlineProcessor.unescape.<locals>.get_stashr|   r€   rB   r¼   rC   r   ù  s    
	zHtmlInlineProcessor.unescapec                 C  sH   z| j jd j}W n tk
r*   | Y S X dddœdd„}| ||¡S )zG Return text with backslash escapes undone (backslashes are restored). r   re   rE   rf   c                 S  s   t t|  d¡ƒƒS )Nr   )ÚchrÚintrq   )rg   rB   rB   rC   Ú	_unescape  s    z9HtmlInlineProcessor.backslash_unescape.<locals>._unescape)r   r}   ÚREr~   r   )ra   rm   rÀ   r¿   rB   rB   rC   r¸     s    
z&HtmlInlineProcessor.backslash_unescapeN)rR   rS   rT   rU   ri   r   r¸   rB   rB   rB   rC   r;   ñ  s   r;   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
dddœdd„Zd	d
dddœdd„Zd	d
dddœdd„Zd
dddddœdd„Zd	d
d
dddœdd„Zd	d
ddœdd „Zd!S )"r@   zGEmphasis processor for handling strong and em matches inside asterisks.Údoubleú	strong,emú	em,strongÚdouble2ÚsingleÚstrongÚemre   rE   r¾   r˜   )rg   r–   Úidxr   c                 C  s(   t  |¡}| d¡}|  ||d|¡ |S )zReturn single tag.r‰   N)rš   r›   rq   Úparse_sub_patterns)ra   rg   r–   rÈ   rµ   rm   rB   rB   rC   Úbuild_single$  s    

zAsteriskProcessor.build_single)rg   rQ   rÈ   r   c           	      C  st   |  d¡\}}t |¡}t |¡}| d¡}|  ||d|¡ | |¡ t| ¡ ƒdkrp| d¡}|  ||||¡ |S )zReturn double tag.r«   r‰   Nr™   )r®   rš   r›   rq   rÉ   Úappendr°   r±   ©	ra   rg   rQ   rÈ   r³   r´   rµ   r¶   rm   rB   rB   rC   Ú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›   rq   rÉ   rË   rÌ   rB   rB   rC   Úbuild_double29  s    




zAsteriskProcessor.build_double2zetree.Element | NoneÚNone)r…   ÚparentÚlastrÈ   r   c                 C  sü   d}d}t |ƒ}||k rÒ| j ||¡rÈd}t| jƒD ]„\}	}
|	|krFq4|
j ||¡}|r4||| d¡… }|r„|dk	r~||_n||_|  	||
j
|
j|	¡}| |¡ |}| d¡ }}d}q4|sÐ|d7 }q|d7 }q||d… }|rø|dk	rò||_n||_dS )a/  
        Parses sub patterns.

        `data`: text to evaluate.

        `parent`: Parent to attach text and sub elements to.

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

        `idx`: Current pattern index that was used to evaluate the parent.
        r   FNTr   )r°   rY   ÚmatchÚ	enumerateÚPATTERNSrO   rŒ   r²   rm   Úbuild_elementrP   rQ   rË   r   )ra   r…   rÐ   rÑ   rÈ   ÚoffsetÚposÚlengthÚmatchedÚindexÚitemrg   rm   rœ   rB   rB   rC   rÉ   F  s:    


z$AsteriskProcessor.parse_sub_patterns)rg   rP   rQ   rÚ   r   c                 C  s>   |dkr|   |||¡S |dkr,|  |||¡S |  |||¡S dS )zElement builder.rÄ   rÁ   N)rÎ   rÍ   rÊ   )ra   rg   rP   rQ   rÚ   rB   rB   rC   rÕ   ƒ  s
    zAsteriskProcessor.build_elementú3tuple[etree.Element | None, int | None, int | None]r„   c           	      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Ô   rO   rÒ   rŒ   r   rÕ   rP   rQ   )	ra   rg   r…   rœ   rŒ   r   rÚ   rÛ   Úm1rB   rB   rC   ri     s    

zAsteriskProcessor.handleMatchN)rR   rS   rT   rU   rM   r\   r]   ÚEM_STRONG_REr^   r_   ÚSTRONG_EM_REÚSTRONG_EM3_REÚ	STRONG_REÚEMPHASIS_RErÔ   rÊ   rÍ   rÎ   rÉ   rÕ   ri   rB   rB   rB   rC   r@     s   û	=
r@   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 )
rA   zIEmphasis processor for handling strong and em matches inside underscores.rÁ   rÂ   rÃ   rÄ   rÅ   rÆ   rÇ   N)rR   rS   rT   rU   rM   r\   r]   ÚEM_STRONG2_REr^   r_   ÚSTRONG_EM2_REÚSMART_STRONG_EM_REÚSMART_STRONG_REÚSMART_EMPHASIS_RErÔ   rB   rB   rB   rC   rA   ž  s   ûrA   c                   @  sd   e Zd ZdZe dejejB ¡Ze d¡Z	ddddœdd	„Z
dd
ddœdd„Zdd
ddœdd„ZdS )r-   z- Return a link element from the given match. z2\(\s*(?:(<[^<>]*>)\s*(?:('[^']*'|"[^"]*")\s*)?\))?z\sre   rE   rÜ   r„   c           	      C  s|   |   || d¡¡\}}}|s dS |  ||¡\}}}}|s<dS t d¡}||_| d|¡ |dk	rl| d|¡ || d¡|fS )zS Return an `a` [`Element`][xml.etree.ElementTree.Element] or `(None, None, None)`. r   ©NNNÚaÚhrefNÚtitle)ÚgetTextr   ÚgetLinkrš   r›   rm   ÚsetrŒ   )	ra   rg   r…   rm   rÚ   Úhandledrê   rë   rœ   rB   rB   rC   ri   °  s    
zLinkInlineProcessor.handleMatchr¾   z!tuple[str, str | None, int, bool])r…   rÚ   r   c                 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k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k	rÚ| j 	dt
|  | ¡ ¡ƒ¡}|  |¡ ¡ }||||fS )z?Parse data between `()` of `[Text]()` allowing recursive `()`. rp   NF©r×   r   rI   r‰   r   Tú(ú))rH   rG   ú )ÚRE_LINKrÒ   rq   r§   r   Úranger°   rt   ÚRE_TITLE_CLEANr   rL   r   )ra   r…   rÚ   rê   rë   rï   rg   Úbracket_countZbacktrack_countÚstart_indexZlast_bracketÚquoteZstart_quoteZ
exit_quoteZignore_matchesZ	alt_quoteZstart_alt_quoteZexit_alt_quoterÑ   r×   ÚcrB   rB   rC   rí   Å  s–    




(








zLinkInlineProcessor.getLinkútuple[str, int, bool]c                 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   rp   )rõ   r°   rË   rt   )ra   r…   rÚ   r÷   rm   r×   rú   rB   rB   rC   rì   9  s    
zLinkInlineProcessor.getTextN)rR   rS   rT   rU   r\   r]   r^   r_   rô   rö   ri   rí   rì   rB   rB   rB   rC   r-   «  s   
tr-   c                   @  s"   e Zd ZdZddddœdd„ZdS )	r/   z. Return a `img` element from the given match. re   rE   rÜ   r„   c           	      C  sˆ   |   || d¡¡\}}}|s dS |  ||¡\}}}}|s<dS t d¡}| d|¡ |dk	rf| d|¡ | d|  |¡¡ || d¡|fS )zU Return an `img` [`Element`][xml.etree.ElementTree.Element] or `(None, None, None)`. r   rè   ÚimgÚsrcNrë   Úalt)rì   r   rí   rš   r›   rî   r   rŒ   )	ra   rg   r…   rm   rÚ   rï   rÿ   rë   rœ   rB   rB   rC   ri   P  s    
z ImageInlineProcessor.handleMatchNrŠ   rB   rB   rB   rC   r/   M  s   r/   c                   @  sl   e Zd ZdZe dej¡Ze dejej	B ¡Z
ddddœdd	„Zdd
dddœdd„Zdddddœdd„ZdS )r+   z6 Match to a stored reference and return link element. z\s+z\s?\[([^\]]*)\]re   rE   rÜ   r„   c           
      C  s   |   || d¡¡\}}}|s dS |  |||¡\}}}|s<dS | j d|¡}|| jjkrfd| d¡|fS | jj| \}}	|  ||	|¡| d¡|fS )zz
        Return [`Element`][xml.etree.ElementTree.Element] returned by `makeTag` method or `(None, None, None)`.

        r   rè   ró   N)	rì   r   ÚevalIdÚNEWLINE_CLEANUP_REr   r   Z
referencesrŒ   ÚmakeTag)
ra   rg   r…   rm   rÚ   rï   rw   r   rê   rë   rB   rB   rC   ri   k  s    z$ReferenceInlineProcessor.handleMatchr¾   ztuple[str | None, int, bool]©r…   rÚ   rm   r   c                 C  sL   | j j||d}|sd|dfS | d¡ ¡ }| d¡}|sB| ¡ }||dfS )z\
        Evaluate the id portion of `[ref][id]`.

        If `[ref][]` use `[ref]`.
        rð   NFr   r   T)rô   rÒ   rq   Úlowerr   )ra   r…   rÚ   rm   rg   rw   r   rB   rB   rC   r    s    

zReferenceInlineProcessor.evalIdr˜   ©rê   rë   rm   r   c                 C  s0   t  d¡}| d|¡ |r&| d|¡ ||_|S )z; Return an `a` [`Element`][xml.etree.ElementTree.Element]. ré   rê   rë   )rš   r›   rî   rm   ©ra   rê   rë   rm   rœ   rB   rB   rC   r  ‘  s    
z ReferenceInlineProcessor.makeTagN)rR   rS   rT   rU   r\   r]   Ú	MULTILINEr  r^   r_   rô   ri   r  r  rB   rB   rB   rC   r+   e  s   r+   c                   @  s$   e Zd ZdZdddddœdd„ZdS )	r3   z%Short form of reference: `[google]`. rE   r¾   rû   r  c                 C  s   |  ¡ |dfS ©zEvaluate the id of `[ref]`.  T©r  ©ra   r…   rÚ   rm   rB   rB   rC   r  Ÿ  s    z$ShortReferenceInlineProcessor.evalIdN©rR   rS   rT   rU   r  rB   rB   rB   rC   r3     s   r3   c                   @  s$   e Zd ZdZdddddœdd„ZdS )r1   z7 Match to a stored reference and return `img` element. rE   r˜   r  c                 C  s<   t  d¡}| d|¡ |r&| d|¡ | d|  |¡¡ |S )z= Return an `img` [`Element`][xml.etree.ElementTree.Element]. rþ   rÿ   rë   r   )rš   r›   rî   r   r  rB   rB   rC   r  §  s    
z%ImageReferenceInlineProcessor.makeTagN)rR   rS   rT   rU   r  rB   rB   rB   rC   r1   ¥  s   r1   c                   @  s$   e Zd ZdZdddddœdd„ZdS )	r4   z* Short form of image reference: `![ref]`. rE   r¾   rû   r  c                 C  s   |  ¡ |dfS r	  r
  r  rB   rB   rC   r  ³  s    z)ShortImageReferenceInlineProcessor.evalIdNr  rB   rB   rB   rC   r4   ±  s   r4   c                   @  s"   e Zd ZdZddddœdd„ZdS )	r5   zD Return a link Element given an auto-link (`<http://example/com>`). re   rE   rž   r„   c                 C  sJ   t  d¡}| d|  | d¡¡¡ t | d¡¡|_|| d¡| 	d¡fS )zI Return an `a` [`Element`][xml.etree.ElementTree.Element] of `group(1)`. ré   rê   r   r   )
rš   r›   rî   r   rq   r   r¦   rm   rŒ   r   rŸ   rB   rB   rC   ri   »  s    
z#AutolinkInlineProcessor.handleMatchNrŠ   rB   rB   rB   rC   r5   ¹  s   r5   c                   @  s"   e Zd ZdZddddœdd„ZdS )	r7   zW
    Return a `mailto` link Element given an auto-mail link (`<foo@example.com>`).
    re   rE   rž   r„   c                   s¦   t  d¡}|  | d¡¡}| d¡r4|tdƒd… }dddœdd	„‰ ‡ fd
d„|D ƒ}t d |¡¡|_	d| }d dd„ |D ƒ¡}| 
d|¡ || d¡| d¡fS )za Return an [`Element`][xml.etree.ElementTree.Element] containing a `mailto` link  of `group(1)`. ré   r   zmailto:Nr¾   rE   )r¤   r   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	   Úcodepoint2namerr   r   r   ÚAMP_SUBSTITUTE)r¤   r    rB   rB   rC   r  Î  s    z;AutomailInlineProcessor.handleMatch.<locals>.codepoint2namec                   s   g | ]}ˆ t |ƒƒ‘qS rB   )r‘   ©Ú.0Úletter©r  rB   rC   Ú
<listcomp>Ö  s     z7AutomailInlineProcessor.handleMatch.<locals>.<listcomp>rp   c                 S  s   g | ]}t jd t|ƒ  ‘qS )z#%d;)r   r  r‘   r  rB   rB   rC   r  Ú  s   ÿÿrê   r   )rš   r›   r   rq   rJ   r°   r   r¦   rt   rm   rî   rŒ   r   )ra   rg   r…   rœ   ÚemailÚlettersÚmailtorB   r  rC   ri   Ç  s    


ÿz#AutomailInlineProcessor.handleMatchNrŠ   rB   rB   rB   rC   r7   Ã  s   r7   )CrU   Ú
__future__r   rp   r   Útypingr   r   r   r   r\   Úxml.etree.ElementTreerš   ÚElementTreer   r	   Zmarkdownr
   rD   ZNOIMGr(   r*   râ   rá   ræ   rç   rå   rÞ   rã   rß   rä   rà   r.   r0   r,   r2   r?   r6   r8   r<   r=   r:   rL   rM   rW   rƒ   rˆ   r>   r)   r”   r   r    r9   r'   r©   r·   r;   r@   rA   r-   r/   r+   r3   r1   r4   r5   r7   rB   rB   rB   rC   Ú<module>   sv   0	V0'  #8
