util/tools/qdoc3/pagegenerator.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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     QString base = node->doc().baseName();
       
    93     if (!base.isEmpty())
       
    94         return base;
       
    95 
       
    96     const Node *p = node;
       
    97 
       
    98     forever {
       
    99         const Node *pp = p->parent();
       
   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             base.prepend("qml-");
       
   109         }
       
   110 #endif        
       
   111         if (!pp || pp->name().isEmpty() || pp->type() == Node::Fake)
       
   112             break;
       
   113         base.prepend(QLatin1Char('-'));
       
   114         p = pp;
       
   115     }
       
   116     
       
   117     if (node->type() == Node::Fake) {
       
   118 #ifdef QDOC2_COMPAT
       
   119         if (base.endsWith(".html"))
       
   120             base.truncate(base.length() - 5);
       
   121 #endif
       
   122     }
       
   123 
       
   124     // the code below is effectively equivalent to:
       
   125     //   base.replace(QRegExp("[^A-Za-z0-9]+"), " ");
       
   126     //   base = base.trimmed();
       
   127     //   base.replace(" ", "-");
       
   128     //   base = base.toLower();
       
   129     // as this function accounted for ~8% of total running time
       
   130     // we optimize a bit...
       
   131 
       
   132     QString res;
       
   133     // +5 prevents realloc in fileName() below
       
   134     res.reserve(base.size() + 5);
       
   135     bool begun = false;
       
   136     for (int i = 0; i != base.size(); ++i) {
       
   137         QChar c = base.at(i);
       
   138         uint u = c.unicode();
       
   139         if (u >= 'A' && u <= 'Z')
       
   140             u -= 'A' - 'a';
       
   141         if ((u >= 'a' &&  u <= 'z') || (u >= '0' && u <= '9')) {
       
   142             res += QLatin1Char(u);
       
   143             begun = true;
       
   144         }
       
   145         else if (begun) {
       
   146             res += QLatin1Char('-');
       
   147             begun = false;
       
   148         }
       
   149     }
       
   150     while (res.endsWith(QLatin1Char('-')))
       
   151         res.chop(1);
       
   152     return res;
       
   153 }
       
   154 
       
   155 QString PageGenerator::fileName(const Node *node)
       
   156 {
       
   157     if (!node->url().isEmpty())
       
   158         return node->url();
       
   159 
       
   160     QString name = fileBase(node);
       
   161     name += QLatin1Char('.');
       
   162     name += fileExtension(node);
       
   163     return name;
       
   164 }
       
   165 
       
   166 QString PageGenerator::outFileName()
       
   167 {
       
   168     return QFileInfo(static_cast<QFile *>(out().device())->fileName()).fileName();
       
   169 }
       
   170 
       
   171 void PageGenerator::beginSubPage(const Location& location,
       
   172                                  const QString& fileName)
       
   173 {
       
   174     QFile *outFile = new QFile(outputDir() + "/" + fileName);
       
   175     if (!outFile->open(QFile::WriteOnly))
       
   176 	location.fatal(tr("Cannot open output file '%1'")
       
   177 			.arg(outFile->fileName()));
       
   178     QTextStream *out = new QTextStream(outFile);
       
   179     out->setCodec("ISO-8859-1");
       
   180     outStreamStack.push(out);
       
   181 }
       
   182 
       
   183 void PageGenerator::endSubPage()
       
   184 {
       
   185     outStreamStack.top()->flush();
       
   186     delete outStreamStack.top()->device();
       
   187     delete outStreamStack.pop();
       
   188 }
       
   189 
       
   190 QTextStream &PageGenerator::out()
       
   191 {
       
   192     return *outStreamStack.top();
       
   193 }
       
   194 
       
   195 /*!
       
   196   Recursive writing of html files from the root \a node.
       
   197  */
       
   198 void PageGenerator::generateInnerNode(const InnerNode *node,
       
   199                                       CodeMarker *marker)
       
   200 {
       
   201     if (!node->url().isNull())
       
   202         return;
       
   203 
       
   204     if (node->type() == Node::Fake) {
       
   205         const FakeNode *fakeNode = static_cast<const FakeNode *>(node);
       
   206         if (fakeNode->subType() == Node::ExternalPage)
       
   207             return;
       
   208 #ifdef QDOC_QML            
       
   209         if (fakeNode->subType() == Node::QmlPropertyGroup)
       
   210             return;
       
   211 #endif            
       
   212     }
       
   213 
       
   214     if (node->parent() != 0) {
       
   215 	beginSubPage(node->location(), fileName(node));
       
   216 	if (node->type() == Node::Namespace || node->type() == Node::Class) {
       
   217 	    generateClassLikeNode(node, marker);
       
   218 	}
       
   219         else if (node->type() == Node::Fake) {
       
   220 	    generateFakeNode(static_cast<const FakeNode *>(node), marker);
       
   221 	}
       
   222 	endSubPage();
       
   223     }
       
   224 
       
   225     NodeList::ConstIterator c = node->childNodes().begin();
       
   226     while (c != node->childNodes().end()) {
       
   227 	if ((*c)->isInnerNode() && (*c)->access() != Node::Private)
       
   228 	    generateInnerNode((const InnerNode *) *c, marker);
       
   229 	++c;
       
   230     }
       
   231 }
       
   232 
       
   233 QT_END_NAMESPACE