diff --git a/TODO.md b/TODO.md index 5542e75b..c633866a 100644 --- a/TODO.md +++ b/TODO.md @@ -34,3 +34,5 @@ - Spinner when loading past room events or images - nio: org.matrix.room.preview\_urls, m.room.aliases + +- Markdown: don't turn #things into title (space), disable __ syntax diff --git a/harmonyqml/backend/backend.py b/harmonyqml/backend/backend.py index 6ff370b0..52aceb45 100644 --- a/harmonyqml/backend/backend.py +++ b/harmonyqml/backend/backend.py @@ -6,7 +6,6 @@ from typing import Dict, Set from PyQt5.QtCore import QObject, pyqtProperty, pyqtSlot -from .client_manager import ClientManager from .html_filter import HtmlFilter from .model.items import User from .model.qml_models import QMLModels @@ -18,7 +17,8 @@ class Backend(QObject): self.past_tokens: Dict[str, str] = {} self.fully_loaded_rooms: Set[str] = set() - self._client_manager: ClientManager = ClientManager() + from .client_manager import ClientManager + self._client_manager: ClientManager = ClientManager(self) self._models: QMLModels = QMLModels() self._html_filter: HtmlFilter = HtmlFilter() # a = self._client_manager; m = self._models diff --git a/harmonyqml/backend/client.py b/harmonyqml/backend/client.py index dd2c158d..39665d27 100644 --- a/harmonyqml/backend/client.py +++ b/harmonyqml/backend/client.py @@ -48,9 +48,13 @@ class Client(QObject): roomTypingUsersUpdated = pyqtSignal(str, list) - def __init__(self, hostname: str, username: str, device_id: str = "" - ) -> None: - super().__init__() + def __init__(self, + manager, + hostname: str, + username: str, + device_id: str = "") -> None: + super().__init__(manager) + self.manager = manager host, *port = hostname.split(":") self.host: str = host @@ -189,10 +193,11 @@ class Client(QObject): @pyqtSlot(str, str) @futurize - def sendMessage(self, room_id: str, text: str) -> None: + def sendMarkdown(self, room_id: str, text: str) -> None: + html = self.manager.backend.htmlFilter.fromMarkdown(text) content = { "body": text, - "formatted_body": text, + "formatted_body": html, "format": "org.matrix.custom.html", "msgtype": "m.text", } diff --git a/harmonyqml/backend/client_manager.py b/harmonyqml/backend/client_manager.py index a6a546b5..e83d5cd2 100644 --- a/harmonyqml/backend/client_manager.py +++ b/harmonyqml/backend/client_manager.py @@ -15,6 +15,7 @@ from PyQt5.QtCore import ( from harmonyqml import __about__ +from .backend import Backend from .client import Client AccountConfig = Dict[str, Dict[str, str]] @@ -28,8 +29,9 @@ class ClientManager(QObject): clientDeleted = pyqtSignal(str) - def __init__(self) -> None: - super().__init__() + def __init__(self, backend: Backend) -> None: + super().__init__(backend) + self.backend = backend self._clients: Dict[str, Client] = {} @@ -45,7 +47,7 @@ class ClientManager(QObject): @pyqtSlot() def configLoad(self) -> None: for user_id, info in self.configAccounts().items(): - client = Client(info["hostname"], user_id) + client = Client(self, info["hostname"], user_id) client.resumeSession(user_id, info["token"], info["device_id"])\ .add_done_callback(lambda _, c=client: self._on_connected(c)) @@ -55,7 +57,7 @@ class ClientManager(QObject): def new(self, hostname: str, username: str, password: str, device_id: str = "") -> None: - client = Client(hostname, username, device_id) + client = Client(self, hostname, username, device_id) client.login(password, self.defaultDeviceName)\ .add_done_callback(lambda _: self._on_connected(client)) diff --git a/harmonyqml/backend/html_filter.py b/harmonyqml/backend/html_filter.py index 64d20a1f..dc3124bd 100644 --- a/harmonyqml/backend/html_filter.py +++ b/harmonyqml/backend/html_filter.py @@ -4,6 +4,7 @@ import re import html_sanitizer.sanitizer as sanitizer +import mistune from lxml.html import HtmlElement, etree from PyQt5.QtCore import QObject, pyqtProperty, pyqtSlot @@ -37,6 +38,14 @@ class HtmlFilter(QObject): sanitizer.lxml.html.clean.Cleaner.safe_attrs |= \ self.sanitizer_settings["attributes"]["font"] + # hard_wrap: convert all \n to
without required two spaces + self._markdown_to_html = mistune.Markdown(hard_wrap=True) + + + @pyqtSlot(str, result=str) + def fromMarkdown(self, text: str) -> str: + return self.filter(self._markdown_to_html(text)) + @pyqtSlot(str, result=str) def filter(self, html: str) -> str: diff --git a/harmonyqml/components/chat/SendBox.qml b/harmonyqml/components/chat/SendBox.qml index 65964338..206346ee 100644 --- a/harmonyqml/components/chat/SendBox.qml +++ b/harmonyqml/components/chat/SendBox.qml @@ -54,7 +54,7 @@ Rectangle { if (textArea.text === "") { return } Backend.clientManager.clients[chatPage.user_id] - .sendMessage(chatPage.room.room_id, textArea.text) + .sendMarkdown(chatPage.room.room_id, textArea.text) textArea.clear() } Keys.onEnterPressed: Keys.onReturnPressed(event) // numpad enter