Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qdochtmlparser.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <qdochtmlparser_p.h>
5#include <QtCore/qregularexpression.h>
6
8
9using namespace Qt::StringLiterals;
10
11// An emprical value to avoid too much content
12static constexpr qsizetype firstIndexOfParagraphTag = 400;
13
14// A paragraph can start with <p><i>, or <p><tt>
15// We need smallest value to use QString::indexOf
16static constexpr auto lengthOfSmallestOpeningTag = qsizetype(std::char_traits<char>::length("<p><i>"));
17static constexpr auto lengthOfStartParagraphTag = qsizetype(std::char_traits<char>::length("<p>"));
18static constexpr auto lengthOfEndParagraphTag = qsizetype(std::char_traits<char>::length("</p>"));
19static constexpr auto lengthOfPeriod = qsizetype(std::char_traits<char>::length("."));
20
21static QString getContentsByMarks(const QString &html, QString startMark, QString endMark)
22{
23 startMark.prepend("$$$"_L1);
24 endMark.prepend("<!-- @@@"_L1);
25
27 qsizetype start = html.indexOf(startMark);
28 if (start != -1) {
29 start = html.indexOf("-->"_L1, start);
30 if (start != -1) {
31 qsizetype end = html.indexOf(endMark, start);
32 if (end != -1) {
33 start += qsizetype(std::char_traits<char>::length("-->"));
34 contents = html.mid(start, end - start);
35 }
36 }
37 }
38 return contents;
39}
40
41
42static void stripAllHtml(QString *html)
43{
44 Q_ASSERT(html);
45 html->remove(QRegularExpression("<.*?>"_L1));
46}
47
52static void processOutput(QString *html)
53{
54 Q_ASSERT(html);
55 if (html->isEmpty())
56 return;
57
58 // Do not write the first paragraph in case it has extra tags below.
59 // <p><i>This is only used on the Maemo platform.</i></p>
60 // or: <p><tt>This is used on Windows only.</tt></p>
61 // or: <p>[Conditional]</p>
62 const auto skipFirstParagraphIfNeeded = [html](qsizetype &index){
63 const bool shouldSkipFirstParagraph = html->indexOf(QLatin1String("<p><i>")) == index ||
64 html->indexOf(QLatin1String("<p><tt>")) == index ||
65 html->indexOf(QLatin1String("<p>[Conditional]</p>")) == index;
66
67 if (shouldSkipFirstParagraph)
69 };
70
71 // Try to get the entire first paragraph, but if one is not found or if its opening
72 // tag is not in the very beginning (using an empirical value as the limit)
73 // the html is cleared out to avoid too much content.
74 qsizetype index = html->indexOf(QLatin1String("<p>"));
75 if (index != -1 && index < firstIndexOfParagraphTag) {
76 skipFirstParagraphIfNeeded(index);
78 if (index != -1) {
79 // Most paragraphs end with a period, but there are cases without punctuation
80 // and cases like this: <p>This is a description. Example:</p>
81 const auto period = html->lastIndexOf(QLatin1Char('.'), index);
82 if (period != -1) {
83 html->truncate(period + lengthOfPeriod);
84 html->append(QLatin1String("</p>"));
85 } else {
87 }
88 } else {
89 html->clear();
90 }
91 } else {
92 html->clear();
93 }
94}
95
97{
98public:
99 QString extract(const QString &code, const QString &keyword, ExtractionMode mode) override;
100};
101
103{
104public:
105 QString extract(const QString &code, const QString &keyword, ExtractionMode mode) override;
106};
107
109{
110public:
111 QString extract(const QString &code, const QString &keyword, ExtractionMode mode) override;
112};
113
115{
117 // Get brief description
119 result = getContentsByMarks(code, element + "-brief"_L1 , element);
120 // Remove More...
121 if (!result.isEmpty()) {
122 const auto tailToRemove = "More..."_L1;
123 const auto lastIndex = result.lastIndexOf(tailToRemove);
124 if (lastIndex != -1)
125 result.remove(lastIndex, tailToRemove.length());
126 }
127 } else {
128 result = getContentsByMarks(code, element + "-description"_L1, element);
129 // Remove header
130 if (!result.isEmpty()) {
131 const auto headerToRemove = "Detailed Description"_L1;
132 const auto firstIndex = result.indexOf(headerToRemove);
133 if (firstIndex != -1)
134 result.remove(firstIndex, headerToRemove.length());
135 }
136 }
137
139 return result.trimmed();
140}
141
143{
144 // Qt 5.15 way of finding properties in doc
145 QString startMark = QString::fromLatin1("<a name=\"%1-prop\">").arg(keyword);
146 qsizetype startIndex = code.indexOf(startMark);
147 if (startIndex == -1) {
148 // if not found, try Qt6
149 startMark = QString::fromLatin1(
150 "<td class=\"tblQmlPropNode\"><p>\n<span class=\"name\">%1</span>")
151 .arg(keyword);
152 startIndex = code.indexOf(startMark);
153 if (startIndex == -1)
154 return {};
155 }
156
157 QString contents = code.mid(startIndex + startMark.size());
158 startIndex = contents.indexOf(QLatin1String("<div class=\"qmldoc\"><p>"));
159 if (startIndex == -1)
160 return {};
161
162 contents = contents.mid(startIndex);
166 return contents.trimmed();
167}
168
170{
171 // the case with <!-- $$$childAt[overload1]$$$childAtrealreal -->
172 QString mark = QString::fromLatin1("$$$%1[overload1]$$$%1").arg(keyword);
173 qsizetype startIndex = code.indexOf(mark);
174 if (startIndex != -1) {
175 startIndex = code.indexOf("-->"_L1, startIndex + mark.length());
176 if (startIndex == -1)
177 return {};
178 } else {
179 // it could be part of the method list
180 mark = QString::fromLatin1("<span class=\"name\">%1</span>")
181 .arg(keyword);
182 startIndex = code.indexOf(mark);
183 if (startIndex != -1)
184 startIndex += mark.length();
185 else
186 return {};
187 }
188
189 startIndex = code.indexOf(QLatin1String("<div class=\"qmldoc\"><p>"), startIndex);
190 if (startIndex == -1)
191 return {};
192
193 QString endMark = QString::fromLatin1("<!-- @@@");
194 qsizetype endIndex = code.indexOf(endMark, startIndex);
195 QString contents = code.mid(startIndex, endIndex);
199 return contents.trimmed();
200}
201
203{
204 using namespace QQmlJS::Dom;
205 switch (domType) {
206 case DomType::QmlObject:
207 m_extractor = std::make_unique<ExtractQmlType>();
208 break;
209 case DomType::PropertyDefinition:
210 m_extractor = std::make_unique<ExtractQmlProperty>();
211 break;
212 case DomType::MethodInfo:
213 m_extractor = std::make_unique<ExtractQmlMethodOrSignal>();
214 break;
215 default:
216 break;
217 }
218}
219
221{
222 Q_ASSERT(m_extractor);
223 return m_extractor->extract(code, keyword, mode);
224}
225
ExtractDocumentation(QQmlJS::Dom::DomType domType)
QString execute(const QString &code, const QString &keyword, HtmlExtractor::ExtractionMode mode)
QString extract(const QString &code, const QString &keyword, ExtractionMode mode) override
QString extract(const QString &code, const QString &keyword, ExtractionMode mode) override
QString extract(const QString &code, const QString &keyword, ExtractionMode mode) override
\inmodule QtCore \reentrant
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
qsizetype indexOf(QLatin1StringView s, qsizetype from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition qstring.cpp:4517
qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
Definition qstring.h:296
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
void truncate(qsizetype pos)
Truncates the string at the given position index.
Definition qstring.cpp:6319
QString mid(qsizetype position, qsizetype n=-1) const &
Definition qstring.cpp:5300
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1252
QString & append(QChar c)
Definition qstring.cpp:3252
QString & remove(qsizetype i, qsizetype len)
Removes n characters from the string, starting at the given position index, and returns a reference t...
Definition qstring.cpp:3466
QString & prepend(QChar c)
Definition qstring.h:478
qsizetype length() const noexcept
Returns the number of characters in this string.
Definition qstring.h:191
Combined button and popup list for selecting options.
static constexpr auto lengthOfSmallestOpeningTag
static constexpr auto lengthOfEndParagraphTag
static constexpr auto lengthOfStartParagraphTag
static constexpr auto lengthOfPeriod
static constexpr qsizetype firstIndexOfParagraphTag
static void processOutput(QString *html)
Process the string obtained from start mark to end mark. This is duplicated from QtC's Utils::HtmlExt...
static void stripAllHtml(QString *html)
static QString getContentsByMarks(const QString &html, QString startMark, QString endMark)
GLenum mode
GLuint index
[2]
GLuint GLuint end
GLuint start
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
ptrdiff_t qsizetype
Definition qtypes.h:165
\inmodule QtCore \reentrant
Definition qchar.h:18