Regression fix for text_html & text_markdown (#777)

`text_html` & `text_markdown` reverted to the old semantics - URLs are not converted to hyperlinks.
To get the new behaviour there are matching `text_html_urled` & `text_markdown_urled` properties.
fixes #773
This commit is contained in:
Eldinnie 2017-08-07 23:10:48 +02:00 committed by Noam Meltzer
parent 8d4b484f7b
commit 70057a67c5

View file

@ -697,23 +697,13 @@ class Message(TelegramObject):
for entity in self.entities if entity.type in types for entity in self.entities if entity.type in types
} }
@property def _text_html(self, urled=False):
def text_html(self):
"""
Creates an HTML-formatted string from the markup entities found in the message.
Use this if you want to retrieve the message text with the entities formatted as HTML.
Returns:
:obj:`str`: Message text with entities formatted as HTML.
"""
entities = self.parse_entities() entities = self.parse_entities()
message_text = self.text message_text = self.text
if not sys.maxunicode == 0xffff: if not sys.maxunicode == 0xffff:
message_text = message_text.encode('utf-16-le') message_text = message_text.encode('utf-16-le')
markdown_text = '' html_text = ''
last_offset = 0 last_offset = 0
for entity, text in sorted(entities.items(), key=(lambda item: item[0].offset)): for entity, text in sorted(entities.items(), key=(lambda item: item[0].offset)):
@ -721,7 +711,7 @@ class Message(TelegramObject):
if entity.type == MessageEntity.TEXT_LINK: if entity.type == MessageEntity.TEXT_LINK:
insert = '<a href="{}">{}</a>'.format(entity.url, text) insert = '<a href="{}">{}</a>'.format(entity.url, text)
elif entity.type == MessageEntity.URL: elif (entity.type == MessageEntity.URL) and urled:
insert = '<a href="{0}">{0}</a>'.format(text) insert = '<a href="{0}">{0}</a>'.format(text)
elif entity.type == MessageEntity.BOLD: elif entity.type == MessageEntity.BOLD:
insert = '<b>' + text + '</b>' insert = '<b>' + text + '</b>'
@ -735,30 +725,46 @@ class Message(TelegramObject):
insert = text insert = text
if sys.maxunicode == 0xffff: if sys.maxunicode == 0xffff:
markdown_text += escape_html(message_text[last_offset:entity.offset]) + insert html_text += escape_html(message_text[last_offset:entity.offset]) + insert
else: else:
markdown_text += escape_html(message_text[last_offset * 2:entity.offset * 2] html_text += escape_html(message_text[last_offset * 2:entity.offset * 2]
.decode('utf-16-le')) + insert .decode('utf-16-le')) + insert
last_offset = entity.offset + entity.length last_offset = entity.offset + entity.length
if sys.maxunicode == 0xffff: if sys.maxunicode == 0xffff:
markdown_text += escape_html(message_text[last_offset:]) html_text += escape_html(message_text[last_offset:])
else: else:
markdown_text += escape_html(message_text[last_offset * 2:].decode('utf-16-le')) html_text += escape_html(message_text[last_offset * 2:].decode('utf-16-le'))
return markdown_text return html_text
@property @property
def text_markdown(self): def text_html(self):
""" """
Creates an Markdown-formatted string from the markup entities found in the message. Creates an HTML-formatted string from the markup entities found in the message.
Use this if you want to retrieve the message text with the entities formatted as Markdown. Use this if you want to retrieve the message text with the entities formatted as HTML in
the same way the original message was formatted.
Returns: Returns:
:obj:`str`: Message text with entities formatted as Markdown. :obj:`str`: Message text with entities formatted as HTML.
""" """
return self._text_html(urled=False)
@property
def text_html_urled(self):
"""
Creates an HTML-formatted string from the markup entities found in the message.
Use this if you want to retrieve the message text with the entities formatted as HTML.
This also formats :attr:`telegram.MessageEntity.URL` as a hyperlink.
Returns:
:obj:`str`: Message text with entities formatted as HTML.
"""
return self._text_html(urled=True)
def _text_markdown(self, urled=False):
entities = self.parse_entities() entities = self.parse_entities()
message_text = self.text message_text = self.text
if not sys.maxunicode == 0xffff: if not sys.maxunicode == 0xffff:
@ -772,7 +778,7 @@ class Message(TelegramObject):
if entity.type == MessageEntity.TEXT_LINK: if entity.type == MessageEntity.TEXT_LINK:
insert = '[{}]({})'.format(text, entity.url) insert = '[{}]({})'.format(text, entity.url)
elif entity.type == MessageEntity.URL: elif (entity.type == MessageEntity.URL) and urled:
insert = '[{0}]({0})'.format(text) insert = '[{0}]({0})'.format(text)
elif entity.type == MessageEntity.BOLD: elif entity.type == MessageEntity.BOLD:
insert = '*' + text + '*' insert = '*' + text + '*'
@ -798,6 +804,34 @@ class Message(TelegramObject):
markdown_text += escape_markdown(message_text[last_offset * 2:].decode('utf-16-le')) markdown_text += escape_markdown(message_text[last_offset * 2:].decode('utf-16-le'))
return markdown_text return markdown_text
@property
def text_markdown(self):
"""
Creates an Markdown-formatted string from the markup entities found in the message.
Use this if you want to retrieve the message text with the entities formatted as Markdown
in the same way the original message was formatted.
Returns:
:obj:`str`: Message text with entities formatted as Markdown.
"""
return self._text_markdown(urled=False)
@property
def text_markdown_urled(self):
"""
Creates an Markdown-formatted string from the markup entities found in the message.
Use this if you want to retrieve the message text with the entities formatted as Markdown.
This also formats :attr:`telegram.MessageEntity.URL` as a hyperlink.
Returns:
:obj:`str`: Message text with entities formatted as Markdown.
"""
return self._text_markdown(urled=True)
@property @property
def new_chat_member(self): def new_chat_member(self):
"""Deprecated""" """Deprecated"""