718 lines
30 KiB
C++
718 lines
30 KiB
C++
#include "HtmlBlockParser.h"
|
|
|
|
#include <QDomDocument>
|
|
#include <QSet>
|
|
#include <algorithm>
|
|
|
|
#include "DocumentChunkBuilder.h"
|
|
#include "backend/formats/HtmlEntities.h"
|
|
#include "backend/formats/ImageUrl.h"
|
|
|
|
namespace HtmlBlockParser {
|
|
|
|
namespace {
|
|
|
|
struct StyleContext {
|
|
TextStyle text;
|
|
TextAlign align = TextAlign::Justify;
|
|
qreal textIndent = 0;
|
|
qreal lineHeight = 0;
|
|
bool preformatted = false;
|
|
bool isHeading = false;
|
|
};
|
|
|
|
QString localName(const QDomElement &e)
|
|
{
|
|
QString name = e.tagName();
|
|
const int idx = name.indexOf(QLatin1Char(':'));
|
|
if (idx >= 0)
|
|
name = name.mid(idx + 1);
|
|
return name.toLower();
|
|
}
|
|
|
|
bool isBlockTag(const QString &tag)
|
|
{
|
|
static const QSet<QString> kBlocks = {
|
|
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("dl"),
|
|
QStringLiteral("dt"), QStringLiteral("dd"),
|
|
QStringLiteral("hr"), QStringLiteral("figure"),
|
|
QStringLiteral("svg"), QStringLiteral("figcaption"),
|
|
QStringLiteral("section"), QStringLiteral("article"),
|
|
QStringLiteral("header"), QStringLiteral("footer"), QStringLiteral("aside"),
|
|
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 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;
|
|
lenCtx.rem = params.baseFontSize;
|
|
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;
|
|
|
|
if (prop == QLatin1String("font-family")) {
|
|
ctx.text.fontFamilies = Css::splitCommaList(val);
|
|
} else if (prop == QLatin1String("font-size")) {
|
|
if (val == QLatin1String("xx-small")) ctx.text.fontSize = params.baseFontSize * 0.6;
|
|
else if (val == QLatin1String("x-small")) ctx.text.fontSize = params.baseFontSize * 0.75;
|
|
else if (val == QLatin1String("small")) ctx.text.fontSize = params.baseFontSize * 0.875;
|
|
else if (val == QLatin1String("medium")) ctx.text.fontSize = params.baseFontSize;
|
|
else if (val == QLatin1String("large")) ctx.text.fontSize = params.baseFontSize * 1.15;
|
|
else if (val == QLatin1String("x-large")) ctx.text.fontSize = params.baseFontSize * 1.5;
|
|
else if (val == QLatin1String("xx-large")) ctx.text.fontSize = params.baseFontSize * 2.0;
|
|
else if (val == QLatin1String("smaller")) ctx.text.fontSize *= 0.85;
|
|
else if (val == QLatin1String("larger")) ctx.text.fontSize *= 1.2;
|
|
else {
|
|
auto sz = Css::parseLength(val, lenCtx);
|
|
if (sz && *sz > 0)
|
|
ctx.text.fontSize = *sz;
|
|
}
|
|
lenCtx.em = ctx.text.fontSize;
|
|
} else if (prop == QLatin1String("font-weight")) {
|
|
if (val == QLatin1String("bold") || val == QLatin1String("bolder")) ctx.text.weight = 700;
|
|
else if (val == QLatin1String("normal") || val == QLatin1String("lighter")) ctx.text.weight = 400;
|
|
else {
|
|
bool ok;
|
|
int w = val.toInt(&ok);
|
|
if (ok && w >= 100 && w <= 900)
|
|
ctx.text.weight = w;
|
|
}
|
|
} else if (prop == QLatin1String("font-style")) {
|
|
ctx.text.italic = (val == QLatin1String("italic") || val == QLatin1String("oblique"));
|
|
} else if (prop == QLatin1String("font-variant")) {
|
|
ctx.text.smallCaps = (val == QLatin1String("small-caps"));
|
|
} else if (prop == QLatin1String("text-transform")) {
|
|
if (val == QLatin1String("uppercase")) ctx.text.transform = TextTransform::Uppercase;
|
|
else if (val == QLatin1String("lowercase")) ctx.text.transform = TextTransform::Lowercase;
|
|
else if (val == QLatin1String("capitalize")) ctx.text.transform = TextTransform::Capitalize;
|
|
else ctx.text.transform = TextTransform::None;
|
|
} else if (prop == QLatin1String("letter-spacing")) {
|
|
auto ls = Css::parseLength(val, lenCtx);
|
|
if (ls) ctx.text.letterSpacing = *ls;
|
|
} else if (prop == QLatin1String("text-decoration") || prop == QLatin1String("text-decoration-line")) {
|
|
ctx.text.underline = val.contains(QLatin1String("underline"));
|
|
ctx.text.strikeOut = val.contains(QLatin1String("line-through"));
|
|
ctx.text.overline = val.contains(QLatin1String("overline"));
|
|
} else if (prop == QLatin1String("color")) {
|
|
QColor c = Css::parseColor(val);
|
|
if (c.isValid()) ctx.text.color = c;
|
|
} else if (prop == QLatin1String("background-color") || prop == QLatin1String("background")) {
|
|
QColor c = Css::parseColor(val);
|
|
if (c.isValid() && c.alpha() > 0) {
|
|
if (isBlock) {
|
|
box.background = c;
|
|
ctx.text.background = QColor();
|
|
} else {
|
|
ctx.text.background = c;
|
|
}
|
|
} else if (val.trimmed() == QLatin1String("none") || val.trimmed() == QLatin1String("transparent")) {
|
|
if (isBlock) {
|
|
box.background = QColor();
|
|
} else {
|
|
ctx.text.background = QColor();
|
|
}
|
|
}
|
|
} else if (prop == QLatin1String("text-align")) {
|
|
if (val == QLatin1String("left") || val == QLatin1String("start")) ctx.align = TextAlign::Left;
|
|
else if (val == QLatin1String("center")) ctx.align = TextAlign::Center;
|
|
else if (val == QLatin1String("right") || val == QLatin1String("end")) ctx.align = TextAlign::Right;
|
|
else if (val == QLatin1String("justify")) ctx.align = TextAlign::Justify;
|
|
} else if (prop == QLatin1String("text-indent")) {
|
|
auto ind = Css::parseLength(val, lenCtx);
|
|
if (ind) ctx.textIndent = *ind;
|
|
} else if (prop == QLatin1String("line-height")) {
|
|
bool ok;
|
|
qreal lh = val.toDouble(&ok);
|
|
if (ok && lh > 0) {
|
|
ctx.lineHeight = lh;
|
|
} else {
|
|
auto len = Css::parseLength(val, lenCtx);
|
|
if (len && *len > 0 && ctx.text.fontSize > 0)
|
|
ctx.lineHeight = *len / ctx.text.fontSize;
|
|
}
|
|
} else if (prop == QLatin1String("margin-top")) {
|
|
auto m = Css::parseLength(val, lenCtx); if (m) box.marginTop = *m;
|
|
} else if (prop == QLatin1String("margin-bottom")) {
|
|
auto m = Css::parseLength(val, lenCtx); if (m) box.marginBottom = *m;
|
|
} else if (prop == QLatin1String("margin-left")) {
|
|
auto m = Css::parseLength(val, lenCtx); if (m) box.marginLeft = *m;
|
|
} else if (prop == QLatin1String("margin-right")) {
|
|
auto m = Css::parseLength(val, lenCtx); if (m) box.marginRight = *m;
|
|
} else if (prop == QLatin1String("margin")) {
|
|
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")) {
|
|
auto p = Css::parseLength(val, lenCtx); if (p) box.paddingBottom = *p;
|
|
} else if (prop == QLatin1String("padding-left")) {
|
|
auto p = Css::parseLength(val, lenCtx); if (p) box.paddingLeft = *p;
|
|
} else if (prop == QLatin1String("padding-right")) {
|
|
auto p = Css::parseLength(val, lenCtx); if (p) box.paddingRight = *p;
|
|
} else if (prop == QLatin1String("padding")) {
|
|
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 = box.borderRight = box.borderBottom = box.borderLeft = *b.width;
|
|
if (b.color.isValid()) box.borderColor = b.color;
|
|
} else if (prop == QLatin1String("border-top")) {
|
|
applySide(box.borderTop, val);
|
|
} else if (prop == QLatin1String("border-right")) {
|
|
applySide(box.borderRight, val);
|
|
} else if (prop == QLatin1String("border-bottom")) {
|
|
applySide(box.borderBottom, val);
|
|
} else if (prop == QLatin1String("border-left")) {
|
|
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")) {
|
|
auto w = Css::parseBorderSide(val, lenCtx).width; if (w) box.borderRight = *w;
|
|
} else if (prop == QLatin1String("border-bottom-width")) {
|
|
auto w = Css::parseBorderSide(val, lenCtx).width; if (w) box.borderBottom = *w;
|
|
} 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")) {
|
|
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;
|
|
} else if (prop == QLatin1String("border-style") || prop.endsWith(QLatin1String("-style"))) {
|
|
if (val.contains(QLatin1String("none")) || val.contains(QLatin1String("hidden"))) {
|
|
if (prop == QLatin1String("border-top-style")) box.borderTop = 0;
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
class Builder {
|
|
public:
|
|
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 rootCtx;
|
|
rootCtx.text.fontSize = params.baseFontSize;
|
|
m_stack.append(rootCtx);
|
|
}
|
|
|
|
void build(const QDomElement &root)
|
|
{
|
|
walk(root);
|
|
m_chunkBuilder.finishParagraph();
|
|
}
|
|
|
|
private:
|
|
void walk(const QDomNode &node)
|
|
{
|
|
if (node.isText()) {
|
|
const StyleContext &ctx = m_stack.last();
|
|
m_chunkBuilder.appendText(node.nodeValue(), ctx.text, ctx.preformatted);
|
|
return;
|
|
}
|
|
|
|
if (!node.isElement())
|
|
return;
|
|
|
|
const QDomElement el = node.toElement();
|
|
const QString tag = localName(el);
|
|
|
|
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 & FB2 semantics
|
|
if (tag == QLatin1String("h1")) {
|
|
ctx.text.fontSize = m_params.baseFontSize * 1.8;
|
|
ctx.text.weight = 700;
|
|
ctx.textIndent = 0;
|
|
ctx.align = TextAlign::Left;
|
|
ctx.isHeading = true;
|
|
box.marginTop = m_params.baseFontSize * 1.0;
|
|
box.marginBottom = m_params.baseFontSize * 0.5;
|
|
} else if (tag == QLatin1String("h2")) {
|
|
ctx.text.fontSize = m_params.baseFontSize * 1.5;
|
|
ctx.text.weight = 700;
|
|
ctx.textIndent = 0;
|
|
ctx.align = TextAlign::Left;
|
|
ctx.isHeading = true;
|
|
box.marginTop = m_params.baseFontSize * 0.8;
|
|
box.marginBottom = m_params.baseFontSize * 0.4;
|
|
} else if (tag == QLatin1String("h3")) {
|
|
ctx.text.fontSize = m_params.baseFontSize * 1.3;
|
|
ctx.text.weight = 700;
|
|
ctx.textIndent = 0;
|
|
ctx.align = TextAlign::Left;
|
|
ctx.isHeading = true;
|
|
box.marginTop = m_params.baseFontSize * 0.6;
|
|
box.marginBottom = m_params.baseFontSize * 0.3;
|
|
} else if (tag == QLatin1String("h4") || tag == QLatin1String("h5") || tag == QLatin1String("h6")) {
|
|
ctx.text.fontSize = m_params.baseFontSize * 1.1;
|
|
ctx.text.weight = 700;
|
|
ctx.textIndent = 0;
|
|
ctx.align = TextAlign::Left;
|
|
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;
|
|
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;
|
|
box.marginBottom = m_params.baseFontSize * 0.5;
|
|
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;
|
|
box.marginTop = m_params.baseFontSize * 0.5;
|
|
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("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") || tag == QLatin1String("strikethrough")) {
|
|
ctx.text.strikeOut = true;
|
|
} else if (tag == QLatin1String("sub")) {
|
|
ctx.text.fontSize *= 0.75;
|
|
ctx.text.baselineShift = -ctx.text.fontSize * 0.35;
|
|
} else if (tag == QLatin1String("sup")) {
|
|
ctx.text.fontSize *= 0.75;
|
|
ctx.text.baselineShift = ctx.text.fontSize * 0.45;
|
|
} else if (tag == QLatin1String("small")) {
|
|
ctx.text.fontSize *= 0.85;
|
|
} else if (tag == QLatin1String("code") || tag == QLatin1String("tt") || tag == QLatin1String("kbd")) {
|
|
ctx.text.fontFamilies = {QStringLiteral("monospace")};
|
|
ctx.text.background = QColor(255, 255, 255, 20);
|
|
} else if (tag == QLatin1String("a")) {
|
|
ctx.text.underline = true;
|
|
ctx.text.link = true;
|
|
} else if (tag == QLatin1String("hr")) {
|
|
m_chunkBuilder.appendRule();
|
|
return;
|
|
} else if (tag == QLatin1String("br")) {
|
|
m_chunkBuilder.appendLineBreak();
|
|
return;
|
|
} else if (tag == QLatin1String("img") || tag == QLatin1String("image")) {
|
|
handleImage(el, ctx);
|
|
return;
|
|
}
|
|
|
|
// Apply HTML attribute alignments
|
|
if (el.hasAttribute(QStringLiteral("align"))) {
|
|
const QString a = el.attribute(QStringLiteral("align")).toLower();
|
|
if (a == QLatin1String("center")) ctx.align = TextAlign::Center;
|
|
else if (a == QLatin1String("right")) ctx.align = TextAlign::Right;
|
|
else if (a == QLatin1String("justify")) ctx.align = TextAlign::Justify;
|
|
else if (a == QLatin1String("left")) ctx.align = TextAlign::Left;
|
|
}
|
|
|
|
const bool isBlock = isBlockTag(tag);
|
|
|
|
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
|
|
|| box.paddingTop > 0 || box.paddingBottom > 0 || box.paddingLeft > 0 || box.paddingRight > 0
|
|
|| box.borderLeft > 0 || box.borderTop > 0 || box.borderBottom > 0 || box.borderRight > 0
|
|
|| box.background.isValid());
|
|
|
|
if (isBlock) {
|
|
m_chunkBuilder.finishParagraph();
|
|
if (hasBox) {
|
|
m_chunkBuilder.startContainer(box);
|
|
}
|
|
}
|
|
|
|
QString marker;
|
|
TextStyle markerStyle;
|
|
if (tag == QLatin1String("li")) {
|
|
marker = QStringLiteral("• ");
|
|
markerStyle = ctx.text;
|
|
} else if (tag == QLatin1String("dt")) {
|
|
ctx.text.weight = 700;
|
|
ctx.textIndent = 0;
|
|
} else if (tag == QLatin1String("dd")) {
|
|
box.marginLeft = m_params.baseFontSize * 1.5;
|
|
ctx.textIndent = 0;
|
|
}
|
|
|
|
if (isBlock) {
|
|
m_chunkBuilder.startParagraph(ctx.align, ctx.textIndent, ctx.lineHeight, ctx.text.fontSize, ctx.preformatted, marker, markerStyle);
|
|
}
|
|
|
|
m_stack.append(ctx);
|
|
for (QDomNode child = el.firstChild(); !child.isNull(); child = child.nextSibling()) {
|
|
walk(child);
|
|
}
|
|
m_stack.removeLast();
|
|
|
|
if (isBlock) {
|
|
m_chunkBuilder.finishParagraph();
|
|
if (hasBox) {
|
|
m_chunkBuilder.endContainer(box);
|
|
}
|
|
}
|
|
}
|
|
|
|
void handleImage(const QDomElement &el, const StyleContext &ctx)
|
|
{
|
|
QString src = el.attribute(QStringLiteral("src"));
|
|
if (src.isEmpty())
|
|
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;
|
|
|
|
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;
|
|
lenCtx.rem = m_params.baseFontSize;
|
|
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) cssW = *w;
|
|
}
|
|
if (el.hasAttribute(QStringLiteral("height"))) {
|
|
auto h = Css::parseLength(el.attribute(QStringLiteral("height")), lenCtx);
|
|
if (h) cssH = *h;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
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;
|
|
bool m_firstTitleLine = false;
|
|
};
|
|
|
|
QDomDocument parseHtmlToDoc(const QString &html)
|
|
{
|
|
QString wrapped = html.trimmed();
|
|
if (!wrapped.startsWith(QLatin1String("<")))
|
|
wrapped = QStringLiteral("<p>") + wrapped + QStringLiteral("</p>");
|
|
if (!wrapped.contains(QLatin1String("<body")))
|
|
wrapped = QStringLiteral("<body>") + wrapped + QStringLiteral("</body>");
|
|
|
|
const QString safeXml = HtmlEntities::toXmlSafe(wrapped);
|
|
QDomDocument doc;
|
|
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;
|
|
textItem.kind = InlineItem::Kind::Text;
|
|
textItem.text = html;
|
|
b.inlines.append(textItem);
|
|
return {b};
|
|
}
|
|
|
|
return parse(doc.documentElement(), params);
|
|
}
|
|
|
|
QVector<ContentChunk> partitionBlocks(const QVector<BlockItem> &blocks,
|
|
const QString §ionHref,
|
|
int targetChars)
|
|
{
|
|
if (blocks.isEmpty())
|
|
return {};
|
|
|
|
DocumentChunkBuilder builder(sectionHref, targetChars);
|
|
for (const auto &b : blocks) {
|
|
builder.appendBlock(b);
|
|
}
|
|
return builder.build();
|
|
}
|
|
|
|
QVector<ContentChunk> parseChunks(const QDomElement &root,
|
|
const QString §ionHref,
|
|
int targetChars,
|
|
const ParseParams ¶ms,
|
|
const Css::StyleSheet *sheet,
|
|
const QString &scheme,
|
|
const QString &sourcePath,
|
|
const QString &baseDir,
|
|
int headingDepth)
|
|
{
|
|
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,
|
|
const QString §ionHref,
|
|
int targetChars,
|
|
const ParseParams ¶ms)
|
|
{
|
|
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
|