o
    ñ+óep•  ã                   @  sŽ  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r5ddlmZ dZd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	ƒZ+G d,d-„ d-ƒZ,G d.d/„ d/e,ƒZ-G d0d1„ d1e,ƒZ.G d2d3„ d3e-ƒZ/G d4d5„ d5e-ƒZ0G d6d7„ d7e,ƒZ1G d8d9„ d9e-ƒZ2G d:d;„ d;e1ƒZ3G d<d=„ d=e2ƒZ4G d>d?„ d?e-ƒZ5G d@dA„ dAe1ƒZ6G dBdC„ dCe2ƒZ7G dDdE„ dEe-ƒZ8G dFdG„ dGe-ƒZ9G dHdI„ dIe9ƒZ:G dJdK„ dKe-ƒZ;G dLdM„ dMe;ƒZ<G dNdO„ dOe;ƒZ=G dPdQ„ dQe=ƒZ>G dRdS„ dSe=ƒZ?G dTdU„ dUe?ƒZ@G dVdW„ dWe-ƒZAG dXdY„ dYe-ƒZBd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)ÚTYPE_CHECKINGÚAnyÚ
CollectionÚ
NamedTupleN)Úentities)ÚMarkdownÚmdr
   Úkwargsr   Úreturnúutil.Registry[InlineProcessor]c                 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.

    Zbacktické¾   Úescapeé´   Z	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© rA   ú7lib/python3.10/site-packages/markdown/inlinepatterns.pyÚbuild_inlinepatterns5   s0   ÿÿÿrC   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)rD   rA   rA   rB   Údequote«   s   ÿÿrK   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__rA   rA   rA   rB   rL   ´   s
   
 rL   c                   @  sf   e Zd ZU dZeƒ Zded< 	 ded< ded< 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.

    zCollection[str]ÚANCESTOR_EXCLUDESrM   Úcompiled_reúMarkdown | Noner   NrN   rE   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)rN   ÚreÚcompileÚDOTALLÚUNICODErX   r   ©ÚselfrN   r   rA   rA   rB   Ú__init__Ü   s
   

ÿ
zPattern.__init__r   ú
re.Patternc                 C  s   | j S )z' Return a compiled regular expression. )rX   ©r_   rA   rA   rB   ÚgetCompiledRegExpí   s   zPattern.getCompiledRegExpÚmúre.Match[str]úetree.Element | strc                 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.

        NrA   ©r_   rd   rA   rA   rB   ÚhandleMatchñ   s   zPattern.handleMatchc                 C  s   | j jS )z+ Return class name, to define pattern type )Ú	__class__rQ   rb   rA   rA   rB   Ú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Ú
isinstancerE   ÚjoinZitertext©rd   ÚidÚvalue©ÚstashrA   rB   Ú	get_stash	  s   


úz#Pattern.unescape.<locals>.get_stash©r   ÚtreeprocessorsZstashed_nodesÚKeyErrorr   ZINLINE_PLACEHOLDER_REÚsub©r_   rl   ry   rA   rw   rB   Úunescape  s   ÿ	zPattern.unescape©N©rN   rE   r   rY   )r   ra   )rd   re   r   rf   )r   rE   ©rl   rE   r   rE   )rQ   rR   rS   rT   ÚtuplerW   rU   r`   rc   ri   rk   r   rA   rA   rA   rB   rV   ¿   s   
 


rV   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.

    NrN   rE   r   rY   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)rN   rZ   r[   r\   r]   rX   Z	safe_moder   r^   rA   rA   rB   r`     s   

zInlineProcessor.__init__rd   re   Údatar   ú9tuple[etree.Element | str | None, int | None, int | None]c                 C  rg   )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.

        NrA   ©r_   rd   r…   rA   rA   rB   ri   /  s   zInlineProcessor.handleMatchr€   r   )rd   re   r…   rE   r   r†   ©rQ   rR   rS   rT   r`   ri   rA   rA   rA   rB   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. rd   re   r   rE   c                 C  s
   |  d¡S )z< Return string content of `group(2)` of a matching pattern. é   )rp   rh   rA   rA   rB   ri   G  s   
zSimpleTextPattern.handleMatchN©rd   re   r   rE   ©rQ   rR   rS   rT   ri   rA   rA   rA   rB   rŠ   E  ó    rŠ   c                   @  ó   e Zd ZdZddd	„Zd
S )r=   z2 Return a simple text of `group(1)` of a Pattern. rd   re   r…   rE   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   )rp   ÚstartÚendr‡   rA   rA   rB   ri   N  s   z%SimpleTextInlineProcessor.handleMatchN©rd   re   r…   rE   r   r   r   rA   rA   rA   rB   r=   L  rŽ   r=   c                   @  r   )r(   z Return an escaped character. rd   re   r…   rE   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)
rp   r   ZESCAPED_CHARSÚformatr   ÚSTXÚordÚETXr‘   r’   )r_   rd   r…   ÚcharrA   rA   rB   ri   V  s   
	(z!EscapeInlineProcessor.handleMatchN)rd   re   r…   rE   r   r”   r   rA   rA   rA   rB   r(   S  ó    r(   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.

    rN   rE   Ú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)rV   r`   r   ©r_   rN   r   rA   rA   rB   r`   l  ó   	zSimpleTagPattern.__init__rd   re   r   úetree.Elementc                 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   rp   rl   )r_   rd   ÚelrA   rA   rB   ri   y  s   zSimpleTagPattern.handleMatchN©rN   rE   r   rE   ©rd   re   r   r¡   rˆ   rA   rA   rA   rB   rœ   f  ó    
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.

    rN   rE   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„   r`   r   rŸ   rA   rA   rB   r`   ‰  r    z!SimpleTagInlineProcessor.__init__rd   re   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   rp   rl   r‘   r’   ©r_   rd   r…   r¥   rA   rA   rB   ri   –  s   z$SimpleTagInlineProcessor.handleMatchNr¦   ©rd   re   r…   rE   r   rª   rˆ   rA   rA   rA   rB   r©   ƒ  r¨   r©   c                   @  r‰   )
ÚSubstituteTagPatternú3 Return an element of type `tag` with no children. rd   re   r   r¡   c                 C  s   t  | j¡S )úH Return empty [`Element`][xml.etree.ElementTree.Element] of type `tag`. )r£   r¤   r   rh   rA   rA   rB   ri   ¢  s   z SubstituteTagPattern.handleMatchNr§   r   rA   rA   rA   rB   r­      rŽ   r­   c                   @  r   )r8   r®   rd   re   r…   rE   r   rª   c                 C  s   t  | j¡| d¡| d¡fS )r¯   r   )r£   r¤   r   r‘   r’   r‡   rA   rA   rB   ri   ©  s   z(SubstituteTagInlineProcessor.handleMatchNr¬   r   rA   rA   rA   rB   r8   §  rŽ   r8   c                   @  s$   e Zd ZdZddd„Zddd„ZdS )r&   zA Return a `<code>` element containing the escaped matching text. rN   rE   c                 C  s.   t  | |¡ d tjtdƒtj¡| _d| _d S )Nr•   ú\Úcode)	r„   r`   r–   r   r—   r˜   r™   ÚESCAPED_BSLASHr   )r_   rN   rA   rA   rB   r`   °  s   z BacktickInlineProcessor.__init__rd   re   r…   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\\)rp   r£   r¤   r   r   ÚAtomicStringZcode_escapeÚstriprl   r‘   r’   Úreplacer²   r«   rA   rA   rB   ri   ¶  s
   
	&z#BacktickInlineProcessor.handleMatchN)rN   rE   )rd   re   r…   rE   r   r³   rˆ   rA   rA   rA   rB   r&   ®  s    
r&   c                   @  r‰   )
ÚDoubleTagPatternúfReturn a ElementTree element nested in tag2 nested in tag1.

    Useful for strong emphasis etc.

    rd   re   r   r¡   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¤   Ú
SubElementrp   rl   ÚlenÚgroupsÚtail)r_   rd   Útag1Útag2Úel1Úel2rA   rA   rB   ri   Í  s   
zDoubleTagPattern.handleMatchNr§   r   rA   rA   rA   rB   r·   Ç  ó    r·   c                   @  r   )ÚDoubleTagInlineProcessorr¸   rd   re   r…   rE   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½   rp   rl   r¾   r¿   rÀ   r‘   r’   )r_   rd   r…   rÁ   rÂ   rÃ   rÄ   rA   rA   rB   ri   â  s   
z$DoubleTagInlineProcessor.handleMatchNr¬   r   rA   rA   rA   rB   rÆ   Ü  rÅ   rÆ   c                   @  s.   e Zd ZdZddd	„Zddd„Zddd„ZdS )r:   z1 Store raw inline html and return a placeholder. rd   re   r…   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   rp   r   Z	htmlStashÚstorer‘   r’   )r_   rd   r…   ZrawhtmlZplace_holderrA   rA   rB   ri   ó  s   zHtmlInlineProcessor.handleMatchrl   c                   sH   z	ˆ j jd j‰W n ty   | Y S w d	‡ ‡fdd„}tj ||¡S )
rm   rn   rd   re   r   rE   c                   sJ   |   d¡}ˆ |¡}|d ur#zˆ j |¡W S  ty"   d|  Y S w d S )Nr   z\%s)rp   rq   r   Z
serializerÚ	Exceptionrt   ©r_   rx   rA   rB   ry      s   

ÿýz/HtmlInlineProcessor.unescape.<locals>.get_stashNrŒ   rz   r~   rA   rÊ   rB   r   ù  s   ÿ	zHtmlInlineProcessor.unescapec                 C  s@   z	| j jd j}W n ty   | Y S w d	dd„}| ||¡S )
zG Return text with backslash escapes undone (backslashes are restored). r   rd   re   r   rE   c                 S  s   t t|  d¡ƒƒS )Nr   )ÚchrÚintrp   )rd   rA   rA   rB   Ú	_unescape  s   z9HtmlInlineProcessor.backslash_unescape.<locals>._unescapeNrŒ   )r   r{   ÚREr|   r}   )r_   rl   rÎ   rÍ   rA   rA   rB   rÇ     s   ÿ
z&HtmlInlineProcessor.backslash_unescapeNr“   r‚   )rQ   rR   rS   rT   ri   r   rÇ   rA   rA   rA   rB   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„Zd(dd„Zd(dd„Zd)dd„Zd*d!d"„Z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Úemrd   re   r   rE   ÚidxrÌ   r   r¡   c                 C  s(   t  |¡}| d¡}|  ||d|¡ |S )zReturn single tag.r‹   N)r£   r¤   rp   Úparse_sub_patterns)r_   rd   r   rÖ   rÃ   rl   rA   rA   rB   Úbuild_single$  s   

zAsteriskProcessor.build_singlerP   c           	      C  st   |  d¡\}}t |¡}t |¡}| d¡}|  ||d|¡ | |¡ t| ¡ ƒdkr8| d¡}|  ||||¡ |S )zReturn double tag.r¹   r‹   Nr¢   )r¼   r£   r¤   rp   r×   Úappendr¾   r¿   ©	r_   rd   rP   rÖ   rÁ   rÂ   rÃ   rÄ   rl   rA   rA   rB   Ú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¤   rp   r×   rÙ   rÚ   rA   rA   rB   Úbuild_double29  s   




zAsteriskProcessor.build_double2r…   ÚparentÚlastúetree.Element | NoneÚNonec                 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`: 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¾   rX   ÚmatchÚ	enumerateÚPATTERNSrN   r‘   rÀ   rl   Úbuild_elementrO   rP   rÙ   r’   )r_   r…   rÝ   rÞ   rÖ   ÚoffsetÚposÚlengthZmatchedÚindexÚitemrd   rl   r¥   rA   rA   rB   r×   F  sB   
€€à#

üz$AsteriskProcessor.parse_sub_patternsrO   rè   c                 C  s:   |dkr|   |||¡S |dkr|  |||¡S |  |||¡S )zElement builder.rÒ   rÏ   )rÜ   rÛ   rØ   )r_   rd   rO   rP   rè   rA   rA   rB   rä   ƒ  s
   zAsteriskProcessor.build_elementú3tuple[etree.Element | None, int | None, int | None]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ã   rN   rá   r‘   r’   rä   rO   rP   )	r_   rd   r…   r¥   r‘   r’   rè   ré   Zm1rA   rA   rB   ri     s   

ü
zAsteriskProcessor.handleMatchN)rd   re   r   rE   rÖ   rÌ   r   r¡   )rd   re   rP   rE   rÖ   rÌ   r   r¡   )
r…   rE   rÝ   r¡   rÞ   rß   rÖ   rÌ   r   rà   )
rd   re   rO   rE   rP   rE   rè   rÌ   r   r¡   ©rd   re   r…   rE   r   rê   )rQ   rR   rS   rT   rL   rZ   r[   ÚEM_STRONG_REr\   r]   ÚSTRONG_EM_REÚSTRONG_EM3_REÚ	STRONG_REÚEMPHASIS_RErã   rØ   rÛ   rÜ   r×   rä   ri   rA   rA   rA   rB   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 )
r@   zIEmphasis processor for handling strong and em matches inside underscores.rÏ   rÐ   rÑ   rÒ   rÓ   rÔ   rÕ   N)rQ   rR   rS   rT   rL   rZ   r[   ÚEM_STRONG2_REr\   r]   ÚSTRONG_EM2_REÚSMART_STRONG_EM_REÚSMART_STRONG_REÚSMART_EMPHASIS_RErã   rA   rA   rA   rB   r@   ž  s    ûr@   c                   @  sL   e Zd ZdZe dejejB ¡Ze d¡Z	dd
d„Z
ddd„Zddd„ZdS )r,   z- Return a link element from the given match. z2\(\s*(?:(<[^<>]*>)\s*(?:('[^']*'|"[^"]*")\s*)?\))?z\srd   re   r…   rE   r   rê   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¤   rl   Úsetr‘   )	r_   rd   r…   rl   rè   Úhandledrø   rù   r¥   rA   rA   rB   ri   °  s   
zLinkInlineProcessor.handleMatchrè   rÌ   ú!tuple[str, str | None, int, bool]c                 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 `()`. ro   NF©ræ   r   rH   r‹   r   Tú(ú))rG   rF   ú )ÚRE_LINKrá   rp   rµ   r’   Úranger¾   rs   ÚRE_TITLE_CLEANr}   rK   r   )r_   r…   rè   rø   rù   rý   rd   Úbracket_countZbacktrack_countÚstart_indexZlast_bracketÚquoteZstart_quoteZ
exit_quoteZignore_matchesZ	alt_quoteZstart_alt_quoteZexit_alt_quoterÞ   ræ   ÚcrA   rA   rB   rû   Å  sœ   


€ 
€


€
zLinkInlineProcessor.getLinkútuple[str, int, bool]c                 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   ro   )r  r¾   rÙ   rs   )r_   r…   rè   r  rl   ræ   r	  rA   rA   rB   rú   9  s   
zLinkInlineProcessor.getTextNrë   )r…   rE   rè   rÌ   r   rþ   )r…   rE   rè   rÌ   r   r
  )rQ   rR   rS   rT   rZ   r[   r\   r]   r  r  ri   rû   rú   rA   rA   rA   rB   r,   «  s    


tr,   c                   @  r   )r.   z. Return a `img` element from the given match. rd   re   r…   rE   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‘   )	r_   rd   r…   rl   rè   rý   r  rù   r¥   rA   rA   rB   ri   P  s   
z ImageInlineProcessor.handleMatchNrë   r   rA   rA   rA   rB   r.   M  r›   r.   c                   @  sP   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d„Zddd„ZdS )r*   z6 Match to a stored reference and return link element. z\s+z\s?\[([^\]]*)\]rd   re   r…   rE   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   Z
referencesr‘   ÚmakeTag)
r_   rd   r…   rl   rè   rý   ru   r’   rø   rù   rA   rA   rB   ri   k  s   z$ReferenceInlineProcessor.handleMatchrè   rÌ   rl   útuple[str | None, int, bool]c                 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á   rp   Úlowerr’   )r_   r…   rè   rl   rd   ru   r’   rA   rA   rB   r    s   


zReferenceInlineProcessor.evalIdrø   rù   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ü   rl   ©r_   rø   rù   rl   r¥   rA   rA   rB   r  ‘  s   
z ReferenceInlineProcessor.makeTagNrë   )r…   rE   rè   rÌ   rl   rE   r   r  ©rø   rE   rù   rE   rl   rE   r   r¡   )rQ   rR   rS   rT   rZ   r[   Ú	MULTILINEr  r\   r]   r  ri   r  r  rA   rA   rA   rB   r*   e  s    

r*   c                   @  ó   e Zd ZdZdd	d
„ZdS )r2   z%Short form of reference: `[google]`. r…   rE   rè   rÌ   rl   r   r
  c                 C  ó   |  ¡ |dfS ©zEvaluate the id of `[ref]`.  T©r  ©r_   r…   rè   rl   rA   rA   rB   r  Ÿ  ó   z$ShortReferenceInlineProcessor.evalIdN©r…   rE   rè   rÌ   rl   rE   r   r
  ©rQ   rR   rS   rT   r  rA   rA   rA   rB   r2     rŽ   r2   c                   @  r   )r0   z7 Match to a stored reference and return `img` element. rø   rE   rù   rl   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  rA   rA   rB   r  §  s   
z%ImageReferenceInlineProcessor.makeTagNr  )rQ   rR   rS   rT   r  rA   rA   rA   rB   r0   ¥  rŽ   r0   c                   @  r  )r3   z* Short form of image reference: `![ref]`. r…   rE   rè   rÌ   rl   r   r
  c                 C  r  r  r  r  rA   rA   rB   r  ³  r  z)ShortImageReferenceInlineProcessor.evalIdNr  r  rA   rA   rA   rB   r3   ±  rŽ   r3   c                   @  r   )r4   zD Return a link Element given an auto-link (`<http://example/com>`). rd   re   r…   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   rp   r   r´   rl   r‘   r’   r«   rA   rA   rB   ri   »  s   
z#AutolinkInlineProcessor.handleMatchNr¬   r   rA   rA   rA   rB   r4   ¹  rŽ   r4   c                   @  r   )r6   zW
    Return a `mailto` link Element given an auto-mail link (`<foo@example.com>`).
    rd   re   r…   rE   r   rª   c                   s    t  d¡}|  | d¡¡}| d¡r|t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±   rÌ   r   rE   c                 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	   Úcodepoint2namerq   r–   r   ÚAMP_SUBSTITUTE)r±   r   rA   rA   rB   r   Î  s   z;AutomailInlineProcessor.handleMatch.<locals>.codepoint2namec                   s   g | ]}ˆ t |ƒƒ‘qS rA   )r˜   ©Ú.0Zletter©r   rA   rB   Ú
<listcomp>Ö  s    z7AutomailInlineProcessor.handleMatch.<locals>.<listcomp>ro   c                 S  s   g | ]}t jd t|ƒ  ‘qS )z#%d;)r   r!  r˜   r"  rA   rA   rB   r%  Ú  s
    ÿ
ÿrø   r   )r±   rÌ   r   rE   )r£   r¤   r   rp   rI   r¾   r   r´   rs   rl   rü   r‘   r’   )r_   rd   r…   r¥   ZemailZlettersÚmailtorA   r$  rB   ri   Ç  s   



ÿz#AutomailInlineProcessor.handleMatchNr¬   r   rA   rA   rA   rB   r6   Ã  s    r6   )r   r
   r   r   r   r   )rD   rE   r   rE   )CrT   Z
__future__r   ro   r   Útypingr   r   r   r   rZ   Zxml.etree.ElementTreer£   ZElementTreer   r	   Zmarkdownr
   rC   ZNOIMGr'   r)   rð   rï   rô   rõ   ró   rì   rñ   rí   rò   rî   r-   r/   r+   r1   r>   r5   r7   r;   r<   r9   rK   rL   rV   r„   rŠ   r=   r(   rœ   r©   r­   r8   r&   r·   rÆ   r:   r?   r@   r,   r.   r*   r2   r0   r3   r4   r6   rA   rA   rA   rB   Ú<module>   s¦   
0
	V0'  #8
