Convert discord markdown code blocks
This commit is contained in:
parent
8ce7c7b3f3
commit
61a60ea4c7
1 changed files with 37 additions and 29 deletions
24
main.py
24
main.py
|
@ -79,10 +79,7 @@ class MatrixClient(nio.AsyncClient):
|
||||||
|
|
||||||
await self.close()
|
await self.close()
|
||||||
|
|
||||||
async def process_emotes(self, message, emotes):
|
async def upload_emote(self, emote_id):
|
||||||
formatted_body = message
|
|
||||||
|
|
||||||
async def upload_emote(emote_id):
|
|
||||||
if emote_id in self.uploaded_emotes.keys():
|
if emote_id in self.uploaded_emotes.keys():
|
||||||
return self.uploaded_emotes[emote_id]
|
return self.uploaded_emotes[emote_id]
|
||||||
|
|
||||||
|
@ -115,16 +112,27 @@ class MatrixClient(nio.AsyncClient):
|
||||||
|
|
||||||
return resp.content_uri
|
return resp.content_uri
|
||||||
|
|
||||||
|
async def get_fmt_body(self, body, emotes):
|
||||||
|
# Markdown code blocks
|
||||||
|
replace = "```"
|
||||||
|
for i in range(body.count(replace)):
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
if i % 2:
|
||||||
|
body = body.replace(replace, "<pre><code>", 1)
|
||||||
|
else:
|
||||||
|
body = body.replace(replace, "</code></pre>", 1)
|
||||||
|
|
||||||
for emote in emotes.keys():
|
for emote in emotes.keys():
|
||||||
emote_ = await upload_emote(emotes[emote])
|
emote_ = await self.upload_emote(emotes[emote])
|
||||||
if emote_:
|
if emote_:
|
||||||
emote = f":{emote}:"
|
emote = f":{emote}:"
|
||||||
formatted_body = formatted_body.replace(
|
body = body.replace(
|
||||||
emote, f"""<img alt=\"{emote}\" title=\"{emote}\"
|
emote, f"""<img alt=\"{emote}\" title=\"{emote}\"
|
||||||
height=\"32\" src=\"{emote_}\" data-mx-emoticon />"""
|
height=\"32\" src=\"{emote_}\" data-mx-emoticon />"""
|
||||||
)
|
)
|
||||||
|
|
||||||
return formatted_body
|
return body
|
||||||
|
|
||||||
async def message_send(self, message, channel_id, emotes,
|
async def message_send(self, message, channel_id, emotes,
|
||||||
reply_id=None, edit_id=None):
|
reply_id=None, edit_id=None):
|
||||||
|
@ -133,7 +141,7 @@ height=\"32\" src=\"{emote_}\" data-mx-emoticon />"""
|
||||||
content = {
|
content = {
|
||||||
"body": message,
|
"body": message,
|
||||||
"format": "org.matrix.custom.html",
|
"format": "org.matrix.custom.html",
|
||||||
"formatted_body": await self.process_emotes(message, emotes),
|
"formatted_body": await self.get_fmt_body(message, emotes),
|
||||||
"msgtype": "m.text"
|
"msgtype": "m.text"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue