Convert messages to Matrix's HTML format

legacy
Charlotte Som 2021-09-12 12:59:18 +01:00
parent ced035cf60
commit e483064ee1
5 changed files with 74 additions and 5 deletions

16
Cargo.lock generated
View File

@ -848,6 +848,15 @@ dependencies = [
"digest", "digest",
] ]
[[package]]
name = "html-escape"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "816ea801a95538fc5f53c836697b3f8b64a9d664c4f0b91efe1fe7c92e4dbcb7"
dependencies = [
"utf8-width",
]
[[package]] [[package]]
name = "http" name = "http"
version = "0.2.4" version = "0.2.4"
@ -1487,6 +1496,7 @@ dependencies = [
"bincode", "bincode",
"discord_message_format", "discord_message_format",
"env_logger", "env_logger",
"html-escape",
"log", "log",
"matrix-sdk", "matrix-sdk",
"serde", "serde",
@ -2811,6 +2821,12 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
name = "utf8-width"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cf7d77f457ef8dfa11e4cd5933c5ddb5dc52a94664071951219a97710f0a32b"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "0.8.2" version = "0.8.2"

View File

@ -13,6 +13,7 @@ tokio = { version = "1.10.1", features = ["full"] }
url = "2.2.2" url = "2.2.2"
log = "0.4.14" log = "0.4.14"
env_logger = "0.9.0" env_logger = "0.9.0"
html-escape = "0.2.9"
[dependencies.serenity] [dependencies.serenity]
version = "0.10.9" version = "0.10.9"

View File

@ -20,7 +20,7 @@ use tokio::sync::mpsc;
use url::Url; use url::Url;
use crate::{ use crate::{
message_ast::{convert_plain, format_discord}, message_ast::{convert_plain, format_discord, format_matrix},
messages::{MessageAuthor, MessageReference, SentMessage}, messages::{MessageAuthor, MessageReference, SentMessage},
}; };
@ -131,10 +131,9 @@ pub async fn forward_to_matrix(
if let Some(room) = client.get_joined_room(&room_id) { if let Some(room) = client.get_joined_room(&room_id) {
let event = room let event = room
.send( .send(
AnyMessageEventContent::RoomMessage(MessageEventContent::text_plain( AnyMessageEventContent::RoomMessage(MessageEventContent::text_html(
format_discord( format_discord(&message.content),
&message.content, // TODO: Format as HTML format_matrix(&message.content),
),
)), )),
None, None,
) )

View File

@ -0,0 +1,51 @@
use super::{MessageContent, Styled};
pub fn convert_matrix(message: &str) -> MessageContent {
todo!();
}
pub fn format_matrix(message_content: &MessageContent) -> String {
message_content
.iter()
.map(|component| match component {
Styled::Plain(text) => html_escape::encode_text(text).to_string(),
Styled::Link { target, text } => format!(
r#"<a href="{}">{}</a>"#,
html_escape::encode_quoted_attribute(target),
format_matrix(text)
),
Styled::Italic(inner) => format!("<em>{}</em>", format_matrix(inner)),
Styled::Bold(inner) => format!("<strong>{}</strong>", format_matrix(inner)),
Styled::Strikethrough(inner) => format!("<del>{}</del>", format_matrix(inner)),
Styled::Underline(inner) => format!("<u>{}</u>", format_matrix(inner)),
Styled::Code(code) => format!("<code>{}</code>", html_escape::encode_text(code)),
Styled::CodeBlock { lang, source } => {
format!(
r#"<pre><code{}>{}</code></pre>"#,
lang.as_ref()
.map(|lang| format!(
r#" class="language-{}""#,
html_escape::encode_quoted_attribute(lang)
))
.unwrap_or("".to_string()),
source,
)
}
Styled::Spoiler { reason, content } => format!(
"<span data-mx-spoiler{}>{}</span>",
reason
.as_ref()
.map(|reason| format!(r#"="{}""#, html_escape::encode_quoted_attribute(reason)))
.unwrap_or("".to_string()),
format_matrix(content)
),
Styled::HardBreak => "<br>".to_string(),
Styled::BlockQuote(inner) => {
format!("<blockquote>{}</blockquote>", format_matrix(inner))
}
})
.collect()
}

View File

@ -1,4 +1,5 @@
mod convert_discord; mod convert_discord;
mod convert_matrix;
mod convert_plain; mod convert_plain;
pub type MessageContent = Vec<Styled>; pub type MessageContent = Vec<Styled>;
@ -31,4 +32,5 @@ pub enum Styled {
} }
pub use convert_discord::{convert_discord, format_discord}; pub use convert_discord::{convert_discord, format_discord};
pub use convert_matrix::{convert_matrix, format_matrix};
pub use convert_plain::convert_plain; pub use convert_plain::convert_plain;