Merge pull request #60 from MonokelPinguin/remove-xml-parsing-of-messages

Simplify linkifyMessage.  Fixes #18.  Thanks @MonokelPinguin
This commit is contained in:
Joseph Donofry 2019-05-01 09:53:40 -04:00 committed by GitHub
commit 3f2781af86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 61 deletions

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <QRegExp> #include <QRegularExpression>
#include <QString> #include <QString>
// Non-theme app configuration. Layouts, fonts spacing etc. // Non-theme app configuration. Layouts, fonts spacing etc.
@ -51,8 +51,11 @@ constexpr auto LABEL_BIG_SIZE_RATIO = 2;
namespace strings { namespace strings {
const QString url_html = "<a href=\"\\1\">\\1</a>"; const QString url_html = "<a href=\"\\1\">\\1</a>";
const QRegExp url_regex( const QRegularExpression url_regex(
"((www\\.(?!\\.)|[a-z][a-z0-9+.-]*://)[^\\s<>'\"]+[^!,\\.\\s<>'\"\\]\\)\\:])"); // match an URL, that is not quoted, i.e.
// vvvvvvv match quote via negative lookahead/lookbehind vvvvvv
// vvvv atomic match url -> fail if there is a " before or after vv
"(?<!\")(?>((www\\.(?!\\.)|[a-z][a-z0-9+.-]*://)[^\\s<>'\"]+[^!,\\.\\s<>'\"\\]\\)\\:]))(?!\")");
} }
// Window geometry. // Window geometry.

View file

@ -291,65 +291,9 @@ utils::linkifyMessage(const QString &body)
{ {
// Convert to valid XML. // Convert to valid XML.
auto doc = QString("<html>%1</html>").arg(body); auto doc = QString("<html>%1</html>").arg(body);
doc.replace(conf::strings::url_regex, conf::strings::url_html);
doc.replace("<mx-reply>", "");
doc.replace("</mx-reply>", "");
doc.replace("<br>", "<br></br>");
QXmlStreamReader xml{doc};
QString textString;
while (!xml.atEnd() && !xml.hasError()) {
auto t = xml.readNext();
switch (t) {
case QXmlStreamReader::Characters: {
auto text = xml.text().toString();
text.replace(conf::strings::url_regex, conf::strings::url_html);
textString += text;
break;
}
case QXmlStreamReader::StartDocument:
case QXmlStreamReader::EndDocument:
break;
case QXmlStreamReader::StartElement: {
if (xml.name() == "html")
break;
textString += QString("<%1").arg(xml.name().toString());
const auto attrs = xml.attributes();
for (const auto &e : attrs)
textString += QString(" %1=\"%2\"")
.arg(e.name().toString())
.arg(e.value().toString());
textString += ">";
break;
}
case QXmlStreamReader::EndElement: {
if (xml.name() == "html")
break;
textString += QString("</%1>").arg(xml.name().toString());
break;
}
default: {
break;
}
}
}
if (xml.hasError()) {
qWarning() << "error while parsing xml" << xml.errorString() << doc;
doc.replace("<html>", "");
doc.replace("</html>", "");
return doc; return doc;
}
return textString;
} }
QString QString