Skip to content

restore text_* #777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 58 additions & 24 deletions telegram/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,31 +697,21 @@ def parse_entities(self, types=None):
for entity in self.entities if entity.type in types
}

@property
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.
"""

def _text_html(self, urled=False):
entities = self.parse_entities()
message_text = self.text
if not sys.maxunicode == 0xffff:
message_text = message_text.encode('utf-16-le')

markdown_text = ''
html_text = ''
last_offset = 0

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

if entity.type == MessageEntity.TEXT_LINK:
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)
elif entity.type == MessageEntity.BOLD:
insert = '<b>' + text + '</b>'
Expand All @@ -735,30 +725,46 @@ def text_html(self):
insert = text

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:
markdown_text += escape_html(message_text[last_offset * 2:entity.offset * 2]
.decode('utf-16-le')) + insert
html_text += escape_html(message_text[last_offset * 2:entity.offset * 2]
.decode('utf-16-le')) + insert

last_offset = entity.offset + entity.length

if sys.maxunicode == 0xffff:
markdown_text += escape_html(message_text[last_offset:])
html_text += escape_html(message_text[last_offset:])
else:
markdown_text += escape_html(message_text[last_offset * 2:].decode('utf-16-le'))
return markdown_text
html_text += escape_html(message_text[last_offset * 2:].decode('utf-16-le'))
return html_text

@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:
: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()
message_text = self.text
if not sys.maxunicode == 0xffff:
Expand All @@ -772,7 +778,7 @@ def text_markdown(self):

if entity.type == MessageEntity.TEXT_LINK:
insert = '[{}]({})'.format(text, entity.url)
elif entity.type == MessageEntity.URL:
elif (entity.type == MessageEntity.URL) and urled:
insert = '[{0}]({0})'.format(text)
elif entity.type == MessageEntity.BOLD:
insert = '*' + text + '*'
Expand All @@ -798,6 +804,34 @@ def text_markdown(self):
markdown_text += escape_markdown(message_text[last_offset * 2:].decode('utf-16-le'))
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
def new_chat_member(self):
"""Deprecated"""
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy