diff options
Diffstat (limited to 'staging/adblock')
-rw-r--r-- | staging/adblock/filterlist.cpp | 48 | ||||
-rw-r--r-- | staging/adblock/filterlist.h | 27 | ||||
-rw-r--r-- | staging/adblock/meson.build | 31 | ||||
-rw-r--r-- | staging/adblock/plugin.cpp | 29 | ||||
-rw-r--r-- | staging/adblock/plugin/AdblockPlusPlugin.json (renamed from staging/adblock/AdblockPlusPlugin.json) | 0 | ||||
-rw-r--r-- | staging/adblock/plugin/plugin.cpp | 54 | ||||
-rw-r--r-- | staging/adblock/plugin/plugin.h (renamed from staging/adblock/plugin.h) | 0 | ||||
-rw-r--r-- | staging/adblock/test/parser.cpp | 33 |
8 files changed, 83 insertions, 139 deletions
diff --git a/staging/adblock/filterlist.cpp b/staging/adblock/filterlist.cpp index 9fb53ad..9c80e88 100644 --- a/staging/adblock/filterlist.cpp +++ b/staging/adblock/filterlist.cpp @@ -32,49 +32,23 @@ FilterList::~FilterList() qDeleteAll(m_rules); } -FilterList::ParseResult FilterList::parse(QTextStream &stream) +FilterList::ParseResult FilterList::parse(const QString &line) { - FilterList::ParseResult result; + if(line.startsWith('!')) { + parseComment(line); + return FilterList::Comment; - if(stream.readLine().trimmed() != "[Adblock Plus 2.0]") { - result.state = FilterList::InvalidFormat; - return result; - } + } else if(line.contains("##") || line.contains("#@#")) { + // ## is element hiding rule + // #@# is element hiding exception rule + return FilterList::Unsupported; - QString line; - while(stream.readLineInto(&line)) { - - if(!line.isEmpty()) { - ++result.lines_total; - - if(line.startsWith('!')) { - ++result.lines_comments; - parseComment(line); - - } else if(line.contains("##") || line.contains("#@#")) { - // ## is element hiding rule - // #@# is element hiding exception rule - if(qgetenv("PRINT_IGNORED") == "1") - qDebug("ignored: >%s<", qUtf8Printable(line)); - ++result.lines_ignored; - - } else { - if(parseRule(line)) - ++result.lines_parsed; - else { - if(qgetenv("PRINT_FAILED") == "1") - qDebug("failed: >%s<", qUtf8Printable(line)); - ++result.lines_failed; - } - } - } + } else { + return parseRule(line) ? FilterList::Rule : FilterList::Failed; } - - result.state = FilterList::Ok; - return result; } -void FilterList::parseComment(QString &line) +void FilterList::parseComment(const QString &line) { if(line.startsWith(comment_lastModified)) { lastModified = QDateTime::fromString(line.mid(comment_lastModified.size()), "dd MMM yyyy HH:mm 'UTC'"); diff --git a/staging/adblock/filterlist.h b/staging/adblock/filterlist.h index 59717af..5ac60b6 100644 --- a/staging/adblock/filterlist.h +++ b/staging/adblock/filterlist.h @@ -6,15 +6,13 @@ * SPDX-License-Identifier: GPL-3.0 */ -#include "filterinterface.h" +#include <filterinterface.h> #include <QDateTime> #include <QObject> #include <QString> #include <QStringList> #include <QVector> -class QTextStream; - namespace AdblockPlus { class Rule; @@ -26,20 +24,13 @@ public: explicit FilterList() = default; ~FilterList(); - enum ParseResultState { - Ok, - InvalidFormat, - }; - struct ParseResult { - int lines_total = 0; - int lines_comments = 0; - int lines_ignored = 0; - int lines_parsed = 0; - int lines_failed = 0; - - ParseResultState state; + enum ParseResult { + Comment, + Rule, + Unsupported, + Failed, }; - ParseResult parse(QTextStream &stream); + ParseResult parse(const QString &line); void filter(QWebEngineUrlRequestInfo &info) const override; bool isUpToDate() const override; @@ -53,12 +44,12 @@ public: } private: - void parseComment(QString &line); + void parseComment(const QString &line); bool parseRule(const QString &line); QDateTime lastModified; QDateTime expires; - QVector<Rule *> m_rules; + QVector<AdblockPlus::Rule *> m_rules; }; } // namespace AdblockPlus diff --git a/staging/adblock/meson.build b/staging/adblock/meson.build index 1b992e5..8d06fb8 100644 --- a/staging/adblock/meson.build +++ b/staging/adblock/meson.build @@ -1,30 +1,17 @@ -dep_adblockfilter = declare_dependency( - include_directories: include_directories('.'), - dependencies: dep_plugininterface, - link_with: static_library('adblockfilter', - [ 'filterlist.cpp', 'rule.cpp', 'options.cpp' ], - dependencies: [ dep_qt5, dep_plugininterface ] - ) +lib_adblockfilter = static_library('adblockfilter', + [ 'filterlist.cpp', 'rule.cpp', 'options.cpp' ], + include_directories: plugininterface_include, + dependencies: [ dep_qt5 ] ) AdblockPlusFilterPlugin = shared_library('AdblockPlusPlugin', - [ 'plugin.cpp', - mod_qt5.preprocess( - include_directories: plugininterface_include, - moc_headers: 'plugin.h', - dependencies: [ dep_qt5 ] - ) + [ 'plugin/plugin.cpp', + mod_qt5.preprocess(include_directories: plugininterface_include, moc_headers: 'plugin/plugin.h', dependencies: dep_qt5) ], - dependencies: [ dep_qt5, dep_plugininterface, dep_adblockfilter ], + include_directories: plugininterface_include, + link_with: lib_adblockfilter, + dependencies: dep_qt5, install: true, install_dir: get_option('libdir')/'smolbote/plugins' ) -test('adblockfilter: parser', - executable('adblockfilter-parsefilter', dependencies: [ dep_qt5, dep_gtest, dep_adblockfilter ], - sources: [ 'test/parser.cpp' ] - ), - workdir: meson.current_source_dir() / 'test', - should_fail: true -) - diff --git a/staging/adblock/plugin.cpp b/staging/adblock/plugin.cpp deleted file mode 100644 index b4f1b56..0000000 --- a/staging/adblock/plugin.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://library.iserlohn-fortress.net/aqua/smolbote.git - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "plugin.h" -#include "filterlist.h" -#include <QTextStream> - -Filter* AdblockPlusFilterPlugin::load(QIODevice* from) const -{ - if(!from->isOpen()) - return nullptr; - - QTextStream stream(from); - auto *list = new AdblockPlus::FilterList; - const auto result = list->parse(stream); - - if(result.state != AdblockPlus::FilterList::Ok) { - delete list; - list = nullptr; - } - - return list; -} - diff --git a/staging/adblock/AdblockPlusPlugin.json b/staging/adblock/plugin/AdblockPlusPlugin.json index 053826a..053826a 100644 --- a/staging/adblock/AdblockPlusPlugin.json +++ b/staging/adblock/plugin/AdblockPlusPlugin.json diff --git a/staging/adblock/plugin/plugin.cpp b/staging/adblock/plugin/plugin.cpp new file mode 100644 index 0000000..028c83f --- /dev/null +++ b/staging/adblock/plugin/plugin.cpp @@ -0,0 +1,54 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://library.iserlohn-fortress.net/aqua/smolbote.git + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "plugin.h" +#include "../filterlist.h" +#include <QTextStream> + +using namespace AdblockPlus; + +Filter* AdblockPlusFilterPlugin::load(QIODevice* from) const +{ + if(!from->isOpen()) + return nullptr; + + + QTextStream stream(from); + + if(stream.readLine().trimmed() != QLatin1String("[Adblock Plus 2.0]")) { + return nullptr; + } + + auto *list = new FilterList; + QString line; + int total, comments, rules, unsupported, failed; + + while(stream.readLineInto(&line)) { + if(!line.isEmpty()) { + ++total; + + switch(list->parse(line)) + { + case FilterList::Comment: + ++comments; + break; + case FilterList::Rule: + ++rules; + break; + case FilterList::Unsupported: + ++unsupported; + break; + case FilterList::Failed: + break; + } + } + } + + return list; +} + diff --git a/staging/adblock/plugin.h b/staging/adblock/plugin/plugin.h index 1ca51aa..1ca51aa 100644 --- a/staging/adblock/plugin.h +++ b/staging/adblock/plugin/plugin.h diff --git a/staging/adblock/test/parser.cpp b/staging/adblock/test/parser.cpp deleted file mode 100644 index 2d2e59e..0000000 --- a/staging/adblock/test/parser.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "filterlist.h" -#include <QFile> -#include <QTextStream> - -int main(int argc, char **argv) -{ - if(argc < 2) { - qDebug("usage: %s list1.txt ...", argv[0]); - return 77; - } - - for(int i = 1; i < argc; ++i) { - QFile f(argv[i]); - if(!f.open(QIODevice::ReadOnly | QIODevice::Text)) { - qDebug("could not open %s", argv[i]); - return -1; - } - - AdblockPlus::FilterList list; - QTextStream stream(&f); - const auto result = list.parse(stream); - qDebug("[%s]: %s", argv[i], (result.state == AdblockPlus::FilterList::Ok) ? "okay" : "failed"); - qDebug(" total: %i\t\tmodified: %s", result.lines_total, qUtf8Printable(list.modified().toString())); - qDebug("comments: %i\t\t expires: %s", result.lines_comments, qUtf8Printable(list.expiresOn().toString())); - qDebug(" ignored: %i\t\t valid: %s", result.lines_ignored, list.isUpToDate() ? "yes" : "no"); - qDebug(" parsed: %i", result.lines_parsed); - qDebug(" failed: %i", result.lines_failed); - qDebug("-------- --------"); - - f.close(); - } - return 0; -} |