diff --git a/pyrogram/client/ext/__init__.py b/pyrogram/client/ext/__init__.py index 8b44ca58..e363d3d4 100644 --- a/pyrogram/client/ext/__init__.py +++ b/pyrogram/client/ext/__init__.py @@ -20,4 +20,5 @@ from .base_client import BaseClient from .dispatcher import Dispatcher from .emoji import Emoji from .file_data import FileData +from .link import Link from .syncer import Syncer diff --git a/pyrogram/client/ext/link.py b/pyrogram/client/ext/link.py new file mode 100644 index 00000000..bd2d82cf --- /dev/null +++ b/pyrogram/client/ext/link.py @@ -0,0 +1,52 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2020 Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +import html + + +class Link(str): + HTML = "{text}" + MD = "[{text}]({url})" + + def __init__(self, url: str, text: str, style: str): + super().__init__() + + self.url = url + self.text = text + self.style = style + + @staticmethod + def format(url: str, text: str, style: str): + if style in ["md", "markdown"]: + fmt = Link.MD + elif style in ["combined", "html", None]: + fmt = Link.HTML + else: + raise ValueError("{} is not a valid style/parse mode".format(style)) + + return fmt.format(url=url, text=html.escape(text)) + + # noinspection PyArgumentList + def __new__(cls, url, text, style): + return str.__new__(cls, Link.format(url, text, style)) + + def __call__(self, other: str = None, *, style: str = None): + return Link.format(self.url, other or self.text, style or self.style) + + def __str__(self): + return Link.format(self.url, self.text, self.style) diff --git a/pyrogram/client/types/user_and_chats/user.py b/pyrogram/client/types/user_and_chats/user.py index a5e475af..f386e161 100644 --- a/pyrogram/client/types/user_and_chats/user.py +++ b/pyrogram/client/types/user_and_chats/user.py @@ -16,11 +16,11 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -import html from typing import List import pyrogram from pyrogram.api import types +from pyrogram.client.ext import Link from .chat_photo import ChatPhoto from .restriction import Restriction from ..object import Object @@ -158,11 +158,9 @@ class User(Object, Update): self.photo = photo self.restrictions = restrictions - def __format__(self, format_spec): - if format_spec == "mention": - return '{1}'.format(self.id, html.escape(self.first_name)) - - return html.escape(str(self)) + @property + def mention(self): + return Link("tg://user?id={}".format(self.id), self.first_name, self._client.parse_mode) @staticmethod def _parse(client, user: types.User) -> "User" or None: