Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
helpprojectwriter.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5
6#include "access.h"
7#include "aggregate.h"
8#include "atom.h"
9#include "classnode.h"
10#include "collectionnode.h"
11#include "config.h"
12#include "enumnode.h"
13#include "functionnode.h"
14#include "genustypes.h"
15#include "htmlgenerator.h"
17#include "location.h"
18#include "namespacenode.h"
19#include "node.h"
20#include "nodecontext.h"
23#include "qdocdatabase.h"
24#include "typedefnode.h"
25
26#include <QtCore/qhash.h>
27
29
30using namespace Qt::StringLiterals;
31
32HelpProjectWriter::HelpProjectWriter(const QString &defaultFileName, Generator *g)
33{
34 reset(defaultFileName, g);
35}
36
37void HelpProjectWriter::reset(const QString &defaultFileName, Generator *g)
38{
39 m_projects.clear();
40 m_gen = g;
41 /*
42 Get the pointer to the singleton for the qdoc database and
43 store it locally. This replaces all the local accesses to
44 the node tree, which are now private.
45 */
47
48 // The output directory should already have been checked by the calling
49 // generator.
50 Config &config = Config::instance();
51 m_outputDir = config.getOutputDir();
52
53 const QStringList names{config.get(CONFIG_QHP + Config::dot + "projects").asStringList()};
54
55 for (const auto &projectName : names) {
56 HelpProject project;
57 project.m_name = projectName;
58
59 QString prefix = CONFIG_QHP + Config::dot + projectName + Config::dot;
60 project.m_helpNamespace = config.get(prefix + "namespace").asString();
61 project.m_virtualFolder = config.get(prefix + "virtualFolder").asString();
62 project.m_version = config.get(CONFIG_VERSION).asString();
63 project.m_fileName = config.get(prefix + "file").asString();
64 if (project.m_fileName.isEmpty())
65 project.m_fileName = defaultFileName;
66 project.m_extraFiles = config.get(prefix + "extraFiles").asStringSet();
67 project.m_extraFiles += config.get(CONFIG_QHP + Config::dot + "extraFiles").asStringSet();
68 project.m_indexTitle = config.get(prefix + "indexTitle").asString();
69 project.m_indexRoot = config.get(prefix + "indexRoot").asString();
70 project.m_filterAttributes = config.get(prefix + "filterAttributes").asStringSet();
71 project.m_includeIndexNodes = config.get(prefix + "includeIndexNodes").asBool();
72 const QSet<QString> customFilterNames = config.subVars(prefix + "customFilters");
73 for (const auto &filterName : customFilterNames) {
74 QString name{config.get(prefix + "customFilters" + Config::dot + filterName
75 + Config::dot + "name").asString()};
76 project.m_customFilters[name] =
77 config.get(prefix + "customFilters" + Config::dot + filterName
78 + Config::dot + "filterAttributes").asStringSet();
79 }
80
81 const auto excludedPrefixes = config.get(prefix + "excluded").asStringSet();
82 for (auto name : excludedPrefixes)
83 project.m_excluded.insert(name.replace(QLatin1Char('\\'), QLatin1Char('/')));
84
85 const auto subprojectPrefixes{config.get(prefix + "subprojects").asStringList()};
86 for (const auto &name : subprojectPrefixes) {
87 SubProject subproject;
88 QString subprefix = prefix + "subprojects" + Config::dot + name + Config::dot;
89 subproject.m_title = config.get(subprefix + "title").asString();
90 if (subproject.m_title.isEmpty())
91 continue;
92 subproject.m_indexTitle = config.get(subprefix + "indexTitle").asString();
93 subproject.m_sortPages = config.get(subprefix + "sortPages").asBool();
94 subproject.m_type = config.get(subprefix + "type").asString();
95 readSelectors(subproject, config.get(subprefix + "selectors").asStringList());
96 subprefix.chop(1);
97 subproject.m_prefix = std::move(subprefix); // Stored for error reporting purposes
98 project.m_subprojects.append(subproject);
99 }
100
101 if (project.m_subprojects.isEmpty()) {
102 SubProject subproject;
103 readSelectors(subproject, config.get(prefix + "selectors").asStringList());
104 project.m_subprojects.insert(0, subproject);
105 }
106
107 m_projects.append(project);
108 }
109}
110
111void HelpProjectWriter::readSelectors(SubProject &subproject, const QStringList &selectors)
112{
113 QHash<QString, NodeType> typeHash;
114 typeHash["namespace"] = NodeType::Namespace;
115 typeHash["class"] = NodeType::Class;
116 typeHash["struct"] = NodeType::Struct;
117 typeHash["union"] = NodeType::Union;
118 typeHash["header"] = NodeType::HeaderFile;
119 typeHash["headerfile"] = NodeType::HeaderFile;
120 typeHash["doc"] = NodeType::Page; // Unused (supported but ignored as a prefix)
121 typeHash["fake"] = NodeType::Page; // Unused (supported but ignored as a prefix)
122 typeHash["page"] = NodeType::Page;
123 typeHash["enum"] = NodeType::Enum;
124 typeHash["example"] = NodeType::Example;
125 typeHash["externalpage"] = NodeType::ExternalPage;
126 typeHash["typedef"] = NodeType::Typedef;
127 typeHash["typealias"] = NodeType::TypeAlias;
128 typeHash["function"] = NodeType::Function;
129 typeHash["property"] = NodeType::Property;
130 typeHash["variable"] = NodeType::Variable;
131 typeHash["group"] = NodeType::Group;
132 typeHash["module"] = NodeType::Module;
133 typeHash["none"] = NodeType::NoType;
134 typeHash["qmlmodule"] = NodeType::QmlModule;
135 typeHash["qmlproperty"] = NodeType::QmlProperty;
136 typeHash["qmlclass"] = NodeType::QmlType; // Legacy alias for 'qmltype'
137 typeHash["qmltype"] = NodeType::QmlType;
138 typeHash["qmlbasictype"] = NodeType::QmlValueType; // Legacy alias for 'qmlvaluetype'
139 typeHash["qmlvaluetype"] = NodeType::QmlValueType;
140
141 for (const QString &selector : selectors) {
142 QStringList pieces = selector.split(QLatin1Char(':'));
143 // Remove doc: or fake: prefix
144 if (pieces.size() > 1 && typeHash.value(pieces[0].toLower()) == NodeType::Page)
145 pieces.takeFirst();
146
147 QString typeName = pieces.takeFirst().toLower();
148 if (!typeHash.contains(typeName))
149 continue;
150
151 subproject.m_selectors << static_cast<unsigned char>(typeHash.value(typeName));
152 // QDoc treats all C++ class types as classes; select them all with 'class'
153 if (typeHash.value(typeName) == NodeType::Class) {
154 subproject.m_selectors << static_cast<unsigned char>(NodeType::Struct)
155 << static_cast<unsigned char>(NodeType::Union);
156 }
157 // Likewise, 'qmltype' selects QML value types as well
158 if (typeHash.value(typeName) == NodeType::QmlType)
159 subproject.m_selectors << static_cast<unsigned char>(NodeType::QmlValueType);
160 if (!pieces.isEmpty()) {
161 pieces = pieces[0].split(QLatin1Char(','));
162 for (const auto &piece : std::as_const(pieces)) {
163 if (typeHash[typeName] == NodeType::Group
164 || typeHash[typeName] == NodeType::Module
165 || typeHash[typeName] == NodeType::QmlModule) {
166 subproject.m_groups << piece.toLower();
167 }
168 }
169 }
170 }
171}
172
173void HelpProjectWriter::addExtraFile(const QString &file)
174{
175 for (HelpProject &project : m_projects)
176 project.m_extraFiles.insert(file);
177}
178
179Keyword HelpProjectWriter::keywordDetails(const Node *node) const
180{
181 QString ref = m_gen->fullDocumentLocation(node);
182
183 if (node->parent() && !node->parent()->name().isEmpty()) {
184 QString name = (node->isEnumType() || node->isTypedef())
185 ? node->parent()->name()+"::"+node->name()
186 : node->name();
187 QString id = (!node->isRelatedNonmember())
188 ? node->parent()->name()+"::"+node->name()
189 : node->name();
190 return Keyword(name, id, ref);
191 } else if (node->isQmlType()) {
192 const QString &name = node->name();
193 QString moduleName = node->logicalModuleName();
194 QStringList ids("QML." + name);
195 if (!moduleName.isEmpty()) {
196 QString majorVersion = node->logicalModule()
197 ? node->logicalModule()->logicalModuleVersion().split('.')[0]
198 : QString();
199 ids << "QML." + moduleName + majorVersion + "." + name;
200 }
201 return Keyword(name, ids, ref);
202 } else if (node->isQmlModule()) {
203 const QLatin1Char delim('.');
204 QStringList parts = node->logicalModuleName().split(delim) << "QML";
205 std::reverse(parts.begin(), parts.end());
206 return Keyword(node->logicalModuleName(), parts.join(delim), ref);
207 } else if (node->isTextPageNode()) {
208 const auto *pageNode = static_cast<const PageNode *>(node);
209 return Keyword(pageNode->fullTitle(), pageNode->fullTitle(), ref);
210 } else {
211 return Keyword(node->name(), node->name(), ref);
212 }
213}
214
215bool HelpProjectWriter::generateSection(HelpProject &project, QXmlStreamWriter & /* writer */,
216 const Node *node)
217{
218 if (!node->url().isEmpty() && !(project.m_includeIndexNodes && !node->url().startsWith("http")))
219 return false;
220
221 // Process (members of) unseen group nodes, i.e. nodes that use \ingroup <group_name> where
222 // \group group_name itself is not documented.
223 bool unseenGroup{node->isGroup() && !node->wasSeen()};
224
225 const InclusionPolicy policy = Config::instance().createInclusionPolicy();
226 const NodeContext context = node->createContext();
227 if ((!InclusionFilter::isIncluded(policy, context) || node->isDontDocument()) && !unseenGroup)
228 return false;
229
230 if (node->name().isEmpty())
231 return true;
232
233 QString docPath = node->doc().location().filePath();
234 if (!docPath.isEmpty() && project.m_excluded.contains(docPath))
235 return false;
236
237 QString objName = node->isTextPageNode() ? node->fullTitle() : node->fullDocumentName();
238 // Only add nodes to the set for each subproject if they match a selector.
239 // Those that match will be listed in the table of contents.
240
241 auto acceptNode = [&](SubProject &subproject, const QString &name, const Node *n) {
242 subproject.m_nodes.insert(name, n);
243 project.markPagePresent(m_gen->fullDocumentLocation(n));
244 };
245
246 for (auto &subproject : project.m_subprojects) {
247 // No selectors: accept all nodes.
248 if (subproject.m_selectors.isEmpty()) {
249 acceptNode(subproject, objName, node);
250 } else if (subproject.m_selectors.contains(static_cast<unsigned char>(node->nodeType()))) {
251 // Add all group members for '[group|module|qmlmodule]:name' selector
252 if (node->isCollectionNode()) {
253 if (subproject.m_groups.contains(node->name().toLower())) {
254 const auto *cn = static_cast<const CollectionNode *>(node);
255 const auto members = cn->members();
256 for (const Node *m : members) {
257 if (!m->isInAPI())
258 continue;
259 QString memberName =
260 m->isTextPageNode() ? m->fullTitle() : m->fullDocumentName();
261 acceptNode(subproject, memberName, m);
262 }
263 continue;
264 } else if (!subproject.m_groups.isEmpty()) {
265 continue; // Node does not represent specified group(s)
266 }
267 } else if (node->isTextPageNode()) {
268 if (node->isExternalPage() || node->fullTitle().isEmpty())
269 continue;
270 }
271 acceptNode(subproject, objName, node);
272 }
273 }
274
275 auto appendDocKeywords = [&](const Node *n) {
277 return;
278 for (const auto *kw : n->doc().keywords()) {
279 if (!kw->string().isEmpty()) {
280 QStringList ref_parts = m_gen->fullDocumentLocation(n).split('#'_L1);
281 // Use keyword's custom anchor if it has one
282 if (kw->count() > 1) {
283 if (ref_parts.count() > 1)
284 ref_parts.pop_back();
285 ref_parts << kw->string(1);
286 }
287 project.m_keywords.append(Keyword(kw->string(), kw->string(),
288 ref_parts.join('#'_L1)));
289 }
290 }
291 };
292 // Unseen group nodes require no further processing as they have no documentation
293 if (unseenGroup)
294 return false;
295
296 switch (node->nodeType()) {
297
298 case NodeType::Class:
299 case NodeType::Struct:
300 case NodeType::Union:
301 project.m_keywords.append(keywordDetails(node));
302 break;
305 appendDocKeywords(node);
306 project.m_keywords.append(keywordDetails(node));
307 break;
308
310 project.m_keywords.append(keywordDetails(node));
311 break;
312
313 case NodeType::Enum:
315 project.m_keywords.append(keywordDetails(node));
316 {
317 const auto *enumNode = static_cast<const EnumNode *>(node);
318 const auto items = enumNode->items();
319 for (const auto &item : items) {
320 if (enumNode->itemAccess(item.name()) == Access::Private)
321 continue;
322
323 QString name;
324 QString id;
325 if (!node->parent()->name().isEmpty()) {
326 name = id = node->parent()->name() + "::" + item.name();
327 } else {
328 name = id = item.name();
329 }
330 QString ref = m_gen->fullDocumentLocation(node);
331 project.m_keywords.append(Keyword(std::move(name), std::move(id), std::move(ref)));
332 }
333 }
334 break;
335
336 case NodeType::Group:
337 case NodeType::Module:
338 case NodeType::QmlModule: {
339 if (!node->fullTitle().isEmpty()) {
340 appendDocKeywords(node);
341 project.m_keywords.append(keywordDetails(node));
342 }
343 } break;
344
347 project.m_keywords.append(keywordDetails(node));
348 break;
349
350 case NodeType::Function: {
351 const auto *funcNode = static_cast<const FunctionNode *>(node);
352
353 /*
354 QML methods, signals, and signal handlers used to be node types,
355 but now they are Function nodes with a Metaness value that specifies
356 what kind of function they are, QmlSignal, QmlMethod, etc.
357 */
358 if (funcNode->isQmlNode()) {
359 project.m_keywords.append(keywordDetails(node));
360 break;
361 }
362 // Only insert keywords for non-constructors. Constructors are covered
363 // by the classes themselves.
364
365 if (!funcNode->isSomeCtor())
366 project.m_keywords.append(keywordDetails(node));
367
368 // Insert member status flags into the entries for the parent
369 // node of the function, or the node it is related to.
370 // Since parent nodes should have already been inserted into
371 // the set of files, we only need to ensure that related nodes
372 // are inserted.
373
374 if (node->parent())
375 project.m_memberStatus[node->parent()].insert(node->status());
376 } break;
378 case NodeType::Typedef: {
379 const auto *typedefNode = static_cast<const TypedefNode *>(node);
380 Keyword typedefDetails = keywordDetails(node);
381 const EnumNode *enumNode = typedefNode->associatedEnum();
382 // Use the location of any associated enum node in preference
383 // to that of the typedef.
384 if (enumNode)
385 typedefDetails.m_ref = m_gen->fullDocumentLocation(enumNode);
386
387 project.m_keywords.append(typedefDetails);
388 } break;
389
390 case NodeType::Variable: {
391 project.m_keywords.append(keywordDetails(node));
392 } break;
393
394 // Page nodes (such as manual pages) contain subtypes, titles and other
395 // attributes.
396 case NodeType::Page: {
397 if (!node->fullTitle().isEmpty()) {
398 appendDocKeywords(node);
399 project.m_keywords.append(keywordDetails(node));
400 }
401 break;
402 }
403 default:;
404 }
405
406 // Add all images referenced in the page to the set of files to include.
407 const Atom *atom = node->doc().body().firstAtom();
408 while (atom) {
409 if (atom->type() == Atom::Image || atom->type() == Atom::InlineImage) {
410 QStringList pieces = atom->string().split('/'_L1);
411 project.m_extraFiles.insert("%1/%2"_L1.arg(m_gen->imagesOutputDir(), pieces.last()));
412 }
413 atom = atom->next();
414 }
415
416 return true;
417}
418
419void HelpProjectWriter::generateSections(HelpProject &project, QXmlStreamWriter &writer,
420 const Node *node)
421{
422 /*
423 Don't include index nodes in the help file.
424 */
425 if (node->isIndexNode())
426 return;
427 if (!generateSection(project, writer, node))
428 return;
429
430 if (node->isAggregate()) {
431 const auto *aggregate = static_cast<const Aggregate *>(node);
432
433 // Ensure that we don't visit nodes more than once.
434 NodeList childSet;
435 NodeList children = aggregate->childNodes();
436 std::sort(children.begin(), children.end(), Node::nodeNameLessThan);
437 for (auto *child : children) {
438 // Skip related non-members adopted by some other aggregate
439 if (child->parent() != aggregate)
440 continue;
441 // Process unseen group nodes (even though they're marked internal)
442 if (child->isGroup() && !child->wasSeen()) {
443 childSet << child;
444 continue;
445 }
446 if (child->isIndexNode())
447 continue;
448
449 const InclusionPolicy policy = Config::instance().createInclusionPolicy();
450 const NodeContext context = child->createContext();
451 if (!InclusionFilter::isIncluded(policy, context))
452 continue;
453
454 if (child->isTextPageNode()) {
455 if (!childSet.contains(child))
456 childSet << child;
457 } else {
458 // Store member status of children
459 project.m_memberStatus[node].insert(child->status());
460 if (child->isFunction() && static_cast<const FunctionNode *>(child)->isOverload())
461 continue;
462 if (!childSet.contains(child))
463 childSet << child;
464 }
465 }
466 for (const auto *child : std::as_const(childSet))
467 generateSections(project, writer, child);
468 }
469}
470
472{
473 // Warn if .qhp configuration was expected but not provided
474 if (auto &config = Config::instance(); m_projects.isEmpty() && config.get(CONFIG_QHP).asBool()) {
475 config.location().warning(u"Documentation configuration for '%1' doesn't define a help project (qhp)"_s
476 .arg(config.get(CONFIG_PROJECT).asString()));
477 }
478 if (m_projects.isEmpty())
479 return;
480
481 // A topic needs to be reachable from *some* help project; collect the
482 // union of the structures before reporting the ones left out.
483 QSet<QString> pagesInHelp;
484 for (HelpProject &project : m_projects) {
485 generateProject(project);
486 pagesInHelp.unite(project.m_structurePages);
487 }
488
489 // An empty structure means the projects themselves are misconfigured (for
490 // example, an indexRoot that matches no page); warning per topic is noise.
491 if (pagesInHelp.isEmpty())
492 return;
493
494 // Topics tagged with `\meta {toc} {nonessential}` are allowed to be left out.
495 auto isNonEssential = [](const PageNode *page) {
496 const auto *metaTags = page->doc().metaTagMap();
497 return metaTags && metaTags->values(u"toc"_s).contains(u"nonessential"_s);
498 };
499
500 for (const auto *page : m_gen->generatedPages()) {
501 // A proxy page lists members that this module contributes to a type
502 // documented in another module, and is linked to from that type's page.
503 if (page->isProxyNode())
504 continue;
505 // A namespace documented in another module gets a page listing only the
506 // members this module contributes; that page links to the full namespace.
507 if (page->isNamespace() && static_cast<const NamespaceNode *>(page)->docNode())
508 continue;
509 if (!pagesInHelp.contains(m_gen->fullDocumentLocation(page)) && !isNonEssential(page))
510 page->doc().location().warning(
511 u"Topic missing from generated Qt Help structure."_s,
512 u"%1 does not define a qhp subproject that includes it."_s.arg(
513 Config::instance().location().fileName()));
514 }
515}
516
517void HelpProjectWriter::writeSection(QXmlStreamWriter &writer, const QString &path,
518 const QString &value)
519{
520 writer.writeStartElement(QStringLiteral("section"));
521 writer.writeAttribute(QStringLiteral("ref"), path);
522 writer.writeAttribute(QStringLiteral("title"), value);
523 writer.writeEndElement(); // section
524}
525
526/*!
527 Write subsections for all members, compatibility members and obsolete members.
528 */
529void HelpProjectWriter::addMembers(HelpProject &project, QXmlStreamWriter &writer, const Node *node)
530{
531 QString href = m_gen->fullDocumentLocation(node);
532 href = href.left(href.size() - 5);
533 if (href.isEmpty())
534 return;
535
536 bool derivedClass = false;
537 if (node->isClassNode())
538 derivedClass = !(static_cast<const ClassNode *>(node)->baseClasses().isEmpty());
539
540 // Do not generate a 'List of all members' for namespaces or header files,
541 // but always generate it for derived classes and QML types (but not QML value types)
542 if (!node->isNamespace() && !node->isHeader() && !node->isQmlBasicType() && !node->isWrapper()
543 && (derivedClass || node->isQmlType() || !project.m_memberStatus[node].isEmpty())) {
544 QString membersPath = href + QStringLiteral("-members.html");
545 writeSection(writer, membersPath, QStringLiteral("List of all members"));
546 }
547 if (project.m_memberStatus[node].contains(Status::Deprecated)) {
548 QString obsoletePath = href + QStringLiteral("-obsolete.html");
549 writeSection(writer, obsoletePath, QStringLiteral("Obsolete members"));
550 }
551}
552
553void HelpProjectWriter::writeNode(HelpProject &project, QXmlStreamWriter &writer, const Node *node)
554{
555 QString href = m_gen->fullDocumentLocation(node);
556 // Not necessarily accepted by a selector; the 'nextpage' walk in
557 // generateProject() reaches nodes that generateSection() never saw.
558 project.markPagePresent(href);
559 QString objName = node->name();
560
561 switch (node->nodeType()) {
562
563 case NodeType::Class:
564 case NodeType::Struct:
565 case NodeType::Union:
568 QString typeStr = NodeText::nodeTypeDescription(node);
569 if (!typeStr.isEmpty())
570 typeStr[0] = typeStr[0].toTitleCase();
571 writer.writeStartElement("section");
572 writer.writeAttribute("ref", href);
573 if (node->parent() && !node->parent()->name().isEmpty())
574 writer.writeAttribute("title",
575 QStringLiteral("%1::%2 %3 Reference")
576 .arg(node->parent()->name(), objName, typeStr));
577 else
578 writer.writeAttribute("title", QStringLiteral("%1 %2 Reference").arg(objName, typeStr));
579
580 addMembers(project, writer, node);
581 writer.writeEndElement(); // section
582 } break;
583
584 case NodeType::Namespace:
585 writeSection(writer, href, "%1 Namespace Reference"_L1.arg(objName));
586 break;
587
590 case NodeType::Page:
591 case NodeType::Group:
592 case NodeType::Module:
593 case NodeType::QmlModule: {
594 writer.writeStartElement("section");
595 writer.writeAttribute("ref", href);
596 writer.writeAttribute("title", node->fullTitle());
597 if (node->nodeType() == NodeType::HeaderFile)
598 addMembers(project, writer, node);
599 writer.writeEndElement(); // section
600 } break;
601 default:;
602 }
603}
604
605void HelpProjectWriter::generateProject(HelpProject &project)
606{
607 const Node *rootNode;
608
609 // Restrict searching only to the local (primary) tree
610 QList<Tree *> searchOrder = m_qdb->searchOrder();
612
613 if (!project.m_indexRoot.isEmpty())
614 rootNode = m_qdb->findPageNodeByTitle(project.m_indexRoot);
615 else
616 rootNode = m_qdb->primaryTreeRoot();
617
618 if (rootNode == nullptr)
619 return;
620
621 project.m_structurePages.clear();
622 project.m_keywords.clear();
623
624 const OutputDirectory outputDir =
625 OutputDirectory::ensure(m_outputDir, Location());
626
627 QFile file(outputDir.absoluteFilePath(project.m_fileName));
628 if (!file.open(QFile::WriteOnly)) {
629 Location().error(u"Cannot open '%1' for writing"_s.arg(file.fileName()));
630 return;
631 }
632
633 QXmlStreamWriter writer(&file);
634 writer.setAutoFormatting(true);
635 writer.writeStartDocument();
636 writer.writeStartElement("QtHelpProject");
637 writer.writeAttribute("version", "1.0");
638
639 // Write metaData, virtualFolder and namespace elements.
640 writer.writeTextElement("namespace", project.m_helpNamespace);
641 writer.writeTextElement("virtualFolder", project.m_virtualFolder);
642 writer.writeStartElement("metaData");
643 writer.writeAttribute("name", "version");
644 writer.writeAttribute("value", project.m_version);
645 writer.writeEndElement();
646
647 // Write customFilter elements.
648 for (auto it = project.m_customFilters.constBegin(); it != project.m_customFilters.constEnd();
649 ++it) {
650 writer.writeStartElement("customFilter");
651 writer.writeAttribute("name", it.key());
652 QStringList sortedAttributes = it.value().values();
653 sortedAttributes.sort();
654 for (const auto &filter : std::as_const(sortedAttributes))
655 writer.writeTextElement("filterAttribute", filter);
656 writer.writeEndElement(); // customFilter
657 }
658
659 // Start the filterSection.
660 writer.writeStartElement("filterSection");
661
662 // Write filterAttribute elements.
663 QStringList sortedFilterAttributes = project.m_filterAttributes.values();
664 sortedFilterAttributes.sort();
665 for (const auto &filterName : std::as_const(sortedFilterAttributes))
666 writer.writeTextElement("filterAttribute", filterName);
667
668 writer.writeStartElement("toc");
669 writer.writeStartElement("section");
670 const Node *node = m_qdb->findPageNodeByTitle(project.m_indexTitle);
671 if (!node)
672 node = m_qdb->findNodeByNameAndType(QStringList(project.m_indexTitle), &Node::isPageNode);
673 if (!node)
674 node = m_qdb->findNodeByNameAndType(QStringList("index.html"), &Node::isPageNode);
675 QString indexPath;
676 if (node)
677 indexPath = m_gen->fullDocumentLocation(node);
678 else
679 indexPath = "index.html";
680 writer.writeAttribute("ref", indexPath);
681 writer.writeAttribute("title", project.m_indexTitle);
682 project.markPagePresent(indexPath);
683
684 generateSections(project, writer, rootNode);
685
686 for (int i = 0; i < project.m_subprojects.size(); i++) {
687 SubProject subproject = project.m_subprojects[i];
688
689 if (subproject.m_type == QLatin1String("manual")) {
690
691 const Node *indexPage = m_qdb->findNodeForTarget(subproject.m_indexTitle, nullptr);
692 if (indexPage) {
693 Text indexBody = indexPage->doc().body();
694 const Atom *atom = indexBody.firstAtom();
695 QStack<int> sectionStack;
696 bool inItem = false;
697
698 while (atom) {
699 switch (atom->type()) {
700 case Atom::ListLeft:
701 sectionStack.push(0);
702 break;
703 case Atom::ListRight:
704 if (sectionStack.pop() > 0)
705 writer.writeEndElement(); // section
706 break;
708 inItem = true;
709 break;
711 inItem = false;
712 break;
713 case Atom::Link:
714 if (inItem) {
715 // Omit external link targets
716 const Node *page = m_qdb->findNodeForTarget(atom->string(), nullptr);
717 if (!page || page->isExternalPage())
718 break;
719
720 if (sectionStack.top() > 0)
721 writer.writeEndElement(); // section
722
723 writer.writeStartElement("section");
724 QString indexPath = m_gen->fullDocumentLocation(page);
725 writer.writeAttribute("ref", indexPath);
726 project.markPagePresent(indexPath);
727 writer.writeAttribute("title", atom->linkText());
728
729 sectionStack.top() += 1;
730 }
731 break;
734 if (const auto *cn = m_qdb->getCollectionNode(atom->string(), NodeType::Group)) {
735 const auto sortOrder{Generator::sortOrder(atom->strings().last())};
736 NodeList members{cn->members()};
737 // Drop non-page nodes and index nodes so that we do not go outside of
738 // this documentation set.
739 members.erase(std::remove_if(members.begin(), members.end(),
740 [](const Node *n) {
742 }), members.end());
743 if (members.isEmpty())
744 break;
745
746 if (sortOrder == Qt::DescendingOrder)
747 std::sort(members.rbegin(), members.rend(), Node::nodeSortKeyOrNameLessThan);
748 else
749 std::sort(members.begin(), members.end(), Node::nodeSortKeyOrNameLessThan);
750
751 for (const auto *m : members) {
752 writer.writeStartElement("section");
753 const QString memberHref = m_gen->fullDocumentLocation(m);
754 writer.writeAttribute("ref", memberHref);
755 project.markPagePresent(memberHref);
756 writer.writeAttribute("title", m->title());
757 writer.writeEndElement();
758 }
759 }
760 break;
761 default:;
762 }
763
764 if (atom == indexBody.lastAtom())
765 break;
766 atom = atom->next();
767 }
768 } else
769 Config::instance().location().warning(
770 "Failed to find %1.indexTitle '%2'"_L1.arg(subproject.m_prefix, subproject.m_indexTitle));
771
772 } else {
773
774 writer.writeStartElement("section");
775 const Node *indexNode = m_qdb->findNodeForTarget(subproject.m_indexTitle, nullptr);
776 QString indexPath = m_gen->fullDocumentLocation(indexNode);
777 if (indexPath.isEmpty() && !subproject.m_indexTitle.isEmpty())
778 Config::instance().location().warning(
779 "Failed to find %1.indexTitle '%2'"_L1.arg(subproject.m_prefix, subproject.m_indexTitle));
780 writer.writeAttribute("ref", indexPath);
781 project.markPagePresent(indexPath);
782 writer.writeAttribute("title", subproject.m_title);
783
784 if (subproject.m_sortPages) {
785 QStringList titles = subproject.m_nodes.keys();
786 titles.sort();
787 for (const auto &title : std::as_const(titles)) {
788 writeNode(project, writer, subproject.m_nodes[title]);
789 }
790 } else {
791 // Find a contents node and navigate from there, using the NextLink values.
792 QSet<QString> visited;
793 bool contentsFound = false;
794 for (const auto *node : std::as_const(subproject.m_nodes)) {
795 QString nextTitle = node->links().value(Node::NextLink).first;
796 if (!nextTitle.isEmpty()
797 && node->links().value(Node::ContentsLink).first.isEmpty()) {
798
799 const Node *nextPage = m_qdb->findNodeForTarget(nextTitle, nullptr);
800
801 // Write the contents node.
802 writeNode(project, writer, node);
803 contentsFound = true;
804
805 while (nextPage) {
806 writeNode(project, writer, nextPage);
807 nextTitle = nextPage->links().value(Node::NextLink).first;
808 if (nextTitle.isEmpty() || visited.contains(nextTitle))
809 break;
810 nextPage = m_qdb->findNodeForTarget(nextTitle, nullptr);
811 visited.insert(nextTitle);
812 }
813 break;
814 }
815 }
816 // No contents/nextpage links found, write all nodes unsorted
817 if (!contentsFound) {
818 QList<const Node *> subnodes = subproject.m_nodes.values();
819
820 std::sort(subnodes.begin(), subnodes.end(), Node::nodeNameLessThan);
821
822 for (const auto *node : std::as_const(subnodes))
823 writeNode(project, writer, node);
824 }
825 }
826
827 writer.writeEndElement(); // section
828 }
829 }
830
831 // Restore original search order
832 m_qdb->setSearchOrder(searchOrder);
833
834 writer.writeEndElement(); // section
835 writer.writeEndElement(); // toc
836
837 writer.writeStartElement("keywords");
838 std::sort(project.m_keywords.begin(), project.m_keywords.end());
839 for (const auto &k : std::as_const(project.m_keywords)) {
840 for (const auto &id : std::as_const(k.m_ids)) {
841 writer.writeStartElement("keyword");
842 writer.writeAttribute("name", k.m_name);
843 writer.writeAttribute("id", id);
844 writer.writeAttribute("ref", k.m_ref);
845 writer.writeEndElement(); //keyword
846 }
847 }
848 writer.writeEndElement(); // keywords
849
850 writer.writeStartElement("files");
851
852 // The list of files to write is the union of generated files and
853 // other files (images and extras) included in the project
854 QSet<QString> files =
855 QSet<QString>(m_gen->outputFileNames().cbegin(), m_gen->outputFileNames().cend());
856 files.unite(QSet<QString>(m_gen->exampleImageFileNames().cbegin(),
857 m_gen->exampleImageFileNames().cend()));
858 files.unite(project.m_extraFiles);
859 QStringList sortedFiles = files.values();
860 sortedFiles.sort();
861 for (const auto &usedFile : std::as_const(sortedFiles)) {
862 if (!usedFile.isEmpty())
863 writer.writeTextElement("file", usedFile);
864 }
865 writer.writeEndElement(); // files
866
867 writer.writeEndElement(); // filterSection
868 writer.writeEndElement(); // QtHelpProject
869 writer.writeEndDocument();
870 file.close();
871}
872
873QT_END_NAMESPACE
The Atom class is the fundamental unit for representing documents internally.
Definition atom.h:19
AtomType type() const
Return the type of this atom.
Definition atom.h:146
@ GeneratedList
Definition atom.h:54
@ InlineImage
Definition atom.h:60
@ AnnotatedList
Definition atom.h:22
@ ListItemRight
Definition atom.h:72
@ Image
Definition atom.h:56
@ ListItemLeft
Definition atom.h:71
@ ListLeft
Definition atom.h:67
@ ListRight
Definition atom.h:73
@ Link
Definition atom.h:65
const Atom * next() const
Return the next atom in the atom list.
Definition atom.h:143
The Config class contains the configuration variables for controlling how qdoc produces documentation...
Definition config.h:95
const Location & location() const
Returns the starting location of a qdoc comment.
Definition doc.cpp:89
const Text & body() const
Definition doc.cpp:114
bool isEmpty() const
Definition doc.cpp:109
HelpProjectWriter(const QString &defaultFileName, Generator *g)
void addExtraFile(const QString &file)
void reset(const QString &defaultFileName, Generator *g)
static bool isIncluded(const InclusionPolicy &policy, const NodeContext &context)
The Location class provides a way to mark a location in a file.
Definition location.h:20
Location()
Constructs an empty location.
Definition location.cpp:50
Represents an output directory that has been verified to exist.
A PageNode is a Node that generates a documentation page.
Definition pagenode.h:19
This class provides exclusive access to the qdoc database, which consists of a forrest of trees and a...
static QDocDatabase * qdocDB()
Creates the singleton.
NamespaceNode * primaryTreeRoot()
Returns a pointer to the root node of the primary tree.
void setLocalSearch()
Status
Specifies the status of the QQmlIncubator.
Definition text.h:12
const Atom * firstAtom() const
Definition text.h:33
Atom * firstAtom()
Definition text.h:21
Atom * lastAtom()
Definition text.h:22
#define CONFIG_QHP
Definition config.h:449
#define CONFIG_VERSION
Definition config.h:470
#define CONFIG_PROJECT
Definition config.h:444
NodeType
Definition genustypes.h:165
Combined button and popup list for selecting options.
QList< Node * > NodeList
Definition node.h:45
@ Deprecated
Definition status.h:12
The Node class is the base class for all the nodes in QDoc's parse tree.
bool isExternalPage() const
Returns true if the node type is ExternalPage.
Definition node.h:100
bool isDontDocument() const
Returns true if this node's status is DontDocument.
Definition node.h:93
const Doc & doc() const
Returns a reference to the node's Doc data member.
Definition node.h:239
bool isGroup() const
Returns true if the node type is Group.
Definition node.h:105
virtual bool isWrapper() const
Returns true if the node is a class node or a QML type node that is marked as being a wrapper class o...
Definition node.cpp:989
bool isNamespace() const
Returns true if the node type is Namespace.
Definition node.h:110
bool isTypedef() const
Returns true if the node type is Typedef.
Definition node.h:128
bool isQmlBasicType() const
Returns true if the node type is QmlBasicType.
Definition node.h:119
bool isQmlType() const
Returns true if the node type is QmlType or QmlValueType.
Definition node.h:123
bool isHeader() const
Returns true if the node type is HeaderFile.
Definition node.h:106
NodeType nodeType() const override
Returns this node's type.
Definition node.h:82
virtual bool isPageNode() const
Returns true if this node represents something that generates a documentation page.
Definition node.h:150
bool isEnumType() const
Returns true if the node type is Enum.
Definition node.h:94
virtual Status status() const
Returns the node's status value.
Definition node.h:243
virtual bool isTextPageNode() const
Returns true if the node is a PageNode but not an Aggregate.
Definition node.h:155
Aggregate * parent() const
Returns the node's parent pointer, that is, the aggregate under which the node is documented.
Definition node.h:211
virtual bool isAggregate() const
Returns true if this node is an aggregate, which means it inherits Aggregate and can therefore have c...
Definition node.h:138
static bool nodeNameLessThan(const Node *first, const Node *second)
Returns true if the node n1 is less than node n2.
Definition node.cpp:111
virtual bool wasSeen() const
Returns the seen flag data member of this node if it is a NamespaceNode or a CollectionNode.
Definition node.h:195
NodeContext createContext() const
Definition node.cpp:175
virtual CollectionNode * logicalModule() const
If this is a QmlTypeNode, a pointer to its QML module is returned, which is a pointer to a Collection...
Definition node.h:262
bool isRelatedNonmember() const
Returns true if this is a related nonmember of something.
Definition node.h:124
virtual bool isClassNode() const
Returns true if this is an instance of ClassNode.
Definition node.h:145
static bool nodeSortKeyOrNameLessThan(const Node *n1, const Node *n2)
Returns true if node n1 is less than node n2 when comparing the sort keys, defined with.
Definition node.cpp:161
bool isQmlModule() const
Returns true if the node type is QmlModule.
Definition node.h:120
bool isIndexNode() const
Returns true if this node was created from something in an index file.
Definition node.h:107