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
104/*!
105 Returns the output prefix for the given \a nodeType.
106 Common values: "QML" -> "qml-", "CPP" -> "cpp-".
107*/
108QString OutputContext::outputPrefix(const QString &nodeType) const
109{
110 return outputPrefixes.value(nodeType);
111}
112
113/*!
114 Returns the output suffix for the given \a nodeType.
115*/
116QString OutputContext::outputSuffix(const QString &nodeType) const
117{
118 return outputSuffixes.value(nodeType);
119}
120
121QT_END_NAMESPACE
The Config class contains the configuration variables for controlling how qdoc produces documentation...
Definition config.h:85
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:367
#define CONFIG_OUTPUTSUFFIXES
Definition config.h:422
#define CONFIG_OUTPUTPREFIXES
Definition config.h:421
#define CONFIG_NOLINKERRORS
Definition config.h:418
#define CONFIG_PROJECT
Definition config.h:425
Combined button and popup list for selecting options.
Bundles output configuration without static variables.
QString outputSuffix(const QString &nodeType) const
Returns the output suffix for the given nodeType.
QString outputPrefix(const QString &nodeType) const
Returns the output prefix for the given nodeType.