o
    ©i e“  ã                   @  sÀ  d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	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w erIdd	lmZ 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*„Z-G d+d,„ d,ed,g d-¢ƒƒZ.G d.d/„ d/ƒZ/G d0d1„ d1e/ƒZ0G d2d3„ d3e/ƒZ1G d4d5„ d5e0ƒZ2G d6d7„ d7e0ƒZ3G d8d9„ d9e/ƒZ4G d:d;„ d;e0ƒZ5G d<d=„ d=e4ƒZ6G d>d?„ d?e5ƒZ7G d@dA„ dAe0ƒZ8G dBdC„ dCe4ƒZ9G dDdE„ dEe5ƒZ:G dFdG„ dGe0ƒZ;G dHdI„ dIe0ƒZ<G dJdK„ dKe<ƒZ=G dLdM„ dMe0ƒZ>G dNdO„ dOe>ƒZ?G dPdQ„ dQe>ƒZ@G dRdS„ dSe@ƒZAG dTdU„ dUe@ƒZBG dVdW„ dWeBƒZCG dXdY„ dYe0ƒZDG dZd[„ d[e0ƒZEdS )^aB  
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)Ú
namedtuple)ÚTYPE_CHECKINGÚMatchÚAnyN)Úentities)ÚMarkdownÚmdr
   Úkwargsr   Úreturnúutil.Registryc                 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é    Ú
image_linké–   Úimage_referenceéŒ   Úshort_referenceé‚   Úshort_image_refé}   Úautolinkéx   Úautomailén   ÚbrÚ	linebreakéd   ÚhtmléZ   ÚentityéP   Ú
not_strongéF   z\*Ú	em_strongé<   Ú_Ú
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)r   r   ÚinlinePatterns© rO   úF/var/www/myenv/lib/python3.10/site-packages/markdown/inlinepatterns.pyÚbuild_inlinepatterns9   s0   ÿÿÿrQ   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ÚstringÚstrc                 C  s8   |   d¡r
|  d¡s|   d¡r|  d¡r| dd… S | S )z#Remove quotes from around a string.ú"ú'r   éÿÿÿÿ)Ú
startswithÚendswith)rR   rO   rO   rP   Údequote¯   s   ÿÿrY   c                   @  s   e Zd ZdZdS )ÚEmStrongItemzEmphasis/strong pattern item.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rO   rO   rO   rP   rZ   ¸   s    rZ   )ÚpatternÚbuilderÚtagsc                   @  sL   e Zd ZdZeƒ Z	 dddd„Zddd„Zddd„Zddd„Z	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.

    Nr_   rS   r   úMarkdown | Nonec                 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)r_   ÚreÚcompileÚDOTALLÚUNICODEÚcompiled_rer   ©Úselfr_   r   rO   rO   rP   Ú__init__Ú   s
   

ÿ
zPattern.__init__r   ú
re.Patternc                 C  s   | j S )z' Return a compiled regular expression. )rh   ©rj   rO   rO   rP   ÚgetCompiledRegExpë   s   zPattern.getCompiledRegExpÚmr   úetree.Elementc                 C  ó   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.

        NrO   ©rj   ro   rO   rO   rP   ÚhandleMatchï   s   zPattern.handleMatchc                 C  s   | j jS )z+ Return class name, to define pattern type )Ú	__class__r[   rm   rO   rO   rP   Útypeü   s   zPattern.typeÚtextc                   sD   z	| j jd j‰ W n ty   | Y S w ‡ fdd„}tj ||¡S )ú> Return unescaped text given text with an inline placeholder. Úinlinec                   s<   |   d¡}|ˆ v rˆ  |¡}t|tƒr|S d | ¡ ¡S d S )Nr   Ú )ÚgroupÚgetÚ
isinstancerS   ÚjoinÚitertext©ro   ÚidÚvalue©ÚstashrO   rP   Ú	get_stash  s   


úz#Pattern.unescape.<locals>.get_stash©r   ÚtreeprocessorsÚstashed_nodesÚKeyErrorr   ÚINLINE_PLACEHOLDER_REÚsub©rj   rv   r„   rO   r‚   rP   Úunescape   s   ÿ	zPattern.unescape©N©r_   rS   r   rc   )r   rl   ©ro   r   r   rp   )r   rS   )rv   rS   r   rS   )r[   r\   r]   r^   ÚtupleÚANCESTOR_EXCLUDESrk   rn   rs   ru   rŒ   rO   rO   rO   rP   rb   À   s    


rb   c                   @  s&   e Zd ZdZdddd„Z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.

    Nr_   rS   r   rc   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)r_   rd   re   rf   rg   rh   Ú	safe_moder   ri   rO   rO   rP   rk     s   

zInlineProcessor.__init__ro   r   Údatar   ú9tuple[etree.Element | str | None, int | None, int | None]c                 C  rq   )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.

        NrO   ©rj   ro   r”   rO   rO   rP   rs   -  s   zInlineProcessor.handleMatchr   rŽ   )ro   r   r”   rS   r   r•   ©r[   r\   r]   r^   rk   rs   rO   rO   rO   rP   r’     s    r’   c                   @  ó   e Zd ZdZd	dd„ZdS )
ÚSimpleTextPatternz2 Return a simple text of `group(2)` of a Pattern. ro   r   r   rS   c                 C  s
   |  d¡S )z< Return string content of `group(2)` of a matching pattern. é   )rz   rr   rO   rO   rP   rs   E  s   
zSimpleTextPattern.handleMatchN)ro   r   r   rS   ©r[   r\   r]   r^   rs   rO   rO   rO   rP   r™   C  ó    r™   c                   @  ó   e Zd ZdZddd	„Zd
S )rJ   z2 Return a simple text of `group(1)` of a Pattern. ro   r   r”   rS   r   útuple[str, int, int]c                 C  s   |  d¡| d¡| d¡fS )z< Return string content of `group(1)` of a matching pattern. r   r   )rz   ÚstartÚendr–   rO   rO   rP   rs   L  s   z%SimpleTextInlineProcessor.handleMatchN©ro   r   r”   rS   r   rž   r›   rO   rO   rO   rP   rJ   J  rœ   rJ   c                   @  r   )r5   z Return an escaped character. ro   r   r”   rS   r   útuple[str | None, int, int]c                 C  sT   |  d¡}|| jjv rd tjt|ƒtj¡| d¡| 	d¡fS d| d¡| 	d¡f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)
rz   r   ÚESCAPED_CHARSÚformatr   ÚSTXÚordÚETXrŸ   r    )rj   ro   r”   ÚcharrO   rO   rP   rs   T  s   
	(z!EscapeInlineProcessor.handleMatchN)ro   r   r”   rS   r   r¢   r›   rO   rO   rO   rP   r5   Q  ó    r5   c                   @  s$   e Zd ZdZddd„Zddd„ZdS )ÚSimpleTagPatternz]
    Return element of type `tag` with a text attribute of `group(3)`
    of a Pattern.

    r_   rS   Útagc                 C  ó   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)rb   rk   r¬   ©rj   r_   r¬   rO   rO   rP   rk   j  ó   	zSimpleTagPattern.__init__ro   r   r   rp   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¬   rz   rv   )rj   ro   ÚelrO   rO   rP   rs   w  s   zSimpleTagPattern.handleMatchN©r_   rS   r¬   rS   r   r—   rO   rO   rO   rP   r«   d  ó    
r«   c                   @  s$   e Zd ZdZddd„Zddd„ZdS )ÚSimpleTagInlineProcessorz]
    Return element of type `tag` with a text attribute of `group(2)`
    of a Pattern.

    r_   rS   r¬   c                 C  r­   )z·
        Create an instant of an simple tag processor.

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

        N)r’   rk   r¬   r®   rO   rO   rP   rk   ‡  r¯   z!SimpleTagInlineProcessor.__init__ro   r   r”   r   útuple[etree.Element, int, int]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¬   rz   rv   rŸ   r    ©rj   ro   r”   r³   rO   rO   rP   rs   ”  s   z$SimpleTagInlineProcessor.handleMatchNr´   ©ro   r   r”   rS   r   r·   r—   rO   rO   rO   rP   r¶     rµ   r¶   c                   @  r˜   )
ÚSubstituteTagPatternú3 Return an element of type `tag` with no children. ro   r   r   rp   c                 C  s   t  | j¡S )úH Return empty [`Element`][xml.etree.ElementTree.Element] of type `tag`. )r±   r²   r¬   rr   rO   rO   rP   rs      s   z SubstituteTagPattern.handleMatchNr   r›   rO   rO   rO   rP   rº   ž  rœ   rº   c                   @  r   )rE   r»   ro   r   r”   rS   r   r·   c                 C  s   t  | j¡| d¡| d¡fS )r¼   r   )r±   r²   r¬   rŸ   r    r–   rO   rO   rP   rs   §  s   z(SubstituteTagInlineProcessor.handleMatchNr¹   r›   rO   rO   rO   rP   rE   ¥  rœ   rE   c                   @  s"   e Zd ZdZdd„ Zdd
d„ZdS )r3   zA Return a `<code>` element containing the escaped matching text. c                 C  s.   t  | |¡ d tjtdƒtj¡| _d| _d S )Nr£   ú\Úcode)	r’   rk   r¥   r   r¦   r§   r¨   ÚESCAPED_BSLASHr¬   )rj   r_   rO   rO   rP   rk   ®  s   z BacktickInlineProcessor.__init__ro   r   r”   rS   r   ú$tuple[etree.Element | str, int, int]c                 C  sn   |  d¡r$t | j¡}t t |  d¡ ¡ ¡¡|_|| 	d¡| 
d¡fS |  d¡ d| j¡| 	d¡| 
d¡f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\\)rz   r±   r²   r¬   r   ÚAtomicStringÚcode_escapeÚstriprv   rŸ   r    Úreplacer¿   r¸   rO   rO   rP   rs   ´  s
   
	&z#BacktickInlineProcessor.handleMatchN)ro   r   r”   rS   r   rÀ   r—   rO   rO   rO   rP   r3   ¬  s    r3   c                   @  r˜   )
ÚDoubleTagPatternúfReturn a ElementTree element nested in tag2 nested in tag1.

    Useful for strong emphasis etc.

    ro   r   r   rp   c                 C  sR   | j  d¡\}}t |¡}t ||¡}| d¡|_t| ¡ ƒdkr'| 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²   Ú
SubElementrz   rv   ÚlenÚgroupsÚtail)rj   ro   Útag1Útag2Úel1Úel2rO   rO   rP   rs   Ë  s   
zDoubleTagPattern.handleMatchNr   r›   rO   rO   rO   rP   rÅ   Å  ó    rÅ   c                   @  r   )ÚDoubleTagInlineProcessorrÆ   ro   r   r”   rS   r   r·   c                 C  sd   | j  d¡\}}t |¡}t ||¡}| d¡|_t| ¡ ƒdkr'| 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Ë   rz   rv   rÌ   rÍ   rÎ   rŸ   r    )rj   ro   r”   rÏ   rÐ   rÑ   rÒ   rO   rO   rP   rs   à  s   
z$DoubleTagInlineProcessor.handleMatchNr¹   r›   rO   rO   rO   rP   rÔ   Ú  rÓ   rÔ   c                   @  s*   e Zd ZdZddd	„Zd
d„ Zdd„ ZdS )rG   z1 Store raw inline html and return a placeholder. ro   r   r”   rS   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Œ   rz   r   Ú	htmlStashÚstorerŸ   r    )rj   ro   r”   ÚrawhtmlÚplace_holderrO   rO   rP   rs   ñ  s   zHtmlInlineProcessor.handleMatchc                   sF   z	ˆ j jd j‰W n ty   | Y S w ‡ ‡fdd„}tj ||¡S )rw   rx   c                   sJ   |   d¡}ˆ |¡}|d ur#zˆ j |¡W S  ty"   d|  Y S w d S )Nr   z\%s)rz   r{   r   Ú
serializerÚ	Exceptionr   ©rj   rƒ   rO   rP   r„   þ  s   

ÿýz/HtmlInlineProcessor.unescape.<locals>.get_stashr…   r‹   rO   rÜ   rP   rŒ   ÷  s   ÿ	zHtmlInlineProcessor.unescapec                 C  s>   z	| j jd j}W n ty   | Y S w dd„ }| ||¡S )zG Return text with backslash escapes undone (backslashes are restored). rŒ   c                 S  s   t t|  d¡ƒƒS )Nr   )ÚchrÚintrz   )ro   rO   rO   rP   Ú	_unescape  s   z9HtmlInlineProcessor.backslash_unescape.<locals>._unescape)r   r†   ÚRErˆ   rŠ   )rj   rv   rà   rß   rO   rO   rP   rÕ   	  s   ÿz&HtmlInlineProcessor.backslash_unescapeNr¡   )r[   r\   r]   r^   rs   rŒ   rÕ   rO   rO   rO   rP   rG   ï  s
    
rG   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d„ZdS )rL   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²   rz   Úparse_sub_patterns)rj   ro   r¬   ÚidxrÑ   rv   rO   rO   rP   Úbuild_single"  s   

zAsteriskProcessor.build_singlec           	      C  st   |  d¡\}}t |¡}t |¡}| d¡}|  ||d|¡ | |¡ t| ¡ ƒdkr8| d¡}|  ||||¡ |S )zReturn double tag.rÇ   rš   Nr°   )rÊ   r±   r²   rz   rè   ÚappendrÌ   rÍ   ©	rj   ro   ra   ré   rÏ   rÐ   rÑ   rÒ   rv   rO   rO   rP   Ú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²   rz   rè   rë   rì   rO   rO   rP   Úbuild_double27  s   




zAsteriskProcessor.build_double2c                 C  s  d}d}t |ƒ}||k rl| j ||¡rdd}t| jƒD ]B\}	}
|	|kr#q|
j ||¡}|r\||| d¡… }|rB|dur?||_n||_|  	||
j
|
j|	¡}| |¡ |}| d¡ }}d}q|sc|d7 }n|d7 }||k s||d… }|r‚|dur}||_dS ||_dS 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Ì   rh   ÚmatchÚ	enumerateÚPATTERNSr_   rŸ   rÎ   rv   Úbuild_elementr`   ra   rë   r    )rj   r”   ÚparentÚlastré   ÚoffsetÚposÚlengthÚmatchedÚindexÚitemro   rv   r³   rO   rO   rP   rè   D  sB   
€€à#

üz$AsteriskProcessor.parse_sub_patternsc                 C  s:   |dkr|   |||¡S |dkr|  |||¡S |  |||¡S )zElement builder.rä   rá   )rî   rí   rê   )rj   ro   r`   ra   rù   rO   rO   rP   rò   „  s
   zAsteriskProcessor.build_elementro   r   r”   rS   r   r·   c           	      C  sn   d}d}d}t | jƒD ]&\}}|j || d¡¡}|r1| d¡}| d¡}|  ||j|j|¡} nq|||fS )zParse patterns.Nr   )	rð   rñ   r_   rï   rŸ   r    rò   r`   ra   )	rj   ro   r”   r³   rŸ   r    rù   rú   Úm1rO   rO   rP   rs   Ž  s   

ü
zAsteriskProcessor.handleMatchNr¹   )r[   r\   r]   r^   rZ   rd   re   ÚEM_STRONG_RErf   rg   ÚSTRONG_EM_REÚSTRONG_EM3_REÚ	STRONG_REÚEMPHASIS_RErñ   rê   rí   rî   rè   rò   rs   rO   rO   rO   rP   rL     s    û@
rL   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 )
rM   zIEmphasis processor for handling strong and em matches inside underscores.rá   râ   rã   rä   rå   ræ   rç   N)r[   r\   r]   r^   rZ   rd   re   ÚEM_STRONG2_RErf   rg   ÚSTRONG_EM2_REÚSMART_STRONG_EM_REÚSMART_STRONG_REÚSMART_EMPHASIS_RErñ   rO   rO   rO   rP   rM   Ÿ  s    ûrM   c                   @  sH   e Zd ZdZe dejejB ¡Ze d¡Z	dd
d„Z
dd„ Zdd„ ZdS )r9   z- Return a link element from the given match. z2\(\s*(?:(<[^<>]*>)\s*(?:('[^']*'|"[^"]*")\s*)?\))?z\sro   r   r”   rS   r   ú3tuple[etree.Element | None, int | None, int | None]c           	      C  s|   |   || d¡¡\}}}|sdS |  ||¡\}}}}|sdS t d¡}||_| d|¡ |dur6| 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²   rv   ÚsetrŸ   )	rj   ro   r”   rv   rù   Úhandledr	  r
  r³   rO   rO   rP   rs   ±  s   
zLinkInlineProcessor.handleMatchc                 C  sÐ  d}d}d}| j j||d}|r7| d¡r7| d¡dd…  ¡ }| d¡r.| d¡dd… }| d¡}d	}n|rHd}d}| ¡ }	|	}d}
d}d}d}d}d}d}d}d}t|t|ƒƒD ]Ð}|| }|d
krw|sn|d7 }nk|dkrv|d8 }nb|dkr¦|dkrƒ||ks‹|dkrŽ||krŽd}nK|s•|d8 }nD|dkr¥|d8 }|dkr¥|d }
n3|dv rÙ|s¹d	}|}d}|d }|}n ||krÆ|sÆ|d }|}n||krÏ|d }n
|rÙ||krÙ|d }|d7 }|dkr&|dkrþ||krþ||	|d … }d |||d … ¡}n&|dkr||kr||	|d … }d |||d … ¡}n||	|d … } n|dkr-|}q]|dkrD|dkrD||	|
d … }|
}d}|dk}|dur[| j 	dt
|  | ¡ ¡ƒ¡}|  |¡ ¡ }||||fS )z?Parse data between `()` of `[Text]()` allowing recursive `()`. ry   NF©rö   r   rV   rš   r   Tú(ú))rU   rT   ú )ÚRE_LINKrï   rz   rÃ   r    ÚrangerÌ   r}   ÚRE_TITLE_CLEANrŠ   rY   rŒ   )rj   r”   rù   r	  r
  r  ro   Úbracket_countÚbacktrack_countÚstart_indexÚlast_bracketÚquoteÚstart_quoteÚ
exit_quoteÚignore_matchesÚ	alt_quoteÚstart_alt_quoteÚexit_alt_quoterô   rö   ÚcrO   rO   rP   r  Æ  sœ   


€ 
€


€
zLinkInlineProcessor.getLinkc                 C  sx   d}g }t |t|ƒƒD ]&}|| }|dkr|d8 }n|dkr"|d7 }|d7 }|dkr, n| |¡ qd |¡||dkfS )zsParse the content between `[]` of the start of an image or link
        resolving nested square brackets.

        r   ú]ú[r   ry   )r  rÌ   rë   r}   )rj   r”   rù   r  rv   rö   r!  rO   rO   rP   r  :  s   
zLinkInlineProcessor.getTextN©ro   r   r”   rS   r   r  )r[   r\   r]   r^   rd   re   rf   rg   r  r  rs   r  r  rO   rO   rO   rP   r9   ¬  s    

tr9   c                   @  r   )r;   z. Return a `img` element from the given match. ro   r   r”   rS   r   r  c           	      C  sˆ   |   || d¡¡\}}}|sdS |  ||¡\}}}}|sdS t d¡}| d|¡ |dur3| 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Ÿ   )	rj   ro   r”   rv   rù   r  r&  r
  r³   rO   rO   rP   rs   Q  s   
z ImageInlineProcessor.handleMatchNr$  r›   rO   rO   rO   rP   r;   N  rª   r;   c                   @  sN   e Zd ZdZe dej¡Ze dejej	B ¡Z
dd
d„Zdd„ Zddd„ZdS )r7   z6 Match to a stored reference and return link element. z\s+z\s?\[([^\]]*)\]ro   r   r”   rS   r   r  c           
      C  s   |   || d¡¡\}}}|sdS |  |||¡\}}}|sdS | j d|¡}|| jjvr3d| 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   Ú
referencesrŸ   ÚmakeTag)
rj   ro   r”   rv   rù   r  r€   r    r	  r
  rO   rO   rP   rs   l  s   z$ReferenceInlineProcessor.handleMatchc                 C  sL   | j j||d}|sd|dfS | d¡ ¡ }| d¡}|s!| ¡ }||dfS )z\
        Evaluate the id portion of `[ref][id]`.

        If `[ref][]` use `[ref]`.
        r  NFr   r   T)r  rï   rz   Úlowerr    )rj   r”   rù   rv   ro   r€   r    rO   rO   rP   r(  ‚  s   


zReferenceInlineProcessor.evalIdr	  r
  rv   rp   c                 C  s0   t  d¡}| d|¡ |r| d|¡ ||_|S )z; Return an `a` [`Element`][xml.etree.ElementTree.Element]. r  r	  r
  )r±   r²   r  rv   ©rj   r	  r
  rv   r³   rO   rO   rP   r+  ’  s   
z ReferenceInlineProcessor.makeTagNr$  ©r	  rS   r
  rS   rv   rS   r   rp   )r[   r\   r]   r^   rd   re   Ú	MULTILINEr)  rf   rg   r  rs   r(  r+  rO   rO   rO   rP   r7   f  s    
r7   c                   @  ó   e Zd ZdZdd„ ZdS )r?   z%Short form of reference: `[google]`. c                 C  ó   |  ¡ |dfS ©zEvaluate the id of `[ref]`.  T©r,  ©rj   r”   rù   rv   rO   rO   rP   r(     ó   z$ShortReferenceInlineProcessor.evalIdN©r[   r\   r]   r^   r(  rO   rO   rO   rP   r?   ž  ó    r?   c                   @  r   )r=   z7 Match to a stored reference and return `img` element. r	  rS   r
  rv   r   rp   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-  rO   rO   rP   r+  ¨  s   
z%ImageReferenceInlineProcessor.makeTagNr.  )r[   r\   r]   r^   r+  rO   rO   rO   rP   r=   ¦  rœ   r=   c                   @  r0  )r@   z* Short form of image reference: `![ref]`. c                 C  r1  r2  r3  r4  rO   rO   rP   r(  ´  r5  z)ShortImageReferenceInlineProcessor.evalIdNr6  rO   rO   rO   rP   r@   ²  r7  r@   c                   @  r   )rA   zD Return a link Element given an auto-link (`<http://example/com>`). ro   r   r”   rS   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Œ   rz   r   rÁ   rv   rŸ   r    r¸   rO   rO   rP   rs   ¼  s   
z#AutolinkInlineProcessor.handleMatchNr¹   r›   rO   rO   rO   rP   rA   º  rœ   rA   c                   @  r   )rC   zW
    Return a `mailto` link Element given an auto-mail link (`<foo@example.com>`).
    ro   r   r”   rS   r   r·   c                   sž   t  d¡}|  | d¡¡}| d¡r|t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:Nc                 S  s,   t j | ¡}|rd tj|¡S dtj| f S )z=Return entity definition by code, or the code if not defined.z{}{};z%s#%d;)r	   Úcodepoint2namer{   r¥   r   ÚAMP_SUBSTITUTE)r¾   r(   rO   rO   rP   r8  Ï  s   z;AutomailInlineProcessor.handleMatch.<locals>.codepoint2namec                   s   g | ]}ˆ t |ƒƒ‘qS rO   )r§   ©Ú.0Úletter©r8  rO   rP   Ú
<listcomp>×  s    z7AutomailInlineProcessor.handleMatch.<locals>.<listcomp>ry   c                 S  s   g | ]}t jd t|ƒ  ‘qS )z#%d;)r   r9  r§   r:  rO   rO   rP   r>  Û  s
    ÿ
ÿr	  r   )r±   r²   rŒ   rz   rW   rÌ   r   rÁ   r}   rv   r  rŸ   r    )rj   ro   r”   r³   ÚemailÚlettersÚmailtorO   r=  rP   rs   È  s   


ÿz#AutomailInlineProcessor.handleMatchNr¹   r›   rO   rO   rO   rP   rC   Ä  s    rC   )r   r
   r   r   r   r   )rR   rS   r   rS   )Fr^   Ú
__future__r   ry   r   Úcollectionsr   Útypingr   r   r   rd   Úxml.etree.ElementTreer±   ÚElementTreer&   r	   ÚImportErrorÚhtmlentitydefsÚmarkdownr
   rQ   ÚNOIMGr4   r6   r   rÿ   r  r  r  rü   r  rý   r  rþ   r:   r<   r8   r>   rK   rB   rD   rH   rI   rF   rY   rZ   rb   r’   r™   rJ   r5   r«   r¶   rº   rE   r3   rÅ   rÔ   rG   rL   rM   r9   r;   r7   r?   r=   r@   rA   rC   rO   rO   rO   rP   Ú<module>   s°   ÿ
0
	S0' 
 #8
