tools/qdoc3/pagegenerator.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the tools applications of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 /*
       
    43   pagegenerator.cpp
       
    44 */
       
    45 
       
    46 #include <QtCore>
       
    47 #include <qfile.h>
       
    48 #include <qfileinfo.h>
       
    49 
       
    50 #include "pagegenerator.h"
       
    51 #include "tree.h"
       
    52 
       
    53 QT_BEGIN_NAMESPACE
       
    54 
       
    55 /*!
       
    56   Nothing to do in the constructor.
       
    57  */
       
    58 PageGenerator::PageGenerator()
       
    59 {
       
    60     // nothing.
       
    61 }
       
    62 
       
    63 /*!
       
    64   The destructor 
       
    65  */
       
    66 PageGenerator::~PageGenerator()
       
    67 {
       
    68     while (!outStreamStack.isEmpty())
       
    69 	endSubPage();
       
    70 }
       
    71 
       
    72 /*!
       
    73   This function is recursive.
       
    74  */
       
    75 void PageGenerator::generateTree(const Tree *tree, CodeMarker *marker)
       
    76 {
       
    77     generateInnerNode(tree->root(), marker);
       
    78 }
       
    79 
       
    80 QString PageGenerator::fileBase(const Node *node)
       
    81 {
       
    82     if (node->relates())
       
    83 	node = node->relates();
       
    84     else if (!node->isInnerNode()) {
       
    85 	node = node->parent();
       
    86 #ifdef QDOC_QML
       
    87         if (node->subType() == Node::QmlPropertyGroup) {
       
    88             node = node->parent();
       
    89         }
       
    90 #endif        
       
    91     }
       
    92 
       
    93     QString base = node->doc().baseName();
       
    94     if (!base.isEmpty())
       
    95         return base;
       
    96 
       
    97     const Node *p = node;
       
    98 
       
    99     forever {
       
   100         base.prepend(p->name());
       
   101 #ifdef QDOC_QML
       
   102         /*
       
   103           To avoid file name conflicts in the html directory,
       
   104           we prepend "qml-" to the file name of QML element doc
       
   105           files.
       
   106          */
       
   107         if ((p->subType() == Node::QmlClass) ||
       
   108             (p->subType() == Node::QmlPropertyGroup))
       
   109             base.prepend("qml-");
       
   110         else if ((p->type() == Node::QmlProperty) ||
       
   111                  (p->type() == Node::QmlSignal) ||
       
   112                  (p->type() == Node::QmlMethod))
       
   113             base.prepend("qml-");
       
   114 #endif        
       
   115         const Node *pp = p->parent();
       
   116         if (!pp || pp->name().isEmpty() || pp->type() == Node::Fake)
       
   117             break;
       
   118         base.prepend(QLatin1Char('-'));
       
   119         p = pp;
       
   120     }
       
   121     
       
   122     if (node->type() == Node::Fake) {
       
   123 #ifdef QDOC2_COMPAT
       
   124         if (base.endsWith(".html"))
       
   125             base.truncate(base.length() - 5);
       
   126 #endif
       
   127     }
       
   128 
       
   129     // the code below is effectively equivalent to:
       
   130     //   base.replace(QRegExp("[^A-Za-z0-9]+"), " ");
       
   131     //   base = base.trimmed();
       
   132     //   base.replace(" ", "-");
       
   133     //   base = base.toLower();
       
   134     // as this function accounted for ~8% of total running time
       
   135     // we optimize a bit...
       
   136 
       
   137     QString res;
       
   138     // +5 prevents realloc in fileName() below
       
   139     res.reserve(base.size() + 5);
       
   140     bool begun = false;
       
   141     for (int i = 0; i != base.size(); ++i) {
       
   142         QChar c = base.at(i);
       
   143         uint u = c.unicode();
       
   144         if (u >= 'A' && u <= 'Z')
       
   145             u -= 'A' - 'a';
       
   146         if ((u >= 'a' &&  u <= 'z') || (u >= '0' && u <= '9')) {
       
   147             res += QLatin1Char(u);
       
   148             begun = true;
       
   149         }
       
   150         else if (begun) {
       
   151             res += QLatin1Char('-');
       
   152             begun = false;
       
   153         }
       
   154     }
       
   155     while (res.endsWith(QLatin1Char('-')))
       
   156         res.chop(1);
       
   157     return res;
       
   158 }
       
   159 
       
   160 QString PageGenerator::fileName(const Node *node)
       
   161 {
       
   162     if (!node->url().isEmpty())
       
   163         return node->url();
       
   164 
       
   165     QString name = fileBase(node);
       
   166     name += QLatin1Char('.');
       
   167     name += fileExtension(node);
       
   168     return name;
       
   169 }
       
   170 
       
   171 QString PageGenerator::outFileName()
       
   172 {
       
   173     return QFileInfo(static_cast<QFile *>(out().device())->fileName()).fileName();
       
   174 }
       
   175 
       
   176 void PageGenerator::beginSubPage(const Location& location,
       
   177                                  const QString& fileName)
       
   178 {
       
   179     QFile *outFile = new QFile(outputDir() + "/" + fileName);
       
   180     if (!outFile->open(QFile::WriteOnly))
       
   181 	location.fatal(tr("Cannot open output file '%1'")
       
   182 			.arg(outFile->fileName()));
       
   183     QTextStream *out = new QTextStream(outFile);
       
   184     out->setCodec("ISO-8859-1");
       
   185     outStreamStack.push(out);
       
   186 }
       
   187 
       
   188 void PageGenerator::endSubPage()
       
   189 {
       
   190     outStreamStack.top()->flush();
       
   191     delete outStreamStack.top()->device();
       
   192     delete outStreamStack.pop();
       
   193 }
       
   194 
       
   195 QTextStream &PageGenerator::out()
       
   196 {
       
   197     return *outStreamStack.top();
       
   198 }
       
   199 
       
   200 /*!
       
   201   Recursive writing of html files from the root \a node.
       
   202  */
       
   203 void PageGenerator::generateInnerNode(const InnerNode *node,
       
   204                                       CodeMarker *marker)
       
   205 {
       
   206     if (!node->url().isNull())
       
   207         return;
       
   208 
       
   209     if (node->type() == Node::Fake) {
       
   210         const FakeNode *fakeNode = static_cast<const FakeNode *>(node);
       
   211         if (fakeNode->subType() == Node::ExternalPage)
       
   212             return;
       
   213 #ifdef QDOC_QML            
       
   214         if (fakeNode->subType() == Node::QmlPropertyGroup)
       
   215             return;
       
   216 #endif            
       
   217     }
       
   218 
       
   219     if (node->parent() != 0) {
       
   220 	beginSubPage(node->location(), fileName(node));
       
   221 	if (node->type() == Node::Namespace || node->type() == Node::Class) {
       
   222 	    generateClassLikeNode(node, marker);
       
   223 	}
       
   224         else if (node->type() == Node::Fake) {
       
   225 	    generateFakeNode(static_cast<const FakeNode *>(node), marker);
       
   226 	}
       
   227 	endSubPage();
       
   228     }
       
   229 
       
   230     NodeList::ConstIterator c = node->childNodes().begin();
       
   231     while (c != node->childNodes().end()) {
       
   232 	if ((*c)->isInnerNode() && (*c)->access() != Node::Private)
       
   233 	    generateInnerNode((const InnerNode *) *c, marker);
       
   234 	++c;
       
   235     }
       
   236 }
       
   237 
       
   238 QT_END_NAMESPACE