o
    MEþg	y  ã                   @   sÖ  d dl mZmZmZ d dlZd dlZd dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ ejdkrGejdkr>d dlZejZnd	d
lmZ eZn	d dlmZ ejZdej Ze d¡Ze de d d d e d d d e d ¡Ze d¡Ze dej ¡Ze dej ej¡Ze d¡Ze d¡Z e d¡Z!e d¡Z"e d¡Z#e dej¡Z$e d¡Z%e d¡Z&e d¡Z'e d ¡Z(e d!¡Z)e d"¡Z*e d#¡Z+e d$¡Z,e d%ej-¡Z.d&d'„ Z/d(d)„ Z0G d*d+„ d+e1ƒZ2dS ),é    )Úabsolute_importÚunicode_literalsÚdivisionN)Úcommon)Únormalize_uriÚunescape_string)ÚNode)Únormalize_reference)é   r   )r
   é   é   )Ú	_unescape)Úentitytransz\\a  [!"#$%&\'()*+,\-./:;<=>?@\[\]\\^_`{|}~\xA1\xA7\xAB\xB6\xB7\xBB\xBF\u037E\u0387\u055A-\u055F\u0589\u058A\u05BE\u05C0\u05C3\u05C6\u05F3\u05F4\u0609\u060A\u060C\u060D\u061B\u061E\u061F\u066A-\u066D\u06D4\u0700-\u070D\u07F7-\u07F9\u0830-\u083E\u085E\u0964\u0965\u0970\u0AF0\u0DF4\u0E4F\u0E5A\u0E5B\u0F04-\u0F12\u0F14\u0F3A-\u0F3D\u0F85\u0FD0-\u0FD4\u0FD9\u0FDA\u104A-\u104F\u10FB\u1360-\u1368\u1400\u166D\u166E\u169B\u169C\u16EB-\u16ED\u1735\u1736\u17D4-\u17D6\u17D8-\u17DA\u1800-\u180A\u1944\u1945\u1A1E\u1A1F\u1AA0-\u1AA6\u1AA8-\u1AAD\u1B5A-\u1B60\u1BFC-\u1BFF\u1C3B-\u1C3F\u1C7E\u1C7F\u1CC0-\u1CC7\u1CD3\u2010-\u2027\u2030-\u2043\u2045-\u2051\u2053-\u205E\u207D\u207E\u208D\u208E\u2308-\u230B\u2329\u232A\u2768-\u2775\u27C5\u27C6\u27E6-\u27EF\u2983-\u2998\u29D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-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)>z/^<[A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>\x00-\x20]*>z^ *(?:\n *)?z^^[ \t\n\x0b\x0c\x0d]z[ \t\n\x0b\x0c\x0d]+z^\sz *$z^ *z^ *(?:\n|$)z^\[(?:[^\\\[\]]|\\.){0,1000}\]z^[^\n`\[\]\\!<&*_\'"]+c                 C   s   t dd ƒ}| |_|S )NÚtext)r   Úliteral)ÚsÚnode© r   úl/var/www/eduai.edurigo.com/doc_train/edurigo_ai/Puru/venv/lib/python3.10/site-packages/commonmark/inlines.pyr   V   s   
r   c                 C   sŽ   d}d}t | ƒd dkrt | ƒd }n,t | ƒd dkr"t | ƒd }nt | ƒd dkr5d}t | ƒd d }n
d}t | ƒd d }d| d|  S )Nr   r
   é   r   r   u   â€”u   â€“)Úlen)ÚcharsÚen_countÚem_countr   r   r   Úsmart_dashes\   s   r   c                   @   sô   e Zd ZdZi fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ ZeZ d8S )9ÚInlineParserz»INLINE PARSER

    These are methods of an InlineParser class, defined below.
    An InlineParser keeps track of a subject (a string to be
    parsed) and a position in that subject.
    c                 C   s"   d| _ d | _d| _i | _|| _d S )NÚ r   )ÚsubjectÚbracketsÚposÚrefmapÚoptions)Úselfr#   r   r   r   Ú__init__y   s
   
zInlineParser.__init__c                 C   s>   t  || j| jd… ¡}|du rdS |  j| ¡ 7  _| ¡ S )z
        If regexString matches at current position in the subject, advance
        position in subject and return the match; otherwise return None.
        N)ÚreÚsearchr   r!   ÚendÚgroup)r$   ÚregexStringÚmatchr   r   r   r+   €   s
   zInlineParser.matchc                 C   s    | j t| jƒk r| j| j  S dS )zh Returns the character at the current subject position, or None if
        there are no more characters.N)r!   r   r   ©r$   r   r   r   ÚpeekŒ   s   zInlineParser.peekc                 C   s   |   t¡ dS )zL Parse zero or more space characters, including at
        most one newline.T)r+   ÚreSpnlr,   r   r   r   Úspnl”   s   
zInlineParser.spnlc                 C   sØ   |   t¡}|du rdS | j}|   t¡}|dur`||krWtddƒ}| j|| jt|ƒ …  dd¡}| d¡rM|d |d   krCdkrMn n|dd… |_	n||_	| 
|¡ d	S |   t¡}|dus|| _| 
t|ƒ¡ d	S )
z Attempt to parse backticks, adding either a backtick code span or a
        literal sequence of backticks to the 'inlines' list.NFÚcodeÚ
ú r   éÿÿÿÿr   T)r+   ÚreTicksHerer!   ÚreTicksr   r   r   ÚreplaceÚlstripr   Úappend_childr   )r$   ÚblockÚticksÚafter_open_ticksÚmatchedr   Úcontentsr   r   r   ÚparseBackticksž   s*   


ÿ*

õzInlineParser.parseBackticksc                 C   s®   | j }|  jd7  _z|| j }W n ty   d}Y nw |  ¡ dkr6|  jd7  _tddƒ}| |¡ dS |rNt t|¡rN| t	|ƒ¡ |  jd7  _dS | t	dƒ¡ dS )a  
        Parse a backslash-escaped special character, adding either the
        escaped character, a hard line break (if the backslash is followed
        by a newline), or a literal backslash to the block's children.
        Assumes current character is a backslash.
        r   Nr1   Ú	linebreakú\T)
r   r!   Ú
IndexErrorr-   r   r8   r&   r'   ÚreEscapabler   )r$   r9   ÚsubjÚsubjcharr   r   r   r   ÚparseBackslash·   s$   ÿ

úþzInlineParser.parseBackslashc                 C   s¨   |   t¡}|r*|dd… }tddƒ}td| ƒ|_d|_| t|ƒ¡ | |¡ dS |   t¡}|rR|dd… }tddƒ}t|ƒ|_d|_| t|ƒ¡ | |¡ dS dS )	z?Attempt to parse an autolink (URL or email in pointy brackets).r   r3   ÚlinkNzmailto:r   TF)	r+   ÚreEmailAutolinkr   r   ÚdestinationÚtitler8   r   Ú
reAutolink)r$   r9   ÚmÚdestr   r   r   r   ÚparseAutolinkÒ   s&   






zInlineParser.parseAutolinkc                 C   s6   |   tj¡}|du rdS tddƒ}||_| |¡ dS )z Attempt to parse a raw HTML tag.NFÚhtml_inlineT)r+   r   Ú	reHtmlTagr   r   r8   )r$   r9   rK   r   r   r   r   ÚparseHtmlTagí   s   

zInlineParser.parseHtmlTagc                 C   sb  d}| j }|dks|dkr|d7 }|  j d7  _ n|  ¡ |kr0|d7 }|  j d7  _ |  ¡ |ks|dkr6dS |dkr<dn| j|d  }|  ¡ }|du rMd}t t|¡pV|dk}t t|¡}t t|¡pf|dk}t t|¡}	| ov| pv|pv|	}
| o€|	 p€|p€|}|dkr”|
o‹| p‹|	}|o’|
 p’|}n|dksœ|dkr¤|
o | }|}n|
}|}|| _ |||d	œS )
a  
        Scan a sequence of characters == c, and return information about
        the number of delimiters and whether they are positioned such that
        they can open and/or close emphasis or strong emphasis.  A utility
        function for strong/emph parsing.
        r   ú'ú"r   Nr1   õ   Â Ú_)Ú	numdelimsÚcan_openÚ	can_close)r!   r-   r   r&   r'   ÚreUnicodeWhitespaceCharÚrePunctuation)r$   ÚcrU   ÚstartposÚc_beforeÚc_afterÚafter_is_whitespaceÚafter_is_punctuationÚbefore_is_whitespaceÚbefore_is_punctuationÚleft_flankingÚright_flankingrV   rW   r   r   r   Ú
scanDelimsø   sl   þÿÿþÿýÿýÿÿ
ýzInlineParser.scanDelimsc              
   C   s¶   |   |¡}|s	dS | d¡}| j}|  j|7  _|dkrd}n|dkr&d}n| j|| j… }t|ƒ}| |¡ ||||| jd| d¡| d	¡d
œ| _| jd durY| j| jd d< dS )z2Handle a delimiter marker for emphasis or a quote.FrU   rQ   õ   â€™rR   õ   â€œNrV   rW   )ÚccrU   Ú
origdelimsr   ÚpreviousÚnextrV   rW   ri   rj   T)rd   Úgetr!   r   r   r8   Ú
delimiters)r$   rg   r9   ÚresrU   r[   r=   r   r   r   r   ÚhandleDelim7  s2   


ø
zInlineParser.handleDelimc                 C   sT   |  d¡d ur|  d¡|d d< |  d¡d u r|  d¡| _d S |  d¡|d d< d S )Nri   rj   )rk   rl   )r$   Údelimr   r   r   ÚremoveDelimiterX  s
   zInlineParser.removeDelimiterc                 C   s&   |   d¡|kr|| d< | |d< d S d S )Nrj   ri   )rk   )ÚbottomÚtopr   r   r   ÚremoveDelimitersBetweena  s   þz$InlineParser.removeDelimitersBetweenc                 C   sx  ||||dœ}d}d}| j }|d ur)| d¡|kr)| d¡}|d ur)| d¡|ks|d ur–| d¡s:| d¡}nX| d¡}d}| d¡}|d ur–||kr–||| kr–| d¡s^| d¡oq|d	 d
 dkoq|d	 |d	  d
 dk}| d¡|krƒ| d¡rƒ|sƒd}n| d¡}|d ur–||kr–||| ksT|}	|dks¡|dkrO|s©| d¡}nÓ|d dkr·|d dkr·dnd}| d¡}
| d¡}|d  |8  < |d  |8  < |
jd t|
jƒ| … |
_|jd t|jƒ| … |_|dkr÷tdd ƒ}ntdd ƒ}|
j}|r||kr|j}| ¡  | |¡ |}|r||ks|
 |¡ |  	||¡ |d dkr8|
 ¡  |  
|¡ |d dkrN| ¡  |d }|  
|¡ |}n-|dkrfd|d _|rad|d _|d }n|dkr|d|d _|rxd|d _|d }|s’|s’|	d ||< |	d s’|  
|	¡ |d us.| j d ur¶| j |krº|  
| j ¡ | j d ur¸| j |ks¢d S d S d S d S )N)rT   Ú*rQ   rR   Fr   ri   rW   rj   rg   rV   rh   r
   Trt   rT   rU   r   r   r   ÚemphÚstrongrQ   re   u   â€˜rR   u   â€rf   )rl   rk   r   r   r   ÚnxtÚunlinkr8   Úinsert_afterrs   rp   )r$   Ústack_bottomÚopeners_bottomÚ	odd_matchÚ
use_delimsÚcloserÚopenerÚopener_foundÚcloserccÚ
old_closerÚ
opener_inlÚ
closer_inlru   Útmprw   Ú	tempstackr   r   r   ÚprocessEmphasisg  sÈ   ü
ÿ




ÿþÿÿÿýÿþ
ôþþ

ÿÿ

ü


€








Ÿd(ÿzInlineParser.processEmphasisc                 C   s&   |   t¡}|du rdS t|dd… ƒS )zn
        Attempt to parse link title (sans quotes), returning the string
        or None if no match.
        Nr   r3   )r+   ÚreLinkTitler   )r$   rI   r   r   r   ÚparseLinkTitleÞ  s   
zInlineParser.parseLinkTitlec                 C   sB  |   t¡}|du r—|  ¡ dkrdS | j}d}	 |  ¡ }|du r n^|dkrJt t| j| jd | jd … ¡rJ|  jd7  _|  ¡ durI|  jd7  _n3|dkrZ|  jd7  _|d7 }n#|d	kro|dk rcn|  jd7  _|d8 }nt t|¡rvn|  jd7  _q| j|kr‰|d	kr‰dS | j|| j… }t	t
|ƒƒS t	t
|dd
… ƒƒS )zf
        Attempt to parse link destination, returning the string or
        None if no match.
        Nú<r   Tr@   r   r   ú(ú)r3   )r+   ÚreLinkDestinationBracesr-   r!   r&   r'   rB   r   ÚreWhitespaceCharr   r   )r$   rm   ÚsaveposÚ
openparensrZ   r   r   r   ÚparseLinkDestinationê  sD   
ÿ€

ëz!InlineParser.parseLinkDestinationc                 C   s*   |   t¡}|du st|ƒdkrdS t|ƒS )z_
        Attempt to parse a link label, returning number of
        characters parsed.
        Nié  r   )r+   ÚreLinkLabelr   )r$   rK   r   r   r   ÚparseLinkLabel  s   
zInlineParser.parseLinkLabelc                 C   s8   | j }|  j d7  _ tdƒ}| |¡ |  ||d¡ dS )zf
        Add open bracket to delimiter stack and add a text node to
        block's children.
        r   ú[FT)r!   r   r8   Ú
addBracket©r$   r9   r[   r   r   r   r   ÚparseOpenBracket!  s   
zInlineParser.parseOpenBracketc                 C   sh   | j }|  j d7  _ |  ¡ dkr+|  j d7  _ tdƒ}| |¡ |  ||d d¡ dS | tdƒ¡ dS )z¥
        If next character is [, and ! delimiter to delimiter stack and
        add a text node to block's children. Otherwise just add a text
        node.
        r   r”   z![Tú!)r!   r-   r   r8   r•   r–   r   r   r   Ú	parseBang0  s   
þzInlineParser.parseBangc                 C   sh  d}d}|  j d7  _ | j }| j}|du r| tdƒ¡ dS | d¡s0| tdƒ¡ |  ¡  dS | d¡}| j }|  ¡ dkr||  j d7  _ |  ¡  |  ¡ }|dury|  ¡ ryt	 
t| j| j d  ¡re|  ¡ }|  ¡ rx|  ¡ d	krx|  j d7  _ d}n|| _ |s¿| j }	|  ¡ }
|
d
kr“| j|	|	|
 … }n| d¡s¢| j| d¡|… }|
dkr©|| _ |r¿| j t|ƒ¡}|r¿|d }|d }d}|r$t|rÇdnddƒ}||_|pÑd|_| d¡j}|rë|j}| ¡  | |¡ |}|sÛ| |¡ |  | d¡¡ |  ¡  | d¡ ¡  |s"| j}|dur"| d¡sd|d< | d¡}|dusdS |  ¡  || _ | tdƒ¡ dS )zþ
        Try to match close bracket against an opening in the delimiter
        stack. Add either a link or image, or a plain [ character,
        to block's children. If there is a matching delimiter,
        remove it from the delimiter stack.
        NFr   ú]TÚactiveÚimager‹   rŒ   r   Úbracket_afterÚindexr   rH   rI   rF   r   r   ÚpreviousDelimiterri   )r!   r    r8   r   rk   ÚremoveBracketr-   r/   r‘   r&   r'   rŽ   r   r‰   r“   r"   r	   r   rH   rI   rw   rx   r‡   )r$   r9   rI   r<   r[   r   Úis_imager   rL   ÚbeforelabelÚnÚreflabelrF   r   r…   rw   r   r   r   ÚparseCloseBracketE  sˆ   

€


ü



üzInlineParser.parseCloseBracketc                 C   s0   | j d ur
d| j d< || j | j||ddœ| _ d S )NTÚbracketAfter)r   ri   rŸ   rž   rœ   r›   )r    rl   )r$   r   rž   rœ   r   r   r   r•   ³  s   

úzInlineParser.addBracketc                 C   s   | j  d¡| _ d S )Nri   )r    rk   r,   r   r   r   r    À  s   zInlineParser.removeBracketc                 C   s(   |   t¡}|r| tt|ƒƒ¡ dS dS )zAttempt to parse an entity.TF)r+   ÚreEntityHerer8   r   ÚHTMLunescape)r$   r9   rK   r   r   r   ÚparseEntityÃ  s
   
zInlineParser.parseEntityc                 C   sb   |   t¡}|r/| j d¡r&t td|¡}t tdd„ |¡}| t	|ƒ¡ dS | t	|ƒ¡ dS dS )zŠ
        Parse a run of ordinary characters, or a single character with
        a special meaning in markdown, as a plain string.
        Úsmartu   â€¦c                 S   s   t |  ¡ ƒS )N)r   r)   )Úxr   r   r   Ú<lambda>Õ  s    z*InlineParser.parseString.<locals>.<lambda>TF)
r+   ÚreMainr#   rk   r&   ÚsubÚ
reEllipsesÚreDashr8   r   )r$   r9   rK   r   r   r   r   ÚparseStringÌ  s   
ÿzInlineParser.parseStringc                 C   s¢   |  j d7  _ |j}|rB|jdkrB|jd dkrBt|jƒdko%|jd dk}t td|j¡|_|r7tdd	ƒ}ntd
d	ƒ}| 	|¡ n| 	td
d	ƒ¡ |  
t¡ dS )z„
        Parse a newline.  If it was preceded by two spaces, return a hard
        line break; otherwise a soft line break.
        r   r   r3   r2   r   éþÿÿÿr   r?   NÚ	softbreakT)r!   Ú
last_childÚtr   r   r&   r®   ÚreFinalSpacer   r8   r+   ÚreInitialSpace)r$   r9   Úlastcr?   r   r   r   r   ÚparseNewlineÝ  s   

zInlineParser.parseNewlinec                 C   s@  || _ d| _| j}|  ¡ }|dks|dkrdS | j d|… }|  ¡ dkr,|  jd7  _n|| _dS |  ¡  |  ¡ }|du rB|| _dS | j}|  ¡  d}| j|krT|  ¡ }|du r]d}|| _d}	|  t¡du r{|dkrmd}	n|dk || _|  t¡du}	|	s‚|| _dS t	|ƒ}
|
dkr|| _dS | 
|
¡s›||d	œ||
< | j| S )
z4Attempt to parse a link reference, modifying refmap.r   r   Nú:r   r   TF)rH   rI   )r   r!   r“   r-   r/   r‘   r‰   r+   ÚreSpaceAtEndOfLiner	   rk   )r$   r   r"   r[   Úmatch_charsÚrawlabelrL   ÚbeforetitlerI   Úat_line_endÚ	normlabelr   r   r   ÚparseReferenceô  sV   


þ
zInlineParser.parseReferencec                 C   s8  d}|   ¡ }|du rdS |dkr|  |¡}nt|dkr |  |¡}nj|dkr*|  |¡}n`|dks2|dkr9|  ||¡}nQ|dksA|d	krN| j d
¡oL|  ||¡}n<|dkrX|  |¡}n2|dkrb|  |¡}n(|dkrl|  	|¡}n|dkr{|  
|¡py|  |¡}n|dkr…|  |¡}n|  |¡}|sš|  jd7  _| t|ƒ¡ dS )zÊ
        Parse the next inline element in subject, advancing subject
        position.

        On success, add the result to block's children and return True.
        On failure, return False.
        FNr1   r@   ú`rt   rT   rQ   rR   rª   r”   r˜   rš   rŠ   ú&r   T)r-   r¹   rE   r>   rn   r#   rk   r—   r™   r¥   rM   rP   r©   r±   r!   r8   r   )r$   r9   rm   rZ   r   r   r   ÚparseInline:  s:   
zInlineParser.parseInlinec                 C   sH   |j  ¡ | _d| _d| _d| _|  |¡r	 |  |¡sd|_ |  d¡ dS )zq
        Parse string content in block into inline children,
        using refmap to resolve references.
        r   N)Ústring_contentÚstripr   r!   rl   r    rÄ   r‡   )r$   r9   r   r   r   ÚparseInlinesc  s   

ÿzInlineParser.parseInlinesN)!Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   r+   r-   r/   r>   rE   rM   rP   rd   rn   rp   Ústaticmethodrs   r‡   r‰   r‘   r“   r—   r™   r¥   r•   r    r©   r±   r¹   rÁ   rÄ   rÇ   Úparser   r   r   r   r   q   s>    
?!	
w*n	F)r   )3Ú
__future__r   r   r   r&   ÚsysÚ
commonmarkr   Úcommonmark.commonr   r   Úcommonmark.noder   Úcommonmark.normalize_referencer	   Úversion_infoÚhtmlÚunescaper¨   r   r   Ú	ESCAPABLEÚESCAPED_CHARÚcompilerY   rˆ   r   rB   ÚENTITYÚ
IGNORECASEr§   r5   r4   r¯   r°   rG   rJ   r.   rŽ   ÚreWhitespacerX   r¶   r·   r»   r’   Ú	MULTILINEr­   r   r   Úobjectr   r   r   r   r   Ú<module>   s€    


ÿ
ÿþþþýüüüÿ




ÿþ







