aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/meson.build4
-rw-r--r--staging/adblock/filterlist.cpp48
-rw-r--r--staging/adblock/filterlist.h27
-rw-r--r--staging/adblock/meson.build31
-rw-r--r--staging/adblock/plugin.cpp29
-rw-r--r--staging/adblock/plugin/AdblockPlusPlugin.json (renamed from staging/adblock/AdblockPlusPlugin.json)0
-rw-r--r--staging/adblock/plugin/plugin.cpp54
-rw-r--r--staging/adblock/plugin/plugin.h (renamed from staging/adblock/plugin.h)0
-rw-r--r--staging/filterlist/meson.build9
-rw-r--r--staging/filterlist/test/parser.cpp (renamed from staging/adblock/test/parser.cpp)6
10 files changed, 97 insertions, 111 deletions
diff --git a/include/meson.build b/include/meson.build
index c2bf758..70d995a 100644
--- a/include/meson.build
+++ b/include/meson.build
@@ -1,6 +1,2 @@
-dep_plugininterface = declare_dependency(
- include_directories: include_directories('.')
-)
-
plugininterface_include = include_directories('.')
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/filterlist/meson.build b/staging/filterlist/meson.build
index b8f2263..eb5b61f 100644
--- a/staging/filterlist/meson.build
+++ b/staging/filterlist/meson.build
@@ -13,3 +13,12 @@ executable('filterlist',
sources: [ 'test/main.cpp' ]
)
+#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/test/parser.cpp b/staging/filterlist/test/parser.cpp
index 2d2e59e..2b925e0 100644
--- a/staging/adblock/test/parser.cpp
+++ b/staging/filterlist/test/parser.cpp
@@ -1,6 +1,7 @@
#include "filterlist.h"
#include <QFile>
#include <QTextStream>
+#include "plugin/plugin.h"
int main(int argc, char **argv)
{
@@ -16,6 +17,9 @@ int main(int argc, char **argv)
return -1;
}
+ AdblockPlusFilterPlugin p;
+ auto *l = p.load(&f);
+/*
AdblockPlus::FilterList list;
QTextStream stream(&f);
const auto result = list.parse(stream);
@@ -26,7 +30,7 @@ int main(int argc, char **argv)
qDebug(" parsed: %i", result.lines_parsed);
qDebug(" failed: %i", result.lines_failed);
qDebug("-------- --------");
-
+*/
f.close();
}
return 0;