From 60f37ebeb130bd58adece6bee06420b40c4e5a05 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 9 Nov 2022 18:49:00 +0100 Subject: Matrix: reply body formatting shouldn't have html, formatted body should be pure html. Dont display urls in codeblocks --- src/Text.cpp | 4 ++-- src/plugins/Fourchan.cpp | 2 +- src/plugins/Matrix.cpp | 38 +++++++++++++++++++++++++------------- 3 files changed, 28 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/Text.cpp b/src/Text.cpp index 836651c..3ecf24c 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -695,7 +695,7 @@ namespace QuickMedia if(!text_format_stack.empty()) { if((text_format_stack.top().text_flags & FORMATTED_TEXT_FLAG_COLOR) && !force_color) text_element_color = text_format_stack.top().color; - if(text_format_stack.top().text_flags & FORMATTED_TEXT_FLAG_MONOSPACE) + if(text_format_stack.top().text_flags & FORMATTED_TEXT_FLAG_CODE) monospace = true; } @@ -789,7 +789,7 @@ namespace QuickMedia for(size_t i = 0; i < textElement.text.size();) { mgl::Color text_color = text_element_color; - if(!force_color) { + if(!force_color && !monospace) { if(url_range_index < url_ranges.size()) { size_t string_offset = (textElement.text.data() + i) - str.data(); if(string_offset >= url_ranges[url_range_index].start && string_offset < url_ranges[url_range_index].start + url_ranges[url_range_index].length) { diff --git a/src/plugins/Fourchan.cpp b/src/plugins/Fourchan.cpp index c58d942..4a9d2d7 100644 --- a/src/plugins/Fourchan.cpp +++ b/src/plugins/Fourchan.cpp @@ -291,7 +291,7 @@ namespace QuickMedia { break; case CommentPiece::Type::CODEBLOCK: // TODO: Use a different colored background - comment_text += Text::formatted_text(std::move(cp.text), mgl::Color(255, 255, 255, 255), FORMATTED_TEXT_FLAG_MONOSPACE); + comment_text += Text::formatted_text(std::move(cp.text), mgl::Color(255, 255, 255, 255), FORMATTED_TEXT_FLAG_CODE); break; } }); diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 31aab76..82ca583 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -131,7 +131,7 @@ namespace QuickMedia { return str; } - static std::string remove_reply_formatting(const Message *message) { + static std::string remove_reply_formatting(const Message *message, bool keep_formatted = false) { if(!message->body_is_formatted && strncmp(message->body.c_str(), "> <@", 4) == 0) { size_t index = message->body.find("> ", 4); if(index != std::string::npos) { @@ -141,7 +141,10 @@ namespace QuickMedia { } return message->body; } else { - return formatted_text_to_qm_text(message->body.c_str(), message->body.size(), false); + if(keep_formatted) + return message->body; + else + return formatted_text_to_qm_text(message->body.c_str(), message->body.size(), false); } } @@ -2391,9 +2394,9 @@ namespace QuickMedia { formatted_text_flags |= FORMATTED_TEXT_FLAG_COLOR; if(parse_userdata.inside_source_highlight || !parse_userdata.supports_syntax_highlight || (parse_userdata.inside_code_tag && parse_userdata.code_tag_language.size() == 0)) { - formatted_text_flags |= FORMATTED_TEXT_FLAG_MONOSPACE; + formatted_text_flags |= FORMATTED_TEXT_FLAG_CODE; } else if(parse_userdata.inside_code_tag) { - formatted_text_flags |= FORMATTED_TEXT_FLAG_MONOSPACE; + formatted_text_flags |= FORMATTED_TEXT_FLAG_CODE; // TODO: guess language from code if no language is set. // TODO: Allow the user to choose style in config file. @@ -3622,14 +3625,18 @@ namespace QuickMedia { return result; } - static std::string get_reply_message(const Message *message) { + static std::string get_reply_message(const Message *message, bool keep_formatted = false) { std::string related_to_body; switch(message->type) { case MessageType::TEXT: { - if(message->related_event_type != RelatedEventType::NONE) - related_to_body = remove_reply_formatting(message); - else - related_to_body = message->body; + if(message->related_event_type != RelatedEventType::NONE) { + related_to_body = remove_reply_formatting(message, keep_formatted); + } else { + if(keep_formatted && message->body_is_formatted) + related_to_body = message->body; + else + related_to_body = message_to_qm_text(message, false); + } break; } case MessageType::IMAGE: @@ -3644,9 +3651,13 @@ namespace QuickMedia { case MessageType::FILE: related_to_body = "sent a file"; break; - case MessageType::REDACTION: - related_to_body = message->body; + case MessageType::REDACTION: { + if(keep_formatted && message->body_is_formatted) + related_to_body = message->body; + else + related_to_body = message_to_qm_text(message, false); break; + } } return related_to_body; } @@ -3664,8 +3675,9 @@ namespace QuickMedia { std::string Matrix::create_formatted_body_for_message_reply(RoomData *room, const Message *message, const std::string &body) { std::string formatted_body = body_to_formatted_body(room, body); - std::string related_to_body = get_reply_message(message); - html_escape_sequences(related_to_body); + std::string related_to_body = get_reply_message(message, true); + if(!message->body_is_formatted) + html_escape_sequences(related_to_body); // TODO: Add keybind to navigate to the reply message, which would also depend on this formatting. // Note: user id and event id is not url escaped here on purpose, because that messes up riot.im replies for certain user ids... return "" -- cgit v1.2.3