Changeset View
Changeset View
Standalone View
Standalone View
src/core/utils.cpp
Show First 20 Lines • Show All 51 Lines • ▼ Show 20 Line(s) | |||||
52 | QString Utils::markdownToRichText(const QString &markDown) | 52 | QString Utils::markdownToRichText(const QString &markDown) | ||
53 | { | 53 | { | ||
54 | //qCDebug(RUQOLA_LOG) << "BEFORE markdownToRichText "<<markDown; | 54 | //qCDebug(RUQOLA_LOG) << "BEFORE markdownToRichText "<<markDown; | ||
55 | QString str = markDown; | 55 | QString str = markDown; | ||
56 | 56 | | |||
57 | //TODO remove replaceSmileys when we will use unicode emoticons | 57 | //TODO remove replaceSmileys when we will use unicode emoticons | ||
58 | const KTextToHTML::Options convertFlags = KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText | KTextToHTML::ReplaceSmileys | KTextToHTML::ConvertPhoneNumbers; | 58 | const KTextToHTML::Options convertFlags = KTextToHTML::PreserveSpaces | KTextToHTML::HighlightText | KTextToHTML::ReplaceSmileys | KTextToHTML::ConvertPhoneNumbers; | ||
59 | str = KTextToHTML::convertToHtml(str, convertFlags); | 59 | str = KTextToHTML::convertToHtml(str, convertFlags); | ||
60 | | ||||
61 | // substitute "[example.com](<a href="...">...</a>)" style urls | ||||
62 | str = Utils::convertTextWithUrl(str); | ||||
63 | | ||||
60 | //Bug 391520 I don't remember why I removed <br /> need to investigate | 64 | //Bug 391520 I don't remember why I removed <br /> need to investigate | ||
61 | //str.remove(QStringLiteral("<br />")); | 65 | //str.remove(QStringLiteral("<br />")); | ||
62 | //qCDebug(RUQOLA_LOG) << "markdownToRichText "<<str; | 66 | //qCDebug(RUQOLA_LOG) << "markdownToRichText "<<str; | ||
63 | return str; | 67 | return str; | ||
64 | } | 68 | } | ||
65 | 69 | | |||
66 | QString Utils::generateRichText(const QString &str, const QString &username) | 70 | QString Utils::generateRichText(const QString &str, const QString &username) | ||
67 | { | 71 | { | ||
68 | QString newStr = Utils::markdownToRichText(str); | 72 | QString newStr = Utils::markdownToRichText(str); | ||
69 | //Need to convert [foo](http:///...) | | |||
70 | newStr = Utils::convertTextWithUrl(newStr); | | |||
71 | static const QRegularExpression regularExpressionUser(QStringLiteral("(^|\\s+)@([\\w._-]+)")); | 73 | static const QRegularExpression regularExpressionUser(QStringLiteral("(^|\\s+)@([\\w._-]+)")); | ||
72 | QRegularExpressionMatchIterator userIterator = regularExpressionUser.globalMatch(newStr); | 74 | QRegularExpressionMatchIterator userIterator = regularExpressionUser.globalMatch(newStr); | ||
73 | while (userIterator.hasNext()) { | 75 | while (userIterator.hasNext()) { | ||
74 | const QRegularExpressionMatch match = userIterator.next(); | 76 | const QRegularExpressionMatch match = userIterator.next(); | ||
75 | const QStringRef word = match.capturedRef(2); | 77 | const QStringRef word = match.capturedRef(2); | ||
76 | //Highlight only if it's yours | 78 | //Highlight only if it's yours | ||
77 | if (word == username) { | 79 | if (word == username) { | ||
78 | //Improve color | 80 | //Improve color | ||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Line(s) | 182 | if (o.contains(key)) { | |||
181 | return QDateTime::fromString(o.value(key).toString(), Qt::ISODate).toMSecsSinceEpoch(); | 183 | return QDateTime::fromString(o.value(key).toString(), Qt::ISODate).toMSecsSinceEpoch(); | ||
182 | } else { | 184 | } else { | ||
183 | return -1; | 185 | return -1; | ||
184 | } | 186 | } | ||
185 | } | 187 | } | ||
186 | 188 | | |||
187 | QString Utils::convertTextWithUrl(const QString &str) | 189 | QString Utils::convertTextWithUrl(const QString &str) | ||
188 | { | 190 | { | ||
191 | static const QRegularExpression regularExpressionAHref(QStringLiteral("<a href=\"(.*)\">(.*)</a>")); | ||||
192 | | ||||
189 | QString newStr; | 193 | QString newStr; | ||
190 | bool isRef = false; | 194 | bool isRef = false; | ||
191 | bool isUrl = false; | 195 | bool isUrl = false; | ||
192 | QString url; | 196 | QString url; | ||
193 | QString references; | 197 | QString references; | ||
194 | for (int i = 0; i < str.count(); ++i) { | 198 | for (int i = 0; i < str.count(); ++i) { | ||
195 | if (str.at(i) == QLatin1Char('[')) { | 199 | if (str.at(i) == QLatin1Char('[')) { | ||
196 | isRef = true; | 200 | isRef = true; | ||
197 | } else if (str.at(i) == QLatin1Char(']')) { | 201 | } else if (str.at(i) == QLatin1Char(']')) { | ||
198 | isRef = false; | 202 | isRef = false; | ||
199 | if ((i == str.count() - 1) || (str.at(i+1) != QLatin1Char('('))) { | 203 | if ((i == str.count() - 1) || (str.at(i+1) != QLatin1Char('('))) { | ||
200 | newStr += QLatin1Char('[') + references + QLatin1Char(']'); | 204 | newStr += QLatin1Char('[') + references + QLatin1Char(']'); | ||
201 | } | 205 | } | ||
202 | } else if (str.at(i) == QLatin1Char('(') && !references.isEmpty()) { | 206 | } else if (str.at(i) == QLatin1Char('(') && !references.isEmpty()) { | ||
203 | isUrl = true; | 207 | isUrl = true; | ||
204 | } else if (str.at(i) == QLatin1Char(')') && !references.isEmpty()) { | 208 | } else if (str.at(i) == QLatin1Char(')') && !references.isEmpty()) { | ||
205 | isUrl = false; | 209 | isUrl = false; | ||
210 | // detect whether the string already contains HTML <a/> tags | ||||
211 | if (url.startsWith(QLatin1Char('<'))) { | ||||
212 | newStr += url.replace(regularExpressionAHref, QStringLiteral("<a href=\"\\1\">%1</a>").arg(references)); | ||||
213 | } else { | ||||
206 | newStr += QStringLiteral("<a href=\'%1'>%2</a>").arg(url, references); | 214 | newStr += QStringLiteral("<a href=\'%1'>%2</a>").arg(url, references); | ||
215 | } | ||||
207 | references.clear(); | 216 | references.clear(); | ||
208 | url.clear(); | 217 | url.clear(); | ||
209 | } else { | 218 | } else { | ||
210 | if (isRef) { | 219 | if (isRef) { | ||
211 | references += str.at(i); | 220 | references += str.at(i); | ||
212 | } else if (isUrl) { | 221 | } else if (isUrl) { | ||
213 | url += str.at(i); | 222 | url += str.at(i); | ||
214 | } else { | 223 | } else { | ||
215 | newStr += str.at(i); | 224 | newStr += str.at(i); | ||
216 | } | 225 | } | ||
217 | } | 226 | } | ||
218 | } | 227 | } | ||
219 | return newStr; | 228 | return newStr; | ||
220 | } | 229 | } |