Browse Source

Merge pull request #6180 from magao/issue6171

Allow | in RSS must contain. Closes #6171.
adaptive-webui-19844
sledgehammer999 8 years ago committed by GitHub
parent
commit
354702140a
  1. 95
      src/base/rss/rssdownloadrule.cpp
  2. 10
      src/gui/rss/automatedrssdownloader.cpp

95
src/base/rss/rssdownloadrule.cpp

@ -50,36 +50,93 @@ DownloadRule::DownloadRule()
bool DownloadRule::matches(const QString &articleTitle) const bool DownloadRule::matches(const QString &articleTitle) const
{ {
QRegExp whitespace("\\s+");
if (!m_mustContain.empty()) { if (!m_mustContain.empty()) {
bool logged = false; bool logged = false;
bool foundMustContain = true;
// Each expression is either a regex, or a set of wildcards separated by whitespace.
// Accept if any complete expression matches.
foreach (const QString &expression, m_mustContain) {
if (expression.isEmpty())
continue;
if (!logged) {
qDebug() << "Checking matching expressions:" << m_mustContain.join("|");
logged = true;
}
if (m_useRegex) {
QRegExp reg(expression, Qt::CaseInsensitive, QRegExp::RegExp);
foreach (const QString &token, m_mustContain) { if (reg.indexIn(articleTitle) > -1)
if (!token.isEmpty()) { foundMustContain = true;
if (!logged) { }
qDebug() << "Checking matching expressions:" << m_mustContain.join("|"); else {
logged = true; // Only accept if every wildcard token (separated by spaces) is present in the article name.
// Order of wildcard tokens is unimportant (if order is important, they should have used *).
foundMustContain = true;
foreach (const QString &wildcard, expression.split(whitespace, QString::SplitBehavior::SkipEmptyParts)) {
QRegExp reg(wildcard, Qt::CaseInsensitive, QRegExp::Wildcard);
if (reg.indexIn(articleTitle) == -1) {
foundMustContain = false;
break;
}
} }
}
QRegExp reg(token, Qt::CaseInsensitive, m_useRegex ? QRegExp::RegExp : QRegExp::Wildcard); if (foundMustContain) {
if (reg.indexIn(articleTitle) < 0) qDebug() << "Found matching expression:" << expression;
return false; break;
} }
} }
if (!foundMustContain)
return false;
} }
if (!m_mustNotContain.empty()) { if (!m_mustNotContain.empty()) {
bool logged = false; bool logged = false;
foreach (const QString &token, m_mustNotContain) { // Each expression is either a regex, or a set of wildcards separated by whitespace.
if (!token.isEmpty()) { // Reject if any complete expression matches.
if (!logged) { foreach (const QString &expression, m_mustNotContain) {
qDebug() << "Checking not matching expressions:" << m_mustNotContain.join("|"); if (expression.isEmpty())
logged = true; continue;
}
QRegExp reg(token, Qt::CaseInsensitive, m_useRegex ? QRegExp::RegExp : QRegExp::Wildcard); if (!logged) {
if (reg.indexIn(articleTitle) > -1) qDebug() << "Checking not matching expressions:" << m_mustNotContain.join("|");
logged = true;
}
if (m_useRegex) {
QRegExp reg(expression, Qt::CaseInsensitive, QRegExp::RegExp);
if (reg.indexIn(articleTitle) > -1) {
qDebug() << "Found not matching expression:" << expression;
return false; return false;
}
}
// Only reject if every wildcard token (separated by spaces) is present in the article name.
// Order of wildcard tokens is unimportant (if order is important, they should have used *).
bool foundMustNotContain = true;
foreach (const QString &wildcard, expression.split(whitespace, QString::SplitBehavior::SkipEmptyParts)) {
QRegExp reg(wildcard, Qt::CaseInsensitive, QRegExp::Wildcard);
if (reg.indexIn(articleTitle) == -1) {
foundMustNotContain = false;
break;
}
}
if (foundMustNotContain) {
qDebug()<< "Found not matching expression:" << expression;
return false;
} }
} }
} }
@ -162,7 +219,7 @@ void DownloadRule::setMustContain(const QString &tokens)
if (m_useRegex) if (m_useRegex)
m_mustContain = QStringList() << tokens; m_mustContain = QStringList() << tokens;
else else
m_mustContain = tokens.split(" "); m_mustContain = tokens.split("|");
} }
void DownloadRule::setMustNotContain(const QString &tokens) void DownloadRule::setMustNotContain(const QString &tokens)
@ -220,7 +277,7 @@ QVariantHash DownloadRule::toVariantHash() const
{ {
QVariantHash hash; QVariantHash hash;
hash["name"] = m_name; hash["name"] = m_name;
hash["must_contain"] = m_mustContain.join(" "); hash["must_contain"] = m_mustContain.join("|");
hash["must_not_contain"] = m_mustNotContain.join("|"); hash["must_not_contain"] = m_mustNotContain.join("|");
hash["save_path"] = m_savePath; hash["save_path"] = m_savePath;
hash["affected_feeds"] = m_rssFeeds; hash["affected_feeds"] = m_rssFeeds;
@ -296,7 +353,7 @@ int DownloadRule::ignoreDays() const
QString DownloadRule::mustContain() const QString DownloadRule::mustContain() const
{ {
return m_mustContain.join(" "); return m_mustContain.join("|");
} }
QString DownloadRule::mustNotContain() const QString DownloadRule::mustNotContain() const

10
src/gui/rss/automatedrssdownloader.cpp

@ -589,14 +589,18 @@ void AutomatedRssDownloader::updateFieldsToolTips(bool regex)
{ {
QString tip; QString tip;
if (regex) { if (regex) {
tip = tr("Regex mode: use Perl-like regular expressions"); tip = "<p>" + tr("Regex mode: use Perl-like regular expressions") + "</p>";
ui->lineContains->setToolTip(tip); ui->lineContains->setToolTip(tip);
ui->lineNotContains->setToolTip(tip); ui->lineNotContains->setToolTip(tip);
} }
else { else {
tip = tr("Wildcard mode: you can use<ul><li>? to match any single character</li><li>* to match zero or more of any characters</li><li>Whitespaces count as AND operators</li></ul>"); tip = "<p>" + tr("Wildcard mode: you can use") + "<ul>"
+ "<li>" + tr("? to match any single character") + "</li>"
+ "<li>" + tr("* to match zero or more of any characters") + "</li>"
+ "<li>" + tr("Whitespaces count as AND operators (all words, any order)") + "</li>"
+ "<li>" + tr("| is used as OR operator") + "</li></ul></p>"
+ "<p>" + tr("If word order is important use * instead of whitespace.") + "</p>";
ui->lineContains->setToolTip(tip); ui->lineContains->setToolTip(tip);
tip = tr("Wildcard mode: you can use<ul><li>? to match any single character</li><li>* to match zero or more of any characters</li><li>| is used as OR operator</li></ul>");
ui->lineNotContains->setToolTip(tip); ui->lineNotContains->setToolTip(tip);
} }
} }

Loading…
Cancel
Save