mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Backport of text decoration utils from 3.0
This commit is contained in:
parent
ce026dfa71
commit
9115a44be6
5 changed files with 242 additions and 109 deletions
|
|
@ -2,7 +2,6 @@ from __future__ import annotations
|
|||
|
||||
import datetime
|
||||
import functools
|
||||
import sys
|
||||
import typing
|
||||
|
||||
from . import base
|
||||
|
|
@ -32,6 +31,7 @@ from .video_note import VideoNote
|
|||
from .voice import Voice
|
||||
from ..utils import helper
|
||||
from ..utils import markdown as md
|
||||
from ..utils.text_decorations import html_decoration, markdown_decoration
|
||||
|
||||
|
||||
class Message(base.TelegramObject):
|
||||
|
|
@ -200,38 +200,10 @@ class Message(base.TelegramObject):
|
|||
if text is None:
|
||||
raise TypeError("This message doesn't have any text.")
|
||||
|
||||
quote_fn = md.quote_html if as_html else md.escape_md
|
||||
|
||||
entities = self.entities or self.caption_entities
|
||||
if not entities:
|
||||
return quote_fn(text)
|
||||
text_decorator = html_decoration if as_html else markdown_decoration
|
||||
|
||||
if not sys.maxunicode == 0xffff:
|
||||
text = text.encode('utf-16-le')
|
||||
|
||||
result = ''
|
||||
offset = 0
|
||||
|
||||
for entity in sorted(entities, key=lambda item: item.offset):
|
||||
entity_text = entity.parse(text, as_html=as_html)
|
||||
|
||||
if sys.maxunicode == 0xffff:
|
||||
part = text[offset:entity.offset]
|
||||
result += quote_fn(part) + entity_text
|
||||
else:
|
||||
part = text[offset * 2:entity.offset * 2]
|
||||
result += quote_fn(part.decode('utf-16-le')) + entity_text
|
||||
|
||||
offset = entity.offset + entity.length
|
||||
|
||||
if sys.maxunicode == 0xffff:
|
||||
part = text[offset:]
|
||||
result += quote_fn(part)
|
||||
else:
|
||||
part = text[offset * 2:]
|
||||
result += quote_fn(part.decode('utf-16-le'))
|
||||
|
||||
return result
|
||||
return text_decorator.unparse(text, entities)
|
||||
|
||||
@property
|
||||
def md_text(self) -> str:
|
||||
|
|
@ -1798,4 +1770,5 @@ class ParseMode(helper.Helper):
|
|||
mode = helper.HelperMode.lowercase
|
||||
|
||||
MARKDOWN = helper.Item()
|
||||
MARKDOWN_V2 = helper.Item()
|
||||
HTML = helper.Item()
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ from . import base
|
|||
from . import fields
|
||||
from .user import User
|
||||
from ..utils import helper, markdown
|
||||
from ..utils.deprecated import deprecated
|
||||
|
||||
|
||||
class MessageEntity(base.TelegramObject):
|
||||
|
|
@ -36,6 +37,7 @@ class MessageEntity(base.TelegramObject):
|
|||
entity_text = entity_text[self.offset * 2:(self.offset + self.length) * 2]
|
||||
return entity_text.decode('utf-16-le')
|
||||
|
||||
@deprecated("This method doesn't work with nested entities and will be removed in aiogram 3.0")
|
||||
def parse(self, text, as_html=True):
|
||||
"""
|
||||
Get entity value with markup
|
||||
|
|
@ -87,6 +89,8 @@ class MessageEntityType(helper.Helper):
|
|||
:key: ITALIC
|
||||
:key: CODE
|
||||
:key: PRE
|
||||
:key: UNDERLINE
|
||||
:key: STRIKETHROUGH
|
||||
:key: TEXT_LINK
|
||||
:key: TEXT_MENTION
|
||||
"""
|
||||
|
|
@ -101,7 +105,9 @@ class MessageEntityType(helper.Helper):
|
|||
PHONE_NUMBER = helper.Item() # phone_number
|
||||
BOLD = helper.Item() # bold - bold text
|
||||
ITALIC = helper.Item() # italic - italic text
|
||||
CODE = helper.Item() # code - monowidth string
|
||||
PRE = helper.Item() # pre - monowidth block
|
||||
CODE = helper.Item() # code - monowidth string
|
||||
PRE = helper.Item() # pre - monowidth block
|
||||
UNDERLINE = helper.Item() # underline
|
||||
STRIKETHROUGH = helper.Item() # strikethrough
|
||||
TEXT_LINK = helper.Item() # text_link - for clickable text URLs
|
||||
TEXT_MENTION = helper.Item() # text_mention - for users without usernames
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue