1919# along with this program. If not, see [http://www.gnu.org/licenses/].
2020"""This module contains an object that represents a Telegram Message."""
2121import sys
22+ from html import escape
2223
2324from telegram import (Audio , Contact , Document , Chat , Location , PhotoSize , Sticker , TelegramObject ,
2425 User , Video , Voice , Venue , MessageEntity , Game , Invoice , SuccessfulPayment ,
2526 VideoNote )
2627from telegram import ParseMode
27- from telegram .utils .helpers import escape_html , escape_markdown , to_timestamp , from_timestamp
28+ from telegram .utils .helpers import escape_markdown , to_timestamp , from_timestamp
2829
2930_UNDEFINED = object ()
3031
@@ -865,17 +866,16 @@ def parse_caption_entities(self, types=None):
865866 for entity in self .caption_entities if entity .type in types
866867 }
867868
868- def _text_html (self , urled = False ):
869- entities = self .parse_entities ()
870- message_text = self .text
869+ @staticmethod
870+ def _parse_html (message_text , entities , urled = False ):
871871 if not sys .maxunicode == 0xffff :
872872 message_text = message_text .encode ('utf-16-le' )
873873
874874 html_text = ''
875875 last_offset = 0
876876
877877 for entity , text in sorted (entities .items (), key = (lambda item : item [0 ].offset )):
878- text = escape_html (text )
878+ text = escape (text )
879879
880880 if entity .type == MessageEntity .TEXT_LINK :
881881 insert = '<a href="{}">{}</a>' .format (entity .url , text )
@@ -893,17 +893,17 @@ def _text_html(self, urled=False):
893893 insert = text
894894
895895 if sys .maxunicode == 0xffff :
896- html_text += escape_html (message_text [last_offset :entity .offset ]) + insert
896+ html_text += escape (message_text [last_offset :entity .offset ]) + insert
897897 else :
898- html_text += escape_html (message_text [last_offset * 2 :entity .offset * 2 ]
899- .decode ('utf-16-le' )) + insert
898+ html_text += escape (message_text [last_offset * 2 :entity .offset * 2 ]
899+ .decode ('utf-16-le' )) + insert
900900
901901 last_offset = entity .offset + entity .length
902902
903903 if sys .maxunicode == 0xffff :
904- html_text += escape_html (message_text [last_offset :])
904+ html_text += escape (message_text [last_offset :])
905905 else :
906- html_text += escape_html (message_text [last_offset * 2 :].decode ('utf-16-le' ))
906+ html_text += escape (message_text [last_offset * 2 :].decode ('utf-16-le' ))
907907 return html_text
908908
909909 @property
@@ -917,7 +917,7 @@ def text_html(self):
917917 :obj:`str`: Message text with entities formatted as HTML.
918918
919919 """
920- return self ._text_html ( urled = False )
920+ return self ._parse_html ( self . text , self . parse_entities (), urled = False )
921921
922922 @property
923923 def text_html_urled (self ):
@@ -930,11 +930,38 @@ def text_html_urled(self):
930930 :obj:`str`: Message text with entities formatted as HTML.
931931
932932 """
933- return self ._text_html ( urled = True )
933+ return self ._parse_html ( self . text , self . parse_entities (), urled = True )
934934
935- def _text_markdown (self , urled = False ):
936- entities = self .parse_entities ()
937- message_text = self .text
935+ @property
936+ def caption_html (self ):
937+ """Creates an HTML-formatted string from the markup entities found in the message's
938+ caption.
939+
940+ Use this if you want to retrieve the message caption with the caption entities formatted as
941+ HTML in the same way the original message was formatted.
942+
943+ Returns:
944+ :obj:`str`: Message caption with captionentities formatted as HTML.
945+
946+ """
947+ return self ._parse_html (self .caption , self .parse_caption_entities (), urled = False )
948+
949+ @property
950+ def caption_html_urled (self ):
951+ """Creates an HTML-formatted string from the markup entities found in the message's
952+ caption.
953+
954+ Use this if you want to retrieve the message caption with the caption entities formatted as
955+ HTML. This also formats :attr:`telegram.MessageEntity.URL` as a hyperlink.
956+
957+ Returns:
958+ :obj:`str`: Message caption with caption entities formatted as HTML.
959+
960+ """
961+ return self ._parse_html (self .caption , self .parse_caption_entities (), urled = True )
962+
963+ @staticmethod
964+ def _parse_markdown (message_text , entities , urled = False ):
938965 if not sys .maxunicode == 0xffff :
939966 message_text = message_text .encode ('utf-16-le' )
940967
@@ -983,7 +1010,7 @@ def text_markdown(self):
9831010 :obj:`str`: Message text with entities formatted as Markdown.
9841011
9851012 """
986- return self ._text_markdown ( urled = False )
1013+ return self ._parse_markdown ( self . text , self . parse_entities (), urled = False )
9871014
9881015 @property
9891016 def text_markdown_urled (self ):
@@ -996,4 +1023,32 @@ def text_markdown_urled(self):
9961023 :obj:`str`: Message text with entities formatted as Markdown.
9971024
9981025 """
999- return self ._text_markdown (urled = True )
1026+ return self ._parse_markdown (self .text , self .parse_entities (), urled = True )
1027+
1028+ @property
1029+ def caption_markdown (self ):
1030+ """Creates an Markdown-formatted string from the markup entities found in the message's
1031+ caption.
1032+
1033+ Use this if you want to retrieve the message caption with the caption entities formatted as
1034+ Markdown in the same way the original message was formatted.
1035+
1036+ Returns:
1037+ :obj:`str`: Message caption with caption entities formatted as Markdown.
1038+
1039+ """
1040+ return self ._parse_markdown (self .caption , self .parse_caption_entities (), urled = False )
1041+
1042+ @property
1043+ def caption_markdown_urled (self ):
1044+ """Creates an Markdown-formatted string from the markup entities found in the message's
1045+ caption.
1046+
1047+ Use this if you want to retrieve the message caption with the caption entities formatted as
1048+ Markdown. This also formats :attr:`telegram.MessageEntity.URL` as a hyperlink.
1049+
1050+ Returns:
1051+ :obj:`str`: Message caption with caption entities formatted as Markdown.
1052+
1053+ """
1054+ return self ._parse_markdown (self .caption , self .parse_caption_entities (), urled = True )
0 commit comments