This commit is contained in:
jpirnay
2026-05-10 21:06:55 +02:00
parent 80eb889256
commit 5304086bcd
5 changed files with 37 additions and 21 deletions
+6 -2
View File
@@ -4,6 +4,7 @@
#include <Logging.h>
#include <cstring>
#include <utility>
namespace {
// Returns the length of href after trimming trailing slashes.
@@ -198,7 +199,7 @@ void XMLCALL OpdsParser::startElement(void* userData, const XML_Char* name, cons
self->currentEntry.acquisitionLinks.push_back(acquisition);
}
} else if (rel && type && strstr(rel, "opds-spec.org/image") != nullptr &&
strstr(rel, "thumbnail") == nullptr && strstr(type, "image/jpeg") != nullptr &&
strstr(rel, "thumbnail") == nullptr && strncmp(type, "image/", 6) == 0 &&
self->currentEntry.imageHref.empty()) {
self->currentEntry.imageHref = href;
} else if (type && strstr(type, "application/atom+xml") != nullptr) {
@@ -238,7 +239,10 @@ void XMLCALL OpdsParser::endElement(void* userData, const XML_Char* name) {
if (strcmp(name, "entry") == 0 || strstr(name, ":entry") != nullptr) {
if (!self->currentEntry.title.empty() && !self->currentEntry.href.empty()) {
if (self->onEntryParsed) self->onEntryParsed(self->currentEntry);
if (self->onEntryParsed) {
self->onEntryParsed(std::move(self->currentEntry));
self->currentEntry = OpdsEntry{};
}
}
self->inEntry = false;
} else if (self->inEntry) {
+10 -7
View File
@@ -43,14 +43,17 @@ using OpdsBook = OpdsEntry;
*
* Usage:
* OpdsParser parser;
* if (parser.parse(xmlData, xmlLength)) {
* for (const auto& entry : parser.getEntries()) {
* if (entry.type == OpdsEntryType::BOOK) {
* // Downloadable book
* } else {
* // Navigation link to another catalog
* }
* parser.onEntryParsed = [](OpdsEntry entry) {
* if (entry.type == OpdsEntryType::BOOK) {
* // Process downloadable book
* } else {
* // Process navigation link
* }
* };
*
* // Entries are emitted immediately as they are parsed from the stream.
* if (parser.parse(xmlData, xmlLength)) {
* // Parsing completed successfully
* }
*/
class OpdsParser final : public Print {
@@ -403,6 +403,8 @@ void OpdsBookBrowserActivity::fetchFeed(const std::string& path) {
return;
}
entryOffsets.clear();
std::string url = (path.find("http") == 0) ? path : UrlUtils::buildUrl(server.url, path);
LOG_DBG("OPDS", "Fetching: %s", url.c_str());
@@ -448,12 +450,14 @@ void OpdsBookBrowserActivity::fetchFeed(const std::string& path) {
const auto& prevUrl = parser.getPrevPageUrl();
if (!prevUrl.empty()) {
OpdsEntry prevEntry{OpdsEntryType::NAVIGATION, tr(STR_PREV_PAGE), "", prevUrl, ""};
std::string resolvedPrevUrl = UrlUtils::buildUrl(url, prevUrl);
OpdsEntry prevEntry{OpdsEntryType::NAVIGATION, tr(STR_PREV_PAGE), "", resolvedPrevUrl, ""};
entryOffsets.insert(entryOffsets.begin(), cacheFile.position());
writeEntryToCache(cacheFile, prevEntry);
}
if (!nextUrl.empty()) {
OpdsEntry nextEntry{OpdsEntryType::NAVIGATION, tr(STR_NEXT_PAGE), "", nextUrl, ""};
std::string resolvedNextUrl = UrlUtils::buildUrl(url, nextUrl);
OpdsEntry nextEntry{OpdsEntryType::NAVIGATION, tr(STR_NEXT_PAGE), "", resolvedNextUrl, ""};
entryOffsets.push_back(cacheFile.position());
writeEntryToCache(cacheFile, nextEntry);
}
-3
View File
@@ -509,9 +509,6 @@ void loop() {
while (buttonEventManager.consumeEvent(ev)) {
const uint8_t action = actionFor(ev);
if (action == BA::BTN_DEFAULT) {
if (ev.type == ButtonEventManager::PressType::Double) {
continue;
}
defaultEvents.push_back(ev);
continue;
}
+15 -7
View File
@@ -51,7 +51,9 @@ bool parseSingleBookEntry(OpdsEntry& entryOut, const char* href, const char* typ
</entry>
</feed>)";
std::vector<OpdsEntry> entries;
OpdsParser parser;
parser.onEntryParsed = [&](OpdsEntry e) { entries.push_back(std::move(e)); };
parser.write(reinterpret_cast<const uint8_t*>(xml.data()), xml.size());
parser.flush();
@@ -60,7 +62,6 @@ bool parseSingleBookEntry(OpdsEntry& entryOut, const char* href, const char* typ
testsFailed++;
return false;
}
const auto& entries = parser.getEntries();
if (entries.size() != 1) {
fprintf(stderr, " FAIL: %s:%d: entries.size() == %zu, expected 1\n", __FILE__, __LINE__, entries.size());
testsFailed++;
@@ -153,12 +154,13 @@ void testDistinctAcquisitionFormatsRemainSeparate() {
</entry>
</feed>)";
std::vector<OpdsEntry> entries;
OpdsParser parser;
parser.onEntryParsed = [&](OpdsEntry e) { entries.push_back(std::move(e)); };
parser.write(reinterpret_cast<const uint8_t*>(xml), strlen(xml));
parser.flush();
ASSERT_TRUE(!parser.error());
const auto& entries = parser.getEntries();
ASSERT_SIZE(entries.size(), 1);
const auto& links = entries.front().acquisitionLinks;
ASSERT_SIZE(links.size(), 4);
@@ -181,12 +183,14 @@ void testUnsupportedMimeType() {
</entry>
</feed>)";
std::vector<OpdsEntry> entries;
OpdsParser parser;
parser.onEntryParsed = [&](OpdsEntry e) { entries.push_back(std::move(e)); };
parser.write(reinterpret_cast<const uint8_t*>(xml), strlen(xml));
parser.flush();
ASSERT_TRUE(!parser.error());
ASSERT_SIZE(parser.getEntries().size(), 0);
ASSERT_SIZE(entries.size(), 0);
PASS();
}
@@ -220,12 +224,13 @@ void testEmptyHrefOrType() {
</entry>
</feed>)";
std::vector<OpdsEntry> entries;
OpdsParser parser;
parser.onEntryParsed = [&](OpdsEntry e) { entries.push_back(std::move(e)); };
parser.write(reinterpret_cast<const uint8_t*>(xml), strlen(xml));
parser.flush();
ASSERT_TRUE(!parser.error());
const auto& entries = parser.getEntries();
ASSERT_SIZE(entries.size(), 0);
PASS();
}
@@ -243,12 +248,13 @@ void testDuplicateAcquisitionLinks() {
</entry>
</feed>)";
std::vector<OpdsEntry> entries;
OpdsParser parser;
parser.onEntryParsed = [&](OpdsEntry e) { entries.push_back(std::move(e)); };
parser.write(reinterpret_cast<const uint8_t*>(xml), strlen(xml));
parser.flush();
ASSERT_TRUE(!parser.error());
const auto& entries = parser.getEntries();
ASSERT_SIZE(entries.size(), 1);
const auto& links = entries.front().acquisitionLinks;
ASSERT_SIZE(links.size(), 2);
@@ -272,12 +278,13 @@ void testIdenticalHrefAcquisitionLinksAreDeduplicated() {
</entry>
</feed>)";
std::vector<OpdsEntry> entries;
OpdsParser parser;
parser.onEntryParsed = [&](OpdsEntry e) { entries.push_back(std::move(e)); };
parser.write(reinterpret_cast<const uint8_t*>(xml), strlen(xml));
parser.flush();
ASSERT_TRUE(!parser.error());
const auto& entries = parser.getEntries();
ASSERT_SIZE(entries.size(), 1);
const auto& links = entries.front().acquisitionLinks;
ASSERT_SIZE(links.size(), 1);
@@ -299,12 +306,13 @@ void testSlashVariantHrefAcquisitionLinksAreDeduplicated() {
</entry>
</feed>)";
std::vector<OpdsEntry> entries;
OpdsParser parser;
parser.onEntryParsed = [&](OpdsEntry e) { entries.push_back(std::move(e)); };
parser.write(reinterpret_cast<const uint8_t*>(xml), strlen(xml));
parser.flush();
ASSERT_TRUE(!parser.error());
const auto& entries = parser.getEntries();
ASSERT_SIZE(entries.size(), 1);
const auto& links = entries.front().acquisitionLinks;
ASSERT_SIZE(links.size(), 1);