o
    MEg                  	   @   s  d dl Z d dlmZmZmZmZmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZmZ ddlmZ e de jZG d	d
 d
eZee Zeegef Zeeegef Ze djfdededefddZdedeeeee ee f  fddZd&dede	eef dedefddZe dkrd dl!m"Z" d dl#mZ e"ddZ$e$%d e$%d e$%d  d d!l&m%Z% e%ed" e%ed# e%ed$ e%ed% dS dS )'    N)CallableIterableListMatch
NamedTupleOptionalTupleUnion   )MarkupError)Style)SpanText)_emoji_replacez((\\*)\[([a-z#\/].*?)\])c                   @   sJ   e Zd ZU dZeed< 	 ee ed< 	 defddZedefddZ	d	S )
TagzA tag in console markup.name
parametersreturnc                 C   s"   | j d u r| jS | j d| j  S )N r   r   self r   e/var/www/eduai.edurigo.com/doc_train/edurigo_ai/Puru/venv/lib/python3.10/site-packages/rich/markup.py__str__   s   zTag.__str__c                 C   s.   | j du rd| j dS d| j d| j  dS )z*Get the string representation of this tag.N[]=r   r   r   r   r   markup   s
   
z
Tag.markupN)
__name__
__module____qualname____doc__str__annotations__r   r   propertyr   r   r   r   r   r      s   
 r   z(\\*)(\[[a-z#\/].*?\])r   _escaper   c                 C   s$   dt t dtfdd}||| } | S )zEscapes text so that it won't be interpreted as markup.

    Args:
        markup (str): Content to be inserted in to markup.

    Returns:
        str: Markup with square brackets escaped.
    matchr   c                 S   s   |   \}}| | d| S )z!Called by re.sub replace matches.\)groups)r'   backslashestextr   r   r   escape_backslashes8   s   z"escape.<locals>.escape_backslashes)r   r#   )r   r&   r,   r   r   r   escape,   s   
r-   c                 c   s   d}t }t}t| D ]`}| \}}}| \}}	||kr)|| || dfV  |rU|t|d\}
}|
rD|d|
 dfV  ||
d 7 }|rU||t|d dfV  |	}q|d\}}}|d|||re|ndfV  |	}q|t| k r|| |d dfV  dS dS )zParse markup in to an iterable of tuples of (position, text, tag).

    Args:
        markup (str): A string containing console markup

    r   N   r(   r   )divmodr   RE_TAGSfinditerr)   spanlen	partition)r   position_divmod_Tagr'   	full_textescapestag_textstartendr*   escapedr+   equalsr   r   r   r   _parseA   s0   r?    Tstyleemojic              
      s  t }d| vrt|r|| |dS | |dS t|d}|j}tj}g j g }|j}t}	t}
dtdt	t
tf f fdd}t| D ]\}}}|durV||rR||n| qB|dur|jdr|jd	d  }|r||}z||\}}W n, ty   td
|j d| ddw z  \}}W n ty   td| ddw ||	|t|t| qB|
||j|j}t||f qBt|}r \}}t|}|r||	||| st||_|S )a9  Render console markup in to a Text instance.

    Args:
        markup (str): A string containing console markup.
        emoji (bool, optional): Also render emoji code. Defaults to True.

    Raises:
        MarkupError: If there is a syntax error in the markup.

    Returns:
        Text: A test instance.
    r   )rA   
style_namer   c                    s<   t tdD ]\}\}}|j| kr |   S qt| )z"Pop tag matching given style name.r
   )	enumeratereversedr   KeyError)rC   index_tagpopstyle_stackr   r   	pop_style   s
   
zrender.<locals>.pop_styleN/r
   zclosing tag 'z' at position z doesn't match any open tagzclosing tag '[/]' at position z has nothing to close)r   r   appendr   	normalizerK   r   r   r#   r   intr?   r   
startswithstriprF   r   r   
IndexErrorr3   r   sortedspans)r   rA   rB   emoji_replacer+   rO   rP   rV   append_span_Spanr7   rM   r5   
plain_textrI   rC   r;   open_tagnormalized_tagtext_lengthr   rJ   r   renderb   sh   
 

r^   __main__)Console)r   F)	highlightzHello [1], [1,2,3] ['hello']fooz;Hello [link=https://www.willmcgugan.com]W[b red]o[/]rld[/]!)printz[red]z\[red]z\\[red]z\\\[red])r@   T)'retypingr   r   r   r   r   r   r   r	   errorsr   rA   r   r+   r   r   r   compileVERBOSEr0   r   r#   _ReStringMatch_ReSubCallable_EscapeSubMethodsubr-   rQ   r?   boolr^   r   rich.consoler`   	rich.textconsolerc   richr   r   r   r   <module>   sJ    (

($!J



