Browse Source

Merge remote-tracking branch 'PKEv/QRegExp_for_local_file'

pull/2298/head
agilob 10 years ago
parent
commit
3ceb289020
No known key found for this signature in database
GPG Key ID: 296F0B764741106C
  1. 25
      src/chatlog/chatmessage.cpp

25
src/chatlog/chatmessage.cpp

@ -177,38 +177,43 @@ void ChatMessage::hideDate()
QString ChatMessage::detectAnchors(const QString &str) QString ChatMessage::detectAnchors(const QString &str)
{ {
QString out = str; QString out;
// detect URIs // detect URIs
QRegExp exp("(" QRegExp exp("("
"(?:\\b)(www\\.|http[s]?|ftp)://" // (protocol)://(printable - non-special character) "(?:\\b)((www\\.)|(http[s]?|ftp)://)" // (protocol)://(printable - non-special character)
// http://ONEORMOREALHPA-DIGIT // http://ONEORMOREALHPA-DIGIT
"\\w+\\S+)" // any other character, lets domains and other "\\w+\\S+)" // any other character, lets domains and other
"|(^tox:[@\\w]+$)"); // starts with `tox` then : and only alpha-digits till the end "|(?:\\b)(file:///.*$)" //link to a local file, valid until the end of the line
"|(?:\\b)(tox:[a-zA-Z\\d]{76}$)" //link with full user address
"|(?:\\b)(tox:\\S+@\\S+)"); // starts with `tox` then : and only alpha-digits till the end
// also accepts tox:agilob@net as simplified TOX ID // also accepts tox:agilob@net as simplified TOX ID
//support for multi-line text
QStringList messageLines = str.split("\n");
QStringList outLines;
for (int i = 0; i < messageLines.size(); ++i)
{
out = messageLines.at(i);
int offset = 0; int offset = 0;
while ((offset = exp.indexIn(out, offset)) != -1) while ((offset = exp.indexIn(out, offset)) != -1)
{ {
QString url = exp.cap(); QString url = exp.cap();
// If there's a trailing " it's a HTML attribute, e.g. a smiley img's title=":tox:" // If there's a trailing " it's a HTML attribute, e.g. a smiley img's title=":tox:"
if (url == "tox:\"") if (url == "tox:\"")
{ {
offset += url.length(); offset += url.length();
continue; continue;
} }
// add scheme if not specified // add scheme if not specified
if (exp.cap(1) == "www.") if (exp.cap(2) == "www.")
url.prepend("http://"); url.prepend("http://");
QString htmledUrl = QString("<a href=\"%1\">%1</a>").arg(url); QString htmledUrl = QString("<a href=\"%1\">%1</a>").arg(url);
out.replace(offset, exp.cap().length(), htmledUrl); out.replace(offset, exp.cap().length(), htmledUrl);
offset += htmledUrl.length(); offset += htmledUrl.length();
} }
outLines.push_back(out);
return out; }
return outLines.join("\n");
} }
QString ChatMessage::detectQuotes(const QString& str, MessageType type) QString ChatMessage::detectQuotes(const QString& str, MessageType type)

Loading…
Cancel
Save