Switch to commonmark
This commit is contained in:
parent
bb45218639
commit
1d03fd83df
@ -14,27 +14,24 @@
|
|||||||
# 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/>.
|
||||||
from typing import Union, Awaitable, Optional, Tuple
|
from typing import Union, Awaitable, Optional, Tuple
|
||||||
from markdown.extensions import Extension
|
from html import escape
|
||||||
import markdown as md
|
|
||||||
import attr
|
import attr
|
||||||
|
|
||||||
from mautrix.client import Client as MatrixClient, SyncStream
|
from mautrix.client import Client as MatrixClient, SyncStream
|
||||||
from mautrix.util.formatter import parse_html
|
from mautrix.util.formatter import parse_html
|
||||||
|
from mautrix.util import markdown
|
||||||
from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent,
|
from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent,
|
||||||
MessageType, TextMessageEventContent, Format, RelatesTo)
|
MessageType, TextMessageEventContent, Format, RelatesTo)
|
||||||
|
|
||||||
|
|
||||||
class EscapeHTML(Extension):
|
def parse_formatted(message: str, allow_html: bool = False, render_markdown: bool = True
|
||||||
def extendMarkdown(self, md):
|
) -> Tuple[str, str]:
|
||||||
md.preprocessors.deregister("html_block")
|
if render_markdown:
|
||||||
md.inlinePatterns.deregister("html")
|
html = markdown.render(message, allow_html=allow_html)
|
||||||
|
elif allow_html:
|
||||||
|
html = message
|
||||||
escape_html = EscapeHTML()
|
else:
|
||||||
|
return message, escape(message)
|
||||||
|
|
||||||
def parse_markdown(markdown: str, allow_html: bool = False) -> Tuple[str, str]:
|
|
||||||
html = md.markdown(markdown, extensions=[escape_html] if not allow_html else [])
|
|
||||||
return parse_html(html), html
|
return parse_html(html), html
|
||||||
|
|
||||||
|
|
||||||
@ -50,14 +47,15 @@ class MaubotMessageEvent(MessageEvent):
|
|||||||
|
|
||||||
def respond(self, content: Union[str, MessageEventContent],
|
def respond(self, content: Union[str, MessageEventContent],
|
||||||
event_type: EventType = EventType.ROOM_MESSAGE, markdown: bool = True,
|
event_type: EventType = EventType.ROOM_MESSAGE, markdown: bool = True,
|
||||||
html_in_markdown: bool = False, reply: bool = False,
|
allow_html: bool = False, reply: bool = False,
|
||||||
edits: Optional[Union[EventID, MessageEvent]] = None) -> Awaitable[EventID]:
|
edits: Optional[Union[EventID, MessageEvent]] = None) -> Awaitable[EventID]:
|
||||||
if isinstance(content, str):
|
if isinstance(content, str):
|
||||||
content = TextMessageEventContent(msgtype=MessageType.NOTICE, body=content)
|
content = TextMessageEventContent(msgtype=MessageType.NOTICE, body=content)
|
||||||
if markdown:
|
if allow_html or markdown:
|
||||||
content.format = Format.HTML
|
content.format = Format.HTML
|
||||||
content.body, content.formatted_body = parse_markdown(content.body,
|
content.body, content.formatted_body = parse_formatted(content.body,
|
||||||
allow_html=html_in_markdown)
|
render_markdown=markdown,
|
||||||
|
allow_html=allow_html)
|
||||||
if edits:
|
if edits:
|
||||||
content.set_edit(edits)
|
content.set_edit(edits)
|
||||||
elif reply and not self.disable_reply:
|
elif reply and not self.disable_reply:
|
||||||
@ -66,9 +64,9 @@ class MaubotMessageEvent(MessageEvent):
|
|||||||
|
|
||||||
def reply(self, content: Union[str, MessageEventContent],
|
def reply(self, content: Union[str, MessageEventContent],
|
||||||
event_type: EventType = EventType.ROOM_MESSAGE, markdown: bool = True,
|
event_type: EventType = EventType.ROOM_MESSAGE, markdown: bool = True,
|
||||||
html_in_markdown: bool = False) -> Awaitable[EventID]:
|
allow_html: bool = False) -> Awaitable[EventID]:
|
||||||
return self.respond(content, event_type, markdown, reply=True,
|
return self.respond(content, event_type, markdown=markdown, reply=True,
|
||||||
html_in_markdown=html_in_markdown)
|
allow_html=allow_html)
|
||||||
|
|
||||||
def mark_read(self) -> Awaitable[None]:
|
def mark_read(self) -> Awaitable[None]:
|
||||||
return self.client.send_receipt(self.room_id, self.event_id, "m.read")
|
return self.client.send_receipt(self.room_id, self.event_id, "m.read")
|
||||||
@ -78,17 +76,19 @@ class MaubotMessageEvent(MessageEvent):
|
|||||||
|
|
||||||
def edit(self, content: Union[str, MessageEventContent],
|
def edit(self, content: Union[str, MessageEventContent],
|
||||||
event_type: EventType = EventType.ROOM_MESSAGE, markdown: bool = True,
|
event_type: EventType = EventType.ROOM_MESSAGE, markdown: bool = True,
|
||||||
html_in_markdown: bool = False) -> Awaitable[EventID]:
|
allow_html: bool = False) -> Awaitable[EventID]:
|
||||||
return self.respond(content, event_type, markdown, edits=self,
|
return self.respond(content, event_type, markdown=markdown, edits=self,
|
||||||
html_in_markdown=html_in_markdown)
|
allow_html=allow_html)
|
||||||
|
|
||||||
|
|
||||||
class MaubotMatrixClient(MatrixClient):
|
class MaubotMatrixClient(MatrixClient):
|
||||||
def send_markdown(self, room_id: RoomID, markdown: str, msgtype: MessageType = MessageType.TEXT,
|
def send_markdown(self, room_id: RoomID, markdown: str, *, allow_html: bool = False,
|
||||||
|
msgtype: MessageType = MessageType.TEXT,
|
||||||
edits: Optional[Union[EventID, MessageEvent]] = None,
|
edits: Optional[Union[EventID, MessageEvent]] = None,
|
||||||
relates_to: Optional[RelatesTo] = None, **kwargs) -> Awaitable[EventID]:
|
relates_to: Optional[RelatesTo] = None, **kwargs
|
||||||
|
) -> Awaitable[EventID]:
|
||||||
content = TextMessageEventContent(msgtype=msgtype, format=Format.HTML)
|
content = TextMessageEventContent(msgtype=msgtype, format=Format.HTML)
|
||||||
content.body, content.formatted_body = parse_markdown(markdown)
|
content.body, content.formatted_body = parse_formatted(markdown, allow_html=allow_html)
|
||||||
if relates_to:
|
if relates_to:
|
||||||
if edits:
|
if edits:
|
||||||
raise ValueError("Can't use edits and relates_to at the same time.")
|
raise ValueError("Can't use edits and relates_to at the same time.")
|
||||||
|
@ -2,7 +2,7 @@ mautrix
|
|||||||
aiohttp
|
aiohttp
|
||||||
SQLAlchemy
|
SQLAlchemy
|
||||||
alembic
|
alembic
|
||||||
Markdown
|
commonmark
|
||||||
ruamel.yaml
|
ruamel.yaml
|
||||||
attrs
|
attrs
|
||||||
bcrypt
|
bcrypt
|
||||||
|
2
setup.py
2
setup.py
@ -26,7 +26,7 @@ setuptools.setup(
|
|||||||
"aiohttp>=3.0.1,<4",
|
"aiohttp>=3.0.1,<4",
|
||||||
"SQLAlchemy>=1.2.3,<2",
|
"SQLAlchemy>=1.2.3,<2",
|
||||||
"alembic>=1.0.0,<2",
|
"alembic>=1.0.0,<2",
|
||||||
"Markdown>=3.0.0,<4",
|
"commonmark>=0.9.1,<1",
|
||||||
"ruamel.yaml>=0.15.35,<0.17",
|
"ruamel.yaml>=0.15.35,<0.17",
|
||||||
"attrs>=18.1.0",
|
"attrs>=18.1.0",
|
||||||
"bcrypt>=3.1.4,<4",
|
"bcrypt>=3.1.4,<4",
|
||||||
|
Loading…
Reference in New Issue
Block a user