Initial support for sending markdown formatted messages

fixes #283
This commit is contained in:
Konstantinos Sideris 2018-09-07 20:05:30 +03:00
parent a21db789e7
commit 9e8f0b7409
7 changed files with 84 additions and 7 deletions

View file

@ -250,6 +250,7 @@ find_package(OpenSSL REQUIRED)
find_package(MatrixClient 0.1.0 REQUIRED) find_package(MatrixClient 0.1.0 REQUIRED)
find_package(Olm 2 REQUIRED) find_package(Olm 2 REQUIRED)
find_package(spdlog 1.0.0 CONFIG REQUIRED) find_package(spdlog 1.0.0 CONFIG REQUIRED)
find_package(maddy 1.0.0 CONFIG REQUIRED)
if(NOT LMDBXX_INCLUDE_DIR) if(NOT LMDBXX_INCLUDE_DIR)
find_path(LMDBXX_INCLUDE_DIR find_path(LMDBXX_INCLUDE_DIR

11
deps/CMakeLists.txt vendored
View file

@ -26,6 +26,7 @@ option(USE_BUNDLED_SPDLOG "Use the bundled version of spdlog." ${USE_BUNDLED})
option(USE_BUNDLED_OLM "Use the bundled version of libolm." ${USE_BUNDLED}) option(USE_BUNDLED_OLM "Use the bundled version of libolm." ${USE_BUNDLED})
option(USE_BUNDLED_TWEENY "Use the bundled version of Tweeny." ${USE_BUNDLED}) option(USE_BUNDLED_TWEENY "Use the bundled version of Tweeny." ${USE_BUNDLED})
option(USE_BUNDLED_LMDBXX "Use the bundled version of lmdbxx." ${USE_BUNDLED}) option(USE_BUNDLED_LMDBXX "Use the bundled version of lmdbxx." ${USE_BUNDLED})
option(USE_BUNDLED_MADDY "Use the bundled version of maddy." ${USE_BUNDLED})
option(USE_BUNDLED_MATRIX_CLIENT "Use the bundled version of mtxclient." option(USE_BUNDLED_MATRIX_CLIENT "Use the bundled version of mtxclient."
${USE_BUNDLED}) ${USE_BUNDLED})
@ -64,6 +65,12 @@ set(SPDLOG_URL https://github.com/gabime/spdlog/archive/v1.1.0.tar.gz)
set(SPDLOG_HASH set(SPDLOG_HASH
3dbcbfd8c07e25f5e0d662b194d3a7772ef214358c49ada23c044c4747ce8b19) 3dbcbfd8c07e25f5e0d662b194d3a7772ef214358c49ada23c044c4747ce8b19)
set(
MADDY_URL
https://github.com/mujx/maddy/archive/d6b32013a580d40de57ac8b6650846abecbb071f.tar.gz
)
set(MADDY_HASH c40df975420aa9f1549d9e528af84243f701b5264ed9c32d86b1cfc9306c15b8)
set(JSON_HEADER_URL set(JSON_HEADER_URL
https://github.com/nlohmann/json/releases/download/v3.2.0/json.hpp) https://github.com/nlohmann/json/releases/download/v3.2.0/json.hpp)
set(JSON_HEADER_HASH set(JSON_HEADER_HASH
@ -80,6 +87,10 @@ if(USE_BUNDLED_SPDLOG)
include(SpdLog) include(SpdLog)
endif() endif()
if(USE_BUNDLED_MADDY)
include(Maddy)
endif()
if(USE_BUNDLED_OLM) if(USE_BUNDLED_OLM)
include(Olm) include(Olm)
endif() endif()

20
deps/cmake/Maddy.cmake vendored Normal file
View file

@ -0,0 +1,20 @@
set(WINDOWS_FLAGS "")
if(MSVC)
set(WINDOWS_FLAGS "-DCMAKE_GENERATOR_PLATFORM=x64")
endif()
ExternalProject_Add(
Maddy
URL ${MADDY_URL}
URL_HASH SHA256=${MADDY_HASH}
BUILD_IN_SOURCE 1
SOURCE_DIR ${DEPS_BUILD_DIR}/maddy
CONFIGURE_COMMAND ${CMAKE_COMMAND}
-DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL_DIR}
${DEPS_BUILD_DIR}/maddy
${WINDOWS_FLAGS})
list(APPEND THIRD_PARTY_DEPS Maddy)

View file

@ -3,10 +3,12 @@
#include <QApplication> #include <QApplication>
#include <QDesktopWidget> #include <QDesktopWidget>
#include <QSettings> #include <QSettings>
#include <QTextDocument>
#include <QXmlStreamReader> #include <QXmlStreamReader>
#include <cmath> #include <cmath>
#include <boost/variant.hpp> #include <boost/variant.hpp>
#include <maddy/parser.h>
#include "Config.h" #include "Config.h"
@ -327,3 +329,24 @@ utils::linkifyMessage(const QString &body)
return textString; return textString;
} }
std::string
utils::markdownToHtml(const std::string &text)
{
std::stringstream markdownInput(text);
auto parser = std::make_shared<maddy::Parser>();
return parser->Parse(markdownInput);
}
std::string
utils::markdownToHtml(const QString &text)
{
return markdownToHtml(text.toStdString());
}
std::string
utils::stripHtml(const std::string &text)
{
return QString::fromStdString(text).remove(QRegExp("<[^>]*>")).toStdString();
}

View file

@ -215,4 +215,15 @@ getMessageBody(const RoomMessageT &event)
//! Replace raw URLs in text with HTML link tags. //! Replace raw URLs in text with HTML link tags.
QString QString
linkifyMessage(const QString &body); linkifyMessage(const QString &body);
//! Convert the input markdown text to html.
std::string
markdownToHtml(const QString &text);
std::string
markdownToHtml(const std::string &text);
//! Return the plain text version of an html document.
std::string
stripHtml(const std::string &text);
} }

View file

@ -265,16 +265,16 @@ TimelineItem::TimelineItem(mtx::events::MessageType ty,
auto timestamp = QDateTime::currentDateTime(); auto timestamp = QDateTime::currentDateTime();
if (ty == mtx::events::MessageType::Emote) { if (ty == mtx::events::MessageType::Emote) {
body = QString("* %1 %2").arg(displayName).arg(body); body = QString("%1 %2").arg(displayName).arg(body);
descriptionMsg_ = {"", userid, body, utils::descriptiveTime(timestamp), timestamp}; descriptionMsg_ = {"", userid, body, utils::descriptiveTime(timestamp), timestamp};
} else { } else {
descriptionMsg_ = { descriptionMsg_ = {
"You: ", userid, body, utils::descriptiveTime(timestamp), timestamp}; "You: ", userid, body, utils::descriptiveTime(timestamp), timestamp};
} }
body = body.toHtmlEscaped(); body = QString::fromStdString(utils::markdownToHtml(body));
body.replace(conf::strings::url_regex, conf::strings::url_html); body = utils::linkifyMessage(body);
body.replace("\n", "<br/>");
generateTimestamp(timestamp); generateTimestamp(timestamp);
if (withSender) { if (withSender) {
@ -489,7 +489,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Emote>
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts); auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
auto displayName = Cache::displayName(room_id_, sender); auto displayName = Cache::displayName(room_id_, sender);
auto emoteMsg = QString("* %1 %2").arg(displayName).arg(formatted_body); auto emoteMsg = QString("%1 %2").arg(displayName).arg(formatted_body);
descriptionMsg_ = {"", sender, emoteMsg, utils::descriptiveTime(timestamp), timestamp}; descriptionMsg_ = {"", sender, emoteMsg, utils::descriptiveTime(timestamp), timestamp};

View file

@ -1233,8 +1233,12 @@ template<>
mtx::events::msg::Emote mtx::events::msg::Emote
toRoomMessage<mtx::events::msg::Emote>(const PendingMessage &m) toRoomMessage<mtx::events::msg::Emote>(const PendingMessage &m)
{ {
auto html = utils::markdownToHtml(m.body);
mtx::events::msg::Emote emote; mtx::events::msg::Emote emote;
emote.body = m.body.toStdString(); emote.body = utils::stripHtml(html);
emote.formatted_body = html;
return emote; return emote;
} }
@ -1254,8 +1258,15 @@ template<>
mtx::events::msg::Text mtx::events::msg::Text
toRoomMessage<mtx::events::msg::Text>(const PendingMessage &m) toRoomMessage<mtx::events::msg::Text>(const PendingMessage &m)
{ {
auto html = utils::markdownToHtml(m.body);
mtx::events::msg::Text text; mtx::events::msg::Text text;
text.body = m.body.toStdString(); text.body = utils::stripHtml(html);
text.formatted_body = html;
std::cout << "body: " << text.body << std::endl;
std::cout << "formatted_body: " << text.formatted_body << std::endl;
return text; return text;
} }