Skip to content

Commit 70057a6

Browse files
Eldinnietsnoam
authored andcommitted
Regression fix for text_html & text_markdown (python-telegram-bot#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 python-telegram-bot#773
1 parent 8d4b484 commit 70057a6

File tree

1 file changed

+58
-24
lines changed

1 file changed

+58
-24
lines changed

telegram/message.py

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -697,31 +697,21 @@ def parse_entities(self, types=None):
697697
for entity in self.entities if entity.type in types
698698
}
699699

700-
@property
701-
def text_html(self):
702-
"""
703-
Creates an HTML-formatted string from the markup entities found in the message.
704-
705-
Use this if you want to retrieve the message text with the entities formatted as HTML.
706-
707-
Returns:
708-
:obj:`str`: Message text with entities formatted as HTML.
709-
"""
710-
700+
def _text_html(self, urled=False):
711701
entities = self.parse_entities()
712702
message_text = self.text
713703
if not sys.maxunicode == 0xffff:
714704
message_text = message_text.encode('utf-16-le')
715705

716-
markdown_text = ''
706+
html_text = ''
717707
last_offset = 0
718708

719709
for entity, text in sorted(entities.items(), key=(lambda item: item[0].offset)):
720710
text = escape_html(text)
721711

722712
if entity.type == MessageEntity.TEXT_LINK:
723713
insert = '<a href="{}">{}</a>'.format(entity.url, text)
724-
elif entity.type == MessageEntity.URL:
714+
elif (entity.type == MessageEntity.URL) and urled:
725715
insert = '<a href="{0}">{0}</a>'.format(text)
726716
elif entity.type == MessageEntity.BOLD:
727717
insert = '<b>' + text + '</b>'
@@ -735,30 +725,46 @@ def text_html(self):
735725
insert = text
736726

737727
if sys.maxunicode == 0xffff:
738-
markdown_text += escape_html(message_text[last_offset:entity.offset]) + insert
728+
html_text += escape_html(message_text[last_offset:entity.offset]) + insert
739729
else:
740-
markdown_text += escape_html(message_text[last_offset * 2:entity.offset * 2]
741-
.decode('utf-16-le')) + insert
730+
html_text += escape_html(message_text[last_offset * 2:entity.offset * 2]
731+
.decode('utf-16-le')) + insert
742732

743733
last_offset = entity.offset + entity.length
744734

745735
if sys.maxunicode == 0xffff:
746-
markdown_text += escape_html(message_text[last_offset:])
736+
html_text += escape_html(message_text[last_offset:])
747737
else:
748-
markdown_text += escape_html(message_text[last_offset * 2:].decode('utf-16-le'))
749-
return markdown_text
738+
html_text += escape_html(message_text[last_offset * 2:].decode('utf-16-le'))
739+
return html_text
750740

751741
@property
752-
def text_markdown(self):
742+
def text_html(self):
753743
"""
754-
Creates an Markdown-formatted string from the markup entities found in the message.
744+
Creates an HTML-formatted string from the markup entities found in the message.
755745
756-
Use this if you want to retrieve the message text with the entities formatted as Markdown.
746+
Use this if you want to retrieve the message text with the entities formatted as HTML in
747+
the same way the original message was formatted.
757748
758749
Returns:
759-
:obj:`str`: Message text with entities formatted as Markdown.
750+
:obj:`str`: Message text with entities formatted as HTML.
751+
"""
752+
return self._text_html(urled=False)
753+
754+
@property
755+
def text_html_urled(self):
756+
"""
757+
Creates an HTML-formatted string from the markup entities found in the message.
758+
759+
Use this if you want to retrieve the message text with the entities formatted as HTML.
760+
This also formats :attr:`telegram.MessageEntity.URL` as a hyperlink.
761+
762+
Returns:
763+
:obj:`str`: Message text with entities formatted as HTML.
760764
"""
765+
return self._text_html(urled=True)
761766

767+
def _text_markdown(self, urled=False):
762768
entities = self.parse_entities()
763769
message_text = self.text
764770
if not sys.maxunicode == 0xffff:
@@ -772,7 +778,7 @@ def text_markdown(self):
772778

773779
if entity.type == MessageEntity.TEXT_LINK:
774780
insert = '[{}]({})'.format(text, entity.url)
775-
elif entity.type == MessageEntity.URL:
781+
elif (entity.type == MessageEntity.URL) and urled:
776782
insert = '[{0}]({0})'.format(text)
777783
elif entity.type == MessageEntity.BOLD:
778784
insert = '*' + text + '*'
@@ -798,6 +804,34 @@ def text_markdown(self):
798804
markdown_text += escape_markdown(message_text[last_offset * 2:].decode('utf-16-le'))
799805
return markdown_text
800806

807+
@property
808+
def text_markdown(self):
809+
"""
810+
Creates an Markdown-formatted string from the markup entities found in the message.
811+
812+
Use this if you want to retrieve the message text with the entities formatted as Markdown
813+
in the same way the original message was formatted.
814+
815+
Returns:
816+
:obj:`str`: Message text with entities formatted as Markdown.
817+
"""
818+
819+
return self._text_markdown(urled=False)
820+
821+
@property
822+
def text_markdown_urled(self):
823+
"""
824+
Creates an Markdown-formatted string from the markup entities found in the message.
825+
826+
Use this if you want to retrieve the message text with the entities formatted as Markdown.
827+
This also formats :attr:`telegram.MessageEntity.URL` as a hyperlink.
828+
829+
Returns:
830+
:obj:`str`: Message text with entities formatted as Markdown.
831+
"""
832+
833+
return self._text_markdown(urled=True)
834+
801835
@property
802836
def new_chat_member(self):
803837
"""Deprecated"""

0 commit comments

Comments
 (0)