more performance optimizations + cleanup
This commit is contained in:
15 files changed
+396
-469
No files matched your search
@@ -579,6 +579,15 @@ private:
|
||||
|
||||
} // namespace
|
||||
|
||||
QVector<BlockItem> parse(const QDomElement &root, const ParseParams ¶ms)
|
||||
{
|
||||
if (root.isNull())
|
||||
return {};
|
||||
|
||||
Builder builder(params);
|
||||
return builder.build(root);
|
||||
}
|
||||
|
||||
QVector<BlockItem> parse(const QString &html, const ParseParams ¶ms)
|
||||
{
|
||||
if (html.trimmed().isEmpty())
|
||||
@@ -606,8 +615,124 @@ QVector<BlockItem> parse(const QString &html, const ParseParams ¶ms)
|
||||
return {b};
|
||||
}
|
||||
|
||||
Builder builder(params);
|
||||
return builder.build(doc.documentElement());
|
||||
return parse(doc.documentElement(), params);
|
||||
}
|
||||
|
||||
QVector<ContentChunk> partitionBlocks(const QVector<BlockItem> &blocks,
|
||||
const QString §ionHref,
|
||||
int targetChars)
|
||||
{
|
||||
if (blocks.isEmpty())
|
||||
return {};
|
||||
|
||||
QVector<ContentChunk> chunks;
|
||||
ContentChunk current;
|
||||
current.sectionHref = sectionHref;
|
||||
|
||||
QVector<BoxStyle> openContainers;
|
||||
|
||||
auto flushCurrent = [&]() {
|
||||
if (current.isEmpty())
|
||||
return;
|
||||
|
||||
// Close outer containers at chunk boundary
|
||||
for (int j = openContainers.size() - 1; j >= 0; --j) {
|
||||
BlockItem endItem;
|
||||
endItem.kind = BlockItem::Kind::ContainerEnd;
|
||||
endItem.box = openContainers[j];
|
||||
endItem.box.borderBottom = 0;
|
||||
endItem.box.paddingBottom = 0;
|
||||
endItem.box.marginBottom = 0;
|
||||
current.blocks.append(endItem);
|
||||
}
|
||||
|
||||
chunks.append(std::move(current));
|
||||
current = ContentChunk();
|
||||
current.sectionHref = sectionHref;
|
||||
current.unnestedContainers = openContainers.size();
|
||||
|
||||
// Reopen outer containers at start of next chunk
|
||||
for (int j = 0; j < openContainers.size(); ++j) {
|
||||
BlockItem startItem;
|
||||
startItem.kind = BlockItem::Kind::ContainerStart;
|
||||
startItem.box = openContainers[j];
|
||||
startItem.box.borderTop = 0;
|
||||
startItem.box.paddingTop = 0;
|
||||
startItem.box.marginTop = 0;
|
||||
current.blocks.append(startItem);
|
||||
}
|
||||
};
|
||||
|
||||
for (int i = 0; i < blocks.size(); ++i) {
|
||||
const auto &b = blocks[i];
|
||||
if (b.kind == BlockItem::Kind::ContainerStart) {
|
||||
openContainers.append(b.box);
|
||||
current.blocks.append(b);
|
||||
} else if (b.kind == BlockItem::Kind::ContainerEnd) {
|
||||
if (!openContainers.isEmpty())
|
||||
openContainers.removeLast();
|
||||
current.blocks.append(b);
|
||||
} else if (b.kind == BlockItem::Kind::Rule) {
|
||||
current.blocks.append(b);
|
||||
current.charCount += 1;
|
||||
if (current.charCount >= targetChars && i + 1 < blocks.size()) {
|
||||
flushCurrent();
|
||||
}
|
||||
} else if (b.kind == BlockItem::Kind::Paragraph) {
|
||||
int pChars = 0;
|
||||
int pWords = 0;
|
||||
for (const auto &inl : b.inlines) {
|
||||
if (inl.kind == InlineItem::Kind::Text) {
|
||||
pChars += inl.text.size();
|
||||
const QStringList words = inl.text.split(QLatin1Char(' '), Qt::SkipEmptyParts);
|
||||
pWords += words.size();
|
||||
} else if (inl.kind == InlineItem::Kind::Image) {
|
||||
pChars += 1;
|
||||
pWords += 1;
|
||||
}
|
||||
}
|
||||
|
||||
current.blocks.append(b);
|
||||
current.charCount += pChars;
|
||||
current.wordCount += pWords;
|
||||
|
||||
// Consume immediately following ContainerEnd that closes this paragraph's leaf box
|
||||
if (i + 1 < blocks.size() && blocks[i + 1].kind == BlockItem::Kind::ContainerEnd) {
|
||||
if (!openContainers.isEmpty())
|
||||
openContainers.removeLast();
|
||||
current.blocks.append(blocks[i + 1]);
|
||||
++i;
|
||||
}
|
||||
|
||||
if (current.charCount >= targetChars && i + 1 < blocks.size()) {
|
||||
flushCurrent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!current.isEmpty()) {
|
||||
chunks.append(std::move(current));
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
QVector<ContentChunk> parseChunks(const QDomElement &root,
|
||||
const QString §ionHref,
|
||||
int targetChars,
|
||||
const ParseParams ¶ms)
|
||||
{
|
||||
const auto blocks = parse(root, params);
|
||||
return partitionBlocks(blocks, sectionHref, targetChars);
|
||||
}
|
||||
|
||||
QVector<ContentChunk> parseChunks(const QString &html,
|
||||
const QString §ionHref,
|
||||
int targetChars,
|
||||
const ParseParams ¶ms)
|
||||
{
|
||||
const auto blocks = parse(html, params);
|
||||
return partitionBlocks(blocks, sectionHref, targetChars);
|
||||
}
|
||||
|
||||
} // namespace HtmlBlockParser
|
||||
|
||||
Reference in new issue
Block a user