more performance optimizations + cleanup
This commit is contained in:
15 files changed
+396
-469
No files matched your search
@@ -128,7 +128,7 @@ void EpubReader::appendSpineChunks(const QString &xhtml,
|
||||
const QString safeXml = HtmlEntities::toXmlSafe(xhtml);
|
||||
QDomDocument doc;
|
||||
if (!doc.setContent(safeXml)) {
|
||||
m_chunks.append(xhtml);
|
||||
m_chunks.append(HtmlBlockParser::parseChunks(xhtml, xhtmlHref));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -209,179 +209,8 @@ void EpubReader::appendSpineChunks(const QString &xhtml,
|
||||
QDomElement root = doc.documentElement();
|
||||
processElement(root);
|
||||
|
||||
// 5. Chunk <body> directly from modified DOM
|
||||
QDomNodeList bodies = doc.elementsByTagName(QStringLiteral("body"));
|
||||
if (bodies.isEmpty()) {
|
||||
QString serialized;
|
||||
QTextStream ts(&serialized);
|
||||
doc.save(ts, 0);
|
||||
m_chunks.append(serialized);
|
||||
return;
|
||||
}
|
||||
|
||||
QDomElement body = bodies.at(0).toElement();
|
||||
|
||||
auto hasBoxDecoration = [](const QDomElement &el) -> bool {
|
||||
const QString style = el.attribute(QStringLiteral("style")).toLower();
|
||||
if (style.isEmpty())
|
||||
return false;
|
||||
const auto decls = Css::parseDeclarations(style);
|
||||
for (const auto &d : decls) {
|
||||
const QString p = d.property.toLower();
|
||||
if (p.contains(QLatin1String("background")) && d.value != QLatin1String("none") && d.value != QLatin1String("transparent"))
|
||||
return true;
|
||||
if (p.contains(QLatin1String("border")) && !d.value.startsWith(QLatin1String("none")) && !d.value.startsWith(QLatin1String("0")))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
static const QStringList kInheritableProperties = {
|
||||
QStringLiteral("color"),
|
||||
QStringLiteral("font-family"),
|
||||
QStringLiteral("font-size"),
|
||||
QStringLiteral("font-style"),
|
||||
QStringLiteral("font-weight"),
|
||||
QStringLiteral("line-height"),
|
||||
QStringLiteral("text-align"),
|
||||
QStringLiteral("letter-spacing"),
|
||||
QStringLiteral("text-transform")
|
||||
};
|
||||
|
||||
auto propagateInheritedStyles = [](const QDomElement &parentEl, QDomElement &childEl) {
|
||||
const auto parentDecls = Css::parseDeclarations(parentEl.attribute(QStringLiteral("style")));
|
||||
if (parentDecls.isEmpty())
|
||||
return;
|
||||
auto childDecls = Css::parseDeclarations(childEl.attribute(QStringLiteral("style")));
|
||||
QSet<QString> childProps;
|
||||
for (const auto &cd : childDecls) {
|
||||
childProps.insert(cd.property.toLower());
|
||||
}
|
||||
bool modified = false;
|
||||
for (const auto &pd : parentDecls) {
|
||||
const QString prop = pd.property.toLower();
|
||||
if (kInheritableProperties.contains(prop) && !childProps.contains(prop)) {
|
||||
childDecls.append(pd);
|
||||
childProps.insert(prop);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (modified) {
|
||||
childEl.setAttribute(QStringLiteral("style"), Css::serialize(childDecls));
|
||||
}
|
||||
};
|
||||
|
||||
static const QSet<QString> kBlockTags = {
|
||||
QStringLiteral("p"), QStringLiteral("div"),
|
||||
QStringLiteral("h1"), QStringLiteral("h2"),
|
||||
QStringLiteral("h3"), QStringLiteral("h4"),
|
||||
QStringLiteral("h5"), QStringLiteral("h6"),
|
||||
QStringLiteral("blockquote"), QStringLiteral("pre"),
|
||||
QStringLiteral("ul"), QStringLiteral("ol"),
|
||||
QStringLiteral("li"), QStringLiteral("table"),
|
||||
QStringLiteral("hr"), QStringLiteral("figure"),
|
||||
QStringLiteral("section"), QStringLiteral("article"),
|
||||
QStringLiteral("header"), QStringLiteral("footer"),
|
||||
QStringLiteral("aside")
|
||||
};
|
||||
|
||||
static const QSet<QString> kStructuralTags = {
|
||||
QStringLiteral("div"),
|
||||
QStringLiteral("section"),
|
||||
QStringLiteral("article"),
|
||||
QStringLiteral("main"),
|
||||
QStringLiteral("header"),
|
||||
QStringLiteral("footer"),
|
||||
QStringLiteral("aside")
|
||||
};
|
||||
|
||||
std::function<bool(const QDomElement &)> hasBlockChild = [&](const QDomElement &el) {
|
||||
for (QDomNode n = el.firstChild(); !n.isNull(); n = n.nextSibling()) {
|
||||
if (n.isElement()) {
|
||||
const QString childTag = n.toElement().tagName().toLower();
|
||||
if (kBlockTags.contains(childTag)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
QVector<QDomNode> blockNodes;
|
||||
std::function<void(const QDomElement &)> collectBlocks = [&](const QDomElement &parent) {
|
||||
QDomDocument parentDoc = parent.ownerDocument();
|
||||
QDomElement pendingInlinePara;
|
||||
|
||||
auto flushPendingInline = [&]() {
|
||||
if (!pendingInlinePara.isNull()) {
|
||||
if (!pendingInlinePara.text().trimmed().isEmpty() || pendingInlinePara.elementsByTagName(QStringLiteral("img")).size() > 0) {
|
||||
blockNodes.append(pendingInlinePara);
|
||||
}
|
||||
pendingInlinePara = QDomElement();
|
||||
}
|
||||
};
|
||||
|
||||
for (QDomNode child = parent.firstChild(); !child.isNull(); child = child.nextSibling()) {
|
||||
if (!child.isElement()) {
|
||||
if (child.isText() && !child.toText().data().trimmed().isEmpty()) {
|
||||
if (pendingInlinePara.isNull()) {
|
||||
pendingInlinePara = parentDoc.createElement(QStringLiteral("p"));
|
||||
}
|
||||
pendingInlinePara.appendChild(child.cloneNode(true));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
QDomElement childEl = child.toElement();
|
||||
const QString tag = childEl.tagName().toLower();
|
||||
|
||||
if (!kBlockTags.contains(tag)) {
|
||||
// Inline element (e.g. <span>, <em>, <a>, <img>) directly in container
|
||||
if (pendingInlinePara.isNull()) {
|
||||
pendingInlinePara = parentDoc.createElement(QStringLiteral("p"));
|
||||
}
|
||||
pendingInlinePara.appendChild(childEl.cloneNode(true));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Child is a block element: flush any preceding inline nodes into their own block
|
||||
flushPendingInline();
|
||||
|
||||
// Unwrap only structural containers that have block-level children and no box decoration
|
||||
if (kStructuralTags.contains(tag) && !hasBoxDecoration(childEl) && hasBlockChild(childEl)) {
|
||||
for (QDomNode n = childEl.firstChild(); !n.isNull(); n = n.nextSibling()) {
|
||||
if (n.isElement()) {
|
||||
QDomElement subEl = n.toElement();
|
||||
propagateInheritedStyles(childEl, subEl);
|
||||
}
|
||||
}
|
||||
collectBlocks(childEl);
|
||||
} else {
|
||||
blockNodes.append(childEl);
|
||||
}
|
||||
}
|
||||
|
||||
flushPendingInline();
|
||||
};
|
||||
collectBlocks(body);
|
||||
|
||||
QString currentChunk;
|
||||
static constexpr int kChunkTargetSize = 512;
|
||||
|
||||
for (const QDomNode &node : blockNodes) {
|
||||
QString serialized;
|
||||
QTextStream ts(&serialized);
|
||||
node.save(ts, 0);
|
||||
|
||||
if (!currentChunk.isEmpty() && (currentChunk.size() + serialized.size() > kChunkTargetSize)) {
|
||||
m_chunks.append(currentChunk);
|
||||
currentChunk.clear();
|
||||
}
|
||||
currentChunk += serialized;
|
||||
}
|
||||
|
||||
if (!currentChunk.isEmpty())
|
||||
m_chunks.append(currentChunk);
|
||||
// 5. Build ContentChunks directly from cascaded DOM
|
||||
m_chunks.append(HtmlBlockParser::parseChunks(root, xhtmlHref));
|
||||
}
|
||||
|
||||
bool EpubReader::parseOpen(const QString &filePath, IByteSource &source, bool metadataOnly)
|
||||
@@ -430,7 +259,12 @@ bool EpubReader::parseOpen(const QString &filePath, IByteSource &source, bool me
|
||||
}
|
||||
}
|
||||
if (m_chunks.isEmpty()) {
|
||||
m_chunks.append(QStringLiteral("<p></p>"));
|
||||
ContentChunk emptyChunk;
|
||||
emptyChunk.sectionHref = m_filePath;
|
||||
BlockItem p;
|
||||
p.kind = BlockItem::Kind::Paragraph;
|
||||
emptyChunk.blocks.append(p);
|
||||
m_chunks.append(emptyChunk);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ public:
|
||||
QString coverImageUrl() const override;
|
||||
QByteArray contentHash() const override { return m_contentHash; }
|
||||
|
||||
QStringList chunks() const override { return m_chunks; }
|
||||
QVector<ContentChunk> chunks() const override { return m_chunks; }
|
||||
|
||||
private:
|
||||
struct ManifestItem {
|
||||
@@ -61,6 +61,6 @@ private:
|
||||
|
||||
QHash<QString, ManifestItem> m_manifest; // id -> item
|
||||
QStringList m_spineHrefs; // ordered spine relative hrefs
|
||||
QStringList m_chunks;
|
||||
QVector<ContentChunk> m_chunks;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user