
    -Bes              	          d Z ddlmZ ddlmZ ddlZddlmc mZ 	 ddl	m
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dZedz   ZdZeZeZdZdZ dZ!dZ"dZ#dZ$d Z% G d d edg d             Z&	  G d! d"      Z' G d# d$e'      Z( G d% d&e'      Z) G d' d(e(      Z* G d) d*e(      Z+ G d+ d,e'      Z, G d- d.e(      Z- G d/ d0e,      Z. G d1 d2e-      Z/ G d3 d4e(      Z0 G d5 d6e,      Z1 G d7 d8e-      Z2 G d9 d:e(      Z3 G d; d<e(      Z4 G d= d>e4      Z5 G d? d@e(      Z6 G dA dBe6      Z7 G dC dDe6      Z8 G dE dFe8      Z9 G dG dHe8      Z: G dI dJe:      Z; G dK dLe(      Z< G dM dNe(      Z=y# e$ r ddlZ
Y Vw xY w)Oa]	  
Python Markdown

A Python implementation of John Gruber's Markdown.

Documentation: https://python-markdown.github.io/
GitHub: https://github.com/Python-Markdown/markdown/
PyPI: https://pypi.org/project/Markdown/

Started by Manfred Stienstra (http://www.dwerg.net/).
Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org).
Currently maintained by Waylan Limberg (https://github.com/waylan),
Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser).

Copyright 2007-2018 The Python Markdown Project (v. 1.7 and later)
Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
Copyright 2004 Manfred Stienstra (the original version)

License: BSD (see LICENSE.md for details).

INLINE PATTERNS
=============================================================================

Inline patterns such as *emphasis* are handled by means of auxiliary
objects, one per pattern.  Pattern objects must be instances of classes
that extend markdown.Pattern.  Each pattern object uses a single regular
expression and needs support the following methods:

    pattern.getCompiledRegExp() # returns a regular expression

    pattern.handleMatch(m) # takes a match object and returns
                           # an ElementTree element or just plain text

All of python markdown's built-in patterns subclass from Pattern,
but you can add additional patterns that don't.

Also note that all the regular expressions used by inline must
capture the whole block.  For this reason, they all start with
'^(.*)' and end with '(.*)!'.  In case with built-in expression
Pattern takes care of adding the "^(.*)" and "(.*)!".

Finally, the order in which regular expressions 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:

* escape and backticks have to go before everything else, so
  that we can preempt any markdown patterns by escaping them.

* then we handle auto-links (must be done 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 hash.

* then inline images (must be done before links)

* then bracketed links, first regular then reference-style

* finally we apply strong and emphasis
   )util    )
namedtupleN)entitiesc                 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 )$z8 Build the default set of inline patterns for Markdown. 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      7lib/python3.12/site-packages/markdown/inlinepatterns.pybuild_inlinepatternsrK   I   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)(\*|_)(\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  \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    rJ   dequoterT      sE    			3	FOOC$8#6??3#7a|rL   c                       e Zd ZdZy)EmStrongItemzEmphasis/strong pattern item.N)__name__
__module____qualname____doc__ rL   rJ   rV   rV      s    'rL   rV   )patternbuildertagsc                   >    e Zd ZdZ e       ZddZd Zd Zd Z	d Z
y)	Patternz*Base class that inline patterns subclass. Nc                     || _         t        j                  d|z  t        j                  t        j                  z        | _        || _        y)
        Create an instant of an inline pattern.

        Keyword arguments:

        * pattern: A regular expression that matches a pattern

        z^(.*?)%s(.*)$N)r\   recompileDOTALLUNICODEcompiled_rerG   selfr\   rG   s      rJ   __init__zPattern.__init__   s=     ::&6&@&(ii"**&<> rL   c                     | j                   S )z' Return a compiled regular expression. )rg   ri   s    rJ   getCompiledRegExpzPattern.getCompiledRegExp   s    rL   c                      y)zReturn a ElementTree element from the given match.

        Subclasses should override this method.

        Keyword arguments:

        * m: A re match object containing a match of the pattern.

        Nr[   ri   ms     rJ   handleMatchzPattern.handleMatch   s     	rL   c                 .    | j                   j                  S )z+ Return class name, to define pattern type )	__class__rW   rl   s    rJ   typezPattern.type   s    ~~&&&rL   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
isinstancestrjoinitertext)rp   idvaluestashs      rJ   	get_stashz#Pattern.unescape.<locals>.get_stash   sP    BU{		"eS) L 775>>#344 rL   rG   treeprocessorsstashed_nodesKeyErrorr   INLINE_PLACEHOLDER_REsubri   textr   r   s      @rJ   unescapezPattern.unescape   sX    	GG**84BBE	5 ))--i>>  	K	s   #A AAN)rW   rX   rY   rZ   tupleANCESTOR_EXCLUDESrj   rm   rq   rt   r   r[   rL   rJ   r`   r`      s&    4 
'?rL   r`   c                       e Zd ZdZddZd Zy)InlineProcessorz
    Base class that inline patterns 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)rb   FN)r\   rc   rd   re   rf   rg   	safe_moderG   rh   s      rJ   rj   zInlineProcessor.__init__  s;     ::gryy2::/EF rL   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.

        Keyword arguments:

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

        Nr[   ri   rp   datas      rJ   rq   zInlineProcessor.handleMatch  s    * 	rL   r   rW   rX   rY   rZ   rj   rq   r[   rL   rJ   r   r      s     rL   r   c                       e Zd ZdZd Zy)SimpleTextPatternz0 Return a simple text of group(2) of a Pattern. c                 $    |j                  d      S )N   )rz   ro   s     rJ   rq   zSimpleTextPattern.handleMatch/  s    wwqzrL   NrW   rX   rY   rZ   rq   r[   rL   rJ   r   r   -  s
    :rL   r   c                       e Zd ZdZd Zy)rC   z0 Return a simple text of group(1) of a Pattern. c                 f    |j                  d      |j                  d      |j                  d      fS Nr   r   )rz   startendr   s      rJ   rq   z%SimpleTextInlineProcessor.handleMatch5  s'    wwqz1771:quuQx//rL   Nr   r[   rL   rJ   rC   rC   3  s
    :0rL   rC   c                       e Zd ZdZd Zy)r.   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 )Nr   {}{}{}r   )
rz   rG   ESCAPED_CHARSformatr   STXordETXr   r   )ri   rp   r   chars       rJ   rq   z!EscapeInlineProcessor.handleMatch<  su    wwqz477(((??488SYA1771:quuUVxWWQUU1X--rL   Nr   r[   rL   rJ   r.   r.   9  s
    (.rL   r.   c                       e Zd ZdZd Zd Zy)SimpleTagPatternz[
    Return element of type `tag` with a text attribute of group(3)
    of a Pattern.

    c                 >    t         j                  | |       || _        y r   )r`   rj   tagri   r\   r   s      rJ   rj   zSimpleTagPattern.__init__J  s    w'rL   c                 p    t        j                  | j                        }|j                  d      |_        |S )N   )etreeElementr   rz   r   )ri   rp   els      rJ   rq   zSimpleTagPattern.handleMatchN  s(    ]]488$''!*	rL   Nr   r[   rL   rJ   r   r   D  s    
rL   r   c                       e Zd ZdZd Zd Zy)SimpleTagInlineProcessorz[
    Return element of type `tag` with a text attribute of group(2)
    of a Pattern.

    c                 >    t         j                  | |       || _        y r   )r   rj   r   r   s      rJ   rj   z!SimpleTagInlineProcessor.__init__Z  s      w/rL   c                     t        j                  | j                        }|j                  d      |_        ||j                  d      |j                  d      fS )Nr   r   )r   r   r   rz   r   r   r   ri   rp   r   r   s       rJ   rq   z$SimpleTagInlineProcessor.handleMatch^  s@    ]]488$''!*1771:quuQx''rL   Nr   r[   rL   rJ   r   r   T  s    
(rL   r   c                       e Zd ZdZd Zy)SubstituteTagPattern3 Return an element of type `tag` with no children. c                 @    t        j                  | j                        S r   )r   r   r   ro   s     rJ   rq   z SubstituteTagPattern.handleMatchf  s    }}TXX&&rL   Nr   r[   rL   rJ   r   r   d  s
    ='rL   r   c                       e Zd ZdZd Zy)r>   r   c                     t        j                  | j                        |j                  d      |j	                  d      fS )Nr   )r   r   r   r   r   r   s      rJ   rq   z(SubstituteTagInlineProcessor.handleMatchl  s,    }}TXX&
AEE!H<<rL   Nr   r[   rL   rJ   r>   r>   j  s
    ==rL   r>   c                       e Zd ZdZd Zd Zy)r,   z9 Return a `<code>` element containing the matching text. c                     t         j                  | |       dj                  t        j                  t        d      t        j                        | _        d| _        y )Nr   \code)	r   rj   r   r   r   r   r   ESCAPED_BSLASHr   )ri   r\   s     rJ   rj   z BacktickInlineProcessor.__init__r  s:      w/&oodhhD	488LrL   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 )Nr   r   r   z\\)rz   r   r   r   r   AtomicStringcode_escapestripr   r   r   replacer   r   s       rJ   rq   z#BacktickInlineProcessor.handleMatchw  s    771:txx(B''(8(89I9I9K(LMBGqwwqz1558++771:%%fd.A.ABAGGAJPQPUPUVWPXXXrL   Nr   r[   rL   rJ   r,   r,   p  s    C
YrL   r,   c                       e Zd ZdZ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 )N,r         )
r   splitr   r   
SubElementrz   r   lengroupstail)ri   rp   tag1tag2el1el2s         rJ   rq   zDoubleTagPattern.handleMatch  sm    XX^^C(
dmmD!sD)771:qxxz?awwqzCH
rL   Nr   r[   rL   rJ   r   r     s    
rL   r   c                       e Zd ZdZ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 )Nr   r   r   r   )r   r   r   r   r   rz   r   r   r   r   r   r   )ri   rp   r   r   r   r   r   s          rJ   rq   z$DoubleTagInlineProcessor.handleMatch  s    XX^^C(
dmmD!sD)771:qxxz?awwqzCHAGGAJa((rL   Nr   r[   rL   rJ   r   r     s    
)rL   r   c                       e Zd ZdZd Zd Zy)r@   z1 Store raw inline html and return a placeholder. c                     | j                  |j                  d            }| j                  j                  j	                  |      }||j                  d      |j                  d      fS r   )r   rz   rG   	htmlStashstorer   r   )ri   rp   r   rawhtmlplace_holders        rJ   rq   zHtmlInlineProcessor.handleMatch  sO    --
+ww((..w7QWWQZq11rL   c                      	  j                   j                  d   j                   fd}t        j
                  j                  ||      S # t        $ r |cY S w xY w)rv   rw   c                     | j                  d      }j                  |      }|	 j                  j                  |      S y # t        $ r d|z  cY S w xY w)Nr   z\%s)rz   r{   rG   
serializer	Exception)rp   r   r   ri   r   s      rJ   r   z/HtmlInlineProcessor.unescape.<locals>.get_stash  s]    BIIbME *77--e44 ! ! *!E>)*s   A AAr   r   s   `  @rJ   r   zHtmlInlineProcessor.unescape  sX    	GG**84BBE	* ))--i>>  	K	s   #A AAN)rW   rX   rY   rZ   rq   r   r[   rL   rJ   r@   r@     s    ;2
?rL   r@   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d	 Zd
 Zd Zd Zd Zd Zy)rE   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   rz   parse_sub_patterns)ri   rp   r   idxr   r   s         rJ   build_singlezAsteriskProcessor.build_single  s6    mmC wwqzc45
rL   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   rz   r   appendr   r   	ri   rp   r^   r   r   r   r   r   r   s	            rJ   build_doublezAsteriskProcessor.build_double  s     ZZ_
dmmD!mmD!wwqzc45

3qxxz?a771:D##D#sC8
rL   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   rz   r   r   r   s	            rJ   build_double2zAsteriskProcessor.build_double2  s     ZZ_
dmmD!mmD!wwqzc45wwqz

3c45
rL   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` (`str`):
            text to evaluate.

        `parent` (`etree.Element`):
            Parent to attach text and sub elements to.

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

        `idx` (`int`):
            Current pattern index that was used to evaluate the parent.

        r   FNTr   )r   rg   match	enumeratePATTERNSr\   r   r   r   build_elementr]   r^   r   r   )ri   r   parentlastr   offsetposlengthmatchedindexitemrp   r   r   s                 rJ   r   z$AsteriskProcessor.parse_sub_patterns  s8   $ TFl%%dC0#,T]]#; 'KE4| **45A
  $F1771:6#/,0	.2!//4<<ERb)!'(uuQx/"&+', 1HC qA FlF FG} 	"	 rL   c                     |dk(  r| j                  |||      S |dk(  r| j                  |||      S | j                  |||      S )zElement builder.r   r   )r   r   r   )ri   rp   r]   r^   r  s        rJ   r   zAsteriskProcessor.build_element'  sS     i%%au55 $$Qe44$$Qe44rL   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^   )	ri   rp   r   r   r   r   r  r  m1s	            rJ   rq   zAsteriskProcessor.handleMatch1  s     $T]]3 	KE4##D!''!*5BffQi''DLL$))UK	 5#~rL   N)rW   rX   rY   rZ   rV   rc   rd   EM_STRONG_REre   rf   STRONG_EM_RESTRONG_EM3_RE	STRONG_REEMPHASIS_REr   r   r   r   r   r   rq   r[   rL   rJ   rE   rE     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>#@5rL   rE   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	)
rF   zIEmphasis processor for handling strong and em matches inside underscores.r   r   r   r   r   r   r   N)rW   rX   rY   rZ   rV   rc   rd   EM_STRONG2_REre   rf   STRONG_EM2_RESMART_STRONG_EM_RESMART_STRONG_RESMART_EMPHASIS_REr   r[   rL   rJ   rF   rF   B  s    S 	ZRZZryy2::/EFR]^ZRZZryy2::/EFR]^ZRZZ 2BII

4JKYXcdZRZZRZZ1GH(T\]ZRZZ 1299rzz3IJHVZ[HrL   rF   c                       e Zd ZdZ ej
                  dej                  ej                  z        Z ej
                  d      Z	d Z
d Zd Zy)r2   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 )Nr   NNNahreftitle)getTextr   getLinkr   r   r   setr   )	ri   rp   r   r   r  handledr  r  r   s	            rJ   rq   zLinkInlineProcessor.handleMatchS  s    #||D!%%(;eW#&*ll4&?#eUG#]]3
vtFF7E"1771:u$$rL   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 `()`. ry   NFr  r   rP   r   r   T())rO   rN    )RE_LINKr   rz   r   r   ranger   r~   RE_TITLE_CLEANr   rT   r   )ri   r   r  r  r  r  rp   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                        rJ   r  zLinkInlineProcessor.getLinkg  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**rL   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   ry   )r$  r   r   r~   )ri   r   r  r&  r   r  r1  s          rJ   r  zLinkInlineProcessor.getText  s    
 D	* 		CS	ACx"c"QJE!KKN		 wwt}e]a%777rL   N)rW   rX   rY   rZ   rc   rd   re   rf   r#  r%  rq   r  r  r[   rL   rJ   r2   r2   N  sL    7bjjRTVT]T]`b`j`jTjkGRZZ&N%(r+h8rL   r2   c                       e Zd ZdZd Zy)r4   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 )Nr   r  imgsrcr  alt)r  r   r  r   r   r  r   r   )	ri   rp   r   r   r  r  r8  r  r   s	            rJ   rq   z ImageInlineProcessor.handleMatch  s    #||D!%%(;eW#%)\\$%>"UE7#]]5!
ucFF7E"
udmmD)*1771:u$$rL   Nr   r[   rL   rJ   r4   r4     s
    6%rL   r4   c                       e Zd ZdZ ej
                  dej                        Z ej
                  dej                  ej                  z        Z
d Zd Zd Zy)r0   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 )Nr   r  r"  )	r  r   evalIdNEWLINE_CLEANUP_REr   rG   
referencesr   makeTag)
ri   rp   r   r   r  r  r   r   r  r  s
             rJ   rq   z$ReferenceInlineProcessor.handleMatch  s    #||D!%%(;eW#;;tUD9C# $$((b1TWW'''S((gg((,e||D%.
C??rL   c                     | j                   j                  ||      }|sd|dfS |j                  d      j                         }|j	                  d      }|s|j                         }||dfS )zV
        Evaluate the id portion of [ref][id].

        If [ref][] use [ref].
        r  NFr   r   T)r#  r   rz   lowerr   )ri   r   r  r   rp   r   r   s          rJ   r<  zReferenceInlineProcessor.evalId  sj     LLt/%%!!#B%%(CZZ\3}rL   c                     t        j                  d      }|j                  d|       |r|j                  d|       ||_        |S )Nr  r  r  )r   r   r  r   ri   r  r  r   r   s        rJ   r?  z ReferenceInlineProcessor.makeTag.  s;    ]]3
vtFF7E"	rL   N)rW   rX   rY   rZ   rc   rd   	MULTILINEr=  re   rf   r#  rq   r<  r?  r[   rL   rJ   r0   r0     sL    @#FBLL9bjj+RYY-CDG@$ rL   r0   c                       e Zd ZdZd Zy)r8   z#Short form of reference: [google]. c                 (    |j                         |dfS zEvaluate the id from of [ref]  TrA  ri   r   r  r   s       rJ   r<  z$ShortReferenceInlineProcessor.evalId;       zz|UD((rL   NrW   rX   rY   rZ   r<  r[   rL   rJ   r8   r8   9  s
    -)rL   r8   c                       e Zd ZdZd Zy)r6   z5 Match to a stored reference and return img element. c                     t        j                  d      }|j                  d|       |r|j                  d|       |j                  d| j                  |             |S )Nr7  r8  r  r9  )r   r   r  r   rC  s        rJ   r?  z%ImageReferenceInlineProcessor.makeTagC  sK    ]]5!
udFF7E"
udmmD)*	rL   N)rW   rX   rY   rZ   r?  r[   rL   rJ   r6   r6   A  s
    ?rL   r6   c                       e Zd ZdZd Zy)r9   z( Short form of inage reference: ![ref]. c                 (    |j                         |dfS rG  rH  rI  s       rJ   r<  z)ShortImageReferenceInlineProcessor.evalIdN  rJ  rL   NrK  r[   rL   rJ   r9   r9   L  s
    2)rL   r9   c                       e Zd ZdZd Zy)r:   zC Return a link Element given an autolink (`<http://example/com>`). c                 $   t        j                  d      }|j                  d| j                  |j	                  d                   t        j                  |j	                  d            |_        ||j                  d      |j                  d      fS )Nr  r  r   r   )
r   r   r  r   rz   r   r   r   r   r   r   s       rJ   rq   z#AutolinkInlineProcessor.handleMatchV  sf    ]]3
vt}}QWWQZ01##AGGAJ/1771:quuQx''rL   Nr   r[   rL   rJ   r:   r:   T  s
    M(rL   r:   c                       e Zd ZdZd Zy)r<   zT
    Return a mailto link Element given an automail link (`<foo@example.com>`).
    c           
      >   t        j                  d      }| j                  |j                  d            }|j	                  d      r|t        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 )	Nr  r   zmailto:c                     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     rJ   rU  z;AutomailInlineProcessor.handleMatch.<locals>.codepoint2nameg  sH    ,,006F~~d&9&96BB4#6#6"===rL   ry   z#%d;r  r   )r   r   r   rz   rQ   r   r   r   r   r~   r   rV  r  r   r   )	ri   rp   r   r   emailrU  letterlettersmailtos	            rJ   rq   z#AutomailInlineProcessor.handleMatcha  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3&DNr   r[   rL   rJ   r<   r<   ]  s    (rL   r<   )>rZ   ry   r   collectionsr   rc   xml.etree.ElementTreer   ElementTreer   r   ImportErrorhtmlentitydefsrK   NOIMGr-   r/   r  r  r  r  r  r	  r  r
  r  r  r3   r5   r1   r7   rD   r;   r=   rA   rB   r?   rT   rV   r`   r   r   rC   r.   r   r   r   r>   r,   r   r   r@   rE   rF   r2   r4   r0   r8   r6   r9   r:   r<   r[   rL   rJ   <module>ra     s3  <|  " 	 % %&
8
 	 M 	   	 ; :  P  - - - - : %-   #  ( = ' I =	 (:n.LM (8? 8?v-g -` 0 0.O .w  ( ( '+ '=#; =Yo Y '  )7 ) ?/ ?4E EP	+ 	^8/ ^8B%. %.02 0f)$< )$< ))F )(o ((o (q  &%&s   E- -	E:9E: