mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
html_text fix and html_caption
html_text now works with text_link html_caption now works for caption/caption_entities
This commit is contained in:
parent
3be21ae361
commit
9c79ba2f87
@ -437,8 +437,7 @@ class Message(JsonDeserializable):
|
|||||||
setattr(self, key, options[key])
|
setattr(self, key, options[key])
|
||||||
self.json = json_string
|
self.json = json_string
|
||||||
|
|
||||||
@property
|
def __html_text(self, text, entities):
|
||||||
def html_text(self):
|
|
||||||
"""
|
"""
|
||||||
Author: @sviat9440
|
Author: @sviat9440
|
||||||
Message: "*Test* parse _formatting_, [url](https://example.com), [text_mention](tg://user?id=123456) and mention @username"
|
Message: "*Test* parse _formatting_, [url](https://example.com), [text_mention](tg://user?id=123456) and mention @username"
|
||||||
@ -455,19 +454,20 @@ class Message(JsonDeserializable):
|
|||||||
>> "<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>"
|
>> "<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>"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if not self.entities:
|
if not entities:
|
||||||
return self.text
|
return text
|
||||||
_subs = {
|
_subs = {
|
||||||
"bold": "<b>{text}</b>",
|
"bold": "<b>{text}</b>",
|
||||||
"italic": "<i>{text}</i>",
|
"italic": "<i>{text}</i>",
|
||||||
"pre": "<pre>{text}</pre>",
|
"pre": "<pre>{text}</pre>",
|
||||||
"code": "<code>{text}</code>",
|
"code": "<code>{text}</code>",
|
||||||
"url": "<a href=\"{url}\">{text}</a>"
|
"url": "<a href=\"{url}\">{text}</a>",
|
||||||
|
"text_link": "<a href=\"{url}\">{text}</a>"
|
||||||
}
|
}
|
||||||
if hasattr(self, "custom_subs"):
|
if hasattr(self, "custom_subs"):
|
||||||
for type in self.custom_subs:
|
for type in self.custom_subs:
|
||||||
_subs[type] = self.custom_subs[type]
|
_subs[type] = self.custom_subs[type]
|
||||||
utf16_text = self.text.encode("utf-16-le")
|
utf16_text = text.encode("utf-16-le")
|
||||||
html_text = ""
|
html_text = ""
|
||||||
def func(text, type=None, url=None, user=None):
|
def func(text, type=None, url=None, user=None):
|
||||||
text = text.decode("utf-16-le")
|
text = text.decode("utf-16-le")
|
||||||
@ -483,7 +483,7 @@ class Message(JsonDeserializable):
|
|||||||
return subs.format(text=text, url=url)
|
return subs.format(text=text, url=url)
|
||||||
|
|
||||||
offset = 0
|
offset = 0
|
||||||
for entity in self.entities:
|
for entity in entities:
|
||||||
if entity.offset > offset:
|
if entity.offset > offset:
|
||||||
html_text += func(utf16_text[offset * 2 : entity.offset * 2])
|
html_text += func(utf16_text[offset * 2 : entity.offset * 2])
|
||||||
offset = entity.offset
|
offset = entity.offset
|
||||||
@ -493,6 +493,13 @@ class Message(JsonDeserializable):
|
|||||||
html_text += func(utf16_text[offset * 2:])
|
html_text += func(utf16_text[offset * 2:])
|
||||||
return html_text
|
return html_text
|
||||||
|
|
||||||
|
@property
|
||||||
|
def html_text(self):
|
||||||
|
return self.__html_text(self.text, self.entities)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def html_caption(self):
|
||||||
|
return self.__html_text(self.caption, self.caption_entities)
|
||||||
|
|
||||||
class MessageEntity(JsonDeserializable):
|
class MessageEntity(JsonDeserializable):
|
||||||
@classmethod
|
@classmethod
|
||||||
|
Loading…
Reference in New Issue
Block a user