source: trunk/tools/qdoc3/pagegenerator.cpp@ 494

Last change on this file since 494 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 6.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
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
53QT_BEGIN_NAMESPACE
54
55/*!
56 Nothing to do in the constructor.
57 */
58PageGenerator::PageGenerator()
59{
60 // nothing.
61}
62
63/*!
64 The destructor
65 */
66PageGenerator::~PageGenerator()
67{
68 while (!outStreamStack.isEmpty())
69 endSubPage();
70}
71
72/*!
73 This function is recursive.
74 */
75void PageGenerator::generateTree(const Tree *tree, CodeMarker *marker)
76{
77 generateInnerNode(tree->root(), marker);
78}
79
80QString PageGenerator::fileBase(const Node *node)
81{
82 if (node->relates())
83 node = node->relates();
84 else if (!node->isInnerNode())
85 node = node->parent();
86
87 QString base = node->doc().baseName();
88 if (!base.isEmpty())
89 return base;
90
91 const Node *p = node;
92
93 forever {
94 base.prepend(p->name());
95 const Node *pp = p->parent();
96 if (!pp || pp->name().isEmpty() || pp->type() == Node::Fake)
97 break;
98 base.prepend(QLatin1Char('-'));
99 p = pp;
100 }
101
102 if (node->type() == Node::Fake) {
103#ifdef QDOC2_COMPAT
104 if (base.endsWith(".html"))
105 base.truncate(base.length() - 5);
106#endif
107 }
108
109 // the code below is effectively equivalent to:
110 // base.replace(QRegExp("[^A-Za-z0-9]+"), " ");
111 // base = base.trimmed();
112 // base.replace(" ", "-");
113 // base = base.toLower();
114 // as this function accounted for ~8% of total running time
115 // we optimize a bit...
116
117 QString res;
118 // +5 prevents realloc in fileName() below
119 res.reserve(base.size() + 5);
120 bool begun = false;
121 for (int i = 0; i != base.size(); ++i) {
122 QChar c = base.at(i);
123 uint u = c.unicode();
124 if (u >= 'A' && u <= 'Z')
125 u -= 'A' - 'a';
126 if ((u >= 'a' && u <= 'z') || (u >= '0' && u <= '9')) {
127 res += QLatin1Char(u);
128 begun = true;
129 } else if (begun) {
130 res += QLatin1Char('-');
131 begun = false;
132 }
133 }
134 while (res.endsWith(QLatin1Char('-')))
135 res.chop(1);
136 return res;
137}
138
139QString PageGenerator::fileName(const Node *node)
140{
141 if (!node->url().isEmpty())
142 return node->url();
143
144 QString name = fileBase(node);
145 name += QLatin1Char('.');
146 name += fileExtension(node);
147 return name;
148}
149
150QString PageGenerator::outFileName()
151{
152 return QFileInfo(static_cast<QFile *>(out().device())->fileName()).fileName();
153}
154
155void PageGenerator::beginSubPage(const Location& location,
156 const QString& fileName)
157{
158 QFile *outFile = new QFile(outputDir() + "/" + fileName);
159 if (!outFile->open(QFile::WriteOnly))
160 location.fatal(tr("Cannot open output file '%1'")
161 .arg(outFile->fileName()));
162 QTextStream *out = new QTextStream(outFile);
163 out->setCodec("ISO-8859-1");
164 outStreamStack.push(out);
165}
166
167void PageGenerator::endSubPage()
168{
169 outStreamStack.top()->flush();
170 delete outStreamStack.top()->device();
171 delete outStreamStack.pop();
172}
173
174QTextStream &PageGenerator::out()
175{
176 return *outStreamStack.top();
177}
178
179/*!
180 Recursive writing of html files from the root \a node.
181 */
182void PageGenerator::generateInnerNode(const InnerNode *node,
183 CodeMarker *marker)
184{
185 if (!node->url().isNull())
186 return;
187
188 if (node->type() == Node::Fake) {
189 const FakeNode *fakeNode = static_cast<const FakeNode *>(node);
190 if (fakeNode->subType() == FakeNode::ExternalPage)
191 return;
192 }
193
194 if (node->parent() != 0) {
195 beginSubPage(node->location(), fileName(node));
196 if (node->type() == Node::Namespace || node->type() == Node::Class) {
197 generateClassLikeNode(node, marker);
198 }
199 else if (node->type() == Node::Fake) {
200 const FakeNode* fakeNode = static_cast<const FakeNode *>(node);
201#ifdef QDOC_QML
202 if (fakeNode->subType() == FakeNode::QmlClass) {
203 //qDebug() << "FILENAME:" << fileName(node);
204 }
205#endif
206 generateFakeNode(static_cast<const FakeNode *>(node), marker);
207 }
208 endSubPage();
209 }
210
211 NodeList::ConstIterator c = node->childNodes().begin();
212 while (c != node->childNodes().end()) {
213 if ((*c)->isInnerNode() && (*c)->access() != Node::Private)
214 generateInnerNode((const InnerNode *) *c, marker);
215 ++c;
216 }
217}
218
219QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.