
    /Vh:                       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c mZ ddlmZ erddlmZ dPd	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   Z 	 dZ!	 e Z"	 e!Z#	 dZ$	 dZ%	 dZ&	 dZ'	 dZ(	 dZ)	 dQdZ* G d  d!e	      Z+ G d" d#      Z, G d$ d%e,      Z- G d& d'e,      Z. G d( d)e-      Z/ G d* d+e-      Z0 G d, d-e,      Z1 G d. d/e-      Z2 G d0 d1e1      Z3 G d2 d3e2      Z4 G d4 d5e-      Z5 G d6 d7e1      Z6 G d8 d9e2      Z7 G d: d;e-      Z8 G d< d=e-      Z9 G d> d?e9      Z: G d@ dAe-      Z; G dB dCe;      Z< G dD dEe;      Z= G dF dGe=      Z> G dH dIe=      Z? G dJ dKe?      Z@ G dL dMe-      ZA G dN dOe-      ZBy)RaB  
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)Markdownc                6   t        j                         }|j                  t        t              dd       |j                  t        t        |       dd       |j                  t        t        |       dd       |j                  t        t        |       dd       |j                  t        t        |       d	d
       |j                  t        t        |       dd       |j                  t        t        |       dd       |j                  t!        t        |       dd       |j                  t#        t$        |       dd       |j                  t'        t(        |       dd       |j                  t+        t,        d      dd       |j                  t/        t0        |       dd       |j                  t/        t2        |       dd       |j                  t5        t6              dd       |j                  t9        d      dd        |j                  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)mdkwargsinlinePatternss      G/home/dcms/DCMS/lib/python3.12/site-packages/markdown/inlinepatterns.pybuild_inlinepatternsrP   5   s   & ]]_N3K@*cR1)R@(CP4\2FUXY/<fcJ0C\SVW%&8"=?PRU %lB79JC *+=rBDUWZ 3KDjRUV3KDjRUV8M{\_`/<fbI/	2>"M5mDlTVW-e4k2F/5|RH    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  \nc                    | j                  d      r| j                  d      s"| j                  d      r| j                  d      r| dd S | S )z#Remove quotes from around a string."'r   )
startswithendswith)strings    rO   dequoterY      sE    			3	FOOC$8#6??3#7a|rQ   c                  0    e Zd ZU dZded<   ded<   ded<   y)EmStrongItemzEmphasis/strong pattern item.re.Pattern[str]patternstrbuildertagsN)__name__
__module____qualname____doc____annotations__ rQ   rO   r[   r[      s    'L
IrQ   r[   c                  j    e Zd ZU dZ e       Zded<   	 ded<   ded<   ddd	Zdd
ZddZ	ddZ
ddZy)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 | NonerL   Nc                    || _         t        j                  d|z  t        j                  t        j                  z        | _        || _        y)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UNICODErj   rL   selfr]   rL   s      rO   __init__zPattern.__init__   s=     ::&6&@&(ii"**&<> rQ   c                    | j                   S )z' Return a compiled regular expression. )rj   rr   s    rO   getCompiledRegExpzPattern.getCompiledRegExp   s    rQ   c                     y)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.

        Nrf   rr   ms     rO   handleMatchzPattern.handleMatch   s     	rQ   c                .    | j                   j                  S )z+ Return class name, to define pattern type )	__class__ra   ru   s    rO   typezPattern.type  s    ~~&&&rQ   c                    	 | j                   j                  d   j                  fd}t        j
                  j                  ||      S # t        $ r |cY S w xY w)> Return unescaped text given text with an inline placeholder. inlinec                    | j                  d      }|v rBj                  |      }t        |t              r|S dj	                  |j                               S y )Nr    )groupget
isinstancer^   joinitertext)ry   idvaluestashs      rO   	get_stashz#Pattern.unescape.<locals>.get_stash  sP    BU{		"eS) L 775>>#344 rQ   rL   treeprocessorsstashed_nodesKeyErrorr   INLINE_PLACEHOLDER_REsubrr   textr   r   s      @rO   unescapezPattern.unescape  sX    	GG**84BBE	5 ))--i>>  	K	s   #A AANr]   r^   rL   rk   )returnz
re.Pattern)ry   re.Match[str]r   zetree.Element | str)r   r^   r   r^   r   r^   )ra   rb   rc   rd   tupleri   re   rs   rv   rz   r}   r   rf   rQ   rO   rh   rh      sB    & */0
 ! " '?rQ   rh   c                  "    e Zd ZdZdddZddZy)InlineProcessorz
    Base class that inline processors subclass.

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

    Nc                    || _         t        j                  |t        j                  t        j                  z        | _        d| _        || _        y)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]   rm   rn   ro   rp   rj   	safe_moderL   rq   s      rO   rs   zInlineProcessor.__init__$  s;     ::gryy2::/EF rQ   c                     y)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.

        Nrf   rr   ry   datas      rO   rz   zInlineProcessor.handleMatch5  s    & 	rQ   r   r   )ry   r   r   r^   r   z9tuple[etree.Element | str | None, int | None, int | None]ra   rb   rc   rd   rs   rz   rf   rQ   rO   r   r     s    "rQ   r   c                      e Zd ZdZddZy)SimpleTextPatternz2 Return a simple text of `group(2)` of a Pattern. c                $    |j                  d      S )z< Return string content of `group(2)` of a matching pattern.    )r   rx   s     rO   rz   zSimpleTextPattern.handleMatchM  s    wwqzrQ   Nry   r   r   r^   ra   rb   rc   rd   rz   rf   rQ   rO   r   r   K  s
    <rQ   r   c                      e Zd ZdZddZy)rH   z2 Return a simple text of `group(1)` of a Pattern. c                f    |j                  d      |j                  d      |j                  d      fS )z< Return string content of `group(1)` of a matching pattern. r   r   )r   startendr   s      rO   rz   z%SimpleTextInlineProcessor.handleMatchT  s'    wwqz1771:quuQx//rQ   Nry   r   r   r^   r   ztuple[str, int, int]r   rf   rQ   rO   rH   rH   R  s
    <0rQ   rH   c                      e Zd ZdZddZy)r3   z Return an escaped character. c                L   |j                  d      }|| j                  j                  v rYdj                  t        j
                  t        |      t        j                        |j                  d      |j                  d      fS d|j                  d      |j                  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   rL   ESCAPED_CHARSformatr   STXordETXr   r   )rr   ry   r   chars       rO   rz   z!EscapeInlineProcessor.handleMatch\  sw     wwqz477(((??488SYA1771:quuUVxWWQUU1X--rQ   N)ry   r   r   r^   r   ztuple[str | None, int, int]r   rf   rQ   rO   r3   r3   Y  s
    (.rQ   r3   c                       e Zd ZdZddZddZy)SimpleTagPatternz]
    Return element of type `tag` with a text attribute of `group(3)`
    of a Pattern.

    c                >    t         j                  | |       || _        y)z
        Create an instant of an simple tag pattern.

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

        N)rh   rs   tagrr   r]   r   s      rO   rs   zSimpleTagPattern.__init__r  s     	w'0rQ   c                p    t        j                  | j                        }|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   r   )rr   ry   els      rO   rz   zSimpleTagPattern.handleMatch  s*    
 ]]488$''!*	rQ   Nr]   r^   r   r^   ry   r   r   etree.Elementr   rf   rQ   rO   r   r   l  s    
1rQ   r   c                       e Zd ZdZddZddZy)SimpleTagInlineProcessorz]
    Return element of type `tag` with a text attribute of `group(2)`
    of a Pattern.

    c                >    t         j                  | |       || _        y)z
        Create an instant of an simple tag processor.

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

        N)r   rs   r   r   s      rO   rs   z!SimpleTagInlineProcessor.__init__  s     	  w/0rQ   c                    t        j                  | j                        }|j                  d      |_        ||j                  d      |j                  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   r   r   r   rr   ry   r   r   s       rO   rz   z$SimpleTagInlineProcessor.handleMatch  sB    
 ]]488$''!*1771:quuQx''rQ   Nr   ry   r   r   r^   r   ztuple[etree.Element, int, int]r   rf   rQ   rO   r   r     s    
1(rQ   r   c                      e Zd ZdZddZy)SubstituteTagPattern3 Return an element of type `tag` with no children. c                @    t        j                  | j                        S )H Return empty [`Element`][xml.etree.ElementTree.Element] of type `tag`. )r   r   r   rx   s     rO   rz   z SubstituteTagPattern.handleMatch  s    }}TXX&&rQ   Nr   r   rf   rQ   rO   r   r     s
    ='rQ   r   c                      e Zd ZdZddZy)rC   r   c                    t        j                  | j                        |j                  d      |j	                  d      fS )r   r   )r   r   r   r   r   r   s      rO   rz   z(SubstituteTagInlineProcessor.handleMatch  s,    }}TXX&
AEE!H<<rQ   Nr   r   rf   rQ   rO   rC   rC     s
    ==rQ   rC   c                       e Zd ZdZddZddZy)r1   zA Return a `<code>` element containing the escaped matching text. c                    t         j                  | |       dj                  t        j                  t        d      t        j                        | _        d| _        y )Nr   \code)	r   rs   r   r   r   r   r   ESCAPED_BSLASHr   )rr   r]   s     rO   rs   z BacktickInlineProcessor.__init__  s=      w/&oodhhD	488L0rQ   c                   |j                  d      rt        j                  | j                        }t	        j
                  t	        j                  |j                  d      j                                     |_        ||j                  d      |j                  d      fS |j                  d      j                  d| j                        |j                  d      |j                  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stripr   r   r   replacer   r   s       rO   rz   z#BacktickInlineProcessor.handleMatch  s     771:txx(B''(8(89I9I9K(LMBGqwwqz1558++771:%%fd.A.ABAGGAJPQPUPUVWPXXXrQ   N)r]   r^   )ry   r   r   r^   r   z$tuple[etree.Element | str, int, int]r   rf   rQ   rO   r1   r1     s    K1YrQ   r1   c                      e Zd ZdZddZy)DoubleTagPatternfReturn a ElementTree element nested in tag2 nested in tag1.

    Useful for strong emphasis etc.

    c                (   | j                   j                  d      \  }}t        j                  |      }t        j                  ||      }|j                  d      |_        t        |j                               dk(  r|j                  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   r   lengroupstail)rr   ry   tag1tag2el1el2s         rO   rz   zDoubleTagPattern.handleMatch  so     XX^^C(
dmmD!sD)771:qxxz?awwqzCH
rQ   Nr   r   rf   rQ   rO   r   r     s    
rQ   r   c                      e Zd ZdZddZy)DoubleTagInlineProcessorr   c                j   | j                   j                  d      \  }}t        j                  |      }t        j                  ||      }|j                  d      |_        t        |j                               dk(  r|j                  d      |_	        ||j                  d      |j                  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   r   r   r   r   r   r   )rr   ry   r   r   r   r   r   s          rO   rz   z$DoubleTagInlineProcessor.handleMatch  s     XX^^C(
dmmD!sD)771:qxxz?awwqzCHAGGAJa((rQ   Nr   r   rf   rQ   rO   r   r     s    
)rQ   r   c                  (    e Zd ZdZddZddZddZy)rE   z1 Store raw inline html and return a placeholder. c                    | j                  | j                  |j                  d                  }| j                  j                  j                  |      }||j                  d      |j                  d      fS )zL Store the text of `group(1)` of a pattern and return a placeholder string. r   r   )backslash_unescaper   r   rL   	htmlStashstorer   r   )rr   ry   r   rawhtmlplace_holders        rO   rz   zHtmlInlineProcessor.handleMatch  sZ    ))$--
*CDww((..w7QWWQZq11rQ   c                     	  j                   j                  d   j                  d fd}t        j
                  j                  ||      S # t        $ r |cY S w xY w)r   r   c                    | j                  d      }j                  |      }|	 j                  j                  |      S y # t        $ r d|z  cY S w xY w)Nr   z\%s)r   r   rL   
serializer	Exception)ry   r   r   rr   r   s      rO   r   z/HtmlInlineProcessor.unescape.<locals>.get_stash  s]    BIIbME *77--e44 ! ! *!E>)*s   A AAr   r   r   s   `  @rO   r   zHtmlInlineProcessor.unescape  sX    	GG**84BBE	* ))--i>>  	K	s   #A AAc                    	 | j                   j                  d   j                  }dd}|j	                  ||      S # t        $ r |cY S w xY w)zG Return text with backslash escapes undone (backslashes are restored). r   c                H    t        t        | j                  d                  S )Nr   )chrintr   )ry   s    rO   	_unescapez9HtmlInlineProcessor.backslash_unescape.<locals>._unescape  s    s1771:''rQ   r   )rL   r   REr   r   )rr   r   r   r   s       rO   r   z&HtmlInlineProcessor.backslash_unescape  sO    	''
366B	( vvi&&  	K	s   #; A	A	Nr   r   )ra   rb   rc   rd   rz   r   r   rf   rQ   rO   rE   rE     s    ;2?$
'rQ   rE   c                  D   e Zd ZdZ e ej                  eej                  ej                  z        dd       e ej                  e
ej                  ej                  z        dd       e ej                  eej                  ej                  z        dd       e ej                  eej                  ej                  z        dd       e ej                  eej                  ej                  z        dd      gZ	 dd	Zdd
ZddZ	 	 	 	 	 	 	 	 	 	 ddZddZddZy)rJ   zGEmphasis processor for handling strong and em matches inside asterisks.double	strong,em	em,strongdouble2singlestrongemc                z    t        j                  |      }|j                  d      }| j                  ||d|       |S )zReturn single tag.r   N)r   r   r   parse_sub_patterns)rr   ry   r   idxr   r   s         rO   build_singlezAsteriskProcessor.build_single*  s6    mmC wwqzc45
rQ   c                p   |j                  d      \  }}t        j                  |      }t        j                  |      }|j                  d      }| j	                  ||d|       |j                  |       t        |j                               dk(  r%|j                  d      }| j	                  ||||       |S )zReturn double tag.r   r   Nr   )r   r   r   r   r  appendr   r   	rr   ry   r`   r	  r   r   r   r   r   s	            rO   build_doublezAsteriskProcessor.build_double1  s     ZZ_
dmmD!mmD!wwqzc45

3qxxz?a771:D##D#sC8
rQ   c                8   |j                  d      \  }}t        j                  |      }t        j                  |      }|j                  d      }| j	                  ||d|       |j                  d      }|j                  |       | j	                  ||d|       |S )zFReturn double tags (variant 2): `<strong>text <em>text</em></strong>`.r   r   Nr   )r   r   r   r   r  r  r  s	            rO   build_double2zAsteriskProcessor.build_double2?  s     ZZ_
dmmD!mmD!wwqzc45wwqz

3c45
rQ   c                2   d}d}t        |      }||k  r| j                  j                  ||      rd}t        | j                        D ]  \  }	}
|	|k  r|
j
                  j                  ||      }|s+|||j                  d       }|r|||_        n||_        | j                  ||
j                  |
j                  |	      }|j                  |       |}|j                  d      x}}d} |s|dz  }n|dz  }||k  r||d }|r|||_        y||_        yy)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   rj   match	enumeratePATTERNSr]   r   r   r   build_elementr_   r`   r  r   )rr   r   parentlastr	  offsetposlengthmatchedindexitemry   r   r   s                 rO   r  z$AsteriskProcessor.parse_sub_patternsL  s8    TFl%%dC0#,T]]#; 'KE4| **45A
  $F1771:6#/,0	.2!//4<<ERb)!'(uuQx/"&+', 1HC qA FlF FG} 	"	 rQ   c                    |dk(  r| j                  |||      S |dk(  r| j                  |||      S | j                  |||      S )zElement builder.r  r   )r  r  r
  )rr   ry   r_   r`   r  s        rO   r  zAsteriskProcessor.build_element  sS     i%%au55 $$Qe44$$Qe44rQ   c                D   d}d}d}t        | j                        D ]~  \  }}|j                  j                  ||j	                  d            }|s4|j	                  d      }|j                  d      }| j                  ||j                  |j                  |      } n |||fS )zParse patterns.Nr   )	r  r  r]   r  r   r   r  r_   r`   )	rr   ry   r   r   r   r   r  r  m1s	            rO   rz   zAsteriskProcessor.handleMatch  s     $T]]3 	KE4##D!''!*5BffQi''DLL$))UK	 5#~rQ   N)ry   r   r   r^   r	  r   r   r   )ry   r   r`   r^   r	  r   r   r   )
r   r^   r  r   r  zetree.Element | Noner	  r   r   None)
ry   r   r_   r^   r`   r^   r  r   r   r   ry   r   r   r^   r   z3tuple[etree.Element | None, int | None, int | None])ra   rb   rc   rd   r[   rm   rn   EM_STRONG_REro   rp   STRONG_EM_RESTRONG_EM3_RE	STRONG_REEMPHASIS_REr  r
  r  r  r  r  rz   rf   rQ   rO   rJ   rJ     s   Q 	ZRZZbii"**.DExQ\]ZRZZbii"**.DExQ\]ZRZZryy2::/EF	S^_ZRZZ	299rzz+ABHhWZRZZRYY-CDhPTUH P;#;#!.;#6J;#QT;#	;#z5rQ   rJ   c                     e Zd ZdZ e ej                  eej                  ej                  z        dd       e ej                  e
ej                  ej                  z        dd       e ej                  eej                  ej                  z        dd       e ej                  eej                  ej                  z        dd       e ej                  eej                  ej                  z        dd      gZy	)
rK   zIEmphasis processor for handling strong and em matches inside underscores.r   r  r  r  r  r  r  N)ra   rb   rc   rd   r[   rm   rn   EM_STRONG2_REro   rp   STRONG_EM2_RESMART_STRONG_EM_RESMART_STRONG_RESMART_EMPHASIS_REr  rf   rQ   rO   rK   rK     s    S 	ZRZZryy2::/EFR]^ZRZZryy2::/EFR]^ZRZZ 2BII

4JKYXcdZRZZRZZ1GH(T\]ZRZZ 1299rzz3IJHVZ[H PrQ   rK   c                      e Zd ZdZ ej
                  dej                  ej                  z        Z ej
                  d      Z	ddZ
d	dZd
dZy)r7   z- Return a link element from the given match. z2\(\s*(?:(<[^<>]*>)\s*(?:('[^']*'|"[^"]*")\s*)?\))?z\sc                2   | j                  ||j                  d            \  }}}|sy| j                  ||      \  }}}}|syt        j                  d      }||_        |j                  d|       ||j                  d|       ||j                  d      |fS )zS Return an `a` [`Element`][xml.etree.ElementTree.Element] or `(None, None, None)`. r   NNNahreftitle)getTextr   getLinkr   r   r   setr   )	rr   ry   r   r   r  handledr2  r3  r   s	            rO   rz   zLinkInlineProcessor.handleMatch  s    #||D!%%(;eW#&*ll4&?#eUG#]]3
vtFF7E"1771:u$$rQ   c           	        d}d}d}| j                   j                  ||      }|rm|j                  d      r\|j                  d      dd j                         }|j                  d      r|j                  d      dd }|j	                  d      }d	}nc|r`d}d}|j	                         }	|	}d}
d}d}d}d}d}d}d}d}t        |t        |            D ]  }||   }|d
k(  r|s|dz  }n~|dkD  ry|dz  }ns|dk(  r4|dk7  r||k(  s
|dk7  r||k(  rd}nW|s|dz  }nO|dkD  rJ|dz  }|dk(  r@|dz   }
n:|dv r6|sd	}|}d}|dz   }|}n&||k7  r
|s|dz   }|}n||k(  r|dz   }n|r
||k(  r|dz   }|dz  }|dk(  r^|dk\  r%||k(  r ||	|dz
   }dj                  |||dz
         }n2|dk\  r%||k(  r ||	|dz
   }dj                  |||dz
         }n||	|dz
   } n|dk7  s|} |dk7  r|dk(  r||	|
dz
   }|
}d}|dk(  }|B| j                  j                  dt        | j                  |j                                           }| j                  |      j                         }||||fS )z?Parse data between `()` of `[Text]()` allowing recursive `()`. r   NFr  r   rU   r   r   T())rT   rS    )RE_LINKr  r   r   r   ranger   r   RE_TITLE_CLEANr   rY   r   )rr   r   r  r2  r3  r7  ry   bracket_countbacktrack_countstart_indexlast_bracketquotestart_quote
exit_quoteignore_matches	alt_quotestart_alt_quoteexit_alt_quoter  r  cs                        rO   r5  zLinkInlineProcessor.getLink  s     LLt/771:a#))+Dwwqz
1R(EE!HEGMO%%'KEL !%EKJ"N I ON DUCI. ;I8 *%*(1,'1,#X $r)etmSUAUZcgkZk()+%*(1,'1,*a/+019L*_  *.*7()&+ai !eI*/!)$%	e%*QY
"qI~).
 !A%!Q5D=#Ka@ "[a(H I'1,d1B#K!0CD "_^a=O(P Q#K	:8Dw;B !o&:Kq(89$ !#q(G''++Cu{{}9U1VWE}}T"((*UE7**rQ   c                    d}g }t        |t        |            D ]9  }||   }|dk(  r|dz  }n
|dk(  r|dz  }|dz  }|dk(  r n|j                  |       ; dj                  |      ||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   )rr   r   r  r@  r   r  rK  s          rO   r4  zLinkInlineProcessor.getText?  s    
 D	* 		CS	ACx"c"QJE!KKN		 wwt}e]a%777rQ   Nr"  )r   r^   r  r   r   z!tuple[str, str | None, int, bool])r   r^   r  r   r   tuple[str, int, bool])ra   rb   rc   rd   rm   rn   ro   rp   r=  r?  rz   r5  r4  rf   rQ   rO   r7   r7     sL    7bjjRTVT]T]`b`j`jTjkGRZZ&N%*r+h8rQ   r7   c                      e Zd ZdZddZy)r9   z. Return a `img` element from the given match. c                f   | j                  ||j                  d            \  }}}|sy| j                  ||      \  }}}}|syt        j                  d      }|j                  d|       ||j                  d|       |j                  d| j                  |             ||j                  d      |fS )zU Return an `img` [`Element`][xml.etree.ElementTree.Element] or `(None, None, None)`. r   r0  imgsrcr3  alt)r4  r   r5  r   r   r6  r   r   )	rr   ry   r   r   r  r7  rS  r3  r   s	            rO   rz   z ImageInlineProcessor.handleMatchV  s    #||D!%%(;eW#%)\\$%>"UE7#]]5!
ucFF7E"
udmmD)*1771:u$$rQ   Nr"  r   rf   rQ   rO   r9   r9   S  s
    8%rQ   r9   c                      e Zd ZdZ ej
                  dej                        Z ej
                  dej                  ej                  z        Z
ddZd	dZd
dZy)r5   z6 Match to a stored reference and return link element. z\s+z\s?\[([^\]]*)\]c                   | j                  ||j                  d            \  }}}|sy| j                  |||      \  }}}|sy| j                  j	                  d|      }|| j
                  j                  vrd|j                  d      |fS | j
                  j                  |   \  }}	| j                  ||	|      |j                  d      |fS )zz
        Return [`Element`][xml.etree.ElementTree.Element] returned by `makeTag` method or `(None, None, None)`.

        r   r0  r<  N)	r4  r   evalIdNEWLINE_CLEANUP_REr   rL   
referencesr   makeTag)
rr   ry   r   r   r  r7  r   r   r2  r3  s
             rO   rz   z$ReferenceInlineProcessor.handleMatchq  s    
  $||D!%%(;eW#;;tUD9C# $$((b1TWW'''S((gg((,e||D%.
C??rQ   c                    | j                   j                  ||      }|sd|dfS |j                  d      j                         }|j	                  d      }|s|j                         }||dfS )z\
        Evaluate the id portion of `[ref][id]`.

        If `[ref][]` use `[ref]`.
        r9  NFr   r   T)r=  r  r   lowerr   )rr   r   r  r   ry   r   r   s          rO   rW  zReferenceInlineProcessor.evalId  sj     LLt/%%!!#B%%(CZZ\3}rQ   c                    t        j                  d      }|j                  d|       |r|j                  d|       ||_        |S )z; Return an `a` [`Element`][xml.etree.ElementTree.Element]. r1  r2  r3  )r   r   r6  r   rr   r2  r3  r   r   s        rO   rZ  z ReferenceInlineProcessor.makeTag  s;    ]]3
vtFF7E"	rQ   Nr"  )r   r^   r  r   r   r^   r   ztuple[str | None, int, bool]r2  r^   r3  r^   r   r^   r   r   )ra   rb   rc   rd   rm   rn   	MULTILINErX  ro   rp   r=  rz   rW  rZ  rf   rQ   rO   r5   r5   k  sL    @#FBLL9bjj+RYY-CDG@, 	rQ   r5   c                      e Zd ZdZddZy)r=   z%Short form of reference: `[google]`. c                (    |j                         |dfS zEvaluate the id of `[ref]`.  Tr\  rr   r   r  r   s       rO   rW  z$ShortReferenceInlineProcessor.evalId       zz|UD((rQ   Nr   r^   r  r   r   r^   r   rO  ra   rb   rc   rd   rW  rf   rQ   rO   r=   r=     s
    /)rQ   r=   c                      e Zd ZdZddZy)r;   z7 Match to a stored reference and return `img` element. c                    t        j                  d      }|j                  d|       |r|j                  d|       |j                  d| j                  |             |S )z= Return an `img` [`Element`][xml.etree.ElementTree.Element]. rR  rS  r3  rT  )r   r   r6  r   r^  s        rO   rZ  z%ImageReferenceInlineProcessor.makeTag  sK    ]]5!
udFF7E"
udmmD)*	rQ   Nr_  )ra   rb   rc   rd   rZ  rf   rQ   rO   r;   r;     s
    ArQ   r;   c                      e Zd ZdZddZy)r>   z* Short form of image reference: `![ref]`. c                (    |j                         |dfS rc  rd  re  s       rO   rW  z)ShortImageReferenceInlineProcessor.evalId  rf  rQ   Nrg  rh  rf   rQ   rO   r>   r>     s
    4)rQ   r>   c                      e Zd ZdZddZy)r?   zD Return a link Element given an auto-link (`<http://example/com>`). c                $   t        j                  d      }|j                  d| j                  |j	                  d                   t        j                  |j	                  d            |_        ||j                  d      |j                  d      fS )zI Return an `a` [`Element`][xml.etree.ElementTree.Element] of `group(1)`. r1  r2  r   r   )
r   r   r6  r   r   r   r   r   r   r   r   s       rO   rz   z#AutolinkInlineProcessor.handleMatch  sf    ]]3
vt}}QWWQZ01##AGGAJ/1771:quuQx''rQ   Nr   r   rf   rQ   rO   r?   r?     s
    N(rQ   r?   c                      e Zd ZdZddZy)rA   zW
    Return a `mailto` link Element given an auto-mail link (`<foo@example.com>`).
    c           
     @   t        j                  d      }| j                  |j                  d            }|j	                  d      r|t        d      d }d
d}|D cg c]  } |t        |             }}t        j                  dj                  |            |_
        d|z   }dj                  |D cg c]!  }t        j                  dt        |      z  z   # c}      }|j                  d|       ||j                  d	      |j                  d	      fS c c}w c c}w )za Return an [`Element`][xml.etree.ElementTree.Element] containing a `mailto` link  of `group(1)`. r1  r   zmailto:Nc                    t         j                  j                  |       }|r dj                  t        j
                  |      S dt        j
                  | fz  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&   s     rO   rr  z;AutomailInlineProcessor.handleMatch.<locals>.codepoint2name  sH    ,,006F~~d&9&96BB4#6#6"===rQ   r   z#%d;r2  r   )r   r   r   r^   )r   r   r   r   rV   r   r   r   r   r   r   rs  r6  r   r   )	rr   ry   r   r   emailrr  letterlettersmailtos	            rO   rz   z#AutomailInlineProcessor.handleMatch  s    ]]3aggaj)I&#i./*E	> >CC6>#f+.CC##BGGG$45U"4:<*0 --f+1& & < =
vv1771:quuQx'' D<s   D4&DNr   r   rf   rQ   rO   rA   rA     s    (rQ   rA   )rL   r   rM   r   r   zutil.Registry[InlineProcessor])rX   r^   r   r^   )Crd   
__future__r   r   r   typingr   r   r   r	   rm   xml.etree.ElementTreer   ElementTreer$   r
   markdownr   rP   NOIMGr2   r4   r'  r&  r,  r-  r+  r#  r)  r$  r*  r%  r8   r:   r6   r<   rI   r@   rB   rF   rG   rD   rY   r[   rh   r   r   rH   r3   r   r   r   rC   r1   r   r   rE   rJ   rK   r7   r9   r5   r=   r;   r>   r?   rA   rf   rQ   rO   <module>r~     s5  (* #  = = 	 % % !*` 	 \L M	 = 7	 5: b9  cO  B, S, V, ?, B9 ?
%- ` S 5"  :8 '< =& ;
  %<	 V (: S? S?l-g -` 0 0.O .&w :( (:'+ '=#; =Yo Y2' *)7 )*$'/ $'NC CL
P+ 
P_8/ _8D%. %052 5p)$< )	$< 	))F )(o ((o (rQ   