aboutsummaryrefslogtreecommitdiff
path: root/src/renderers/htmlrenderer.cpp
blob: 48f4709aae3bef90d484663df2dc88e82f286d00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include "htmlrenderer.hpp"

#include "renderhelpers.hpp"
#include "textstyleinstance.hpp"
#include "gumbo.h"

#include <QDebug>
#include <QTextTable>
#include <QRegularExpression>

static void* malloc_wrapper(void*, size_t size) { return malloc(size); }

static void free_wrapper(void*, void* ptr) { free(ptr); }

static GumboOptions const gumbo_options = {
    &malloc_wrapper, &free_wrapper, // memory management
    nullptr,                        // user pointer
    4,                              // tab width
    false,                          // stop on first error
    -1,                             // maximum numbers of errors (-1 = infinite)
    GUMBO_TAG_LAST,
    GUMBO_NAMESPACE_HTML
};

static void destroyGumboOutput(GumboOutput * output)
{
    gumbo_destroy_output(&gumbo_options, output);
}

static const char* find_title(const GumboNode* root) {
    assert(root->type == GUMBO_NODE_ELEMENT);
    if(root->v.element.children.length < 2)
        return nullptr;

    const GumboVector* root_children = &root->v.element.children;
    GumboNode* head = nullptr;
    for (size_t i = 0; i < root_children->length; ++i) {
        GumboNode* child = (GumboNode*)root_children->data[i];
        if (child->type == GUMBO_NODE_ELEMENT and child->v.element.tag == GUMBO_TAG_HEAD) {
            head = child;
            break;
        }
    }
    if(head == nullptr)
        return nullptr;

    GumboVector* head_children = &head->v.element.children;
    for (size_t i = 0; i < head_children->length; ++i) {
        GumboNode* child = (GumboNode*)head_children->data[i];
        if (child->type == GUMBO_NODE_ELEMENT and child->v.element.tag == GUMBO_TAG_TITLE) {
            if (child->v.element.children.length != 1) {
                return "";
            }
            GumboNode* title_text = (GumboNode*)child->v.element.children.data[0];
            if(title_text->type == GUMBO_NODE_TEXT or title_text->type == GUMBO_NODE_WHITESPACE)
                return title_text->v.text.text;
            return nullptr;
        }
    }
    return nullptr;
}

struct RenderState
{
    QTextCursor cursor;
    TextStyleInstance text_style;
    QUrl root_url;
    DocumentStyle const * style;
    DocumentOutlineModel * outline;
};

static char const * getAttribute(GumboElement const & element, char const * attrib_name)
{
    for(size_t i = 0; i < element.attributes.length; i++)
    {
        auto const attrib = static_cast<GumboAttribute const *>(element.attributes.data[i]);
        if(strcmp(attrib->name, attrib_name) == 0)
            return attrib->value;
    }
    return nullptr;
}

struct TextFormatReset
{
    QTextCursor * cursor;

    QTextCharFormat char_format;
    QTextBlockFormat block_format;

    TextFormatReset(QTextCursor * cursor) :
        cursor(cursor),
        char_format(cursor->charFormat()),
        block_format(cursor->blockFormat())
    {

    }

    TextFormatReset(TextFormatReset const &) = delete;
    TextFormatReset(TextFormatReset &&) = delete;

    ~TextFormatReset()
    {
        this->cursor->setCharFormat(this->char_format);
        this->cursor->setBlockFormat(this->block_format);
    }

};

// Problems:
// Style/theme elements must use a push/pop
// use instead of "replacing" styles
// Otherwise, <h1><a>Foo</a></h1> will be rendered as a link, not as a heading.
// Should be combined here.

static void renderRecursive(RenderState & state, GumboNode const & node, int nesting = 0)
{
    auto & cursor = state.cursor;
    auto & text_style = state.text_style;
    switch(node.type)
    {
    /** Document node.  v will be a GumboDocument. */
    case GUMBO_NODE_DOCUMENT: {
        qWarning() << "Detected embedded document";
    }

    /** Element node.  v will be a GumboElement. */
    case GUMBO_NODE_ELEMENT: {
        auto const & element = node.v.element;

        TextFormatReset format_reset { &cursor };

        // qDebug() << "begin node(" << gumbo_normalized_tagname(element.tag) << ")";

        switch(element.tag) {

        // Stripped tags
        case GUMBO_TAG_STYLE:
        case GUMBO_TAG_SCRIPT:
            return;

        case GUMBO_TAG_NAV: {
            // TODO: Optionally strip navigation from sites
            if(true)
                return;
            break;
        }

        // Terminal tags
        case GUMBO_TAG_IMG: {
            // TODO: Insert link to image here
            cursor.insertText("[IMG]");
            return;
        }
        case GUMBO_TAG_SVG: {
            // TODO: Insert link to image here
            cursor.insertText("[SVG]");
            return;
        }
        case GUMBO_TAG_BUTTON: {
            // TODO: Insert link to image here
            cursor.insertText("[BUTTON]");
            return;
        }
        case GUMBO_TAG_INPUT: {
            // TODO: Insert link to image here
            cursor.insertText("[INPUT]");
            return;
        }

        // Paragraph-like elements:
        case GUMBO_TAG_DIV: // <div> is the same as <p> for us
        case GUMBO_TAG_P: {
            // cursor.insertBlock();
            break;
        }
        case GUMBO_TAG_H1: {
            // cursor.insertBlock();
            cursor.setBlockFormat(text_style.heading_format);
            cursor.setCharFormat(text_style.standard_h1);
            break;
        }
        case GUMBO_TAG_H2: {
            // cursor.insertBlock();
            cursor.setBlockFormat(text_style.heading_format);
            cursor.setCharFormat(text_style.standard_h2);

            break;
        }
        case GUMBO_TAG_H3: {
            // cursor.insertBlock();
            cursor.setBlockFormat(text_style.heading_format);
            cursor.setCharFormat(text_style.standard_h3);
            break;
        }

        case GUMBO_TAG_PRE: {
            // cursor.insertBlock();
            cursor.setBlockFormat(text_style.preformatted_format);
            cursor.setCharFormat(text_style.preformatted);
            break;
        }

        case GUMBO_TAG_OL:
        case GUMBO_TAG_UL: {
            // cursor.insertBlock();

            if(element.tag == GUMBO_TAG_OL) {
                auto fmt = text_style.list_format;
                fmt.setStyle(QTextListFormat::ListDecimal);
                fmt.setNumberPrefix("");
                fmt.setNumberSuffix(".");
                cursor.createList(fmt);
            }
            else {
                cursor.createList(text_style.list_format);
            }
            break;
        }
        case GUMBO_TAG_LI: {
            break;
        }
        case GUMBO_TAG_BLOCKQUOTE: {
            QTextTable *table = cursor.insertTable(1, 1,text_style.blockquote_tableformat);
            cursor.setBlockFormat(text_style.blockquote_format);
            QTextTableCell cell = table->cellAt(0, 0);
            cell.setFormat(text_style.blockquote);

            cursor.setCharFormat(text_style.blockquote);

            break;
        }

        // Text modification elements:
        case GUMBO_TAG_SPAN: {
            // This usually has a style change, but we ignore that completly
            break;
        }
        case GUMBO_TAG_BR: {
            cursor.insertText("\n");
            break;
        }
        case GUMBO_TAG_I: {
            auto fmt = cursor.charFormat();
            fmt.setFontItalic(true);
            cursor.setCharFormat(fmt);
            break;
        }
        case GUMBO_TAG_B: {
            auto fmt = cursor.charFormat();
            fmt.setFontWeight(QFont::Bold);
            cursor.setCharFormat(fmt);
            break;
        }
        case GUMBO_TAG_U: {
            auto fmt = cursor.charFormat();
            fmt.setFontUnderline(true);
            cursor.setCharFormat(fmt);
            break;
        }
        case GUMBO_TAG_A: {
            char const * anchor = getAttribute(element, "href");
            if(anchor == nullptr) {
                anchor = "#";
            }

            auto fmt = text_style.standard_link;
            fmt.setAnchor(true);
            fmt.setAnchorHref(QString::fromUtf8(anchor));
            cursor.setCharFormat(fmt);
            break;
        }
        default:
            qDebug() << "unhandled tag:" << gumbo_normalized_tagname(element.tag);
            break;
        }

        for (size_t i = 0; i < element.children.length; ++i) {
            GumboNode* child = (GumboNode*)element.children.data[i];
            renderRecursive(state, *child, nesting + 1);
        }

        switch(element.tag) {
        // case GUMBO_TAG_PRE: {
//            // Set the last line of the preformatted block to have
//            // standard line height.
//            QTextBlockFormat fmt = cursor.blockFormat();
//            fmt.setLineHeight(state.style->line_height_p, QTextBlockFormat::LineDistanceHeight);
//            cursor.movePosition(QTextCursor::PreviousBlock);
//            cursor.setBlockFormat(fmt);

//            cursor.movePosition(QTextCursor::NextBlock);
//            break;
//        }

        // Requires closing block
        case GUMBO_TAG_PRE:
        case GUMBO_TAG_P:
        case GUMBO_TAG_DIV:
        case GUMBO_TAG_H1:
        case GUMBO_TAG_H2:
        case GUMBO_TAG_H3:
            cursor.insertBlock();
            break;

        case GUMBO_TAG_OL:
        case GUMBO_TAG_UL:
            // cursor.insertBlock();
            break;

        case GUMBO_TAG_LI:
            // Terminate the <li> by pressing "enter"
            cursor.insertBlock();
            break;

        case GUMBO_TAG_BLOCKQUOTE:
            cursor.deletePreviousChar();
            cursor.movePosition(QTextCursor::NextBlock);
            break;

        default: break;
        }

        // qDebug() << "end node(" << gumbo_normalized_tagname(element.tag) << ")";

        break;
    }

    /** Text node.  v will be a GumboText. */
    case GUMBO_NODE_TEXT: {
        auto const & text = node.v.text;

        auto contents = QString::fromUtf8(text.text);
        // qDebug() << contents;

        QRegularExpression regex { "\\s+", QRegularExpression::DotMatchesEverythingOption };

        // TODO: This is not quite right, but QTextCursor::inserText
        // will insert spurious blocks when a "\n" is encountered.
        state.cursor.insertText(contents.replace(regex, " "));
        break;
    }

    /** CDATA node. v will be a GumboText. */
    case GUMBO_NODE_CDATA: {
        auto const & text = node.v.text;

        auto const contents = QString::fromUtf8(text.text);

        // TODO: This is not quite right, but QTextCursor::inserText
        // will insert spurious blocks when a "\n" is encountered.
        state.cursor.insertText(contents.trimmed());
        break;
    }

    /** Comment node.  v will be a GumboText, excluding comment delimiters. */
    case GUMBO_NODE_COMMENT: {
        // qDebug() << "comment(" << ")";
        break;
    }

    /** Text node, where all contents is whitespace.  v will be a GumboText. */
    case GUMBO_NODE_WHITESPACE: {
        // qDebug() << "whitespace(" << ")";
        break;
    }

    /** Template node.  This is separate from GUMBO_NODE_ELEMENT because many
     * client libraries will want to ignore the contents of template nodes, as
     * the spec suggests.  Recursing on GUMBO_NODE_ELEMENT will do the right thing
     * here, while clients that want to include template contents should also
     * check for GUMBO_NODE_TEMPLATE.  v will be a GumboElement.  */
    case GUMBO_NODE_TEMPLATE: {
        qDebug() << "template(" << "???" << ")";
        break;
    }
    }
}

std::unique_ptr<QTextDocument> HtmlRenderer::render(
        QByteArray const &input,
        QUrl const & root_url,
        DocumentStyle const & style,
        DocumentOutlineModel & outline,
        QString & page_title)
{
    std::unique_ptr<GumboOutput, decltype(&destroyGumboOutput)> gumbo_output {
        gumbo_parse_with_options(&gumbo_options, input.data(), input.length()),
        &destroyGumboOutput,
    };

    if(gumbo_output->errors.length > 0) {
        qDebug() << "Parsing the html document yielded" << gumbo_output->errors.length << "errors!";
    }

    if(gumbo_output->root->type != GUMBO_NODE_ELEMENT) {
        qWarning() << "html document has no proper root node!";
        return nullptr;
    }

    auto doc = std::make_unique<QTextDocument>();
    renderhelpers::setPageMargins(doc.get(), style.margin_h, style.margin_v);
    doc->setIndentWidth(style.indent_size);

    outline.beginBuild();

    // Find page title
    {
        const char* title = find_title(gumbo_output->root);
        if(title != nullptr) {
            page_title = QString::fromUtf8(title);
        }
    }

    {
        GumboVector const * const root_children = &gumbo_output->root->v.element.children;
        GumboNode* body = nullptr;
        for (size_t i = 0; i < root_children->length; ++i) {
            GumboNode* child = (GumboNode*)root_children->data[i];
            if (child->type == GUMBO_NODE_ELEMENT and child->v.element.tag == GUMBO_TAG_BODY) {
                body = child;
                break;
            }
        }
        if(body != nullptr)
        {
            RenderState state {
                QTextCursor { doc.get() },
                TextStyleInstance { style },
                root_url,
                &style,
                &outline,
            };

            state.cursor.setBlockFormat(state.text_style.standard_format);
            state.cursor.setCharFormat(state.text_style.standard);

            renderRecursive(state, *body);
        }
    }


    outline.endBuild();

    return doc;
}