This commit is contained in:
Tulir Asokan
2018-07-25 11:53:31 -04:00
parent d5f6e45363
commit c08659c75a
4 changed files with 8 additions and 8 deletions
@@ -78,7 +78,7 @@ def matrix_to_telegram(html: str) -> ParsedMessage:
html = add_surrogates(html)
text, entities = parse_html(add_surrogates(html))
text = remove_surrogates(html.strip())
text = remove_surrogates(text.strip())
text, entities = cut_long_message(text, entities)
return text, entities
@@ -15,17 +15,17 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import re
from typing import List, Tuple
from typing import List, Tuple, Pattern
from telethon.tl.types import TypeMessageEntity
class MatrixParserCommon:
mention_regex = re.compile("https://matrix.to/#/(@.+:.+)")
room_regex = re.compile("https://matrix.to/#/(#.+:.+)")
mention_regex = re.compile("https://matrix.to/#/(@.+:.+)") # type: Pattern
room_regex = re.compile("https://matrix.to/#/(#.+:.+)") # type: Pattern
block_tags = ("br", "p", "pre", "blockquote",
"ol", "ul", "li",
"h1", "h2", "h3", "h4", "h5", "h6",
"div", "hr", "table")
"div", "hr", "table") # type: Tuple[str, ...]
ParsedMessage = Tuple[str, List[TypeMessageEntity]]
@@ -38,7 +38,7 @@ def parse_html(html: str) -> ParsedMessage:
class MatrixParser(HTMLParser, MatrixParserCommon):
def __init__(self):
super(HTMLParser, self).__init__()
super(MatrixParser, self).__init__()
self.text = "" # type: str
self.entities = [] # type: List[TypeMessageEntity]
self._building_entities = {} # type: Dict[str, TypeMessageEntity]
+2 -2
View File
@@ -789,9 +789,9 @@ class Portal:
def _matrix_event_to_entities(event: dict) -> Tuple[str, Optional[List[TypeMessageEntity]]]:
try:
if event.get("format", None) == "org.matrix.custom.html":
message, entities = formatter.matrix_to_telegram(event["formatted_body"])
message, entities = formatter.matrix_to_telegram(event.get("formatted_body", ""))
else:
message, entities = formatter.matrix_text_to_telegram(event["body"])
message, entities = formatter.matrix_text_to_telegram(event.get("body", ""))
except KeyError:
message, entities = None, None
return message, entities