Browse Source

Parse URLs in torrent description

adaptive-webui-19844
Christophe Dumez 14 years ago
parent
commit
ea04f79e0f
  1. 60
      src/misc.cpp
  2. 2
      src/misc.h
  3. 2
      src/properties/propertieswidget.cpp

60
src/misc.cpp

@ -832,3 +832,63 @@ bool misc::removeEmptyFolder(const QString &dirpath) @@ -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<a href=\"\\2\">\\2</a>");
// Capture links without scheme
QRegExp reNoScheme("<a\\s+href=\"(?!http(s?))([a-zA-Z0-9\\?%=&/_\\.-:#]+)\\s*\">");
result.replace(reNoScheme, "<a href=\"http://\\1\">");
return result;
}

2
src/misc.h

@ -123,6 +123,8 @@ public: @@ -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);

2
src/properties/propertieswidget.cpp

@ -263,7 +263,7 @@ void PropertiesWidget::loadTorrentInfos(const QTorrentHandle &_h) { @@ -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

Loading…
Cancel
Save