X Tutup
Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions telegram/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from telegram import (Audio, Contact, Document, Chat, Location, PhotoSize, Sticker, TelegramObject,
User, Video, Voice, Venue, MessageEntity, Game)
from telegram.utils.helpers import escape_html, escape_markdown


class Message(TelegramObject):
Expand Down Expand Up @@ -635,6 +636,7 @@ def text_html(self):
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)
Expand All @@ -649,7 +651,7 @@ def text_html(self):
else:
insert = text

markdown_text += message_text[last_offset:entity.offset] + insert
markdown_text += escape_html(message_text[last_offset:entity.offset]) + insert
last_offset = entity.offset + entity.length

markdown_text += message_text[last_offset:]
Expand All @@ -673,6 +675,7 @@ def text_markdown(self):
last_offset = 0

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

if entity.type == MessageEntity.TEXT_LINK:
insert = '[{}]({})'.format(text, entity.url)
Expand All @@ -687,7 +690,7 @@ def text_markdown(self):
else:
insert = text

markdown_text += message_text[last_offset:entity.offset] + insert
markdown_text += escape_markdown(message_text[last_offset:entity.offset]) + insert
last_offset = entity.offset + entity.length

markdown_text += message_text[last_offset:]
Expand Down
5 changes: 5 additions & 0 deletions telegram/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

import re

try:
from html import escape as escape_html # noqa: F401
except ImportError:
from cgi import escape as escape_html # noqa: F401


def escape_markdown(text):
"""Helper function to escape telegram markup symbols"""
Expand Down
18 changes: 9 additions & 9 deletions tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,33 @@ def setUp(self):
self.test_entities = [
{
'length': 4,
'offset': 9,
'offset': 10,
'type': 'bold'
},
{
'length': 6,
'offset': 15,
'length': 7,
'offset': 16,
'type': 'italic'
},
{
'length': 4,
'offset': 23,
'offset': 25,
'type': 'code'
},
{
'length': 5,
'offset': 29,
'offset': 31,
'type': 'text_link',
'url': 'http://github.com/'
},
{
'length': 3,
'offset': 39,
'offset': 41,
'type': 'pre'
},
]

self.test_text = 'Test for bold, italic, code, links and pre.'
self.test_text = 'Test for <bold, ita_lic, code, links and pre.'
self.test_message = telegram.Message(
message_id=1,
from_user=None,
Expand Down Expand Up @@ -99,12 +99,12 @@ def test_parse_entities(self):
entity_2: 'h'})

def test_text_html(self):
test_html_string = 'Test for <b>bold</b>, <i>italic</i>, <code>code</code>, ' '<a href="http://github.com/">links</a> and <pre>pre</pre>.'
test_html_string = 'Test for &lt;<b>bold</b>, <i>ita_lic</i>, <code>code</code>, <a href="http://github.com/">links</a> and <pre>pre</pre>.'
text_html = self.test_message.text_html
self.assertEquals(test_html_string, text_html)

def test_text_markdown(self):
test_md_string = 'Test for *bold*, _italic_, `code`, [links](http://github.com/) and ```pre```.'
test_md_string = 'Test for <*bold*, _ita\_lic_, `code`, [links](http://github.com/) and ```pre```.'
text_markdown = self.test_message.text_markdown
self.assertEquals(test_md_string, text_markdown)

Expand Down
X Tutup