Skip to content

Commit

Permalink
Add method for generating formatted_body from body correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 23, 2022
1 parent f7ff483 commit 01a82f6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v0.15.1 (2022-03-23)

* Added `ensure_has_html` method for `TextMessageEventContent` to generate
a HTML `formatted_body` from the plaintext `body` correctly (i.e. escaping
HTML and replacing newlines).

## v0.15.0 (2022-03-16)

* **Breaking change** Removed Python 3.7 support.
Expand Down
2 changes: 1 addition & 1 deletion mautrix/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.15.0"
__version__ = "0.15.1"
__author__ = "Tulir Asokan <tulir@maunium.net>"
__all__ = [
"api",
Expand Down
9 changes: 6 additions & 3 deletions mautrix/types/event/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,18 @@ def set_reply(
super().set_reply(reply_to)
if isinstance(reply_to, str):
return
if not self.formatted_body or len(self.formatted_body) == 0 or self.format != Format.HTML:
self.format = Format.HTML
self.formatted_body = escape(self.body).replace("\n", "<br/>")
if isinstance(reply_to, MessageEvent):
self.ensure_has_html()
self.formatted_body = (
reply_to.make_reply_fallback_html(displayname) + self.formatted_body
)
self.body = reply_to.make_reply_fallback_text(displayname) + self.body

def ensure_has_html(self) -> None:
if not self.formatted_body or self.format != Format.HTML:
self.format = Format.HTML
self.formatted_body = escape(self.body).replace("\n", "<br/>")

def formatted(self, format: Format) -> Optional[str]:
if self.format == format:
return self.formatted_body
Expand Down

0 comments on commit 01a82f6

Please sign in to comment.