Merge branch 'fix_epub_author_parsing' into render-rewrite

This commit is contained in:
Agel committed 2026-10-29 15:33:04 +01:00
commit 18ea76bd89
6 files changed
+1005

No files matched your search

+227
View File
@@ -0,0 +1,227 @@
#include "AuthorAttribution.h"
#include <QHash>
#include <QStringList>
QString AuthorAttribution::mapRole(const std::optional<QString> &roleCode)
{
if (!roleCode.has_value()) {
return QStringLiteral("Author");
}
const QString trimmed = roleCode->trimmed();
if (trimmed.isEmpty() || trimmed.compare(QLatin1String("aut"), Qt::CaseInsensitive) == 0) {
return QStringLiteral("Author");
}
if (trimmed.compare(QLatin1String("edt"), Qt::CaseInsensitive) == 0) {
return QStringLiteral("Editor");
}
if (trimmed.compare(QLatin1String("trl"), Qt::CaseInsensitive) == 0) {
return QStringLiteral("Translator");
}
if (trimmed.compare(QLatin1String("ill"), Qt::CaseInsensitive) == 0) {
return QStringLiteral("Illustrator");
}
// Unrecognized or custom roles are capitalized as-is
QString capitalized = trimmed;
if (!capitalized.isEmpty() && capitalized.at(0).isLower()) {
capitalized[0] = capitalized.at(0).toUpper();
}
return capitalized;
}
QString AuthorAttribution::formatCreators(const QList<CreatorItem> &creators)
{
QList<QString> roleOrder;
QHash<QString, QStringList> roleToNames;
for (const auto &creator : creators) {
const QString trimmedName = creator.name.trimmed();
if (trimmedName.isEmpty()) {
continue;
}
const QString role = mapRole(creator.roleCode);
if (!roleToNames.contains(role)) {
roleOrder.append(role);
}
QStringList &names = roleToNames[role];
if (!names.contains(trimmedName)) {
names.append(trimmedName);
}
}
if (roleOrder.isEmpty()) {
return {};
}
QStringList formattedGroups;
formattedGroups.reserve(roleOrder.size());
for (const auto &role : roleOrder) {
formattedGroups.append(QStringLiteral("%1: %2").arg(role, roleToNames[role].join(QStringLiteral(", "))));
}
return formattedGroups.join(QStringLiteral(", "));
}
QString AuthorAttribution::formatStructuredAuthors(const QList<StructuredAuthor> &authors)
{
QStringList authorList;
for (const auto &author : authors) {
QStringList nameParts;
if (author.firstName.has_value() && !author.firstName->trimmed().isEmpty()) {
nameParts.append(author.firstName->trimmed());
}
if (author.middleName.has_value() && !author.middleName->trimmed().isEmpty()) {
nameParts.append(author.middleName->trimmed());
}
if (author.lastName.has_value() && !author.lastName->trimmed().isEmpty()) {
nameParts.append(author.lastName->trimmed());
}
QString authorName;
if (!nameParts.isEmpty()) {
authorName = nameParts.join(QLatin1Char(' '));
} else if (author.nickname.has_value() && !author.nickname->trimmed().isEmpty()) {
authorName = author.nickname->trimmed();
}
if (!authorName.isEmpty() && !authorList.contains(authorName)) {
authorList.append(authorName);
}
}
return authorList.join(QStringLiteral(", "));
}
BookMetadata AuthorAttribution::loadMetadata(const BookEntity &book,
const QList<CreatorItem> &creators,
const QList<StructuredAuthor> &authors)
{
BookMetadata metadata;
metadata.book = book;
metadata.status = MetadataStatus::Unparsed;
metadata.rawDcCreators = creators;
metadata.rawStructuredAuthors = authors;
metadata.authorAttribution = std::nullopt;
return metadata;
}
bool AuthorAttribution::parseMetadataWithDcCreators(BookMetadata &metadata)
{
if (metadata.status != MetadataStatus::Unparsed) {
return false;
}
if (metadata.rawDcCreators.isEmpty()) {
return false;
}
metadata.status = MetadataStatus::Parsed;
metadata.authorAttribution = formatCreators(metadata.rawDcCreators);
return true;
}
bool AuthorAttribution::parseMetadataFallbackToStructuredAuthors(BookMetadata &metadata)
{
if (metadata.status != MetadataStatus::Unparsed) {
return false;
}
if (!metadata.rawDcCreators.isEmpty()) {
return false;
}
metadata.status = MetadataStatus::Parsed;
metadata.authorAttribution = formatStructuredAuthors(metadata.rawStructuredAuthors);
return true;
}
bool AuthorAttribution::parseMetadata(BookMetadata &metadata)
{
if (metadata.status != MetadataStatus::Unparsed) {
return false;
}
if (!metadata.rawDcCreators.isEmpty()) {
return parseMetadataWithDcCreators(metadata);
}
return parseMetadataFallbackToStructuredAuthors(metadata);
}
BookMetadataSurface::BookMetadataSurface(const BookMetadata &metadata, bool engineActive)
: m_metadata(metadata), m_engineActive(engineActive)
{
}
bool BookMetadataSurface::isAccessible() const
{
return m_engineActive;
}
QString BookMetadataSurface::filePath() const
{
return m_metadata.book.filePath;
}
std::optional<QString> BookMetadataSurface::authorAttribution() const
{
if (m_metadata.status == MetadataStatus::Parsed) {
return m_metadata.authorAttribution;
}
return std::nullopt;
}
bool BookMetadataSurface::canProvideLoadMetadata() const
{
return m_engineActive;
}
bool BookMetadataSurface::canProvideParseMetadata() const
{
return m_engineActive && m_metadata.status == MetadataStatus::Unparsed;
}
void BookMetadataSurface::loadMetadata(const QList<CreatorItem> &creators, const QList<StructuredAuthor> &authors)
{
if (!canProvideLoadMetadata()) {
return;
}
m_metadata = AuthorAttribution::loadMetadata(m_metadata.book, creators, authors);
}
bool BookMetadataSurface::parseMetadata()
{
if (!canProvideParseMetadata()) {
return false;
}
return AuthorAttribution::parseMetadata(m_metadata);
}
QString BookMetadataSurface::resolveAttribution(const QString &filePath)
{
if (!m_engineActive) {
return {};
}
m_metadata.book.filePath = filePath;
if (m_metadata.status == MetadataStatus::Unparsed) {
AuthorAttribution::parseMetadata(m_metadata);
}
return m_metadata.authorAttribution.value_or(QString());
}
const BookMetadata &BookMetadataSurface::metadata() const
{
return m_metadata;
}
void BookMetadataSurface::setMetadata(const BookMetadata &metadata)
{
m_metadata = metadata;
}
void BookMetadataSurface::setEngineActive(bool active)
{
m_engineActive = active;
}
+127
View File
@@ -0,0 +1,127 @@
#ifndef AUTHORATTRIBUTION_H
#define AUTHORATTRIBUTION_H
#include <QList>
#include <QString>
#include <optional>
struct StructuredAuthor {
std::optional<QString> firstName;
std::optional<QString> middleName;
std::optional<QString> lastName;
std::optional<QString> nickname;
bool operator==(const StructuredAuthor &other) const
{
return firstName == other.firstName &&
middleName == other.middleName &&
lastName == other.lastName &&
nickname == other.nickname;
}
bool operator!=(const StructuredAuthor &other) const
{
return !(*this == other);
}
};
struct CreatorItem {
std::optional<QString> roleCode;
QString name;
bool operator==(const CreatorItem &other) const
{
return roleCode == other.roleCode && name == other.name;
}
bool operator!=(const CreatorItem &other) const
{
return !(*this == other);
}
};
enum class MetadataStatus {
Unparsed,
Parsed
};
struct BookEntity {
QString filePath;
bool isActive = true;
bool operator==(const BookEntity &other) const
{
return filePath == other.filePath && isActive == other.isActive;
}
bool operator!=(const BookEntity &other) const
{
return !(*this == other);
}
};
struct BookMetadata {
BookEntity book;
MetadataStatus status = MetadataStatus::Unparsed;
QList<CreatorItem> rawDcCreators;
QList<StructuredAuthor> rawStructuredAuthors;
std::optional<QString> authorAttribution;
bool operator==(const BookMetadata &other) const
{
return book == other.book &&
status == other.status &&
rawDcCreators == other.rawDcCreators &&
rawStructuredAuthors == other.rawStructuredAuthors &&
authorAttribution == other.authorAttribution;
}
bool operator!=(const BookMetadata &other) const
{
return !(*this == other);
}
};
class AuthorAttributionService {
public:
virtual ~AuthorAttributionService() = default;
virtual QString resolveAttribution(const QString &filePath) = 0;
};
class AuthorAttribution {
public:
static QString mapRole(const std::optional<QString> &roleCode);
static QString formatCreators(const QList<CreatorItem> &creators);
static QString formatStructuredAuthors(const QList<StructuredAuthor> &authors);
static BookMetadata loadMetadata(const BookEntity &book,
const QList<CreatorItem> &creators,
const QList<StructuredAuthor> &authors);
static bool parseMetadataWithDcCreators(BookMetadata &metadata);
static bool parseMetadataFallbackToStructuredAuthors(BookMetadata &metadata);
static bool parseMetadata(BookMetadata &metadata);
};
class BookMetadataSurface : public AuthorAttributionService {
public:
explicit BookMetadataSurface(const BookMetadata &metadata, bool engineActive = true);
bool isAccessible() const;
QString filePath() const;
std::optional<QString> authorAttribution() const;
bool canProvideLoadMetadata() const;
bool canProvideParseMetadata() const;
void loadMetadata(const QList<CreatorItem> &creators, const QList<StructuredAuthor> &authors);
bool parseMetadata();
QString resolveAttribution(const QString &filePath) override;
const BookMetadata &metadata() const;
void setMetadata(const BookMetadata &metadata);
void setEngineActive(bool active);
private:
BookMetadata m_metadata;
bool m_engineActive;
};
#endif // AUTHORATTRIBUTION_H