From 9371649925d687cf72aeff119dcbe79cb6d5797f Mon Sep 17 00:00:00 2001 From: Pieter Schutz Date: Mon, 19 Feb 2018 13:50:50 +0100 Subject: [PATCH 1/4] Add caption_(html|markdown)(_urled)? --- telegram/message.py | 89 ++++++++++++++++++++++++++++++++++--------- tests/test_message.py | 46 +++++++++++++++++++++- 2 files changed, 117 insertions(+), 18 deletions(-) diff --git a/telegram/message.py b/telegram/message.py index d36ba32ab5e..1594f3411ac 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -19,13 +19,14 @@ # along with this program. If not, see [http://www.gnu.org/licenses/]. """This module contains an object that represents a Telegram Message.""" import sys +from html import escape from telegram import (Audio, Contact, Document, Chat, Location, PhotoSize, Sticker, TelegramObject, User, Video, Voice, Venue, MessageEntity, Game, Invoice, SuccessfulPayment, VideoNote) from telegram import ParseMode from telegram.utils.deprecate import warn_deprecate_obj -from telegram.utils.helpers import escape_html, escape_markdown, to_timestamp, from_timestamp +from telegram.utils.helpers import escape_markdown, to_timestamp, from_timestamp _UNDEFINED = object() @@ -873,9 +874,8 @@ def parse_caption_entities(self, types=None): for entity in self.caption_entities if entity.type in types } - def _text_html(self, urled=False): - entities = self.parse_entities() - message_text = self.text + def _parse_html(self, text, entities, urled=False): + message_text = text if not sys.maxunicode == 0xffff: message_text = message_text.encode('utf-16-le') @@ -883,7 +883,7 @@ def _text_html(self, urled=False): last_offset = 0 for entity, text in sorted(entities.items(), key=(lambda item: item[0].offset)): - text = escape_html(text) + text = escape(text) if entity.type == MessageEntity.TEXT_LINK: insert = '{}'.format(entity.url, text) @@ -901,17 +901,17 @@ def _text_html(self, urled=False): insert = text if sys.maxunicode == 0xffff: - html_text += escape_html(message_text[last_offset:entity.offset]) + insert + html_text += escape(message_text[last_offset:entity.offset]) + insert else: - html_text += escape_html(message_text[last_offset * 2:entity.offset * 2] - .decode('utf-16-le')) + insert + html_text += escape(message_text[last_offset * 2:entity.offset * 2] + .decode('utf-16-le')) + insert last_offset = entity.offset + entity.length if sys.maxunicode == 0xffff: - html_text += escape_html(message_text[last_offset:]) + html_text += escape(message_text[last_offset:]) else: - html_text += escape_html(message_text[last_offset * 2:].decode('utf-16-le')) + html_text += escape(message_text[last_offset * 2:].decode('utf-16-le')) return html_text @property @@ -925,7 +925,7 @@ def text_html(self): :obj:`str`: Message text with entities formatted as HTML. """ - return self._text_html(urled=False) + return self._parse_html(self.text, self.parse_entities(), urled=False) @property def text_html_urled(self): @@ -938,11 +938,38 @@ def text_html_urled(self): :obj:`str`: Message text with entities formatted as HTML. """ - return self._text_html(urled=True) + return self._parse_html(self.text, self.parse_entities(), urled=True) - def _text_markdown(self, urled=False): - entities = self.parse_entities() - message_text = self.text + @property + def caption_html(self): + """Creates an HTML-formatted string from the markup entities found in the message's + caption. + + Use this if you want to retrieve the message caption with the caption entities formatted as + HTML in the same way the original message was formatted. + + Returns: + :obj:`str`: Message caption with captionentities formatted as HTML. + + """ + return self._parse_html(self.caption, self.parse_caption_entities(), urled=False) + + @property + def caption_html_urled(self): + """Creates an HTML-formatted string from the markup entities found in the message's + caption. + + Use this if you want to retrieve the message caption with the caption entities formatted as + HTML. This also formats :attr:`telegram.MessageEntity.URL` as a hyperlink. + + Returns: + :obj:`str`: Message caption with caption entities formatted as HTML. + + """ + return self._parse_html(self.caption, self.parse_caption_entities(), urled=True) + + def _parse_markdown(self, text, entities, urled=False): + message_text = text if not sys.maxunicode == 0xffff: message_text = message_text.encode('utf-16-le') @@ -991,7 +1018,7 @@ def text_markdown(self): :obj:`str`: Message text with entities formatted as Markdown. """ - return self._text_markdown(urled=False) + return self._parse_markdown(self.text, self.parse_entities(), urled=False) @property def text_markdown_urled(self): @@ -1004,7 +1031,35 @@ def text_markdown_urled(self): :obj:`str`: Message text with entities formatted as Markdown. """ - return self._text_markdown(urled=True) + return self._parse_markdown(self.text, self.parse_entities(), urled=True) + + @property + def caption_markdown(self): + """Creates an Markdown-formatted string from the markup entities found in the message's + caption. + + Use this if you want to retrieve the message caption with the caption entities formatted as + Markdown in the same way the original message was formatted. + + Returns: + :obj:`str`: Message caption with caption entities formatted as Markdown. + + """ + return self._parse_markdown(self.caption, self.parse_caption_entities(), urled=False) + + @property + def caption_markdown_urled(self): + """Creates an Markdown-formatted string from the markup entities found in the message's + caption. + + Use this if you want to retrieve the message caption with the caption entities formatted as + Markdown. This also formats :attr:`telegram.MessageEntity.URL` as a hyperlink. + + Returns: + :obj:`str`: Message caption with caption entities formatted as Markdown. + + """ + return self._parse_markdown(self.caption, self.parse_caption_entities(), urled=True) @property def new_chat_member(self): diff --git a/tests/test_message.py b/tests/test_message.py index c61d44138c6..2ce115fd355 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -117,7 +117,9 @@ class TestMessage(object): date=None, chat=None, text=test_text, - entities=[MessageEntity(**e) for e in test_entities]) + entities=[MessageEntity(**e) for e in test_entities], + caption=test_text, + caption_entities=[MessageEntity(**e) for e in test_entities]) def test_all_posibilities_de_json_and_to_dict(self, bot, message_params): new = Message.de_json(message_params.to_dict(), bot) @@ -206,6 +208,48 @@ def test_text_markdown_emoji(self): text=text, entities=[bold_entity]) assert expected == message.text_markdown + def test_caption_html_simple(self): + test_html_string = ('Test for <bold, ita_lic, code, ' + 'links and
pre
. ' + 'http://google.com') + caption_html = self.test_message.caption_html + assert caption_html == test_html_string + + def test_caption_html_urled(self): + test_html_string = ('Test for <bold, ita_lic, code, ' + 'links and
pre
. ' + 'http://google.com') + caption_html = self.test_message.caption_html_urled + assert caption_html == test_html_string + + def test_caption_markdown_simple(self): + test_md_string = ('Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/) and ' + '```pre```. http://google.com') + caption_markdown = self.test_message.caption_markdown + assert caption_markdown == test_md_string + + def test_caption_markdown_urled(self): + test_md_string = ('Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/) and ' + '```pre```. [http://google.com](http://google.com)') + caption_markdown = self.test_message.caption_markdown_urled + assert caption_markdown == test_md_string + + def test_caption_html_emoji(self): + caption = b'\\U0001f469\\u200d\\U0001f469\\u200d ABC'.decode('unicode-escape') + expected = b'\\U0001f469\\u200d\\U0001f469\\u200d ABC'.decode('unicode-escape') + bold_entity = MessageEntity(type=MessageEntity.BOLD, offset=7, length=3) + message = Message(1, self.from_user, self.date, self.chat, + caption=caption, caption_entities=[bold_entity]) + assert expected == message.caption_html + + def test_caption_markdown_emoji(self): + caption = b'\\U0001f469\\u200d\\U0001f469\\u200d ABC'.decode('unicode-escape') + expected = b'\\U0001f469\\u200d\\U0001f469\\u200d *ABC*'.decode('unicode-escape') + bold_entity = MessageEntity(type=MessageEntity.BOLD, offset=7, length=3) + message = Message(1, self.from_user, self.date, self.chat, + caption=caption, caption_entities=[bold_entity]) + assert expected == message.caption_markdown + def test_parse_entities_url_emoji(self): url = b'http://github.com/?unicode=\\u2713\\U0001f469'.decode('unicode-escape') text = 'some url' From e4377b2da0561018db5c401bf5d59491f6e40cb1 Mon Sep 17 00:00:00 2001 From: Pieter Schutz Date: Mon, 19 Feb 2018 13:50:50 +0100 Subject: [PATCH 2/4] Add caption_(html|markdown)(_urled)? --- telegram/message.py | 89 ++++++++++++++++++++++++++++++++++--------- tests/test_message.py | 46 +++++++++++++++++++++- 2 files changed, 117 insertions(+), 18 deletions(-) diff --git a/telegram/message.py b/telegram/message.py index d36ba32ab5e..1594f3411ac 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -19,13 +19,14 @@ # along with this program. If not, see [http://www.gnu.org/licenses/]. """This module contains an object that represents a Telegram Message.""" import sys +from html import escape from telegram import (Audio, Contact, Document, Chat, Location, PhotoSize, Sticker, TelegramObject, User, Video, Voice, Venue, MessageEntity, Game, Invoice, SuccessfulPayment, VideoNote) from telegram import ParseMode from telegram.utils.deprecate import warn_deprecate_obj -from telegram.utils.helpers import escape_html, escape_markdown, to_timestamp, from_timestamp +from telegram.utils.helpers import escape_markdown, to_timestamp, from_timestamp _UNDEFINED = object() @@ -873,9 +874,8 @@ def parse_caption_entities(self, types=None): for entity in self.caption_entities if entity.type in types } - def _text_html(self, urled=False): - entities = self.parse_entities() - message_text = self.text + def _parse_html(self, text, entities, urled=False): + message_text = text if not sys.maxunicode == 0xffff: message_text = message_text.encode('utf-16-le') @@ -883,7 +883,7 @@ def _text_html(self, urled=False): last_offset = 0 for entity, text in sorted(entities.items(), key=(lambda item: item[0].offset)): - text = escape_html(text) + text = escape(text) if entity.type == MessageEntity.TEXT_LINK: insert = '{}'.format(entity.url, text) @@ -901,17 +901,17 @@ def _text_html(self, urled=False): insert = text if sys.maxunicode == 0xffff: - html_text += escape_html(message_text[last_offset:entity.offset]) + insert + html_text += escape(message_text[last_offset:entity.offset]) + insert else: - html_text += escape_html(message_text[last_offset * 2:entity.offset * 2] - .decode('utf-16-le')) + insert + html_text += escape(message_text[last_offset * 2:entity.offset * 2] + .decode('utf-16-le')) + insert last_offset = entity.offset + entity.length if sys.maxunicode == 0xffff: - html_text += escape_html(message_text[last_offset:]) + html_text += escape(message_text[last_offset:]) else: - html_text += escape_html(message_text[last_offset * 2:].decode('utf-16-le')) + html_text += escape(message_text[last_offset * 2:].decode('utf-16-le')) return html_text @property @@ -925,7 +925,7 @@ def text_html(self): :obj:`str`: Message text with entities formatted as HTML. """ - return self._text_html(urled=False) + return self._parse_html(self.text, self.parse_entities(), urled=False) @property def text_html_urled(self): @@ -938,11 +938,38 @@ def text_html_urled(self): :obj:`str`: Message text with entities formatted as HTML. """ - return self._text_html(urled=True) + return self._parse_html(self.text, self.parse_entities(), urled=True) - def _text_markdown(self, urled=False): - entities = self.parse_entities() - message_text = self.text + @property + def caption_html(self): + """Creates an HTML-formatted string from the markup entities found in the message's + caption. + + Use this if you want to retrieve the message caption with the caption entities formatted as + HTML in the same way the original message was formatted. + + Returns: + :obj:`str`: Message caption with captionentities formatted as HTML. + + """ + return self._parse_html(self.caption, self.parse_caption_entities(), urled=False) + + @property + def caption_html_urled(self): + """Creates an HTML-formatted string from the markup entities found in the message's + caption. + + Use this if you want to retrieve the message caption with the caption entities formatted as + HTML. This also formats :attr:`telegram.MessageEntity.URL` as a hyperlink. + + Returns: + :obj:`str`: Message caption with caption entities formatted as HTML. + + """ + return self._parse_html(self.caption, self.parse_caption_entities(), urled=True) + + def _parse_markdown(self, text, entities, urled=False): + message_text = text if not sys.maxunicode == 0xffff: message_text = message_text.encode('utf-16-le') @@ -991,7 +1018,7 @@ def text_markdown(self): :obj:`str`: Message text with entities formatted as Markdown. """ - return self._text_markdown(urled=False) + return self._parse_markdown(self.text, self.parse_entities(), urled=False) @property def text_markdown_urled(self): @@ -1004,7 +1031,35 @@ def text_markdown_urled(self): :obj:`str`: Message text with entities formatted as Markdown. """ - return self._text_markdown(urled=True) + return self._parse_markdown(self.text, self.parse_entities(), urled=True) + + @property + def caption_markdown(self): + """Creates an Markdown-formatted string from the markup entities found in the message's + caption. + + Use this if you want to retrieve the message caption with the caption entities formatted as + Markdown in the same way the original message was formatted. + + Returns: + :obj:`str`: Message caption with caption entities formatted as Markdown. + + """ + return self._parse_markdown(self.caption, self.parse_caption_entities(), urled=False) + + @property + def caption_markdown_urled(self): + """Creates an Markdown-formatted string from the markup entities found in the message's + caption. + + Use this if you want to retrieve the message caption with the caption entities formatted as + Markdown. This also formats :attr:`telegram.MessageEntity.URL` as a hyperlink. + + Returns: + :obj:`str`: Message caption with caption entities formatted as Markdown. + + """ + return self._parse_markdown(self.caption, self.parse_caption_entities(), urled=True) @property def new_chat_member(self): diff --git a/tests/test_message.py b/tests/test_message.py index c61d44138c6..2ce115fd355 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -117,7 +117,9 @@ class TestMessage(object): date=None, chat=None, text=test_text, - entities=[MessageEntity(**e) for e in test_entities]) + entities=[MessageEntity(**e) for e in test_entities], + caption=test_text, + caption_entities=[MessageEntity(**e) for e in test_entities]) def test_all_posibilities_de_json_and_to_dict(self, bot, message_params): new = Message.de_json(message_params.to_dict(), bot) @@ -206,6 +208,48 @@ def test_text_markdown_emoji(self): text=text, entities=[bold_entity]) assert expected == message.text_markdown + def test_caption_html_simple(self): + test_html_string = ('Test for <bold, ita_lic, code, ' + 'links and
pre
. ' + 'http://google.com') + caption_html = self.test_message.caption_html + assert caption_html == test_html_string + + def test_caption_html_urled(self): + test_html_string = ('Test for <bold, ita_lic, code, ' + 'links and
pre
. ' + 'http://google.com') + caption_html = self.test_message.caption_html_urled + assert caption_html == test_html_string + + def test_caption_markdown_simple(self): + test_md_string = ('Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/) and ' + '```pre```. http://google.com') + caption_markdown = self.test_message.caption_markdown + assert caption_markdown == test_md_string + + def test_caption_markdown_urled(self): + test_md_string = ('Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/) and ' + '```pre```. [http://google.com](http://google.com)') + caption_markdown = self.test_message.caption_markdown_urled + assert caption_markdown == test_md_string + + def test_caption_html_emoji(self): + caption = b'\\U0001f469\\u200d\\U0001f469\\u200d ABC'.decode('unicode-escape') + expected = b'\\U0001f469\\u200d\\U0001f469\\u200d ABC'.decode('unicode-escape') + bold_entity = MessageEntity(type=MessageEntity.BOLD, offset=7, length=3) + message = Message(1, self.from_user, self.date, self.chat, + caption=caption, caption_entities=[bold_entity]) + assert expected == message.caption_html + + def test_caption_markdown_emoji(self): + caption = b'\\U0001f469\\u200d\\U0001f469\\u200d ABC'.decode('unicode-escape') + expected = b'\\U0001f469\\u200d\\U0001f469\\u200d *ABC*'.decode('unicode-escape') + bold_entity = MessageEntity(type=MessageEntity.BOLD, offset=7, length=3) + message = Message(1, self.from_user, self.date, self.chat, + caption=caption, caption_entities=[bold_entity]) + assert expected == message.caption_markdown + def test_parse_entities_url_emoji(self): url = b'http://github.com/?unicode=\\u2713\\U0001f469'.decode('unicode-escape') text = 'some url' From c9eae2e12040b1723e41137373520458e469e209 Mon Sep 17 00:00:00 2001 From: Pieter Schutz Date: Thu, 22 Feb 2018 14:59:10 +0100 Subject: [PATCH 3/4] Changes as per CR --- telegram/message.py | 8 ++++---- telegram/utils/helpers.py | 9 ++------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/telegram/message.py b/telegram/message.py index 1594f3411ac..c0f2eaee202 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -874,8 +874,8 @@ def parse_caption_entities(self, types=None): for entity in self.caption_entities if entity.type in types } - def _parse_html(self, text, entities, urled=False): - message_text = text + @staticmethod + def _parse_html(message_text, entities, urled=False): if not sys.maxunicode == 0xffff: message_text = message_text.encode('utf-16-le') @@ -968,8 +968,8 @@ def caption_html_urled(self): """ return self._parse_html(self.caption, self.parse_caption_entities(), urled=True) - def _parse_markdown(self, text, entities, urled=False): - message_text = text + @staticmethod + def _parse_markdown(message_text, entities, urled=False): if not sys.maxunicode == 0xffff: message_text = message_text.encode('utf-16-le') diff --git a/telegram/utils/helpers.py b/telegram/utils/helpers.py index 3dcf5ce04af..7d28dc13125 100644 --- a/telegram/utils/helpers.py +++ b/telegram/utils/helpers.py @@ -17,17 +17,12 @@ # You should have received a copy of the GNU Lesser Public License # along with this program. If not, see [http://www.gnu.org/licenses/]. """This module contains helper functions.""" +from html import escape import re import signal from datetime import datetime -try: - from html import escape as escape_html # noqa: F401 -except ImportError: - from cgi import escape as escape_html # noqa: F401 - - # From https://stackoverflow.com/questions/2549939/get-signal-names-from-numbers-in-python _signames = {v: k for k, v in reversed(sorted(vars(signal).items())) @@ -99,7 +94,7 @@ def mention_html(user_id, name): :obj:`str`: The inline mention for the user as html. """ if isinstance(user_id, int): - return '{}'.format(user_id, escape_html(name)) + return '{}'.format(user_id, escape(name)) def mention_markdown(user_id, name): From d9b6a116232e2a64669ceb34ca073222e3286981 Mon Sep 17 00:00:00 2001 From: Pieter Schutz Date: Thu, 22 Feb 2018 16:08:39 +0100 Subject: [PATCH 4/4] Remove unnused Import --- telegram/message.py | 1 - 1 file changed, 1 deletion(-) diff --git a/telegram/message.py b/telegram/message.py index b81c064bfce..f67a3018d74 100644 --- a/telegram/message.py +++ b/telegram/message.py @@ -25,7 +25,6 @@ User, Video, Voice, Venue, MessageEntity, Game, Invoice, SuccessfulPayment, VideoNote) from telegram import ParseMode -from telegram.utils.deprecate import warn_deprecate_obj from telegram.utils.helpers import escape_markdown, to_timestamp, from_timestamp _UNDEFINED = object() 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