
    x'h<              	          d Z ddlZddlZddlmZmZmZ dddZdedefd	Z	dedefd
Z
d%dedee   defdZd%dedee   defdZd%dedee   defdZd%dedee   defdZd%dedee   defdZd%dedee   defdZd%dedee   defdZd%dedee   defdZd%dedee   defdZd%dedee   defdZd%dededee   defdZd%dededee   defdZd&dededee   defdZd%dedee   defdZd'dedee   dedefdZdedefdZd(dedee   dee   defdZd(dedee   dee   defd Zd!ed"ee   d#eeeef      defd$Zy))z@
Markdown & HTML formatting functions.

.. versionadded:: 4.5.1
    N)OptionalListDict
)	separatorc                 $    | j                  |      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   argss     J/var/www/flask-api/venv/lib/python3.12/site-packages/telebot/formatting.pyformat_textr      s    ( >>$    contentreturnc                 ,    t        j                  |       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   s    r   escape_htmlr   #   s     ;;wr   c                 b    t        j                  dd|       }t        j                  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reparses      r   escape_markdownr   0   s/     FF6IEff<eUKGNr   r   c                 F    dj                  |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   s     r   mboldr   B   "     ==V1IIIIr   c                 F    dj                  |rt        |             S |       S )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   s     r   hboldr"   R   $     fk'2JJ'JJr   c                 F    dj                  |rt        |             S |       S )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   s     r   mitalicr%   b   "     ??v?73KK7KKr   c                 F    dj                  |rt        |             S |       S )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   s     r   hitalicr(   r   r#   r   c                 F    dj                  |rt        |             S |       S )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   s     r   
munderliner*      r&   r   c                 F    dj                  |rt        |             S |       S )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   s     r   
hunderliner,      s$     fk'2JJ'JJr   c                 F    dj                  |rt        |             S |       S )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   s     r   mstrikethroughr.      r   r   c                 F    dj                  |rt        |             S |       S )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   s     r   hstrikethroughr0      r#   r   c                 F    dj                  |rt        |             S |       S )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   s     r   mspoilerr2      r&   r   c                 F    dj                  |rt        |             S |       S )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   s     r   hspoilerr4      s%     )//G0D\\T[\\r   urlc                 Z    dj                  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   r5   r   s      r   mlinkr8      s,      _W5vs7K__[^__r   c                 Z    dj                  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!   r7   s      r   hlinkr:      s-      !''C(8RX+g:Nff^effr   languagec                 H    dj                  ||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   s      r   mcoder=   	  s'       v?7+C[[SZ[[r   c                 F    dj                  |rt        |             S |       S )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   s     r   hcoder?     s$     ##FK$8PPPPr   c                 H    dj                  ||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;   s      r   hprerA   )  s)     399(\bKPWDXpphoppr   c                     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> )r5   s    r   	hide_linkrD   9  s     se=))r   
expandablec                     |rt        |       n| } dj                  | j                  d      D cg c]  }d|z   	 c}      } |rd|  dS | S c c}w )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   >z**z||)r   r	   split)r   r   rE   lines       r   mciterJ   F  sU      +1og&gGiid0CDtDEGG9BN Es   Ac                 P    dj                  |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 expandable r!   )r   r   rE   s      r   hciterM   ]  s5      +11# &G ,3 r   textentitiescustom_subsc                 .   |s2| j                  dd      j                  dd      j                  dd      S ddd	d
dddddddd|r|j                         D ]
  \  }}||<    | j                  d      }d}dfd	}d}d}	d}
|D ]  }|j                  |kD  r| |||dz  |j                  dz         z  }|j                  } |||dz  ||j                  z   dz   |j
                  |j                  |j                  |j                  |j                        }t        |      }	||z  }||j                  z  }t        |      }
|j                  |k(  r |||dz  ||j                  z   dz   |j
                  |j                  |j                  |j                  |j                        }t        |      }	||z  }t        |      }
||j                  z  }O||	|
 j                  d      } |||j
                  |j                  |j                  |j                  |j                        j                  dd      j                  dd      j                  dd      }|d|	 |z   ||
d z   }t        |      }
 |dz  t        |      k  r| |||dz  d       z  }|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;rG   &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-lerL   Nc                    | j                  d      } |dk(  rd}dj                  |j                        }n|dk(  rdj                  | dd        }| j                  dd	      j                  d
d      j                  dd      } |rj	                  |      s| S j	                  |      }|dk(  r|j                  | |      S |dk(  r|rdj                  ||       S |j                  | |      S )Nrb   text_mentionr[   ztg://user?id={0}mentionzhttps://t.me/{0}   rR   rS   rT   rU   rG   rV   r_   )rN   custom_emoji_idrY   z0<pre><code class="language-{0}">{1}</code></pre>)rN   r5   )decoder   idreplaceget)upd_text
subst_typer5   userrg   r;   subs_subss          r   funcz!apply_html_entities.<locals>.func  s    ??;/'$J$++DGG4C9$$++HQRL9C##C199#vFNNsTZ[:!6Oyy$';;Ho;NNE!xGNNxYabb{{c{22r   r      )rm   r5   rn   rg   r;   )NNNNN)rj   itemsencodeoffsetlengthtyper5   rn   rg   r;   len)rN   rO   rP   keyvalue
utf16_text	html_textrq   ru   start_index	end_indexentity
new_stringentity_stringformatted_stringrp   s                  @r   apply_html_entitiesr   s  s   2 ||C)11#v>FFsFSS  !"%3($=T7!ME  %++- 	JCE#J	[)JI3" FKI !'==6! j!V]]Q5FGHHI]]Fj!fv}}6LPQ5QR_e_j_j"(**6;;PVPfPf'-8J i.K#Ifmm#FII]]f$j!fv}}6LPQ5QR_e_j_j"(**6;;PVPfPf'-8J i.K#IIIfmm#F &k9=DD[QM#Mfkkvzz`f`k`k4:4J4J-3__ > %ggfc&:7763;O  ",;/2BBYyzEZZIIIC!'F zC
O#T*VaZ[122	r   )T)rL   T)TrL   )TF)__doc__r   r   typingr   r   r   r   strr   r   boolr   r"   r%   r(   r*   r,   r.   r0   r2   r4   r8   r:   r=   r?   rA   rD   rJ   rM   r   rC   r   r   <module>r      s
   
  ' ' "&  .
  
  
 S S $J3 J J J K3 K K K LS L(4. L# L KS K(4. K# K L LXd^ LS L K KXd^ KS K"JC J$ J J KC K$ K K Lc L8D> L3 L ]c ]8D> ]3 ] `3 `S `(4. `# `&g3 gS g(4. g# g&\3 \# \(4. \# \ Q3 Q Q Q q# qx~ qc q q 
*3 
*3 
*3  8D> ^a .3  8D> ^a ,lc lXd^ l(SWX[]`X`SaJb lgj lr   