aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlls/qdochtmlparser.cpp
blob: bba18facb52d0a12e357e668f9443dff19f06ff1 (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include <qdochtmlparser_p.h>
#include <QtCore/qregularexpression.h>

QT_BEGIN_NAMESPACE

using namespace Qt::StringLiterals;

namespace {  //anonymous

// An emprical value to avoid too much content
static constexpr qsizetype firstIndexOfParagraphTag = 400;

// A paragraph can start with <p><i>, or <p><tt>
// We need smallest value to use QString::indexOf
static constexpr auto lengthOfSmallestOpeningTag = qsizetype(std::char_traits<char>::length("<p><i>"));
static constexpr auto lengthOfStartParagraphTag = qsizetype(std::char_traits<char>::length("<p>"));
static constexpr auto lengthOfEndParagraphTag = qsizetype(std::char_traits<char>::length("</p>"));
static constexpr auto lengthOfPeriod = qsizetype(std::char_traits<char>::length("."));

QString getContentsByMarks(const QString &html, QString startMark, QString endMark)
{
    startMark.prepend("$$$"_L1);
    endMark.prepend("<!-- @@@"_L1);

    QString contents;
    qsizetype start = html.indexOf(startMark);
    if (start != -1) {
        start = html.indexOf("-->"_L1, start);
        if (start != -1) {
            qsizetype end = html.indexOf(endMark, start);
            if (end != -1) {
                start += qsizetype(std::char_traits<char>::length("-->"));
                contents = html.mid(start, end - start);
            }
        }
    }
    return contents;
}


void stripAllHtml(QString *html)
{
    Q_ASSERT(html);
    html->remove(QRegularExpression("<.*?>"_L1));
}

/*! \internal
    \brief Process the string obtained from start mark to end mark.
    This is duplicated from QtC's Utils::HtmlExtractor, modified on top of it.
*/
void processOutput(QString *html)
{
    Q_ASSERT(html);
    if (html->isEmpty())
        return;

    // Do not write the first paragraph in case it has extra tags below.
    // <p><i>This is only used on the Maemo platform.</i></p>
    // or: <p><tt>This is used on Windows only.</tt></p>
    // or: <p>[Conditional]</p>
    const auto skipFirstParagraphIfNeeded = [html](qsizetype &index){
        const bool shouldSkipFirstParagraph = html->indexOf(QLatin1String("<p><i>")) == index ||
                html->indexOf(QLatin1String("<p><tt>")) == index ||
                html->indexOf(QLatin1String("<p>[Conditional]</p>")) == index;

        if (shouldSkipFirstParagraph)
            index = html->indexOf(QLatin1String("<p>"), index + lengthOfSmallestOpeningTag);
    };

    // Try to get the entire first paragraph, but if one is not found or if its opening
    // tag is not in the very beginning (using an empirical value as the limit)
    // the html is cleared out to avoid too much content.
    qsizetype index = html->indexOf(QLatin1String("<p>"));
    if (index != -1 && index < firstIndexOfParagraphTag) {
        skipFirstParagraphIfNeeded(index);
        index = html->indexOf(QLatin1String("</p>"), index + lengthOfStartParagraphTag);
        if (index != -1) {
            // Most paragraphs end with a period, but there are cases without punctuation
            // and cases like this: <p>This is a description. Example:</p>
            const auto period = html->lastIndexOf(QLatin1Char('.'), index);
            if (period != -1) {
                html->truncate(period + lengthOfPeriod);
                html->append(QLatin1String("</p>"));
            } else {
                html->truncate(index + lengthOfEndParagraphTag);
            }
        } else {
            html->clear();
        }
    } else {
        html->clear();
    }
}

}

QDocHtmlExtractor::QDocHtmlExtractor(const QString &code) : m_code{ code }
{
}

QString QDocHtmlExtractor::extract(const QDocHtmlExtractor::Element &element, ExtractionMode mode)
{
    QString result;
    switch (element.type) {
    case ElementType::QmlType:
        result = parseForQmlType(element.name, mode);
        break;

    case ElementType::QmlProperty:
        result = parseForQmlProperty(element.name, mode);
        break;
    case ElementType::QmlMethod:
    case ElementType::QmlSignal:
        result = parseForQmlMethodOrSignal(element.name, mode);
        break;
    default:
        return {};
    }

    stripAllHtml(&result);

    // Also remove leading and trailing whitespaces
    return result.trimmed();
}

QString QDocHtmlExtractor::parseForQmlType(const QString &element, ExtractionMode mode)
{
    QString result;
    // Get brief description
    if (mode == QDocHtmlExtractor::ExtractionMode::Simplified) {
        result = getContentsByMarks(m_code, element + "-brief"_L1 , element);
        // Remove More...
        if (!result.isEmpty()) {
            const auto tailToRemove = "More..."_L1;
            const auto lastIndex = result.lastIndexOf(tailToRemove);
            if (lastIndex != -1)
                result.remove(lastIndex, tailToRemove.length());
        }
    } else {
        result = getContentsByMarks(m_code, element + "-description"_L1, element);
        // Remove header
        if (!result.isEmpty()) {
            const auto headerToRemove = "Detailed Description"_L1;
            const auto firstIndex = result.indexOf(headerToRemove);
            if (firstIndex != -1)
                result.remove(firstIndex, headerToRemove.length());
        }
    }

    return result;
}

QString QDocHtmlExtractor::parseForQmlProperty(const QString &element, ExtractionMode mode)
{
    // Qt 5.15 way of finding properties in doc
    QString startMark = QString::fromLatin1("<a name=\"%1-prop\">").arg(element);
    qsizetype startIndex = m_code.indexOf(startMark);
    if (startIndex == -1) {
        // if not found, try Qt6
        startMark = QString::fromLatin1(
                            "<td class=\"tblQmlPropNode\"><p>\n<span class=\"name\">%1</span>")
                            .arg(element);
        startIndex = m_code.indexOf(startMark);
        if (startIndex == -1)
            return {};
    }

    QString contents = m_code.mid(startIndex + startMark.size());
    startIndex = contents.indexOf(QLatin1String("<div class=\"qmldoc\"><p>"));
    if (startIndex == -1)
        return {};

    contents = contents.mid(startIndex);
    if (mode == ExtractionMode::Simplified)
        processOutput(&contents);
    return contents;
}

QString QDocHtmlExtractor::parseForQmlMethodOrSignal(const QString &functionName, ExtractionMode mode)
{
    // the case with <!-- $$$childAt[overload1]$$$childAtrealreal -->
    QString mark = QString::fromLatin1("$$$%1[overload1]$$$%1").arg(functionName);
    qsizetype startIndex = m_code.indexOf(mark);
    if (startIndex != -1) {
        startIndex = m_code.indexOf("-->"_L1, startIndex + mark.length());
        if (startIndex == -1)
            return {};
    } else {
        // it could be part of the method list
        mark = QString::fromLatin1("<span class=\"name\">%1</span>")
                .arg(functionName);
        startIndex = m_code.indexOf(mark);
        if (startIndex != -1)
            startIndex += mark.length();
        else
            return {};
    }

    startIndex = m_code.indexOf(QLatin1String("<div class=\"qmldoc\"><p>"), startIndex);
    if (startIndex == -1)
        return {};

    QString endMark = QString::fromLatin1("<!-- @@@");
    qsizetype endIndex = m_code.indexOf(endMark, startIndex);
    QString contents = m_code.mid(startIndex, endIndex);
    if (mode == ExtractionMode::Simplified)
        processOutput(&contents);
    return contents;
}

QT_END_NAMESPACE