/**************************************************************************** ** ** 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://www.qt.io/licensing. For further information ** use the contact form at http://www.qt.io/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 or version 3 as published by the Free ** Software Foundation and appearing in the file LICENSE.LGPLv21 and ** LICENSE.LGPLv3 included in the packaging of this file. Please review the ** following information to ensure the GNU Lesser General Public License ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** 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 #include 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("More...")); 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::fromLatin1("%1").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("").arg(mark); int index = html.indexOf(startMark); if (index == -1) { startMark = QString::fromLatin1("").arg(mark); index = html.indexOf(startMark); } if (index == -1) return QString(); QString contents = html.mid(index + startMark.size()); index = contents.indexOf(QLatin1String("

")); 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("").arg(mark); int index = html.indexOf(startMark); if (index == -1) return QString(); QString contents = html.mid(index + startMark.size()); index = contents.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: //

This is only used on the Maemo platform.

// or:

This is used on Windows only.

// or:

[Conditional]

// include also the next paragraph. int index = html->indexOf(QLatin1String("

")); if (index != -1 && index < 400) { if (html->indexOf(QLatin1String("

")) == index || html->indexOf(QLatin1String("

")) == index || html->indexOf(QLatin1String("

[Conditional]

")) == index) index = html->indexOf(QLatin1String("

"), index + 6); // skip the first paragraph index = html->indexOf(QLatin1String("

"), index + 3); if (index != -1) { // Most paragraphs end with a period, but there are cases without punctuation // and cases like this:

This is a description. Example:

const int period = html->lastIndexOf(QLatin1Char('.'), index); if (period != -1) { html->truncate(period + 1); html->append(QLatin1String("

")); } 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("|"))); } void HtmlDocExtractor::stripLinks(QString *html) { html->remove(createMinimalExp(QLatin1String("|"))); } void HtmlDocExtractor::stripHorizontalLines(QString *html) { html->remove(createMinimalExp(QLatin1String(""))); } void HtmlDocExtractor::stripDivs(QString *html) { html->remove(createMinimalExp(QLatin1String("|
|"))); } 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("")); html->remove(QLatin1String("")); } void HtmlDocExtractor::stripImagens(QString *html) { html->remove(createMinimalExp(QLatin1String(""))); } void HtmlDocExtractor::stripBold(QString *html) { html->remove(QLatin1String("")); html->remove(QLatin1String("")); } void HtmlDocExtractor::stripEmptyParagraphs(QString *html) { html->remove(QLatin1String("

")); } void HtmlDocExtractor::replaceNonStyledHeadingsForBold(QString *html) { const QRegExp &hStart = createMinimalExp(QLatin1String("")); const QRegExp &hEnd = createMinimalExp(QLatin1String("")); html->replace(hStart, QLatin1String("

")); html->replace(hEnd, QLatin1String("

")); } void HtmlDocExtractor::replaceTablesForSimpleLines(QString *html) { html->replace(createMinimalExp(QLatin1String("(?:

)?")), QLatin1String("

")); html->replace(QLatin1String(""), QLatin1String("

")); html->remove(createMinimalExp(QLatin1String(""))); html->remove(QLatin1String("")); html->remove(createMinimalExp(QLatin1String(""))); html->remove(QLatin1String("")); html->remove(createMinimalExp(QLatin1String(".*"))); html->replace(QLatin1String(" remove(createMinimalExp(QLatin1String("

"))); html->remove(createMinimalExp(QLatin1String(""))); html->remove(createMinimalExp(QLatin1String("(?:

)?"))); html->replace(createMinimalExp(QLatin1String("")), QLatin1String("    ")); html->replace(QLatin1String(""), QLatin1String("
")); } void HtmlDocExtractor::replaceListsForSimpleLines(QString *html) { html->remove(createMinimalExp(QLatin1String("<(?:ul|ol).*>"))); html->remove(createMinimalExp(QLatin1String(""))); html->replace(QLatin1String("
  • "), QLatin1String("    ")); html->replace(QLatin1String("
  • "), QLatin1String("
    ")); }