o
    mĎi                     @  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.

    backtick   escape   	reference   link   
image_link   image_reference   short_reference   short_image_ref}   autolinkx   automailn   br	linebreakd   htmlZ   entityP   
not_strongF   z\*	em_strong<   _
em_strong22   )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   S/home/jeff/fluffinator/venv/lib/python3.10/site-packages/markdown/inlinepatterns.pybuild_inlinepatterns5   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][^<>@ ]*( [^<>]*)?|!--(?:(?!<!--|-->).)*--|[?](?:(?!<[?]|[?]>).)*[?]|!\[CDATA\[(?:(?!<!\[CDATA\[|\]\]>).)*\]\])>)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U dZded< ded< ded< dS )EmStrongItemzEmphasis/strong pattern item.re.Pattern[str]patternrS   buildertagsN)__name__
__module____qualname____doc____annotations__rO   rO   rO   rP   rZ      s
   
 rZ   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_EXCLUDESr[   compiled_reMarkdown | Noner   Nr\   rS   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)r\   recompileDOTALLUNICODErf   r   selfr\   r   rO   rO   rP   __init__   s
   


zPattern.__init__r   
re.Patternc                 C  s   | j S )z' Return a compiled regular expression. )rf   rm   rO   rO   rP   getCompiledRegExp   s   zPattern.getCompiledRegExpmre.Match[str]etree.Element | strc                 C     dS )zReturn 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   rm   rr   rO   rO   rP   handleMatch   s   zPattern.handleMatchc                 C  s   | j jS )z+ Return class name, to define pattern type )	__class__r_   rp   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rr   idvaluestashrO   rP   	get_stash  s   


z#Pattern.unescape.<locals>.get_stashr   treeprocessorsstashed_nodesKeyErrorr   INLINE_PLACEHOLDER_REsubrm   rz   r   rO   r   rP   unescape  s   	zPattern.unescapeNr\   rS   r   rg   )r   ro   )rr   rs   r   rt   )r   rS   rz   rS   r   rS   )r_   r`   ra   rb   tuplere   rc   rn   rq   rw   ry   r   rO   rO   rO   rP   rd      s   
 


rd   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   rg   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\   rh   ri   rj   rk   rf   	safe_moder   rl   rO   rO   rP   rn   $  s   

zInlineProcessor.__init__rr   rs   datar   9tuple[etree.Element | str | None, int | None, int | None]c                 C  ru   )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   rm   rr   r   rO   rO   rP   rw   5  s   zInlineProcessor.handleMatchr   r   )rr   rs   r   rS   r   r   r_   r`   ra   rb   rn   rw   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. rr   rs   r   rS   c                 C  s
   | dS )z< Return string content of `group(2)` of a matching pattern.    )r~   rv   rO   rO   rP   rw   M  s   
zSimpleTextPattern.handleMatchNrr   rs   r   rS   r_   r`   ra   rb   rw   rO   rO   rO   rP   r   K      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. rr   rs   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   )r~   startendr   rO   rO   rP   rw   T  s   z%SimpleTextInlineProcessor.handleMatchNrr   rs   r   rS   r   r   r   rO   rO   rO   rP   rJ   R  r   rJ   c                   @  r   )r5   z Return an escaped character. rr   rs   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)
r~   r   ESCAPED_CHARSformatr   STXordETXr   r   )rm   rr   r   charrO   rO   rP   rw   \  s   
	(z!EscapeInlineProcessor.handleMatchN)rr   rs   r   rS   r   r   r   rO   rO   rO   rP   r5   Y      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)rd   rn   r   rm   r\   r   rO   rO   rP   rn   r     	zSimpleTagPattern.__init__rr   rs   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   r~   rz   )rm   rr   elrO   rO   rP   rw     s   zSimpleTagPattern.handleMatchNr\   rS   r   rS   rr   rs   r   r   r   rO   rO   rO   rP   r   l      
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   rn   r   r   rO   rO   rP   rn     r   z!SimpleTagInlineProcessor.__init__rr   rs   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   r~   rz   r   r   rm   rr   r   r   rO   rO   rP   rw     s   z$SimpleTagInlineProcessor.handleMatchNr   rr   rs   r   rS   r   r   r   rO   rO   rO   rP   r     r   r   c                   @  r   )
SubstituteTagPattern3 Return an element of type `tag` with no children. rr   rs   r   r   c                 C  s   t | jS )H Return empty [`Element`][xml.etree.ElementTree.Element] of type `tag`. )r   r   r   rv   rO   rO   rP   rw     s   z SubstituteTagPattern.handleMatchNr   r   rO   rO   rO   rP   r     r   r   c                   @  r   )rE   r   rr   rs   r   rS   r   r   c                 C  s   t | j|d|dfS )r   r   )r   r   r   r   r   r   rO   rO   rP   rw     s   z(SubstituteTagInlineProcessor.handleMatchNr   r   rO   rO   rO   rP   rE     r   rE   c                   @  s$   e Zd ZdZdddZdddZdS )r3   zA Return a `<code>` element containing the escaped matching text. r\   rS   c                 C  s.   t | | dtjtdtj| _d| _d S )Nr   \code)	r   rn   r   r   r   r   r   ESCAPED_BSLASHr   )rm   r\   rO   rO   rP   rn     s   z BacktickInlineProcessor.__init__rr   rs   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\\)r~   r   r   r   r   AtomicStringcode_escapestriprz   r   r   replacer   r   rO   rO   rP   rw     s
   
	&z#BacktickInlineProcessor.handleMatchN)r\   rS   )rr   rs   r   rS   r   r   r   rO   rO   rO   rP   r3     s    
r3   c                   @  r   )
DoubleTagPatternfReturn a ElementTree element nested in tag2 nested in tag1.

    Useful for strong emphasis etc.

    rr   rs   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   
SubElementr~   rz   lengroupstail)rm   rr   tag1tag2el1el2rO   rO   rP   rw     s   
zDoubleTagPattern.handleMatchNr   r   rO   rO   rO   rP   r         r   c                   @  r   )DoubleTagInlineProcessorr   rr   rs   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   r~   rz   r   r   r   r   r   )rm   rr   r   r   r   r   r   rO   rO   rP   rw     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dZdddZdS )rG   z1 Store raw inline html and return a placeholder. rr   rs   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   r~   r   	htmlStashstorer   r   )rm   rr   r   rawhtmlplace_holderrO   rO   rP   rw     s   zHtmlInlineProcessor.handleMatchrz   c                   sH   z	 j jd jW n ty   | Y S w d	 fdd}tj||S )
r{   r|   rr   rs   r   rS   c                   sP   |  d}|}|d ur&z
  j|W S  ty%   d|  Y S w d S )Nr   z\%s)r~   r   r   r   
serializer	Exceptionr   rm   r   rO   rP   r     s   

z/HtmlInlineProcessor.unescape.<locals>.get_stashNr   r   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d}|||S )
zG Return text with backslash escapes undone (backslashes are restored). r   rr   rs   r   rS   c                 S  s   t t| dS )Nr   )chrintr~   )rr   rO   rO   rP   	_unescape  s   z9HtmlInlineProcessor.backslash_unescape.<locals>._unescapeNr   )r   r   REr   r   )rm   rz   r   r   rO   rO   rP   r     s   
z&HtmlInlineProcessor.backslash_unescapeNr   r   )r_   r`   ra   rb   rw   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dZd(ddZd(ddZd)ddZd*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emrr   rs   r   rS   idxr   r   r   c                 C  s(   t |}|d}| ||d| |S )zReturn single tag.r   N)r   r   r~   parse_sub_patterns)rm   rr   r   r   r   rz   rO   rO   rP   build_single+  s   

zAsteriskProcessor.build_singler^   c           	      C  st   | d\}}t|}t|}|d}| ||d| || t| dkr8|d}| |||| |S )zReturn double tag.r   r   Nr   )r   r   r   r~   r   appendr   r   	rm   rr   r^   r   r   r   r   r   rz   rO   rO   rP   build_double2  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   r~   r   r   r   rO   rO   rP   build_double2@  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   rf   match	enumeratePATTERNSr\   r   r   rz   build_elementr]   r^   r   r   )rm   r   r   r   r   offsetposlengthmatchedindexitemrr   rz   r   rO   rO   rP   r   M  sB   
#

z$AsteriskProcessor.parse_sub_patternsr]   r  c                 C  s:   |dkr|  |||S |dkr| |||S | |||S )zElement builder.r   r   )r   r   r   )rm   rr   r]   r^   r  rO   rO   rP   r     s
   zAsteriskProcessor.build_element3tuple[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   r\   r   r   r   r   r]   r^   )	rm   rr   r   r   r   r   r  r  m1rO   rO   rP   rw     s   


zAsteriskProcessor.handleMatchN)rr   rs   r   rS   r   r   r   r   )rr   rs   r^   rS   r   r   r   r   )
r   rS   r   r   r   r   r   r   r   r   )
rr   rs   r]   rS   r^   rS   r  r   r   r   rr   rs   r   rS   r   r  )r_   r`   ra   rb   rZ   rh   ri   EM_STRONG_RErj   rk   STRONG_EM_RESTRONG_EM3_RE	STRONG_REEMPHASIS_REr   r   r   r   r   r   rw   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`   ra   rb   rZ   rh   ri   EM_STRONG2_RErj   rk   STRONG_EM2_RESMART_STRONG_EM_RESMART_STRONG_RESMART_EMPHASIS_REr   rO   rO   rO   rP   rM     s    rM   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 )r9   z- Return a link element from the given match. z2\(\s*(?:(<[^<>]*>)\s*(?:('[^']*'|"[^"]*")\s*)?\))?z\srr   rs   r   rS   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   rz   setr   )	rm   rr   r   rz   r  handledr  r  r   rO   rO   rP   rw     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rd}nK|s|d8 }nD|dkr|d8 }|dkr|d }
n3|dv r|sd	}|}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 `()`. r}   NFr   r   rV   r   r   T())rU   rT    )RE_LINKr   r~   r   r   ranger   r   RE_TITLE_CLEANr   rY   r   )rm   r   r  r  r  r  rr   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.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   r}   )r  r   r   r   )rm   r   r  r   rz   r   r+  rO   rO   rP   r  @  s   
zLinkInlineProcessor.getTextNr  )r   rS   r  r   r   r  )r   rS   r  r   r   r,  )r_   r`   ra   rb   rh   ri   rj   rk   r  r  rw   r  r  rO   rO   rO   rP   r9     s    


tr9   c                   @  r   )r;   z. Return a `img` element from the given match. rr   rs   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   )	rm   rr   r   rz   r  r  r0  r  r   rO   rO   rP   rw   W  s   
z ImageInlineProcessor.handleMatchNr  r   rO   rO   rO   rP   r;   T  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 )r7   z6 Match to a stored reference and return link element. z\s+z\s?\[([^\]]*)\]rr   rs   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)
rm   rr   r   rz   r  r  r   r   r  r  rO   rO   rP   rw   r  s   z$ReferenceInlineProcessor.handleMatchr  r   rz   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   r~   lowerr   )rm   r   r  rz   rr   r   r   rO   rO   rP   r2    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  rz   rm   r  r  rz   r   rO   rO   rP   r5    s   
z ReferenceInlineProcessor.makeTagNr  )r   rS   r  r   rz   rS   r   r6  r  rS   r  rS   rz   rS   r   r   )r_   r`   ra   rb   rh   ri   	MULTILINEr3  rj   rk   r  rw   r2  r5  rO   rO   rO   rP   r7   l  s    

r7   c                   @     e Zd ZdZdd	d
ZdS )r?   z%Short form of reference: `[google]`. r   rS   r  r   rz   r   r,  c                 C     |  |dfS zEvaluate the id of `[ref]`.  Tr7  rm   r   r  rz   rO   rO   rP   r2       z$ShortReferenceInlineProcessor.evalIdNr   rS   r  r   rz   rS   r   r,  r_   r`   ra   rb   r2  rO   rO   rO   rP   r?     r   r?   c                   @  r   )r=   z7 Match to a stored reference and return `img` element. r  rS   r  rz   r   r   c                 C  s<   t d}|d| |r|d| |d| | |S )z= Return an `img` [`Element`][xml.etree.ElementTree.Element]. r/  r0  r  r1  )r   r   r  r   r8  rO   rO   rP   r5    s   
z%ImageReferenceInlineProcessor.makeTagNr9  )r_   r`   ra   rb   r5  rO   rO   rO   rP   r=     r   r=   c                   @  r;  )r@   z* Short form of image reference: `![ref]`. r   rS   r  r   rz   r   r,  c                 C  r<  r=  r>  r?  rO   rO   rP   r2    r@  z)ShortImageReferenceInlineProcessor.evalIdNrA  rB  rO   rO   rO   rP   r@     r   r@   c                   @  r   )rA   zD Return a link Element given an auto-link (`<http://example/com>`). rr   rs   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   r~   r   r   rz   r   r   r   rO   rO   rP   rw     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>`).
    rr   rs   r   rS   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   rS   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	   codepoint2namer   r   r   AMP_SUBSTITUTE)r   r(   rO   rO   rP   rC    s   z;AutomailInlineProcessor.handleMatch.<locals>.codepoint2namec                   s   g | ]} t |qS rO   )r   .0letterrC  rO   rP   
<listcomp>  s    z7AutomailInlineProcessor.handleMatch.<locals>.<listcomp>r}   c                 S  s   g | ]}t jd t|  qS )z#%d;)r   rD  r   rE  rO   rO   rP   rI    s
    
r  r   )r   r   r   rS   )r   r   r   r~   rW   r   r   r   r   rz   r  r   r   )rm   rr   r   r   emaillettersmailtorO   rH  rP   rw     s   



z#AutomailInlineProcessor.handleMatchNr   r   rO   rO   rO   rP   rC     s    rC   )r   r
   r   r   r   r   )rR   rS   r   rS   )Crb   
__future__r   r}   r   typingr   r   r   r   rh   xml.etree.ElementTreer   ElementTreer&   r	   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   rd   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
	V0(  #8
