mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2025-12-18 15:17:42 +03:00
Handle nested navpoint elements in nxc TOC
This commit is contained in:
parent
4186c7da9e
commit
7704772ebe
@ -3,7 +3,6 @@
|
|||||||
#include <HardwareSerial.h>
|
#include <HardwareSerial.h>
|
||||||
#include <SD.h>
|
#include <SD.h>
|
||||||
#include <ZipFile.h>
|
#include <ZipFile.h>
|
||||||
#include <tinyxml2.h>
|
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
@ -162,14 +161,14 @@ bool Epub::parseContentOpf(ZipFile& zip, std::string& content_opf_file) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Epub::parseTocNcxFile(ZipFile& zip) {
|
bool Epub::parseTocNcxFile(const ZipFile& zip) {
|
||||||
// the ncx file should have been specified in the content.opf file
|
// the ncx file should have been specified in the content.opf file
|
||||||
if (tocNcxItem.empty()) {
|
if (tocNcxItem.empty()) {
|
||||||
Serial.println("No ncx file specified");
|
Serial.println("No ncx file specified");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto ncxData = zip.readTextFileToMemory(tocNcxItem.c_str());
|
const auto ncxData = zip.readTextFileToMemory(tocNcxItem.c_str());
|
||||||
if (!ncxData) {
|
if (!ncxData) {
|
||||||
Serial.printf("Could not find %s\n", tocNcxItem.c_str());
|
Serial.printf("Could not find %s\n", tocNcxItem.c_str());
|
||||||
return false;
|
return false;
|
||||||
@ -177,7 +176,7 @@ bool Epub::parseTocNcxFile(ZipFile& zip) {
|
|||||||
|
|
||||||
// Parse the Toc contents
|
// Parse the Toc contents
|
||||||
tinyxml2::XMLDocument doc;
|
tinyxml2::XMLDocument doc;
|
||||||
auto result = doc.Parse(ncxData);
|
const auto result = doc.Parse(ncxData);
|
||||||
free(ncxData);
|
free(ncxData);
|
||||||
|
|
||||||
if (result != tinyxml2::XML_SUCCESS) {
|
if (result != tinyxml2::XML_SUCCESS) {
|
||||||
@ -185,27 +184,30 @@ bool Epub::parseTocNcxFile(ZipFile& zip) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto ncx = doc.FirstChildElement("ncx");
|
const auto ncx = doc.FirstChildElement("ncx");
|
||||||
if (!ncx) {
|
if (!ncx) {
|
||||||
Serial.println("Could not find first child ncx in toc");
|
Serial.println("Could not find first child ncx in toc");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto navMap = ncx->FirstChildElement("navMap");
|
const auto navMap = ncx->FirstChildElement("navMap");
|
||||||
if (!navMap) {
|
if (!navMap) {
|
||||||
Serial.println("Could not find navMap child in ncx");
|
Serial.println("Could not find navMap child in ncx");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto navPoint = navMap->FirstChildElement("navPoint");
|
recursivelyParseNavMap(navMap->FirstChildElement("navPoint"));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Epub::recursivelyParseNavMap(tinyxml2::XMLElement* element) {
|
||||||
// Fills toc map
|
// Fills toc map
|
||||||
while (navPoint) {
|
while (element) {
|
||||||
std::string navTitle = navPoint->FirstChildElement("navLabel")->FirstChildElement("text")->FirstChild()->Value();
|
std::string navTitle = element->FirstChildElement("navLabel")->FirstChildElement("text")->FirstChild()->Value();
|
||||||
auto content = navPoint->FirstChildElement("content");
|
const auto content = element->FirstChildElement("content");
|
||||||
std::string href = contentBasePath + content->Attribute("src");
|
std::string href = contentBasePath + content->Attribute("src");
|
||||||
// split the href on the # to get the href and the anchor
|
// split the href on the # to get the href and the anchor
|
||||||
size_t pos = href.find('#');
|
const size_t pos = href.find('#');
|
||||||
std::string anchor;
|
std::string anchor;
|
||||||
|
|
||||||
if (pos != std::string::npos) {
|
if (pos != std::string::npos) {
|
||||||
@ -214,10 +216,13 @@ bool Epub::parseTocNcxFile(ZipFile& zip) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
toc.emplace_back(navTitle, href, anchor, 0);
|
toc.emplace_back(navTitle, href, anchor, 0);
|
||||||
navPoint = navPoint->NextSiblingElement("navPoint");
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
tinyxml2::XMLElement* nestedNavPoint = element->FirstChildElement("navPoint");
|
||||||
|
if (nestedNavPoint) {
|
||||||
|
recursivelyParseNavMap(nestedNavPoint);
|
||||||
|
}
|
||||||
|
element = element->NextSiblingElement("navPoint");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// load in the meta data for the epub file
|
// load in the meta data for the epub file
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <HardwareSerial.h>
|
#include <HardwareSerial.h>
|
||||||
|
#include <tinyxml2.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
@ -38,36 +39,29 @@ class Epub {
|
|||||||
// find the path for the content.opf file
|
// find the path for the content.opf file
|
||||||
static bool findContentOpfFile(const ZipFile& zip, std::string& contentOpfFile);
|
static bool findContentOpfFile(const ZipFile& zip, std::string& contentOpfFile);
|
||||||
bool parseContentOpf(ZipFile& zip, std::string& content_opf_file);
|
bool parseContentOpf(ZipFile& zip, std::string& content_opf_file);
|
||||||
bool parseTocNcxFile(ZipFile& zip);
|
bool parseTocNcxFile(const ZipFile& zip);
|
||||||
|
void recursivelyParseNavMap(tinyxml2::XMLElement* element);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Epub(std::string filepath, const std::string& cacheDir) : filepath(std::move(filepath)) {
|
explicit Epub(std::string filepath, const std::string& cacheDir) : filepath(std::move(filepath)) {
|
||||||
// create a cache key based on the filepath
|
// create a cache key based on the filepath
|
||||||
|
|
||||||
cachePath = cacheDir + "/epub_" + std::to_string(std::hash<std::string>{}(this->filepath));
|
cachePath = cacheDir + "/epub_" + std::to_string(std::hash<std::string>{}(this->filepath));
|
||||||
}
|
}
|
||||||
~Epub() = default;
|
~Epub() = default;
|
||||||
std::string& getBasePath() { return contentBasePath; }
|
std::string& getBasePath() { return contentBasePath; }
|
||||||
bool load();
|
bool load();
|
||||||
|
|
||||||
void clearCache() const;
|
void clearCache() const;
|
||||||
|
|
||||||
void setupCacheDir() const;
|
void setupCacheDir() const;
|
||||||
|
|
||||||
const std::string& getCachePath() const;
|
const std::string& getCachePath() const;
|
||||||
const std::string& getPath() const;
|
const std::string& getPath() const;
|
||||||
const std::string& getTitle() const;
|
const std::string& getTitle() const;
|
||||||
const std::string& getCoverImageItem() const;
|
const std::string& getCoverImageItem() const;
|
||||||
uint8_t* getItemContents(const std::string& itemHref, size_t* size = nullptr) const;
|
uint8_t* getItemContents(const std::string& itemHref, size_t* size = nullptr) const;
|
||||||
char* getTextItemContents(const std::string& itemHref, size_t* size = nullptr) const;
|
char* getTextItemContents(const std::string& itemHref, size_t* size = nullptr) const;
|
||||||
|
|
||||||
std::string& getSpineItem(int spineIndex);
|
std::string& getSpineItem(int spineIndex);
|
||||||
int getSpineItemsCount() const;
|
int getSpineItemsCount() const;
|
||||||
|
|
||||||
EpubTocEntry& getTocItem(int tocTndex);
|
EpubTocEntry& getTocItem(int tocTndex);
|
||||||
int getTocItemsCount() const;
|
int getTocItemsCount() const;
|
||||||
// work out the section index for a toc index
|
|
||||||
int getSpineIndexForTocIndex(int tocIndex) const;
|
int getSpineIndexForTocIndex(int tocIndex) const;
|
||||||
|
|
||||||
int getTocIndexForSpineIndex(int spineIndex) const;
|
int getTocIndexForSpineIndex(int spineIndex) const;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user