diff --git a/src/misc.cpp b/src/misc.cpp index 8a65d74b8..a998bc86b 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -832,3 +832,63 @@ bool misc::removeEmptyFolder(const QString &dirpath) } return false; } + +QString misc::parseHtmlLinks(const QString &raw_text) +{ + QString result = raw_text; + QRegExp reURL("(\\s|^)" //start with whitespace or beginning of line + "(" + "(" //case 1 -- URL with scheme + "(http(s?))\\://" //start with scheme + "([a-zA-Z0-9_-]+\\.)+" // domainpart. at least one of these must exist + "([a-zA-Z0-9\\?%=&/_\\.:#;-]+)" // everything to 1st non-URI char, must be at least one char after the previous dot (cannot use ".*" because it can be too greedy) + ")" + "|" + "(" //case 2a -- no scheme, contains common TLD example.com + "([a-zA-Z0-9_-]+\\.)+" // domainpart. at least one of these must exist + "(?=" // must be followed by TLD + "AERO|aero|" //N.B. assertions are non-capturing + "ARPA|arpa|" + "ASIA|asia|" + "BIZ|biz|" + "CAT|cat|" + "COM|com|" + "COOP|coop|" + "EDU|edu|" + "GOV|gov|" + "INFO|info|" + "INT|int|" + "JOBS|jobs|" + "MIL|mil|" + "MOBI|mobi|" + "MUSEUM|museum|" + "NAME|name|" + "NET|net|" + "ORG|org|" + "PRO|pro|" + "RO|ro|" + "RU|ru|" + "TEL|tel|" + "TRAVEL|travel" + ")" + "([a-zA-Z0-9\\?%=&/_\\.:#;-]+)" // everything to 1st non-URI char, must be at least one char after the previous dot (cannot use ".*" because it can be too greedy) + ")" + "|" + "(" // case 2b no scheme, no TLD, must have at least 2 aphanum strings plus uncommon TLD string --> del.icio.us + "([a-zA-Z0-9_-]+\\.){2,}" //2 or more domainpart. --> del.icio. + "[a-zA-Z]{2,}" //one ab (2 char or longer) --> us + "([a-zA-Z0-9\\?%=&/_\\.:#;-]*)" // everything to 1st non-URI char, maybe nothing in case of del.icio.us/path + ")" + ")" + ); + + + // Capture links + result.replace(reURL, "\\1\\2"); + + // Capture links without scheme + QRegExp reNoScheme(""); + result.replace(reNoScheme, ""); + + return result; +} diff --git a/src/misc.h b/src/misc.h index 0c783dd69..72927bcae 100644 --- a/src/misc.h +++ b/src/misc.h @@ -123,6 +123,8 @@ public: return MyFile.remove(); } + static QString parseHtmlLinks(const QString &raw_text); + static bool removeEmptyFolder(const QString &dirpath); static quint64 computePathSize(QString path); diff --git a/src/properties/propertieswidget.cpp b/src/properties/propertieswidget.cpp index 443cee073..c2ec88851 100644 --- a/src/properties/propertieswidget.cpp +++ b/src/properties/propertieswidget.cpp @@ -263,7 +263,7 @@ void PropertiesWidget::loadTorrentInfos(const QTorrentHandle &_h) { // Pieces size pieceSize_lbl->setText(misc::friendlyUnit(h.piece_length())); // Comment - comment_text->setHtml(h.comment()); + comment_text->setHtml(misc::parseHtmlLinks(h.comment())); // URL seeds loadUrlSeeds(); // List files in torrent