Refactor ContentOpfParser to simplify item handling and improve TOC parsing

This commit is contained in:
Arthur Tazhitdinov 2025-12-16 17:43:09 +03:00
parent 8d2f1de660
commit b540fbc6fd
3 changed files with 11 additions and 18 deletions

View File

@ -70,7 +70,7 @@ bool Epub::parseContentOpf(const std::string& contentOpfFilePath) {
// Grab data from opfParser into epub
title = opfParser.title;
if (!opfParser.coverItemId.empty() && opfParser.items.count(opfParser.coverItemId) > 0) {
coverImageItem = opfParser.items.at(opfParser.coverItemId).href;
coverImageItem = opfParser.items.at(opfParser.coverItemId);
}
if (!opfParser.tocNcxPath.empty()) {
@ -79,7 +79,7 @@ bool Epub::parseContentOpf(const std::string& contentOpfFilePath) {
for (auto& spineRef : opfParser.spineRefs) {
if (opfParser.items.count(spineRef)) {
spine.emplace_back(spineRef, opfParser.items.at(spineRef).href);
spine.emplace_back(spineRef, opfParser.items.at(spineRef));
}
}

View File

@ -110,23 +110,23 @@ void XMLCALL ContentOpfParser::startElement(void* userData, const XML_Char* name
if (self->state == IN_MANIFEST && (strcmp(name, "item") == 0 || strcmp(name, "opf:item") == 0)) {
std::string itemId;
ManifestItem item;
std::string href;
std::string mediaType;
for (int i = 0; atts[i]; i += 2) {
if (strcmp(atts[i], "id") == 0) {
itemId = atts[i + 1];
} else if (strcmp(atts[i], "href") == 0) {
item.href = self->baseContentPath + atts[i + 1];
href = self->baseContentPath + atts[i + 1];
} else if (strcmp(atts[i], "media-type") == 0) {
item.mediaType = atts[i + 1];
mediaType = atts[i + 1];
}
}
if (!itemId.empty()) {
self->items[itemId] = item;
if (item.mediaType == "application/x-dtbncx+xml" && self->tocNcxPath.empty()) {
self->tocNcxPath = item.href;
}
self->items[itemId] = href;
if (mediaType == "application/x-dtbncx+xml") {
self->tocNcxPath = href;
}
return;
}

View File

@ -2,8 +2,6 @@
#include <Print.h>
#include <map>
#include <string>
#include <vector>
#include "Epub.h"
#include "expat.h"
@ -28,15 +26,10 @@ class ContentOpfParser final : public Print {
static void endElement(void* userData, const XML_Char* name);
public:
struct ManifestItem {
std::string href;
std::string mediaType;
};
std::string title;
std::string tocNcxPath;
std::string coverItemId;
std::map<std::string, ManifestItem> items;
std::map<std::string, std::string> items;
std::vector<std::string> spineRefs;
explicit ContentOpfParser(const std::string& baseContentPath, const size_t xmlSize)