o
    @Mh<                  	   @   s  d Z ddlZddlZddlmZmZmZ ddddZded	efd
dZ	ded	efddZ
d>dedee d	efddZd>dedee d	efddZd>dedee d	efddZd>dedee d	efddZd>dedee d	efddZd>dedee d	efddZd>dedee d	efddZd>dedee d	efddZd>dedee d	efd d!Zd>dedee d	efd"d#Zd>ded$edee d	efd%d&Zd>ded$edee d	efd'd(Zd?ded*edee d	efd+d,Zd>dedee d	efd-d.Zd@dedee d*ed	efd/d0Zd$ed	efd1d2ZdAdedee d4ee d	efd5d6ZdAdedee d4ee d	efd7d8Zd9ed:ee d;eeeef  d	efd<d=ZdS )Bz@
Markdown & HTML formatting functions.

.. versionadded:: 4.5.1
    N)OptionalListDict
)	separatorc                 G   s
   |  |S )a  
    Formats a list of strings into a single string.

    .. code:: python3

        format_text( # just an example
            mbold('Hello'),
            mitalic('World')
        )

    :param args: Strings to format.
    :type args: :obj:`str`

    :param separator: The separator to use between each string.
    :type separator: :obj:`str`

    :return: The formatted string.
    :rtype: :obj:`str`
    )join)r   args r	   E/var/www/html/venv/lib/python3.10/site-packages/telebot/formatting.pyformat_text   s   
r   contentreturnc                 C   s
   t | S )z
    Escapes HTML characters in a string of HTML.

    :param content: The string of HTML to escape.
    :type content: :obj:`str`

    :return: The escaped string.
    :rtype: :obj:`str`
    )htmlescape)r   r	   r	   r
   escape_html#   s   

r   c                 C   s    t dd| }t dd|}|S )z
    Escapes Markdown characters in a string of Markdown.

    Credits to: simonsmh

    :param content: The string of Markdown to escape.
    :type content: :obj:`str`

    :return: The escaped string.
    :rtype: :obj:`str`
    z ([_*\[\]()~`>\#\+\-=|\.!\{\}\\])z\\\1z$\\\\([_*\[\]()~`>\#\+\-=|\.!\{\}\\])z\1)resub)r   parsereparser	   r	   r
   escape_markdown0   s   r   Tr   c                 C      d |r	t| S | S )a(  
    Returns a Markdown-formatted bold string.

    :param content: The string to bold.
    :type content: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    z*{}*formatr   r   r   r	   r	   r
   mboldB      r   c                 C   r   )a%  
    Returns an HTML-formatted bold string.

    :param content: The string to bold.
    :type content: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    z	<b>{}</b>r   r   r   r	   r	   r
   hboldR   r   r   c                 C   r   )a/  
    Returns a Markdown-formatted italic string.

    :param content: The string to italicize.
    :type content: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    z_{}_r   r   r	   r	   r
   mitalicb   r   r   c                 C   r   )a,  
    Returns an HTML-formatted italic string.

    :param content: The string to italicize.
    :type content: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    z	<i>{}</i>r   r   r	   r	   r
   hitalicr   r   r   c                 C   r   )a2  
    Returns a Markdown-formatted underline string.

    :param content: The string to underline.
    :type content: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    z__{}__r   r   r	   r	   r
   
munderline   r   r    c                 C   r   )a0  
    Returns an HTML-formatted underline string.

    :param content: The string to underline.
    :type content: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`

    z	<u>{}</u>r   r   r	   r	   r
   
hunderline   s   r!   c                 C   r   )a:  
    Returns a Markdown-formatted strikethrough string.

    :param content: The string to strikethrough.
    :type content: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    z~{}~r   r   r	   r	   r
   mstrikethrough   r   r"   c                 C   r   )a7  
    Returns an HTML-formatted strikethrough string.

    :param content: The string to strikethrough.
    :type content: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    z	<s>{}</s>r   r   r	   r	   r
   hstrikethrough   r   r#   c                 C   r   )a.  
    Returns a Markdown-formatted spoiler string.

    :param content: The string to spoiler.
    :type content: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    z||{}||r   r   r	   r	   r
   mspoiler   r   r$   c                 C   r   )a+  
    Returns an HTML-formatted spoiler string.

    :param content: The string to spoiler.
    :type content: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    z<tg-spoiler>{}</tg-spoiler>r   r   r	   r	   r
   hspoiler   r   r%   urlc                 C   s   d t| |rt|S |S )a`  
    Returns a Markdown-formatted link string.

    :param content: The string to link.
    :type content: :obj:`str`

    :param url: The URL to link to.
    :type url: str

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    z[{}]({})r   r   r&   r   r	   r	   r
   mlink      r(   c                 C   s   d t||rt| S | S )ad  
    Returns an HTML-formatted link string.

    :param content: The string to link.
    :type content: :obj:`str`

    :param url: The URL to link to.
    :type url: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    z<a href="{}">{}</a>r   r'   r	   r	   r
   hlink   r)   r*    languagec                 C   s   d ||r
t| S | S )a(  
    Returns a Markdown-formatted code string.

    :param content: The string to code.
    :type content: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    z```{}
{}```r   )r   r,   r   r	   r	   r
   mcode	     r-   c                 C   r   )a%  
    Returns an HTML-formatted code string.

    :param content: The string to code.
    :type content: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    z<code>{}</code>r   r   r	   r	   r
   hcode  r   r/   c                 C   s   d ||r
t| S | S )a5  
    Returns an HTML-formatted preformatted string.

    :param content: The string to preformatted.
    :type content: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    z%<pre><code class="{}">{}</code></pre>r   )r   r   r,   r	   r	   r
   hpre)  r.   r0   c                 C   s   d|  dS )z
    Hide url of an image.

    :param url: The url of the image.
    :type url: :obj:`str`
    
    :return: The hidden url.
    :rtype: :obj:`str`
    z	<a href="z">&#8288;</a>r	   )r&   r	   r	   r
   	hide_link9  s   
r1   F
expandablec                 C   s>   |rt | n| } ddd | dD } |rd|  dS | S )a  
    Returns a Markdown-formatted block-quotation string.

    :param content: The string to bold.
    :type content: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :param expandable: True if you need the quote to be expandable. Defaults to False.
    :type expandable: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    r   c                 S   s   g | ]}d | qS )>r	   ).0liner	   r	   r
   
<listcomp>W  s    zmcite.<locals>.<listcomp>z**z||)r   r   splitr   r   r2   r	   r	   r
   mciteF  s
   r9   c                 C   s"   d |rdnd|rt| S | S )a  
    Returns a html-formatted block-quotation string.

    :param content: The string to bold.
    :type content: :obj:`str`

    :param escape: True if you need to escape special characters. Defaults to True.
    :type escape: :obj:`bool`

    :param expandable: True if you need the quote to be expandable. Defaults to False.
    :type expandable: :obj:`bool`

    :return: The formatted string.
    :rtype: :obj:`str`
    z<blockquote{}>{}</blockquote>z expandabler+   r   r8   r	   r	   r
   hcite]  s   

r:   textentitiescustom_subsc                    s  |s|  dd dd ddS ddd	d
dddddddd |r-| D ]\}}| |< q$| d}d}d fdd	}d}d}	d}
|D ]}|j|kr||||d |jd  7 }|j}|||d ||j d  |j|j|j|j|j	d}t
|}	||7 }||j7 }t
|}
qC|j|kr|||d ||j d  |j|j|j|j|j	d}t
|}	||7 }t
|}
||j7 }qC||	|
 d}|||j|j|j|j|j	d dd dd dd}|d|	 | ||
d  }t
|}
qC|d t
|k r||||d d 7 }|S )a  
    Author: @sviat9440
    Updaters: @badiboy, @EgorKhabarov
    Message: "*Test* parse _formatting_, [url](https://example.com), [text_mention](tg://user?id=123456) and mention @username"

    .. code-block:: python3
        :caption: Example:

        apply_html_entities(text, entities)
        >> "<b>Test</b> parse <i>formatting</i>, <a href="https://example.com">url</a>, <a href="tg://user?id=123456">text_mention</a> and mention @username"

    Custom subs:
        You can customize the substitutes. By default, there is no substitute for the entities: hashtag, bot_command, email. You can add or modify substitute an existing entity.
    .. code-block:: python3
        :caption: Example:

        apply_html_entities(
            text,
            entities,
            {"bold": "<strong class="example">{text}</strong>", "italic": "<i class="example">{text}</i>", "mention": "<a href={url}>{text}</a>"},
        )
        >> "<strong class="example">Test</strong> parse <i class="example">formatting</i>, <a href="https://example.com">url</a> and <a href="tg://user?id=123456">text_mention</a> and mention <a href="https://t.me/username">@username</a>"
    &&amp;<&lt;r3   &gt;z<b>{text}</b>z<i>{text}</i>z<pre>{text}</pre>z<code>{text}</code>z<a href="{url}">{text}</a>z<s>{text}</s>z<u>{text}</u>z&<span class="tg-spoiler">{text}</span>z8<tg-emoji emoji-id="{custom_emoji_id}">{text}</tg-emoji>z<blockquote>{text}</blockquote>z*<blockquote expandable>{text}</blockquote>)bolditalicprecode	text_linkstrikethrough	underlinespoilercustom_emoji
blockquoteexpandable_blockquote	utf-16-ler+   Nc                    s   |  d} |dkrd}d|j}n|dkrd| dd  }| dd	d
ddd} |r4 |s6| S  |}|dkrF|j| |dS |dkrR|rRd|| S |j| |dS )NrN   text_mentionrG   ztg://user?id={0}mentionzhttps://t.me/{0}   r>   r?   r@   rA   r3   rB   rK   )r;   custom_emoji_idrE   z0<pre><code class="language-{0}">{1}</code></pre>)r;   r&   )decoder   idreplaceget)upd_text
subst_typer&   userrR   r,   subs_subsr	   r
   func  s   

z!apply_html_entities.<locals>.funcr      )rX   r&   rY   rR   r,   )NNNNN)rU   itemsencodeoffsetlengthtyper&   rY   rR   r,   len)r;   r<   r=   keyvalue
utf16_text	html_textr]   ra   start_index	end_indexentity
new_stringentity_stringformatted_stringr	   r[   r
   apply_html_entitiess  sp   






ro   )T)r+   T)Tr+   )TF)__doc__r   r   typingr   r   r   r   strr   r   boolr   r   r   r   r    r!   r"   r#   r$   r%   r(   r*   r-   r/   r0   r1   r9   r:   ro   r	   r	   r	   r
   <module>   s4        $$.