some refactor
This commit is contained in:
20 files changed
+1204
-948
No files matched your search
@@ -1,11 +1,12 @@
|
||||
#include "HtmlBlockParser.h"
|
||||
|
||||
#include <QDomDocument>
|
||||
#include <QRegularExpression>
|
||||
#include <QSet>
|
||||
#include <algorithm>
|
||||
|
||||
#include "CssParser.h"
|
||||
#include "DocumentChunkBuilder.h"
|
||||
#include "backend/formats/HtmlEntities.h"
|
||||
#include "backend/formats/ImageUrl.h"
|
||||
|
||||
namespace HtmlBlockParser {
|
||||
|
||||
@@ -31,7 +32,7 @@ QString localName(const QDomElement &e)
|
||||
|
||||
bool isBlockTag(const QString &tag)
|
||||
{
|
||||
static const QStringList kBlocks = {
|
||||
static const QSet<QString> kBlocks = {
|
||||
QStringLiteral("p"), QStringLiteral("div"),
|
||||
QStringLiteral("h1"), QStringLiteral("h2"),
|
||||
QStringLiteral("h3"), QStringLiteral("h4"),
|
||||
@@ -44,16 +45,75 @@ bool isBlockTag(const QString &tag)
|
||||
QStringLiteral("svg"), QStringLiteral("figcaption"),
|
||||
QStringLiteral("section"), QStringLiteral("article"),
|
||||
QStringLiteral("header"), QStringLiteral("footer"), QStringLiteral("aside"),
|
||||
QStringLiteral("body")
|
||||
QStringLiteral("body"),
|
||||
// FB2 semantic block tags
|
||||
QStringLiteral("title"), QStringLiteral("subtitle"),
|
||||
QStringLiteral("epigraph"), QStringLiteral("poem"),
|
||||
QStringLiteral("stanza"), QStringLiteral("v"),
|
||||
QStringLiteral("text-author"),QStringLiteral("date"),
|
||||
QStringLiteral("empty-line")
|
||||
};
|
||||
return kBlocks.contains(tag);
|
||||
}
|
||||
|
||||
void applyCssDeclarations(const QVector<Css::Declaration> &decls,
|
||||
StyleContext &ctx,
|
||||
BoxStyle &box,
|
||||
const ParseParams ¶ms,
|
||||
bool isBlock)
|
||||
void applyFourSides(qreal &top, qreal &right, qreal &bottom, qreal &left,
|
||||
const QString &val, const Css::LengthContext &lenCtx)
|
||||
{
|
||||
const QStringList tokens = Css::splitValue(val);
|
||||
QVector<qreal> v;
|
||||
v.reserve(tokens.size());
|
||||
for (const QString &tok : tokens) {
|
||||
if (auto len = Css::parseLength(tok, lenCtx))
|
||||
v.append(*len);
|
||||
}
|
||||
if (v.size() == 1) {
|
||||
top = right = bottom = left = v[0];
|
||||
} else if (v.size() == 2) {
|
||||
top = bottom = v[0];
|
||||
right = left = v[1];
|
||||
} else if (v.size() == 3) {
|
||||
top = v[0];
|
||||
right = left = v[1];
|
||||
bottom = v[2];
|
||||
} else if (v.size() >= 4) {
|
||||
top = v[0];
|
||||
right = v[1];
|
||||
bottom = v[2];
|
||||
left = v[3];
|
||||
}
|
||||
}
|
||||
|
||||
void applyBorderWidths(BoxStyle &box, const QString &val, const Css::LengthContext &lenCtx)
|
||||
{
|
||||
const QStringList tokens = Css::splitValue(val);
|
||||
QVector<qreal> widths;
|
||||
widths.reserve(tokens.size());
|
||||
for (const QString &tok : tokens) {
|
||||
if (auto w = Css::parseBorderSide(tok, lenCtx).width)
|
||||
widths.append(*w);
|
||||
}
|
||||
if (widths.size() == 1) {
|
||||
box.borderTop = box.borderRight = box.borderBottom = box.borderLeft = widths[0];
|
||||
} else if (widths.size() == 2) {
|
||||
box.borderTop = box.borderBottom = widths[0];
|
||||
box.borderRight = box.borderLeft = widths[1];
|
||||
} else if (widths.size() == 3) {
|
||||
box.borderTop = widths[0];
|
||||
box.borderRight = box.borderLeft = widths[1];
|
||||
box.borderBottom = widths[2];
|
||||
} else if (widths.size() >= 4) {
|
||||
box.borderTop = widths[0];
|
||||
box.borderRight = widths[1];
|
||||
box.borderBottom = widths[2];
|
||||
box.borderLeft = widths[3];
|
||||
}
|
||||
}
|
||||
|
||||
void applyDeclarations(const QVector<Css::Declaration> &decls,
|
||||
StyleContext &ctx,
|
||||
BoxStyle &box,
|
||||
const ParseParams ¶ms,
|
||||
bool isBlock)
|
||||
{
|
||||
Css::LengthContext lenCtx;
|
||||
lenCtx.em = ctx.text.fontSize;
|
||||
@@ -61,6 +121,12 @@ void applyCssDeclarations(const QVector<Css::Declaration> &decls,
|
||||
lenCtx.percentBase = params.contentWidth;
|
||||
lenCtx.absoluteScale = params.baseFontSize / 16.0;
|
||||
|
||||
auto applySide = [&](qreal &w, const QString &v) {
|
||||
auto b = Css::parseBorderSide(v, lenCtx);
|
||||
if (b.width) w = *b.width;
|
||||
if (b.color.isValid()) box.borderColor = b.color;
|
||||
};
|
||||
|
||||
for (const auto &d : decls) {
|
||||
const QString &prop = d.property;
|
||||
const QString &val = d.value;
|
||||
@@ -154,27 +220,7 @@ void applyCssDeclarations(const QVector<Css::Declaration> &decls,
|
||||
} else if (prop == QLatin1String("margin-right")) {
|
||||
auto m = Css::parseLength(val, lenCtx); if (m) box.marginRight = *m;
|
||||
} else if (prop == QLatin1String("margin")) {
|
||||
const QStringList tokens = Css::splitValue(val);
|
||||
QVector<qreal> vals;
|
||||
for (const QString &tok : tokens) {
|
||||
auto m = Css::parseLength(tok, lenCtx);
|
||||
if (m) vals.append(*m);
|
||||
}
|
||||
if (vals.size() == 1) {
|
||||
box.marginTop = box.marginRight = box.marginBottom = box.marginLeft = vals[0];
|
||||
} else if (vals.size() == 2) {
|
||||
box.marginTop = box.marginBottom = vals[0];
|
||||
box.marginRight = box.marginLeft = vals[1];
|
||||
} else if (vals.size() == 3) {
|
||||
box.marginTop = vals[0];
|
||||
box.marginRight = box.marginLeft = vals[1];
|
||||
box.marginBottom = vals[2];
|
||||
} else if (vals.size() >= 4) {
|
||||
box.marginTop = vals[0];
|
||||
box.marginRight = vals[1];
|
||||
box.marginBottom = vals[2];
|
||||
box.marginLeft = vals[3];
|
||||
}
|
||||
applyFourSides(box.marginTop, box.marginRight, box.marginBottom, box.marginLeft, val, lenCtx);
|
||||
} else if (prop == QLatin1String("padding-top")) {
|
||||
auto p = Css::parseLength(val, lenCtx); if (p) box.paddingTop = *p;
|
||||
} else if (prop == QLatin1String("padding-bottom")) {
|
||||
@@ -184,52 +230,19 @@ void applyCssDeclarations(const QVector<Css::Declaration> &decls,
|
||||
} else if (prop == QLatin1String("padding-right")) {
|
||||
auto p = Css::parseLength(val, lenCtx); if (p) box.paddingRight = *p;
|
||||
} else if (prop == QLatin1String("padding")) {
|
||||
const QStringList tokens = Css::splitValue(val);
|
||||
QVector<qreal> vals;
|
||||
for (const QString &tok : tokens) {
|
||||
auto p = Css::parseLength(tok, lenCtx);
|
||||
if (p) vals.append(*p);
|
||||
}
|
||||
if (vals.size() == 1) {
|
||||
box.paddingTop = box.paddingRight = box.paddingBottom = box.paddingLeft = vals[0];
|
||||
} else if (vals.size() == 2) {
|
||||
box.paddingTop = box.paddingBottom = vals[0];
|
||||
box.paddingRight = box.paddingLeft = vals[1];
|
||||
} else if (vals.size() == 3) {
|
||||
box.paddingTop = vals[0];
|
||||
box.paddingRight = box.paddingLeft = vals[1];
|
||||
box.paddingBottom = vals[2];
|
||||
} else if (vals.size() >= 4) {
|
||||
box.paddingTop = vals[0];
|
||||
box.paddingRight = vals[1];
|
||||
box.paddingBottom = vals[2];
|
||||
box.paddingLeft = vals[3];
|
||||
}
|
||||
applyFourSides(box.paddingTop, box.paddingRight, box.paddingBottom, box.paddingLeft, val, lenCtx);
|
||||
} else if (prop == QLatin1String("border")) {
|
||||
auto b = Css::parseBorderSide(val, lenCtx);
|
||||
if (b.width) {
|
||||
box.borderTop = *b.width;
|
||||
box.borderRight = *b.width;
|
||||
box.borderBottom = *b.width;
|
||||
box.borderLeft = *b.width;
|
||||
}
|
||||
if (b.width) box.borderTop = box.borderRight = box.borderBottom = box.borderLeft = *b.width;
|
||||
if (b.color.isValid()) box.borderColor = b.color;
|
||||
} else if (prop == QLatin1String("border-top")) {
|
||||
auto b = Css::parseBorderSide(val, lenCtx);
|
||||
if (b.width) box.borderTop = *b.width;
|
||||
if (b.color.isValid()) box.borderColor = b.color;
|
||||
applySide(box.borderTop, val);
|
||||
} else if (prop == QLatin1String("border-right")) {
|
||||
auto b = Css::parseBorderSide(val, lenCtx);
|
||||
if (b.width) box.borderRight = *b.width;
|
||||
if (b.color.isValid()) box.borderColor = b.color;
|
||||
applySide(box.borderRight, val);
|
||||
} else if (prop == QLatin1String("border-bottom")) {
|
||||
auto b = Css::parseBorderSide(val, lenCtx);
|
||||
if (b.width) box.borderBottom = *b.width;
|
||||
if (b.color.isValid()) box.borderColor = b.color;
|
||||
applySide(box.borderBottom, val);
|
||||
} else if (prop == QLatin1String("border-left")) {
|
||||
auto b = Css::parseBorderSide(val, lenCtx);
|
||||
if (b.width) box.borderLeft = *b.width;
|
||||
if (b.color.isValid()) box.borderColor = b.color;
|
||||
applySide(box.borderLeft, val);
|
||||
} else if (prop == QLatin1String("border-top-width")) {
|
||||
auto w = Css::parseBorderSide(val, lenCtx).width; if (w) box.borderTop = *w;
|
||||
} else if (prop == QLatin1String("border-right-width")) {
|
||||
@@ -239,27 +252,7 @@ void applyCssDeclarations(const QVector<Css::Declaration> &decls,
|
||||
} else if (prop == QLatin1String("border-left-width")) {
|
||||
auto w = Css::parseBorderSide(val, lenCtx).width; if (w) box.borderLeft = *w;
|
||||
} else if (prop == QLatin1String("border-width")) {
|
||||
const QStringList tokens = Css::splitValue(val);
|
||||
QVector<qreal> widths;
|
||||
for (const QString &tok : tokens) {
|
||||
auto w = Css::parseBorderSide(tok, lenCtx).width;
|
||||
if (w) widths.append(*w);
|
||||
}
|
||||
if (widths.size() == 1) {
|
||||
box.borderTop = box.borderRight = box.borderBottom = box.borderLeft = widths[0];
|
||||
} else if (widths.size() == 2) {
|
||||
box.borderTop = box.borderBottom = widths[0];
|
||||
box.borderRight = box.borderLeft = widths[1];
|
||||
} else if (widths.size() == 3) {
|
||||
box.borderTop = widths[0];
|
||||
box.borderRight = box.borderLeft = widths[1];
|
||||
box.borderBottom = widths[2];
|
||||
} else if (widths.size() >= 4) {
|
||||
box.borderTop = widths[0];
|
||||
box.borderRight = widths[1];
|
||||
box.borderBottom = widths[2];
|
||||
box.borderLeft = widths[3];
|
||||
}
|
||||
applyBorderWidths(box, val, lenCtx);
|
||||
} else if (prop == QLatin1String("border-color") || prop.endsWith(QLatin1String("-color"))) {
|
||||
QColor c = Css::parseColor(val);
|
||||
if (c.isValid()) box.borderColor = c;
|
||||
@@ -269,7 +262,7 @@ void applyCssDeclarations(const QVector<Css::Declaration> &decls,
|
||||
else if (prop == QLatin1String("border-right-style")) box.borderRight = 0;
|
||||
else if (prop == QLatin1String("border-bottom-style")) box.borderBottom = 0;
|
||||
else if (prop == QLatin1String("border-left-style")) box.borderLeft = 0;
|
||||
else { box.borderTop = box.borderRight = box.borderBottom = box.borderLeft = 0; }
|
||||
else box.borderTop = box.borderRight = box.borderBottom = box.borderLeft = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -277,128 +270,40 @@ void applyCssDeclarations(const QVector<Css::Declaration> &decls,
|
||||
|
||||
class Builder {
|
||||
public:
|
||||
Builder(const ParseParams ¶ms) : m_params(params)
|
||||
Builder(DocumentChunkBuilder &chunkBuilder,
|
||||
const ParseParams ¶ms,
|
||||
const Css::StyleSheet *sheet = nullptr,
|
||||
const QString &scheme = {},
|
||||
const QString &sourcePath = {},
|
||||
const QString &baseDir = {},
|
||||
int headingDepth = 1,
|
||||
const QDomNode &root = QDomNode())
|
||||
: m_chunkBuilder(chunkBuilder)
|
||||
, m_params(params)
|
||||
, m_sheet(sheet)
|
||||
, m_scheme(scheme)
|
||||
, m_sourcePath(sourcePath)
|
||||
, m_baseDir(baseDir)
|
||||
, m_headingDepth(headingDepth)
|
||||
, m_root(root)
|
||||
{
|
||||
StyleContext root;
|
||||
root.text.fontSize = params.baseFontSize;
|
||||
m_stack.append(root);
|
||||
StyleContext rootCtx;
|
||||
rootCtx.text.fontSize = params.baseFontSize;
|
||||
m_stack.append(rootCtx);
|
||||
}
|
||||
|
||||
QVector<BlockItem> build(const QDomElement &root)
|
||||
void build(const QDomElement &root)
|
||||
{
|
||||
walk(root);
|
||||
finishCurrentParagraph();
|
||||
return m_blocks;
|
||||
m_chunkBuilder.finishParagraph();
|
||||
}
|
||||
|
||||
private:
|
||||
void finishCurrentParagraph()
|
||||
{
|
||||
if (m_currentInlines.isEmpty())
|
||||
return;
|
||||
|
||||
if (!m_currentPreformatted) {
|
||||
// Drop trailing line breaks and whitespace
|
||||
while (!m_currentInlines.isEmpty()) {
|
||||
const auto &last = m_currentInlines.last();
|
||||
if (last.kind == InlineItem::Kind::LineBreak) {
|
||||
m_currentInlines.removeLast();
|
||||
} else if (last.kind == InlineItem::Kind::Text && last.text.trimmed().isEmpty()) {
|
||||
m_currentInlines.removeLast();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_currentInlines.isEmpty() && m_currentInlines.last().kind == InlineItem::Kind::Text) {
|
||||
while (m_currentInlines.last().text.endsWith(QLatin1Char(' '))) {
|
||||
m_currentInlines.last().text.chop(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Drop leading whitespace
|
||||
while (!m_currentInlines.isEmpty()) {
|
||||
const auto &first = m_currentInlines.first();
|
||||
if (first.kind == InlineItem::Kind::Text && first.text.trimmed().isEmpty()) {
|
||||
m_currentInlines.removeFirst();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_currentInlines.isEmpty() && m_currentInlines.first().kind == InlineItem::Kind::Text) {
|
||||
while (m_currentInlines.first().text.startsWith(QLatin1Char(' '))) {
|
||||
m_currentInlines.first().text.remove(0, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (m_currentInlines.isEmpty())
|
||||
return;
|
||||
|
||||
BlockItem p;
|
||||
p.kind = BlockItem::Kind::Paragraph;
|
||||
p.align = m_currentAlign;
|
||||
p.textIndent = m_currentIndent;
|
||||
p.lineHeight = m_currentLineHeight;
|
||||
p.fontSize = m_currentBlockFontSize;
|
||||
p.preformatted = m_currentPreformatted;
|
||||
p.marker = m_currentMarker;
|
||||
p.markerStyle = m_currentMarkerStyle;
|
||||
p.inlines = std::move(m_currentInlines);
|
||||
|
||||
m_blocks.append(p);
|
||||
|
||||
m_currentInlines.clear();
|
||||
m_currentMarker.clear();
|
||||
m_currentIndent = 0;
|
||||
}
|
||||
|
||||
void startParagraph(const StyleContext &ctx, const QString &marker = {}, const TextStyle &markerStyle = {})
|
||||
{
|
||||
finishCurrentParagraph();
|
||||
m_currentAlign = ctx.align;
|
||||
m_currentIndent = ctx.textIndent;
|
||||
m_currentLineHeight = ctx.lineHeight;
|
||||
m_currentBlockFontSize = ctx.text.fontSize;
|
||||
m_currentPreformatted = ctx.preformatted;
|
||||
m_currentMarker = marker;
|
||||
m_currentMarkerStyle = markerStyle;
|
||||
}
|
||||
|
||||
void appendText(const QString &text, const TextStyle &style, bool pre)
|
||||
{
|
||||
if (text.isEmpty())
|
||||
return;
|
||||
|
||||
if (m_currentInlines.isEmpty()) {
|
||||
startParagraph(m_stack.last());
|
||||
}
|
||||
|
||||
QString processed = text;
|
||||
if (!pre) {
|
||||
// Collapse whitespaces
|
||||
static const QRegularExpression kSpaces(QStringLiteral("\\s+"));
|
||||
processed.replace(kSpaces, QStringLiteral(" "));
|
||||
if (m_currentInlines.isEmpty() && processed == QLatin1String(" "))
|
||||
return;
|
||||
}
|
||||
|
||||
if (processed.isEmpty())
|
||||
return;
|
||||
|
||||
InlineItem item;
|
||||
item.kind = InlineItem::Kind::Text;
|
||||
item.text = processed;
|
||||
item.style = style;
|
||||
m_currentInlines.append(item);
|
||||
}
|
||||
|
||||
void walk(const QDomNode &node)
|
||||
{
|
||||
if (node.isText()) {
|
||||
const StyleContext &ctx = m_stack.last();
|
||||
appendText(node.nodeValue(), ctx.text, ctx.preformatted);
|
||||
m_chunkBuilder.appendText(node.nodeValue(), ctx.text, ctx.preformatted);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -411,10 +316,28 @@ private:
|
||||
if (tag == QLatin1String("head") || tag == QLatin1String("style") || tag == QLatin1String("script"))
|
||||
return;
|
||||
|
||||
// Skip nested sections in FB2 so they can be processed independently
|
||||
if (node != m_root && tag == QLatin1String("section") && m_scheme == QLatin1String("fb2"))
|
||||
return;
|
||||
|
||||
// Child handling within heading/title
|
||||
if (m_stack.last().isHeading) {
|
||||
if (tag == QLatin1String("p") || tag == QLatin1String("empty-line")) {
|
||||
if (!m_firstTitleLine) {
|
||||
m_chunkBuilder.appendLineBreak();
|
||||
}
|
||||
m_firstTitleLine = false;
|
||||
for (QDomNode child = el.firstChild(); !child.isNull(); child = child.nextSibling()) {
|
||||
walk(child);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
StyleContext ctx = m_stack.last();
|
||||
BoxStyle box;
|
||||
|
||||
// Default HTML semantics
|
||||
// Default HTML & FB2 semantics
|
||||
if (tag == QLatin1String("h1")) {
|
||||
ctx.text.fontSize = m_params.baseFontSize * 1.8;
|
||||
ctx.text.weight = 700;
|
||||
@@ -447,15 +370,30 @@ private:
|
||||
ctx.isHeading = true;
|
||||
box.marginTop = m_params.baseFontSize * 0.5;
|
||||
box.marginBottom = m_params.baseFontSize * 0.25;
|
||||
} else if (tag == QLatin1String("title")) {
|
||||
const int hLevel = qBound(1, m_headingDepth, 6);
|
||||
const qreal hScale = (hLevel == 1) ? 1.8 : (hLevel == 2 ? 1.5 : (hLevel == 3 ? 1.3 : 1.1));
|
||||
const qreal hMarginTop = (hLevel == 1) ? 1.0 : (hLevel == 2 ? 0.8 : (hLevel == 3 ? 0.6 : 0.5));
|
||||
ctx.text.fontSize = m_params.baseFontSize * hScale;
|
||||
ctx.text.weight = 700;
|
||||
ctx.textIndent = 0;
|
||||
ctx.align = TextAlign::Left;
|
||||
ctx.isHeading = true;
|
||||
box.marginTop = m_params.baseFontSize * hMarginTop;
|
||||
box.marginBottom = m_params.baseFontSize * (hMarginTop * 0.5);
|
||||
m_firstTitleLine = true;
|
||||
} else if (tag == QLatin1String("subtitle")) {
|
||||
ctx.align = TextAlign::Center;
|
||||
ctx.text.weight = 700;
|
||||
ctx.textIndent = 0;
|
||||
box.marginTop = 0;
|
||||
box.marginBottom = 0;
|
||||
} else if (tag == QLatin1String("p")) {
|
||||
box.marginTop = 0;
|
||||
box.marginBottom = 0;
|
||||
if (!ctx.isHeading) {
|
||||
ctx.textIndent = m_params.baseFontSize * 2.0;
|
||||
} else {
|
||||
ctx.textIndent = 0;
|
||||
}
|
||||
} else if (tag == QLatin1String("blockquote")) {
|
||||
ctx.textIndent = ctx.isHeading ? 0 : (m_params.baseFontSize * 2.0);
|
||||
} else if (tag == QLatin1String("blockquote") || tag == QLatin1String("epigraph")
|
||||
|| (tag == QLatin1String("cite") && m_scheme == QLatin1String("fb2"))) {
|
||||
box.marginLeft = m_params.baseFontSize * 1.5;
|
||||
box.marginRight = m_params.baseFontSize * 1.0;
|
||||
box.marginTop = m_params.baseFontSize * 0.5;
|
||||
@@ -463,6 +401,33 @@ private:
|
||||
box.borderLeft = 3;
|
||||
box.paddingLeft = m_params.baseFontSize * 0.75;
|
||||
ctx.text.italic = true;
|
||||
} else if (tag == QLatin1String("poem")) {
|
||||
box.marginLeft = m_params.baseFontSize * 2.0;
|
||||
box.marginRight = m_params.baseFontSize * 2.0;
|
||||
} else if (tag == QLatin1String("stanza")) {
|
||||
box.marginTop = m_params.baseFontSize * 1.0;
|
||||
box.marginBottom = m_params.baseFontSize * 1.0;
|
||||
} else if (tag == QLatin1String("v")) {
|
||||
ctx.textIndent = 0;
|
||||
box.marginTop = 0;
|
||||
box.marginBottom = 0;
|
||||
} else if (tag == QLatin1String("text-author") || tag == QLatin1String("date")) {
|
||||
ctx.align = TextAlign::Right;
|
||||
ctx.text.italic = true;
|
||||
ctx.textIndent = 0;
|
||||
box.marginTop = 0;
|
||||
box.marginBottom = 0;
|
||||
} else if (tag == QLatin1String("empty-line")) {
|
||||
m_chunkBuilder.finishParagraph();
|
||||
BoxStyle emptyBox;
|
||||
emptyBox.marginTop = m_params.baseFontSize * 0.5;
|
||||
emptyBox.marginBottom = m_params.baseFontSize * 0.5;
|
||||
m_chunkBuilder.startContainer(emptyBox);
|
||||
m_chunkBuilder.startParagraph(TextAlign::Left, 0, 0, m_params.baseFontSize);
|
||||
m_chunkBuilder.appendLineBreak();
|
||||
m_chunkBuilder.finishParagraph();
|
||||
m_chunkBuilder.endContainer(emptyBox);
|
||||
return;
|
||||
} else if (tag == QLatin1String("pre")) {
|
||||
ctx.text.fontFamilies = {QStringLiteral("monospace")};
|
||||
ctx.preformatted = true;
|
||||
@@ -470,11 +435,12 @@ private:
|
||||
box.marginBottom = m_params.baseFontSize * 0.5;
|
||||
} else if (tag == QLatin1String("b") || tag == QLatin1String("strong")) {
|
||||
ctx.text.weight = 700;
|
||||
} else if (tag == QLatin1String("i") || tag == QLatin1String("em") || tag == QLatin1String("cite")) {
|
||||
} else if (tag == QLatin1String("i") || tag == QLatin1String("em") || tag == QLatin1String("emphasis")
|
||||
|| (tag == QLatin1String("cite") && m_scheme != QLatin1String("fb2"))) {
|
||||
ctx.text.italic = true;
|
||||
} else if (tag == QLatin1String("u") || tag == QLatin1String("ins")) {
|
||||
ctx.text.underline = true;
|
||||
} else if (tag == QLatin1String("s") || tag == QLatin1String("strike") || tag == QLatin1String("del")) {
|
||||
} else if (tag == QLatin1String("s") || tag == QLatin1String("strike") || tag == QLatin1String("del") || tag == QLatin1String("strikethrough")) {
|
||||
ctx.text.strikeOut = true;
|
||||
} else if (tag == QLatin1String("sub")) {
|
||||
ctx.text.fontSize *= 0.75;
|
||||
@@ -491,15 +457,10 @@ private:
|
||||
ctx.text.underline = true;
|
||||
ctx.text.link = true;
|
||||
} else if (tag == QLatin1String("hr")) {
|
||||
finishCurrentParagraph();
|
||||
BlockItem r;
|
||||
r.kind = BlockItem::Kind::Rule;
|
||||
m_blocks.append(r);
|
||||
m_chunkBuilder.appendRule();
|
||||
return;
|
||||
} else if (tag == QLatin1String("br")) {
|
||||
InlineItem br;
|
||||
br.kind = InlineItem::Kind::LineBreak;
|
||||
m_currentInlines.append(br);
|
||||
m_chunkBuilder.appendLineBreak();
|
||||
return;
|
||||
} else if (tag == QLatin1String("img") || tag == QLatin1String("image")) {
|
||||
handleImage(el, ctx);
|
||||
@@ -517,10 +478,14 @@ private:
|
||||
|
||||
const bool isBlock = isBlockTag(tag);
|
||||
|
||||
// Apply CSS declarations from cascaded style attribute
|
||||
if (el.hasAttribute(QStringLiteral("style"))) {
|
||||
const auto decls = Css::parseDeclarations(el.attribute(QStringLiteral("style")));
|
||||
applyCssDeclarations(decls, ctx, box, m_params, isBlock);
|
||||
QVector<Css::Declaration> decls;
|
||||
if (m_sheet && !m_sheet->rules.isEmpty()) {
|
||||
decls = Css::cascade(*m_sheet, el);
|
||||
} else if (el.hasAttribute(QStringLiteral("style"))) {
|
||||
decls = Css::parseDeclarations(el.attribute(QStringLiteral("style")));
|
||||
}
|
||||
if (!decls.isEmpty()) {
|
||||
applyDeclarations(decls, ctx, box, m_params, isBlock);
|
||||
}
|
||||
|
||||
const bool hasBox = (box.marginTop > 0 || box.marginBottom > 0 || box.marginLeft > 0 || box.marginRight > 0
|
||||
@@ -529,12 +494,9 @@ private:
|
||||
|| box.background.isValid());
|
||||
|
||||
if (isBlock) {
|
||||
finishCurrentParagraph();
|
||||
m_chunkBuilder.finishParagraph();
|
||||
if (hasBox) {
|
||||
BlockItem start;
|
||||
start.kind = BlockItem::Kind::ContainerStart;
|
||||
start.box = box;
|
||||
m_blocks.append(start);
|
||||
m_chunkBuilder.startContainer(box);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -552,7 +514,7 @@ private:
|
||||
}
|
||||
|
||||
if (isBlock) {
|
||||
startParagraph(ctx, marker, markerStyle);
|
||||
m_chunkBuilder.startParagraph(ctx.align, ctx.textIndent, ctx.lineHeight, ctx.text.fontSize, ctx.preformatted, marker, markerStyle);
|
||||
}
|
||||
|
||||
m_stack.append(ctx);
|
||||
@@ -562,12 +524,9 @@ private:
|
||||
m_stack.removeLast();
|
||||
|
||||
if (isBlock) {
|
||||
finishCurrentParagraph();
|
||||
m_chunkBuilder.finishParagraph();
|
||||
if (hasBox) {
|
||||
BlockItem end;
|
||||
end.kind = BlockItem::Kind::ContainerEnd;
|
||||
end.box = box;
|
||||
m_blocks.append(end);
|
||||
m_chunkBuilder.endContainer(box);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -579,12 +538,15 @@ private:
|
||||
src = el.attribute(QStringLiteral("xlink:href"));
|
||||
if (src.isEmpty())
|
||||
src = el.attribute(QStringLiteral("href"));
|
||||
if (src.isEmpty())
|
||||
src = el.attribute(QStringLiteral("l:href"));
|
||||
if (src.isEmpty())
|
||||
return;
|
||||
|
||||
InlineItem item;
|
||||
item.kind = InlineItem::Kind::Image;
|
||||
item.imageUrl = src;
|
||||
QString resolved = src;
|
||||
if (!m_scheme.isEmpty()) {
|
||||
resolved = ImageUrl::resolve(m_scheme, src, m_sourcePath, m_baseDir);
|
||||
}
|
||||
|
||||
Css::LengthContext lenCtx;
|
||||
lenCtx.em = ctx.text.fontSize;
|
||||
@@ -592,64 +554,53 @@ private:
|
||||
lenCtx.percentBase = m_params.contentWidth;
|
||||
lenCtx.absoluteScale = m_params.baseFontSize / 16.0;
|
||||
|
||||
qreal cssW = -1;
|
||||
qreal cssH = -1;
|
||||
qreal cssMaxW = -1;
|
||||
|
||||
if (el.hasAttribute(QStringLiteral("width"))) {
|
||||
auto w = Css::parseLength(el.attribute(QStringLiteral("width")), lenCtx);
|
||||
if (w) item.cssWidth = *w;
|
||||
if (w) cssW = *w;
|
||||
}
|
||||
if (el.hasAttribute(QStringLiteral("height"))) {
|
||||
auto h = Css::parseLength(el.attribute(QStringLiteral("height")), lenCtx);
|
||||
if (h) item.cssHeight = *h;
|
||||
if (h) cssH = *h;
|
||||
}
|
||||
|
||||
if (el.hasAttribute(QStringLiteral("style"))) {
|
||||
const auto decls = Css::parseDeclarations(el.attribute(QStringLiteral("style")));
|
||||
for (const auto &d : decls) {
|
||||
if (d.property == QLatin1String("width")) {
|
||||
auto w = Css::parseLength(d.value, lenCtx); if (w) item.cssWidth = *w;
|
||||
} else if (d.property == QLatin1String("height")) {
|
||||
auto h = Css::parseLength(d.value, lenCtx); if (h) item.cssHeight = *h;
|
||||
} else if (d.property == QLatin1String("max-width")) {
|
||||
auto mw = Css::parseLength(d.value, lenCtx); if (mw) item.cssMaxWidth = *mw;
|
||||
}
|
||||
QVector<Css::Declaration> decls;
|
||||
if (m_sheet && !m_sheet->rules.isEmpty()) {
|
||||
decls = Css::cascade(*m_sheet, el);
|
||||
} else if (el.hasAttribute(QStringLiteral("style"))) {
|
||||
decls = Css::parseDeclarations(el.attribute(QStringLiteral("style")));
|
||||
}
|
||||
|
||||
for (const auto &d : decls) {
|
||||
if (d.property == QLatin1String("width")) {
|
||||
auto w = Css::parseLength(d.value, lenCtx); if (w) cssW = *w;
|
||||
} else if (d.property == QLatin1String("height")) {
|
||||
auto h = Css::parseLength(d.value, lenCtx); if (h) cssH = *h;
|
||||
} else if (d.property == QLatin1String("max-width")) {
|
||||
auto mw = Css::parseLength(d.value, lenCtx); if (mw) cssMaxW = *mw;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_currentInlines.isEmpty()) {
|
||||
startParagraph(ctx);
|
||||
}
|
||||
m_currentInlines.append(item);
|
||||
m_chunkBuilder.appendImage(resolved, cssW, cssH, cssMaxW, ctx.text);
|
||||
}
|
||||
|
||||
DocumentChunkBuilder &m_chunkBuilder;
|
||||
const ParseParams &m_params;
|
||||
const Css::StyleSheet *m_sheet = nullptr;
|
||||
QString m_scheme;
|
||||
QString m_sourcePath;
|
||||
QString m_baseDir;
|
||||
int m_headingDepth = 1;
|
||||
QDomNode m_root;
|
||||
QVector<StyleContext> m_stack;
|
||||
QVector<BlockItem> m_blocks;
|
||||
|
||||
QVector<InlineItem> m_currentInlines;
|
||||
TextAlign m_currentAlign = TextAlign::Justify;
|
||||
qreal m_currentIndent = 0;
|
||||
qreal m_currentLineHeight = 0;
|
||||
qreal m_currentBlockFontSize = 16;
|
||||
bool m_currentPreformatted = false;
|
||||
QString m_currentMarker;
|
||||
TextStyle m_currentMarkerStyle;
|
||||
bool m_firstTitleLine = false;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
QVector<BlockItem> parse(const QDomElement &root, const ParseParams ¶ms)
|
||||
QDomDocument parseHtmlToDoc(const QString &html)
|
||||
{
|
||||
if (root.isNull())
|
||||
return {};
|
||||
|
||||
Builder builder(params);
|
||||
return builder.build(root);
|
||||
}
|
||||
|
||||
QVector<BlockItem> parse(const QString &html, const ParseParams ¶ms)
|
||||
{
|
||||
if (html.trimmed().isEmpty())
|
||||
return {};
|
||||
|
||||
QString wrapped = html.trimmed();
|
||||
if (!wrapped.startsWith(QLatin1String("<")))
|
||||
wrapped = QStringLiteral("<p>") + wrapped + QStringLiteral("</p>");
|
||||
@@ -657,12 +608,40 @@ QVector<BlockItem> parse(const QString &html, const ParseParams ¶ms)
|
||||
wrapped = QStringLiteral("<body>") + wrapped + QStringLiteral("</body>");
|
||||
|
||||
const QString safeXml = HtmlEntities::toXmlSafe(wrapped);
|
||||
|
||||
QDomDocument doc;
|
||||
QString errorMsg;
|
||||
int errorLine = 0, errorColumn = 0;
|
||||
if (!doc.setContent(safeXml, false, &errorMsg, &errorLine, &errorColumn)) {
|
||||
// Fallback: return as a simple paragraph
|
||||
doc.setContent(safeXml);
|
||||
return doc;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
QVector<BlockItem> parse(const QDomElement &root,
|
||||
const ParseParams ¶ms,
|
||||
const Css::StyleSheet *sheet,
|
||||
const QString &scheme,
|
||||
const QString &sourcePath,
|
||||
const QString &baseDir,
|
||||
int headingDepth)
|
||||
{
|
||||
if (root.isNull())
|
||||
return {};
|
||||
|
||||
DocumentChunkBuilder chunkBuilder({}, 1000000000);
|
||||
Builder builder(chunkBuilder, params, sheet, scheme, sourcePath, baseDir, headingDepth, root);
|
||||
builder.build(root);
|
||||
auto chunks = chunkBuilder.build();
|
||||
if (chunks.isEmpty())
|
||||
return {};
|
||||
return chunks.first().blocks;
|
||||
}
|
||||
|
||||
QVector<BlockItem> parse(const QString &html, const ParseParams ¶ms)
|
||||
{
|
||||
if (html.trimmed().isEmpty())
|
||||
return {};
|
||||
|
||||
QDomDocument doc = parseHtmlToDoc(html);
|
||||
if (doc.documentElement().isNull()) {
|
||||
BlockItem b;
|
||||
b.kind = BlockItem::Kind::Paragraph;
|
||||
InlineItem textItem;
|
||||
@@ -682,105 +661,30 @@ QVector<ContentChunk> partitionBlocks(const QVector<BlockItem> &blocks,
|
||||
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();
|
||||
}
|
||||
}
|
||||
DocumentChunkBuilder builder(sectionHref, targetChars);
|
||||
for (const auto &b : blocks) {
|
||||
builder.appendBlock(b);
|
||||
}
|
||||
|
||||
if (!current.isEmpty()) {
|
||||
chunks.append(std::move(current));
|
||||
}
|
||||
|
||||
return chunks;
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
QVector<ContentChunk> parseChunks(const QDomElement &root,
|
||||
const QString §ionHref,
|
||||
int targetChars,
|
||||
const ParseParams ¶ms)
|
||||
const ParseParams ¶ms,
|
||||
const Css::StyleSheet *sheet,
|
||||
const QString &scheme,
|
||||
const QString &sourcePath,
|
||||
const QString &baseDir,
|
||||
int headingDepth)
|
||||
{
|
||||
const auto blocks = parse(root, params);
|
||||
return partitionBlocks(blocks, sectionHref, targetChars);
|
||||
if (root.isNull())
|
||||
return {};
|
||||
|
||||
DocumentChunkBuilder chunkBuilder(sectionHref, targetChars);
|
||||
Builder builder(chunkBuilder, params, sheet, scheme, sourcePath, baseDir, headingDepth, root);
|
||||
builder.build(root);
|
||||
return chunkBuilder.build();
|
||||
}
|
||||
|
||||
QVector<ContentChunk> parseChunks(const QString &html,
|
||||
@@ -788,9 +692,26 @@ QVector<ContentChunk> parseChunks(const QString &html,
|
||||
int targetChars,
|
||||
const ParseParams ¶ms)
|
||||
{
|
||||
const auto blocks = parse(html, params);
|
||||
return partitionBlocks(blocks, sectionHref, targetChars);
|
||||
if (html.trimmed().isEmpty())
|
||||
return {};
|
||||
|
||||
QDomDocument doc = parseHtmlToDoc(html);
|
||||
if (doc.documentElement().isNull()) {
|
||||
ContentChunk chunk;
|
||||
chunk.sectionHref = sectionHref;
|
||||
BlockItem b;
|
||||
b.kind = BlockItem::Kind::Paragraph;
|
||||
InlineItem textItem;
|
||||
textItem.kind = InlineItem::Kind::Text;
|
||||
textItem.text = html;
|
||||
b.inlines.append(textItem);
|
||||
chunk.blocks.append(b);
|
||||
chunk.charCount = html.size();
|
||||
chunk.wordCount = html.split(QLatin1Char(' '), Qt::SkipEmptyParts).size();
|
||||
return {chunk};
|
||||
}
|
||||
|
||||
return parseChunks(doc.documentElement(), sectionHref, targetChars, params);
|
||||
}
|
||||
|
||||
} // namespace HtmlBlockParser
|
||||
|
||||
Reference in new issue
Block a user