U
    h                     @   s   d Z ddlZddlZddlmZmZmZmZ ddlm	Z	 ddl
mZmZmZmZmZmZmZ eeeeedZedZd	Zdd
dZdddZdS )z
Simple markdown parser which does not support nesting. Intended primarily
for use within the library, which attempts to handle emojies correctly,
since they seem to count as two characters and it's a bit strange.
    N   )add_surrogatedel_surrogatewithin_surrogate
strip_text)TLObject)MessageEntityBoldMessageEntityItalicMessageEntityCodeMessageEntityPreMessageEntityTextUrlMessageEntityMentionNameMessageEntityStrike)z**__z~~`z```z\[([^]]*?)\]\(([\s\S]*?)\)z
[{0}]({1})c              
   C   s  | s| g fS |dkrt }nt|tr.t|}|sF|dk	rB| g fS t}tddd t|tddD }d}g }t	| } |t| k r|j
| |d}|rttd| }| ||t| d	 }|d
krd| d| | |t| | | |t| d f} |D ]h}	|	j|	j |kr|	j|kr`|	j|	j |t| kr`|	 jt|d 8  _n|	 jt|8  _q|| }	|	tkr||	||| t| d n||	||| t|  |	ttfkrz|t| }qzn|r|j
| |d}|rd| d|  |d	| | d f} | |  t|d	 }
|D ]*}	|	j|	j | krJ|	 j|
8  _qJ|t| t|d	t|dd |t|d	7 }qz|d	7 }qzt| |} t| |fS )a  
    Parses the given markdown message and returns its stripped representation
    plus a list of the MessageEntity's that were found.

    :param message: the message with markdown-like syntax to be parsed.
    :param delimiters: the delimiters to be used, {delimiter: type}.
    :param url_re: the URL bytes regex to be used. Must have two groups.
    :return: a tuple consisting of (clean message, [message entities]).
    N|c                 s   s   | ]}d  t|V  qdS )z({})N)formatreescape).0k r   @/tmp/pip-unpacked-wheel-c81u5j2r/telethon/extensions/markdown.py	<genexpr>7   s   zparse.<locals>.<genexpr>T)keyreverser   )pos    r   )offsetlengthurl)DEFAULT_URL_RE
isinstancestrr   compileDEFAULT_DELIMITERSjoinsortedlenr   matchnextfiltergroupsfindr    r!   r   appendr
   startgroupendr   r   r   )message
delimitersZurl_reZdelim_reiresultmdelimr3   entZ
delim_sizer   r   r   parse   sz    





&
  

r;   c                 C   sx  | r|s| S |s |dk	r| S t }|dk	r2td t|trB|f}t| } dd | D }g }t|D ]\}}|j}|j|j	 }|
t|d}	|	r||||	f ||| |	f qhd}
t|tr|j}
nt|trd|j}
|
rh|||df ||| d|
f qh|jdd	 d
 |rp| \}}}t| |rP|d7 }q8| d| | | |d  } q$t| S )aZ  
    Performs the reverse operation to .parse(), effectively returning
    markdown-like syntax given a normal text and its MessageEntity's.

    :param text: the text to be reconverted into markdown.
    :param entities: the MessageEntity's applied to the text.
    :return: a markdown-like text representing the combination of both inputs.
    Nzurl_fmt is deprecatedc                 S   s   i | ]\}}||qS r   r   )r   r   vr   r   r   
<dictcomp>   s      zunparse.<locals>.<dictcomp>ztg://user?id={}[z]({})c                 S   s   | d | d fS )Nr   r   r   )tr   r   r   <lambda>       zunparse.<locals>.<lambda>)r   r   )r'   warningswarnr$   r   r   items	enumerater    r!   gettyper0   r   r"   r   r   Zuser_idsortpopr   r   )textentitiesr5   Zurl_fmtZ	insert_atr6   entityse	delimiterr"   at_whatr   r   r   unparse   sF    	



 rS   )NN)NN)__doc__r   rB   Zhelpersr   r   r   r   tlr   Ztl.typesr   r	   r
   r   r   r   r   r'   r&   r#   ZDEFAULT_URL_FORMATr;   rS   r   r   r   r   <module>   s   $

l