Parse the author name from content.opf file (#165)

## Summary

* Parse the author name from content.opf file
  * Listed in the dc:creator tag within the metadata section
This commit is contained in:
Dave Allie 2025-12-30 21:15:44 +10:00 committed by GitHub
parent 3dd52f30fa
commit be1b5bad21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 28 additions and 2 deletions

View File

@ -72,8 +72,7 @@ bool Epub::parseContentOpf(BookMetadataCache::BookMetadata& bookMetadata) {
// Grab data from opfParser into epub // Grab data from opfParser into epub
bookMetadata.title = opfParser.title; bookMetadata.title = opfParser.title;
// TODO: Parse author bookMetadata.author = opfParser.author;
bookMetadata.author = "";
bookMetadata.coverItemHref = opfParser.coverItemHref; bookMetadata.coverItemHref = opfParser.coverItemHref;
if (!opfParser.tocNcxPath.empty()) { if (!opfParser.tocNcxPath.empty()) {
@ -253,6 +252,15 @@ const std::string& Epub::getTitle() const {
return bookMetadataCache->coreMetadata.title; return bookMetadataCache->coreMetadata.title;
} }
const std::string& Epub::getAuthor() const {
static std::string blank;
if (!bookMetadataCache || !bookMetadataCache->isLoaded()) {
return blank;
}
return bookMetadataCache->coreMetadata.author;
}
std::string Epub::getCoverBmpPath() const { return cachePath + "/cover.bmp"; } std::string Epub::getCoverBmpPath() const { return cachePath + "/cover.bmp"; }
bool Epub::generateCoverBmp() const { bool Epub::generateCoverBmp() const {

View File

@ -40,6 +40,7 @@ class Epub {
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& getAuthor() const;
std::string getCoverBmpPath() const; std::string getCoverBmpPath() const;
bool generateCoverBmp() const; bool generateCoverBmp() const;
uint8_t* readItemContentsToBytes(const std::string& itemHref, size_t* size = nullptr, uint8_t* readItemContentsToBytes(const std::string& itemHref, size_t* size = nullptr,

View File

@ -102,6 +102,11 @@ void XMLCALL ContentOpfParser::startElement(void* userData, const XML_Char* name
return; return;
} }
if (self->state == IN_METADATA && strcmp(name, "dc:creator") == 0) {
self->state = IN_BOOK_AUTHOR;
return;
}
if (self->state == IN_PACKAGE && (strcmp(name, "manifest") == 0 || strcmp(name, "opf:manifest") == 0)) { if (self->state == IN_PACKAGE && (strcmp(name, "manifest") == 0 || strcmp(name, "opf:manifest") == 0)) {
self->state = IN_MANIFEST; self->state = IN_MANIFEST;
if (!SdMan.openFileForWrite("COF", self->cachePath + itemCacheFile, self->tempItemStore)) { if (!SdMan.openFileForWrite("COF", self->cachePath + itemCacheFile, self->tempItemStore)) {
@ -209,6 +214,11 @@ void XMLCALL ContentOpfParser::characterData(void* userData, const XML_Char* s,
self->title.append(s, len); self->title.append(s, len);
return; return;
} }
if (self->state == IN_BOOK_AUTHOR) {
self->author.append(s, len);
return;
}
} }
void XMLCALL ContentOpfParser::endElement(void* userData, const XML_Char* name) { void XMLCALL ContentOpfParser::endElement(void* userData, const XML_Char* name) {
@ -232,6 +242,11 @@ void XMLCALL ContentOpfParser::endElement(void* userData, const XML_Char* name)
return; return;
} }
if (self->state == IN_BOOK_AUTHOR && strcmp(name, "dc:creator") == 0) {
self->state = IN_METADATA;
return;
}
if (self->state == IN_METADATA && (strcmp(name, "metadata") == 0 || strcmp(name, "opf:metadata") == 0)) { if (self->state == IN_METADATA && (strcmp(name, "metadata") == 0 || strcmp(name, "opf:metadata") == 0)) {
self->state = IN_PACKAGE; self->state = IN_PACKAGE;
return; return;

View File

@ -12,6 +12,7 @@ class ContentOpfParser final : public Print {
IN_PACKAGE, IN_PACKAGE,
IN_METADATA, IN_METADATA,
IN_BOOK_TITLE, IN_BOOK_TITLE,
IN_BOOK_AUTHOR,
IN_MANIFEST, IN_MANIFEST,
IN_SPINE, IN_SPINE,
}; };
@ -31,6 +32,7 @@ class ContentOpfParser final : public Print {
public: public:
std::string title; std::string title;
std::string author;
std::string tocNcxPath; std::string tocNcxPath;
std::string coverItemHref; std::string coverItemHref;