Skip to content
This repository was archived by the owner on Oct 17, 2019. It is now read-only.

Commit 9e8f0b7

Browse files
committed
Initial support for sending markdown formatted messages
fixes #283
1 parent a21db78 commit 9e8f0b7

File tree

7 files changed

+84
-7
lines changed

7 files changed

+84
-7
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ find_package(OpenSSL REQUIRED)
250250
find_package(MatrixClient 0.1.0 REQUIRED)
251251
find_package(Olm 2 REQUIRED)
252252
find_package(spdlog 1.0.0 CONFIG REQUIRED)
253+
find_package(maddy 1.0.0 CONFIG REQUIRED)
253254

254255
if(NOT LMDBXX_INCLUDE_DIR)
255256
find_path(LMDBXX_INCLUDE_DIR

deps/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ option(USE_BUNDLED_SPDLOG "Use the bundled version of spdlog." ${USE_BUNDLED})
2626
option(USE_BUNDLED_OLM "Use the bundled version of libolm." ${USE_BUNDLED})
2727
option(USE_BUNDLED_TWEENY "Use the bundled version of Tweeny." ${USE_BUNDLED})
2828
option(USE_BUNDLED_LMDBXX "Use the bundled version of lmdbxx." ${USE_BUNDLED})
29+
option(USE_BUNDLED_MADDY "Use the bundled version of maddy." ${USE_BUNDLED})
2930
option(USE_BUNDLED_MATRIX_CLIENT "Use the bundled version of mtxclient."
3031
${USE_BUNDLED})
3132

@@ -64,6 +65,12 @@ set(SPDLOG_URL https://github.com/gabime/spdlog/archive/v1.1.0.tar.gz)
6465
set(SPDLOG_HASH
6566
3dbcbfd8c07e25f5e0d662b194d3a7772ef214358c49ada23c044c4747ce8b19)
6667

68+
set(
69+
MADDY_URL
70+
https://github.com/mujx/maddy/archive/d6b32013a580d40de57ac8b6650846abecbb071f.tar.gz
71+
)
72+
set(MADDY_HASH c40df975420aa9f1549d9e528af84243f701b5264ed9c32d86b1cfc9306c15b8)
73+
6774
set(JSON_HEADER_URL
6875
https://github.com/nlohmann/json/releases/download/v3.2.0/json.hpp)
6976
set(JSON_HEADER_HASH
@@ -80,6 +87,10 @@ if(USE_BUNDLED_SPDLOG)
8087
include(SpdLog)
8188
endif()
8289

90+
if(USE_BUNDLED_MADDY)
91+
include(Maddy)
92+
endif()
93+
8394
if(USE_BUNDLED_OLM)
8495
include(Olm)
8596
endif()

deps/cmake/Maddy.cmake

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
set(WINDOWS_FLAGS "")
2+
3+
if(MSVC)
4+
set(WINDOWS_FLAGS "-DCMAKE_GENERATOR_PLATFORM=x64")
5+
endif()
6+
7+
ExternalProject_Add(
8+
Maddy
9+
10+
URL ${MADDY_URL}
11+
URL_HASH SHA256=${MADDY_HASH}
12+
13+
BUILD_IN_SOURCE 1
14+
SOURCE_DIR ${DEPS_BUILD_DIR}/maddy
15+
CONFIGURE_COMMAND ${CMAKE_COMMAND}
16+
-DCMAKE_INSTALL_PREFIX=${DEPS_INSTALL_DIR}
17+
${DEPS_BUILD_DIR}/maddy
18+
${WINDOWS_FLAGS})
19+
20+
list(APPEND THIRD_PARTY_DEPS Maddy)

src/Utils.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
#include <QApplication>
44
#include <QDesktopWidget>
55
#include <QSettings>
6+
#include <QTextDocument>
67
#include <QXmlStreamReader>
78
#include <cmath>
89

910
#include <boost/variant.hpp>
11+
#include <maddy/parser.h>
1012

1113
#include "Config.h"
1214

@@ -327,3 +329,24 @@ utils::linkifyMessage(const QString &body)
327329

328330
return textString;
329331
}
332+
333+
std::string
334+
utils::markdownToHtml(const std::string &text)
335+
{
336+
std::stringstream markdownInput(text);
337+
auto parser = std::make_shared<maddy::Parser>();
338+
339+
return parser->Parse(markdownInput);
340+
}
341+
342+
std::string
343+
utils::markdownToHtml(const QString &text)
344+
{
345+
return markdownToHtml(text.toStdString());
346+
}
347+
348+
std::string
349+
utils::stripHtml(const std::string &text)
350+
{
351+
return QString::fromStdString(text).remove(QRegExp("<[^>]*>")).toStdString();
352+
}

src/Utils.h

+11
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,15 @@ getMessageBody(const RoomMessageT &event)
215215
//! Replace raw URLs in text with HTML link tags.
216216
QString
217217
linkifyMessage(const QString &body);
218+
219+
//! Convert the input markdown text to html.
220+
std::string
221+
markdownToHtml(const QString &text);
222+
223+
std::string
224+
markdownToHtml(const std::string &text);
225+
226+
//! Return the plain text version of an html document.
227+
std::string
228+
stripHtml(const std::string &text);
218229
}

src/timeline/TimelineItem.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,16 @@ TimelineItem::TimelineItem(mtx::events::MessageType ty,
265265
auto timestamp = QDateTime::currentDateTime();
266266

267267
if (ty == mtx::events::MessageType::Emote) {
268-
body = QString("* %1 %2").arg(displayName).arg(body);
268+
body = QString("%1 %2").arg(displayName).arg(body);
269269
descriptionMsg_ = {"", userid, body, utils::descriptiveTime(timestamp), timestamp};
270270
} else {
271271
descriptionMsg_ = {
272272
"You: ", userid, body, utils::descriptiveTime(timestamp), timestamp};
273273
}
274274

275-
body = body.toHtmlEscaped();
276-
body.replace(conf::strings::url_regex, conf::strings::url_html);
277-
body.replace("\n", "<br/>");
275+
body = QString::fromStdString(utils::markdownToHtml(body));
276+
body = utils::linkifyMessage(body);
277+
278278
generateTimestamp(timestamp);
279279

280280
if (withSender) {
@@ -489,7 +489,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Emote>
489489

490490
auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
491491
auto displayName = Cache::displayName(room_id_, sender);
492-
auto emoteMsg = QString("* %1 %2").arg(displayName).arg(formatted_body);
492+
auto emoteMsg = QString("%1 %2").arg(displayName).arg(formatted_body);
493493

494494
descriptionMsg_ = {"", sender, emoteMsg, utils::descriptiveTime(timestamp), timestamp};
495495

src/timeline/TimelineView.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -1233,8 +1233,12 @@ template<>
12331233
mtx::events::msg::Emote
12341234
toRoomMessage<mtx::events::msg::Emote>(const PendingMessage &m)
12351235
{
1236+
auto html = utils::markdownToHtml(m.body);
1237+
12361238
mtx::events::msg::Emote emote;
1237-
emote.body = m.body.toStdString();
1239+
emote.body = utils::stripHtml(html);
1240+
emote.formatted_body = html;
1241+
12381242
return emote;
12391243
}
12401244

@@ -1254,8 +1258,15 @@ template<>
12541258
mtx::events::msg::Text
12551259
toRoomMessage<mtx::events::msg::Text>(const PendingMessage &m)
12561260
{
1261+
auto html = utils::markdownToHtml(m.body);
1262+
12571263
mtx::events::msg::Text text;
1258-
text.body = m.body.toStdString();
1264+
text.body = utils::stripHtml(html);
1265+
text.formatted_body = html;
1266+
1267+
std::cout << "body: " << text.body << std::endl;
1268+
std::cout << "formatted_body: " << text.formatted_body << std::endl;
1269+
12591270
return text;
12601271
}
12611272

0 commit comments

Comments
 (0)