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
outputcontext.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 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 "config.h"
7#include "location.h"
8
9#include <QtCore/qregularexpression.h>
10
12
13using namespace Qt::StringLiterals;
14
15/*!
16 \class OutputContext
17 \internal
18 \brief Bundles output configuration without static variables.
19
20 OutputContext captures all the configuration state that generators
21 need for output, replacing Generator's 19 static variables with
22 explicit, injectable state.
23
24 This enables:
25 \list
26 \li Testability - configurations can be created for tests.
27 \li Reusability - HrefResolver, IndexWriter can use the same context.
28 \li Statelessness - no global mutable state between runs.
29 \endlist
30
31 \section1 Relationship to Generator Statics
32
33 | OutputContext field | Generator static |
34 |---------------------|------------------|
35 | outputDir | s_outDir |
36 | project | s_project |
37 | subdir | s_outSubdir |
38 | outputPrefixes | s_outputPrefixes |
39 | outputSuffixes | s_outputSuffixes |
40 | useSubdirs | s_useOutputSubdirs |
41
42 \sa DocumentWriter, FileDocumentWriter
43*/
44
45/*!
46 Creates an OutputContext from the given \a config for the specified output
47 \a format.
48
49 This method extracts all relevant configuration values that generators need,
50 centralizing the logic that was previously scattered across Generator's
51 static initialization.
52*/
53OutputContext OutputContext::fromConfig(const Config &config, const QString &format)
54{
55 QString project = config.get(CONFIG_PROJECT).asString();
56
57 // Determine output directory
58 QString outDir = config.getOutputDir(format);
59 if (outDir.isEmpty())
60 outDir = config.getOutputDir(); // Fallback to default
61
62 QString subdir;
63 OutputDirectory outputDir{outDir};
64 if (!outDir.isEmpty()) {
65 outputDir = OutputDirectory::ensure(outDir, Location{});
66 subdir = outDir.mid(outDir.lastIndexOf('/'_L1) + 1);
67 }
68
69 // Extract output prefixes
70 QHash<QString, QString> prefixes;
71 QStringList prefixItems = config.get(CONFIG_OUTPUTPREFIXES).asStringList();
72 for (const auto &prefix : prefixItems) {
73 prefixes[prefix] =
74 config.get(CONFIG_OUTPUTPREFIXES + Config::dot + prefix).asString();
75 }
76 // Default QML prefix if not specified
77 if (!prefixItems.contains(u"QML"_s))
78 prefixes[u"QML"_s] = u"qml-"_s;
79
80 // Extract output suffixes
81 QHash<QString, QString> suffixes;
82 for (const auto &suffix : config.get(CONFIG_OUTPUTSUFFIXES).asStringList()) {
83 suffixes[suffix] =
84 config.get(CONFIG_OUTPUTSUFFIXES + Config::dot + suffix).asString();
85 }
86
87 bool useSubdirs = !config.get(format + Config::dot + "nosubdirs").asBool();
88 bool noLinkErrors = config.get(CONFIG_NOLINKERRORS).asBool();
89 bool autolinkErrors = config.get(CONFIG_AUTOLINKERRORS).asBool();
90
91 return OutputContext{
92 std::move(outputDir),
93 std::move(project),
94 std::move(subdir),
95 QString{}, // Is set by caller based on generator
96 std::move(prefixes),
97 std::move(suffixes),
98 useSubdirs,
99 noLinkErrors,
100 autolinkErrors
101 };
102}
103
104namespace {
105
106QString genusKey(Genus genus)
107{
108 switch (genus) {
109 case Genus::QML:
110 return u"QML"_s;
111 case Genus::CPP:
112 return u"CPP"_s;
113 default:
114 return {};
115 }
116}
117
118} // namespace
119
120/*!
121 Returns the output prefix for the given \a genus. The configured
122 prefixes are stored against the QDoc-internal \c "QML" and \c "CPP"
123 keys; the Genus-typed interface lets callers avoid handling those
124 string keys (or having to reach for Node) just to answer
125 "what prefix belongs to this page?".
126*/
128{
129 return outputPrefixes.value(genusKey(genus));
130}
131
132/*!
133 Returns the output suffix for the given \a genus. See outputPrefix()
134 for why the interface is Genus-typed.
135*/
137{
138 return outputSuffixes.value(genusKey(genus));
139}
140
141QT_END_NAMESPACE
The Config class contains the configuration variables for controlling how qdoc produces documentation...
Definition config.h:95
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:48
Represents an output directory that has been verified to exist.
#define CONFIG_AUTOLINKERRORS
Definition config.h:377
#define CONFIG_OUTPUTSUFFIXES
Definition config.h:432
#define CONFIG_OUTPUTPREFIXES
Definition config.h:431
#define CONFIG_NOLINKERRORS
Definition config.h:428
#define CONFIG_PROJECT
Definition config.h:435
Combined button and popup list for selecting options.
Bundles output configuration without static variables.
QString outputPrefix(Genus genus) const
Returns the output prefix for the given genus.
QString outputSuffix(Genus genus) const
Returns the output suffix for the given genus.