summaryrefslogtreecommitdiffstats
path: root/htmlhelpparser.cpp
blob: 32985681a29327553f3733b3129998f2b706071f (plain)
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/****************************************************************************
 **
 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
 ** This file is part of the doxygen2qthelp project on Trolltech Labs.
 **
 ** This file may be used under the terms of the GNU General Public
 ** License version 2.0 or 3.0 as published by the Free Software Foundation
 ** and appearing in the file LICENSE.GPL included in the packaging of
 ** this file.  Please review the following information to ensure GNU
 ** General Public Licensing requirements will be met:
 ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
 ** http://www.gnu.org/copyleft/gpl.html.
 **
 ** If you are unsure which license is appropriate for your use, please
 ** contact the sales department at qt-sales@nokia.com.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/

#include "htmlhelpparser_p.h"
#include "simplehtmlparser_p.h"
#include "qhelpdatainterface_p.h"

#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QFile>
#include <QtCore/QHash>
#include <QtCore/QSet>

#ifdef HELP_LIB_DATA_DEBUG
# include <QDebug>
#endif

QT_BEGIN_NAMESPACE

class ContainerNode;
class LinkNode;

class HtmlHelpNode
{
public:
    virtual ~HtmlHelpNode() { }

    virtual bool isContainer() const = 0;
    virtual ContainerNode * toContainer() const = 0;
    virtual const LinkNode * toLink() const = 0;

#ifdef HELP_LIB_DATA_DEBUG
    virtual void dump(int level) const = 0;
#endif
};

class ContainerNode : public HtmlHelpNode
{
public:
    QList<HtmlHelpNode *> m_children;

    ~ContainerNode()
    {
        foreach(const HtmlHelpNode * node, m_children) {
            delete node;
        }
    }

    static void insertItem(QHelpDataIndexItem &item, QList<QHelpDataIndexItem> &dest,
        QSet<QHelpDataIndexItem> &pastEntries, QSet<QString> &pastIds);

    QHelpDataContentItem * convertAndStealChildren(
            QHelpDataContentItem * parent,
            const QString &name = QString(), const QString &href = QString());
    QList<QHelpDataIndexItem> convertAndStealChildren(int level,
            const QString &rootLabel, QSet<QHelpDataIndexItem> &pastEntries,
            QSet<QString> &pastIds);

    bool isContainer() const { return true; }
    ContainerNode * toContainer() const
    {
        return const_cast<ContainerNode *>(this);
    }
    const LinkNode * toLink() const { return NULL; }

#ifdef HELP_LIB_DATA_DEBUG
    void dump(int level = 0) const
    {
        QString indent;
        for (int i = 0; i < level; i++) {
            indent.append(" _");
        }
        foreach(const HtmlHelpNode * node, m_children) {
            Q_ASSERT(node != NULL);
            node->dump(level + 1);
        }
    }
#endif
};

class LinkNode : public HtmlHelpNode
{
public:
    QString m_name;
    QString m_subName;
    QString m_href;

    LinkNode(const QString &name, const QString &subName, const QString &href)
            : m_name(name), m_subName(subName), m_href(href) { }

    QHelpDataContentItem * convert(QHelpDataContentItem * parent) const;
    QHelpDataIndexItem convert() const;
    bool isContainer() const { return false; }
    ContainerNode * toContainer() const { return NULL; }
    const LinkNode * toLink() const { return this; }

#ifdef HELP_LIB_DATA_DEBUG
    void dump(int level = 0) const
    {
        QString indent;
        for (int i = 0; i < level; i++) {
            indent.append(" _");
        }
        qDebug() << indent << m_name << m_subName << m_href;
    }
#endif
};

QHelpDataContentItem * ContainerNode::convertAndStealChildren(
        QHelpDataContentItem * parent, const QString &name,
        const QString &href)
{
    QHelpDataContentItem * const res
            = new QHelpDataContentItem(parent, name, href);

    const int childCount = m_children.count();
    for (int i = 0; i < childCount; i++) {
        QHelpDataContentItem * item = NULL;
        HtmlHelpNode * const node = m_children[i];
        Q_ASSERT(node != NULL);
        if (node->isContainer()) {
            // No Name node before,
            item = node->toContainer()->convertAndStealChildren(res);
        } else {
            const LinkNode * const linkNode = node->toLink();
            if ((i < childCount - 1) && (m_children[i + 1]->isContainer())) {
                // Not last node, check successor
                HtmlHelpNode * const succ = m_children[i + 1];
                item = succ->toContainer()->convertAndStealChildren(
                        res, linkNode->m_name, linkNode->m_href);
                delete succ;
                // Skip container node
                i++;
            } else {
                // Successor (if existing) is a link node too
                // Delete link to the 'main page' so we don't have two of them
                if ((parent != NULL) || (i != 0)
                        || (linkNode->m_href != href)) {
                    if (!linkNode->m_href.isEmpty()) {
                        item = linkNode->convert(res);
                    }
                }
            }
        }

        delete node;
    }
    m_children.clear();

    return res;
}

static uint qHash(const QHelpDataIndexItem &key)
{
    return qHash(key.name) ^ qHash(key.reference);
}

/*static*/ void ContainerNode::insertItem(QHelpDataIndexItem &item,
        QList<QHelpDataIndexItem> &dest, QSet<QHelpDataIndexItem> &pastEntries,
        QSet<QString> &pastIds)
{
    const QString &id = item.identifier;
    const bool nameRefPairFound = pastEntries.contains(item);
    const bool idFound = pastIds.contains(id);
    if (!(nameRefPairFound && idFound)) {
        if (nameRefPairFound) {
            if (idFound) {
                // (1) Name with exact same link already there
                // (2) ID already taken
                //  \--> don't add
                Q_ASSERT(false);
            } else {
                // (1) Name with exact same link already there
                // (2) ID not taken
                //  \--> add as ("", id, ref)
                item.name = QString();
            }
        } else {
            if (idFound) {
                // (1) Name not linking to this reference yet
                // (2) ID already taken
                //  \--> add as (name, "", ref)
                item.identifier = QString();
            } else {
                // (1) Name not linking to this reference yet
                // (2) ID not taken
                //  \--> add unmodified
            }
        }
#ifdef HELP_LIB_DATA_DEBUG
        item.dump();
#endif
        pastEntries.insert(item);
        pastIds.insert(id);
        dest << item;
    }
}

QList<QHelpDataIndexItem> ContainerNode::convertAndStealChildren(
        int level, const QString &rootLabel,
        QSet<QHelpDataIndexItem> &pastEntries, QSet<QString> &pastIds)
{
    QList<QHelpDataIndexItem> res;
    QString workLabel(rootLabel);

    foreach (HtmlHelpNode * node, m_children) {
        if (node->isContainer()) {
            res += node->toContainer()->convertAndStealChildren(
                    level + 1, workLabel, pastEntries, pastIds);
        } else {
            QHelpDataIndexItem item = node->toLink()->convert();
            insertItem(item, res, pastEntries, pastIds);
            if (level == 0) {
                workLabel = item.name;
            } else {
                // -- Sample input index tree
                // clone {#1, #2}
                // * Chicken {#3}
                // * Salad {#4}
                //
                // Chicken {#3}
                // * clone {#1}
                //
                // Salad {#4}
                // * clone {#2}
                //
                // -- Created index entries
                // NAME    | ID             | REF
                // --------+----------------+--------
                // clone   | clone          | 1
                // clone   |                | 2
                // Chicken | Chicken        | 3
                //         | clone::Chicken | 3
                //         | Chicken::clone | 1
                // Salad   | Salad          | 4
                //         | clone::Salad   | 4
                //         | Salad::clone   | 2
                const QString id = rootLabel + QLatin1String("::") + item.name;
                QHelpDataIndexItem extraItem = QHelpDataIndexItem(QString(), id, item.reference);
                insertItem(extraItem, res, pastEntries, pastIds);
            }
        }
        delete node;
    }

    m_children.clear();
    return res;
}

QHelpDataContentItem * LinkNode::convert(QHelpDataContentItem * parent) const
{
    return new QHelpDataContentItem(parent, m_name, m_href);
}

QHelpDataIndexItem LinkNode::convert() const
{
    return QHelpDataIndexItem(m_name, m_name, m_href);
}

HtmlHelpParser::HtmlHelpParser()
        : m_state(WAIT_NAME_OR_LOCAL),
        m_tree(new ContainerNode)
{
    m_nodeStack.push(m_tree);
}

HtmlHelpParser::~HtmlHelpParser()
{
    delete m_tree;
}

void HtmlHelpParser::parseFile(const QString &fileName)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        return;
    }

    QString content;
    while (!file.atEnd()) {
        content.append(file.readLine());
    }
    file.close();

    parseString(content);
}

void HtmlHelpParser::parseString(const QString &text)
{
    SimpleHtmlParser parser;
    parser.parse(text, this);
}

const QList<QHelpDataIndexItem> HtmlHelpParser::stealIndex()
{
    QSet<QHelpDataIndexItem> noDupEntries;
    QSet<QString> noDupIds;
    return m_tree->convertAndStealChildren(0, QString(),
            noDupEntries, noDupIds);
}

const QList<QHelpDataContentItem*> HtmlHelpParser::stealToc(
        const QString &rootName, const QString &rootHref)
{
    Q_ASSERT(m_tree != NULL);
    QHelpDataContentItem * const convertedTree
            = m_tree->convertAndStealChildren(NULL, rootName, rootHref);
    Q_ASSERT(convertedTree != NULL);
#ifdef HELP_LIB_DATA_DEBUG
    convertedTree->dump();
#endif
    QList<QHelpDataContentItem*> res;
    res.append(convertedTree);
    return res;
}

void HtmlHelpParser::onTagOpen(const QString &tagName,
        const QStringList &attributes)
{
#ifdef HELP_LIB_DATA_DEBUG
    qDebug() << "OPEN" << tagName << "ATTS" << attributes;
#endif
    if (tagName == QString("ul")) {
        ContainerNode * const top = m_nodeStack.top();
        Q_ASSERT(top != NULL);
        if (!((top == m_tree) && (m_tree->m_children.isEmpty()))) {
            ContainerNode * const node = new ContainerNode;
            Q_ASSERT(node != NULL);
            top->m_children.append(node);
            m_nodeStack.push(node);
        }
    } else if (tagName == QString("param")) {
        const QString name = findAttribute(attributes, "name");
        if (name.isEmpty()) {
            return;
        }
        const QString value = findAttribute(attributes, "value");
        if (value.isEmpty()) {
            return;
        }

        if (name == QString("Name")) {
            switch (m_state) {
            case WAIT_NAME_OR_LOCAL:
                // First entry is a name, nothing fixed yet
                m_name = value;
                m_state = WAIT_NAME_NAME_OR_NAME_LOCAL;
                break;

            case WAIT_NAME_NAME_OR_NAME_LOCAL:
                // Second entry is a name too -> must be a list
                m_subName = value;
                m_state = WAIT_NAME_NAME_LOCAL;
                break;

            case WAIT_NAME_NAME_LOCAL_NAME:
                m_subName = value;
                m_state = WAIT_NAME_NAME_LOCAL;
                break;

            case WAIT_LOCAL_NAME:
                { ContainerNode * const top = m_nodeStack.top();
                Q_ASSERT(top != NULL);
                top->m_children.append(new LinkNode(value, "", m_href)); }
                m_state = WAIT_NAME_OR_LOCAL;
                break;

            default:
                ; // NOOP
            }
        } else if (name == QString("Local")) {
            switch (m_state) {
            case WAIT_NAME_OR_LOCAL:
                // First entry is a local -> must be a single link, name follows
                m_href = value;
                m_state = WAIT_LOCAL_NAME;
                break;

            case WAIT_NAME_NAME_OR_NAME_LOCAL:
                // Second entry is a href -> must be a single link
                { ContainerNode * const top = m_nodeStack.top();
                Q_ASSERT(top != NULL);
                top->m_children.append(new LinkNode(m_name, "", value)); }

                m_state = WAIT_NAME_OR_LOCAL;
                break;

            case WAIT_NAME_NAME_LOCAL:
                { ContainerNode * const top = m_nodeStack.top();
                Q_ASSERT(top != NULL);
                top->m_children.append(new LinkNode(m_name, m_subName, value)); }

                m_state = WAIT_NAME_NAME_LOCAL_NAME;
                break;

            default:
                ; // NOOP
            }
        }
    }
}

void HtmlHelpParser::onTagClose(const QString &tagName)
{
#ifdef HELP_LIB_DATA_DEBUG
    qDebug() << "CLOSE" << tagName;
#endif
    if (tagName == QString("ul")) {
        if (m_nodeStack.size() > 1) {
            m_nodeStack.pop();
        }
    } else if (tagName == QString("object")) {
        switch (m_state) {
        case WAIT_NAME_NAME_OR_NAME_LOCAL:
            // Name only, no href
            { ContainerNode * const top = m_nodeStack.top();
            Q_ASSERT(top != NULL);
            top->m_children.append(new LinkNode(m_name, "", "")); }
            break;

        default:
            ; // NOOP
        }

        m_state = WAIT_NAME_OR_LOCAL;
        m_name.clear();
        m_subName.clear();
    }
}

void HtmlHelpParser::onTextChunk(const QString & /*text*/)
{
    // NOOP
}

void HtmlHelpParser::onStop()
{
    m_nodeStack.clear();
#ifdef HELP_LIB_DATA_DEBUG
    m_tree->dump();
#endif
}

/*static*/ QString HtmlHelpParser::findAttribute(const QStringList &list,
        const QString &key)
{
    const int count = list.count();
    for (int i = 0; i < count - 1; i += 2) {
        if (list.at(i) == key) {
            return list.at(i + 1);
        }
    }
    return QString();
}

QT_END_NAMESPACE