Skip to content

Commit

Permalink
Switch to commonmark
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Nov 17, 2019
1 parent bb45218 commit 1d03fd8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
52 changes: 26 additions & 26 deletions maubot/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,24 @@
# 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/>.
from typing import Union, Awaitable, Optional, Tuple
from markdown.extensions import Extension
import markdown as md
from html import escape
import attr

from mautrix.client import Client as MatrixClient, SyncStream
from mautrix.util.formatter import parse_html
from mautrix.util import markdown
from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent,
MessageType, TextMessageEventContent, Format, RelatesTo)


class EscapeHTML(Extension):
def extendMarkdown(self, md):
md.preprocessors.deregister("html_block")
md.inlinePatterns.deregister("html")


escape_html = EscapeHTML()


def parse_markdown(markdown: str, allow_html: bool = False) -> Tuple[str, str]:
html = md.markdown(markdown, extensions=[escape_html] if not allow_html else [])
def parse_formatted(message: str, allow_html: bool = False, render_markdown: bool = True
) -> Tuple[str, str]:
if render_markdown:
html = markdown.render(message, allow_html=allow_html)
elif allow_html:
html = message
else:
return message, escape(message)
return parse_html(html), html


Expand All @@ -50,14 +47,15 @@ def __init__(self, base: MessageEvent, client: MatrixClient):

def respond(self, content: Union[str, MessageEventContent],
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]:
if isinstance(content, str):
content = TextMessageEventContent(msgtype=MessageType.NOTICE, body=content)
if markdown:
if allow_html or markdown:
content.format = Format.HTML
content.body, content.formatted_body = parse_markdown(content.body,
allow_html=html_in_markdown)
content.body, content.formatted_body = parse_formatted(content.body,
render_markdown=markdown,
allow_html=allow_html)
if edits:
content.set_edit(edits)
elif reply and not self.disable_reply:
Expand All @@ -66,9 +64,9 @@ def respond(self, content: Union[str, MessageEventContent],

def reply(self, content: Union[str, MessageEventContent],
event_type: EventType = EventType.ROOM_MESSAGE, markdown: bool = True,
html_in_markdown: bool = False) -> Awaitable[EventID]:
return self.respond(content, event_type, markdown, reply=True,
html_in_markdown=html_in_markdown)
allow_html: bool = False) -> Awaitable[EventID]:
return self.respond(content, event_type, markdown=markdown, reply=True,
allow_html=allow_html)

def mark_read(self) -> Awaitable[None]:
return self.client.send_receipt(self.room_id, self.event_id, "m.read")
Expand All @@ -78,17 +76,19 @@ def react(self, key: str) -> Awaitable[EventID]:

def edit(self, content: Union[str, MessageEventContent],
event_type: EventType = EventType.ROOM_MESSAGE, markdown: bool = True,
html_in_markdown: bool = False) -> Awaitable[EventID]:
return self.respond(content, event_type, markdown, edits=self,
html_in_markdown=html_in_markdown)
allow_html: bool = False) -> Awaitable[EventID]:
return self.respond(content, event_type, markdown=markdown, edits=self,
allow_html=allow_html)


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,
relates_to: Optional[RelatesTo] = None, **kwargs) -> Awaitable[EventID]:
relates_to: Optional[RelatesTo] = None, **kwargs
) -> Awaitable[EventID]:
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 edits:
raise ValueError("Can't use edits and relates_to at the same time.")
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mautrix
aiohttp
SQLAlchemy
alembic
Markdown
commonmark
ruamel.yaml
attrs
bcrypt
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"aiohttp>=3.0.1,<4",
"SQLAlchemy>=1.2.3,<2",
"alembic>=1.0.0,<2",
"Markdown>=3.0.0,<4",
"commonmark>=0.9.1,<1",
"ruamel.yaml>=0.15.35,<0.17",
"attrs>=18.1.0",
"bcrypt>=3.1.4,<4",
Expand Down

0 comments on commit 1d03fd8

Please sign in to comment.