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
|
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "htmldocextractor.h"
#include <QStringList>
#include <QRegExp>
using namespace Utils;
namespace {
QRegExp createMinimalExp(const QString &pattern) {
QRegExp exp(pattern);
exp.setMinimal(true);
return exp;
}
}
HtmlDocExtractor::HtmlDocExtractor() :
m_formatContents(true),
m_mode(FirstParagraph)
{}
void HtmlDocExtractor::setMode(Mode mode)
{ m_mode = mode; }
void HtmlDocExtractor::applyFormatting(const bool format)
{ m_formatContents = format; }
QString HtmlDocExtractor::getClassOrNamespaceBrief(const QString &html, const QString &mark) const
{
QString contents = getContentsByMarks(html, mark + QLatin1String("-brief"), mark);
if (!contents.isEmpty() && m_formatContents)
contents.remove(QLatin1String("<a href=\"#details\">More...</a>"));
processOutput(&contents);
return contents;
}
QString HtmlDocExtractor::getClassOrNamespaceDescription(const QString &html,
const QString &mark) const
{
if (m_mode == FirstParagraph)
return getClassOrNamespaceBrief(html, mark);
QString contents = getContentsByMarks(html, mark + QLatin1String("-description"), mark);
if (!contents.isEmpty() && m_formatContents)
contents.remove(QLatin1String("Detailed Description"));
processOutput(&contents);
return contents;
}
QString HtmlDocExtractor::getEnumDescription(const QString &html, const QString &mark) const
{
return getClassOrNamespaceMemberDescription(html, mark, mark);
}
QString HtmlDocExtractor::getTypedefDescription(const QString &html, const QString &mark) const
{
return getClassOrNamespaceMemberDescription(html, mark, mark);
}
QString HtmlDocExtractor::getMacroDescription(const QString &html,
const QString &mark) const
{
return getClassOrNamespaceMemberDescription(html, mark, mark);
}
QString HtmlDocExtractor::getFunctionDescription(const QString &html,
const QString &mark,
const bool mainOverload) const
{
QString cleanMark = mark;
QString startMark = mark;
const int parenthesis = mark.indexOf(QLatin1Char('('));
if (parenthesis != -1) {
startMark = mark.left(parenthesis);
cleanMark = startMark;
if (mainOverload) {
startMark.append(QLatin1String("[overload1]"));
} else {
QString complement = mark.right(mark.length() - parenthesis);
complement.remove(QRegExp(QLatin1String("[\\(\\), ]")));
startMark.append(complement);
}
}
QString contents = getClassOrNamespaceMemberDescription(html, startMark, cleanMark);
if (contents.isEmpty()) {
// Maybe this is a property function, which is documented differently. Besides
// setX/isX/hasX there are other (not so usual) names for them. A few examples of those:
// - toPlainText / Prop. plainText from QPlainTextEdit.
// - resize / Prop. size from QWidget.
// - move / Prop. pos from QWidget (nothing similar in the names in this case).
// So I try to find the link to this property in the list of properties, extract its
// anchor and then follow by the name found.
const QString &pattern =
QString(QLatin1String("<a href=\"[a-z\\.]+#([A-Za-z]+)-prop\">%1</a>")).arg(cleanMark);
QRegExp exp = createMinimalExp(pattern);
if (exp.indexIn(html) != -1) {
const QString &prop = exp.cap(1);
contents = getClassOrNamespaceMemberDescription(html,
prop + QLatin1String("-prop"),
prop);
}
}
return contents;
}
QString HtmlDocExtractor::getQmlComponentDescription(const QString &html, const QString &mark) const
{
return getClassOrNamespaceDescription(html, mark);
}
QString HtmlDocExtractor::getQmlPropertyDescription(const QString &html, const QString &mark) const
{
QString startMark = QString::fromLatin1("<a name=\"%1-prop\">").arg(mark);
int index = html.indexOf(startMark);
if (index == -1) {
startMark = QString::fromLatin1("<a name=\"%1-signal\">").arg(mark);
index = html.indexOf(startMark);
}
if (index == -1)
return QString();
QString contents = html.mid(index + startMark.size());
index = contents.indexOf(QLatin1String("<div class=\"qmldoc\"><p>"));
if (index == -1)
return QString();
contents = contents.mid(index);
processOutput(&contents);
return contents;
}
QString HtmlDocExtractor::getQMakeVariableOrFunctionDescription(const QString &html,
const QString &mark) const
{
const QString startMark = QString::fromLatin1("<a name=\"%1\"></a>").arg(mark);
int index = html.indexOf(startMark);
if (index == -1)
return QString();
QString contents = html.mid(index + startMark.size());
index = contents.indexOf(QLatin1String("<!-- @@@qmake"));
if (index == -1)
return QString();
contents = contents.left(index);
processOutput(&contents);
return contents;
}
QString HtmlDocExtractor::getQMakeFunctionId(const QString &html,
const QString &mark) const
{
const QString startMark = QString::fromLatin1("<a name=\"%1-").arg(mark);
const int startIndex = html.indexOf(startMark);
if (startIndex == -1)
return QString();
const int startKeyIndex = html.indexOf(mark, startIndex);
const QString endMark = QLatin1String("\"></a>");
const int endKeyIndex = html.indexOf(endMark, startKeyIndex);
if (endKeyIndex == -1)
return QString();
return html.mid(startKeyIndex, endKeyIndex - startKeyIndex);
}
QString HtmlDocExtractor::getClassOrNamespaceMemberDescription(const QString &html,
const QString &startMark,
const QString &endMark) const
{
QString contents = getContentsByMarks(html, startMark, endMark);
processOutput(&contents);
return contents;
}
QString HtmlDocExtractor::getContentsByMarks(const QString &html,
QString startMark,
QString endMark) const
{
startMark.prepend(QLatin1String("$$$"));
endMark.prepend(QLatin1String("<!-- @@@"));
QString contents;
int start = html.indexOf(startMark);
if (start != -1) {
start = html.indexOf(QLatin1String("-->"), start);
if (start != -1) {
int end = html.indexOf(endMark, start);
if (end != -1) {
start += 3;
contents = html.mid(start, end - start);
}
}
}
return contents;
}
void HtmlDocExtractor::processOutput(QString *html) const
{
if (html->isEmpty())
return;
if (m_mode == FirstParagraph) {
// 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 to avoid too much content. In case the first paragraph looks like:
// <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>
// include also the next paragraph.
int index = html->indexOf(QLatin1String("<p>"));
if (index != -1 && index < 400) {
if (html->indexOf(QLatin1String("<p><i>")) == index ||
html->indexOf(QLatin1String("<p><tt>")) == index ||
html->indexOf(QLatin1String("<p>[Conditional]</p>")) == index)
index = html->indexOf(QLatin1String("<p>"), index + 6); // skip the first paragraph
index = html->indexOf(QLatin1String("</p>"), index + 3);
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 int period = html->lastIndexOf(QLatin1Char('.'), index);
if (period != -1) {
html->truncate(period + 1);
html->append(QLatin1String("</p>"));
} else {
html->truncate(index + 4);
}
} else {
html->clear();
}
} else {
html->clear();
}
}
if (!html->isEmpty() && m_formatContents) {
stripBold(html);
replaceNonStyledHeadingsForBold(html);
replaceTablesForSimpleLines(html);
replaceListsForSimpleLines(html);
stripLinks(html);
stripHorizontalLines(html);
stripDivs(html);
stripTagsStyles(html);
stripHeadings(html);
stripImagens(html);
stripEmptyParagraphs(html);
}
}
void HtmlDocExtractor::stripAllHtml(QString *html)
{
html->remove(createMinimalExp(QLatin1String("<.*>")));
}
void HtmlDocExtractor::stripHeadings(QString *html)
{
html->remove(createMinimalExp(QLatin1String("<h\\d{1}.*>|</h\\d{1}>")));
}
void HtmlDocExtractor::stripLinks(QString *html)
{
html->remove(createMinimalExp(QLatin1String("<a\\s+.*>|</a>")));
}
void HtmlDocExtractor::stripHorizontalLines(QString *html)
{
html->remove(createMinimalExp(QLatin1String("<hr\\s+/>")));
}
void HtmlDocExtractor::stripDivs(QString *html)
{
html->remove(createMinimalExp(QLatin1String("<div\\s+.*>|</div>|<div\\s+.*/\\s*>")));
}
void HtmlDocExtractor::stripTagsStyles(QString *html)
{
const QRegExp &exp = createMinimalExp(QLatin1String("<(.*\\s+)class=\".*\">"));
html->replace(exp, QLatin1String("<\\1>"));
}
void HtmlDocExtractor::stripTeletypes(QString *html)
{
html->remove(QLatin1String("<tt>"));
html->remove(QLatin1String("</tt>"));
}
void HtmlDocExtractor::stripImagens(QString *html)
{
html->remove(createMinimalExp(QLatin1String("<img.*>")));
}
void HtmlDocExtractor::stripBold(QString *html)
{
html->remove(QLatin1String("<b>"));
html->remove(QLatin1String("</b>"));
}
void HtmlDocExtractor::stripEmptyParagraphs(QString *html)
{
html->remove(QLatin1String("<p></p>"));
}
void HtmlDocExtractor::replaceNonStyledHeadingsForBold(QString *html)
{
const QRegExp &hStart = createMinimalExp(QLatin1String("<h\\d{1}>"));
const QRegExp &hEnd = createMinimalExp(QLatin1String("</h\\d{1}>"));
html->replace(hStart, QLatin1String("<p><b>"));
html->replace(hEnd, QLatin1String("</b></p>"));
}
void HtmlDocExtractor::replaceTablesForSimpleLines(QString *html)
{
html->replace(createMinimalExp(QLatin1String("(?:<p>)?<table.*>")), QLatin1String("<p>"));
html->replace(QLatin1String("</table>"), QLatin1String("</p>"));
html->remove(createMinimalExp(QLatin1String("<thead.*>")));
html->remove(QLatin1String("</thead>"));
html->remove(createMinimalExp(QLatin1String("<tfoot.*>")));
html->remove(QLatin1String("</tfoot>"));
html->remove(createMinimalExp(QLatin1String("<tr.*><th.*>.*</th></tr>")));
html->replace(QLatin1String("</td><td"), QLatin1String("</td> <td"));
html->remove(createMinimalExp(QLatin1String("<td.*><p>")));
html->remove(createMinimalExp(QLatin1String("<td.*>")));
html->remove(createMinimalExp(QLatin1String("(?:</p>)?</td>")));
html->replace(createMinimalExp(QLatin1String("<tr.*>")),
QLatin1String(" "));
html->replace(QLatin1String("</tr>"), QLatin1String("<br />"));
}
void HtmlDocExtractor::replaceListsForSimpleLines(QString *html)
{
html->remove(createMinimalExp(QLatin1String("<(?:ul|ol).*>")));
html->remove(createMinimalExp(QLatin1String("</(?:ul|ol)>")));
html->replace(QLatin1String("<li>"), QLatin1String(" "));
html->replace(QLatin1String("</li>"), QLatin1String("<br />"));
}
|