Fix bugs
This commit is contained in:
@@ -78,7 +78,7 @@ def matrix_to_telegram(html: str) -> ParsedMessage:
|
|||||||
|
|
||||||
html = add_surrogates(html)
|
html = add_surrogates(html)
|
||||||
text, entities = parse_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)
|
text, entities = cut_long_message(text, entities)
|
||||||
|
|
||||||
return text, entities
|
return text, entities
|
||||||
|
|||||||
@@ -15,17 +15,17 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# 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/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
import re
|
import re
|
||||||
from typing import List, Tuple
|
from typing import List, Tuple, Pattern
|
||||||
from telethon.tl.types import TypeMessageEntity
|
from telethon.tl.types import TypeMessageEntity
|
||||||
|
|
||||||
|
|
||||||
class MatrixParserCommon:
|
class MatrixParserCommon:
|
||||||
mention_regex = re.compile("https://matrix.to/#/(@.+:.+)")
|
mention_regex = re.compile("https://matrix.to/#/(@.+:.+)") # type: Pattern
|
||||||
room_regex = re.compile("https://matrix.to/#/(#.+:.+)")
|
room_regex = re.compile("https://matrix.to/#/(#.+:.+)") # type: Pattern
|
||||||
block_tags = ("br", "p", "pre", "blockquote",
|
block_tags = ("br", "p", "pre", "blockquote",
|
||||||
"ol", "ul", "li",
|
"ol", "ul", "li",
|
||||||
"h1", "h2", "h3", "h4", "h5", "h6",
|
"h1", "h2", "h3", "h4", "h5", "h6",
|
||||||
"div", "hr", "table")
|
"div", "hr", "table") # type: Tuple[str, ...]
|
||||||
|
|
||||||
|
|
||||||
ParsedMessage = Tuple[str, List[TypeMessageEntity]]
|
ParsedMessage = Tuple[str, List[TypeMessageEntity]]
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ def parse_html(html: str) -> ParsedMessage:
|
|||||||
|
|
||||||
class MatrixParser(HTMLParser, MatrixParserCommon):
|
class MatrixParser(HTMLParser, MatrixParserCommon):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(HTMLParser, self).__init__()
|
super(MatrixParser, self).__init__()
|
||||||
self.text = "" # type: str
|
self.text = "" # type: str
|
||||||
self.entities = [] # type: List[TypeMessageEntity]
|
self.entities = [] # type: List[TypeMessageEntity]
|
||||||
self._building_entities = {} # type: Dict[str, TypeMessageEntity]
|
self._building_entities = {} # type: Dict[str, TypeMessageEntity]
|
||||||
|
|||||||
@@ -789,9 +789,9 @@ class Portal:
|
|||||||
def _matrix_event_to_entities(event: dict) -> Tuple[str, Optional[List[TypeMessageEntity]]]:
|
def _matrix_event_to_entities(event: dict) -> Tuple[str, Optional[List[TypeMessageEntity]]]:
|
||||||
try:
|
try:
|
||||||
if event.get("format", None) == "org.matrix.custom.html":
|
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:
|
else:
|
||||||
message, entities = formatter.matrix_text_to_telegram(event["body"])
|
message, entities = formatter.matrix_text_to_telegram(event.get("body", ""))
|
||||||
except KeyError:
|
except KeyError:
|
||||||
message, entities = None, None
|
message, entities = None, None
|
||||||
return message, entities
|
return message, entities
|
||||||
|
|||||||
Reference in New Issue
Block a user