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
document.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "document.h"
5
6#include <optional>
7
8QT_BEGIN_NAMESPACE
9
10using namespace Qt::Literals::StringLiterals;
11
12namespace IR {
13
14/*!
15 \struct IR::Document
16 \brief Intermediate representation for a documentation topic.
17
18 Document contains all information needed to render a single documentation
19 page using templates. All links are pre-resolved, sections are pre-organized,
20 and file paths are pre-computed. The template engine receives only this IR
21 and performs no lookups or resolution itself.
22
23 The struct includes classification metadata (nodeType, genus, status, access)
24 that allows templates to conditionally render content based on the type and
25 visibility of the documented entity.
26*/
27
28// Classification fields use a two-part structure:
29// - "id": Stable kebab-case identifier for template conditionals (e.g., "qml-type")
30// - "label": Human-readable display name (e.g., "QML type")
31//
32// This separation allows templates to use stable ids for logic while displaying
33// user-friendly labels. Changing labels won't break template conditionals.
34
35// Helper to create a classification JSON object with id and label
36static QJsonObject classificationObject(const QString &id, const QString &label)
37{
38 QJsonObject obj;
39 obj["id"_L1] = id;
40 obj["label"_L1] = label;
41 return obj;
42}
43
44// Returns {id, label} pair for NodeType. Returns nullopt for NoType (unclassified).
46{
47 switch (t) {
48 case NodeType::NoType: return std::nullopt;
49 case NodeType::Namespace: return classificationObject("namespace"_L1, "Namespace"_L1);
50 case NodeType::Class: return classificationObject("class"_L1, "Class"_L1);
51 case NodeType::Struct: return classificationObject("struct"_L1, "Struct"_L1);
52 case NodeType::Union: return classificationObject("union"_L1, "Union"_L1);
53 case NodeType::HeaderFile: return classificationObject("header-file"_L1, "Header file"_L1);
54 case NodeType::Page: return classificationObject("page"_L1, "Page"_L1);
55 case NodeType::Enum: return classificationObject("enum"_L1, "Enum"_L1);
56 case NodeType::Example: return classificationObject("example"_L1, "Example"_L1);
57 case NodeType::ExternalPage: return classificationObject("external-page"_L1, "External page"_L1);
58 case NodeType::TypeAlias: return classificationObject("type-alias"_L1, "Type alias"_L1);
59 case NodeType::Typedef: return classificationObject("typedef"_L1, "Typedef"_L1);
60 case NodeType::Function: return classificationObject("function"_L1, "Function"_L1);
61 case NodeType::Property: return classificationObject("property"_L1, "Property"_L1);
62 case NodeType::Proxy: return classificationObject("proxy"_L1, "Proxy"_L1);
63 case NodeType::Variable: return classificationObject("variable"_L1, "Variable"_L1);
64 case NodeType::Group: return classificationObject("group"_L1, "Group"_L1);
65 case NodeType::Module: return classificationObject("module"_L1, "Module"_L1);
66 case NodeType::QmlType: return classificationObject("qml-type"_L1, "QML type"_L1);
67 case NodeType::QmlValueType: return classificationObject("qml-value-type"_L1, "QML value type"_L1);
68 case NodeType::QmlModule: return classificationObject("qml-module"_L1, "QML module"_L1);
69 case NodeType::QmlProperty: return classificationObject("qml-property"_L1, "QML property"_L1);
70 case NodeType::QmlEnum: return classificationObject("qml-enum"_L1, "QML enum"_L1);
71 case NodeType::SharedComment: return classificationObject("shared-comment"_L1, "Shared comment"_L1);
72 case NodeType::Collection: return classificationObject("collection"_L1, "Collection"_L1);
73 }
74 Q_UNREACHABLE();
75}
76
77// Returns {id, label} pair for Genus. Returns nullopt for DontCare (unclassified).
79{
80 switch (g) {
81 case Genus::DontCare: return std::nullopt;
82 case Genus::CPP: return classificationObject("cpp"_L1, "C++"_L1);
83 case Genus::QML: return classificationObject("qml"_L1, "QML"_L1);
84 case Genus::DOC: return classificationObject("doc"_L1, "Documentation"_L1);
85 case Genus::API: return classificationObject("api"_L1, "API"_L1);
86 }
87 Q_UNREACHABLE();
88}
89
90// Returns {id, label} pair for Status.
92{
93 switch (s) {
94 case Status::Deprecated: return classificationObject("deprecated"_L1, "Deprecated"_L1);
95 case Status::Preliminary: return classificationObject("preliminary"_L1, "Preliminary"_L1);
96 case Status::Active: return classificationObject("active"_L1, "Active"_L1);
97 case Status::Internal: return classificationObject("internal"_L1, "Internal"_L1);
98 case Status::DontDocument: return classificationObject("ignored"_L1, "Ignored"_L1);
99 }
100 Q_UNREACHABLE();
101}
102
103// Returns {id, label} pair for Access.
105{
106 switch (a) {
107 case Access::Public: return classificationObject("public"_L1, "Public"_L1);
108 case Access::Protected: return classificationObject("protected"_L1, "Protected"_L1);
109 case Access::Private: return classificationObject("private"_L1, "Private"_L1);
110 }
111 Q_UNREACHABLE();
112}
113
114/*!
115 Converts the Document to a QJsonObject for template rendering.
116
117 The JSON structure follows a convention where field names use camelCase
118 and match template variable names. Classification fields (nodeType, genus,
119 status, access) use a two-part structure with "id" (stable kebab-case
120 identifier for conditionals) and "label" (human-readable display name).
121
122 The \c contentJson field is nested under a 'content' key to provide better
123 structure and namespace separation in templates.
124
125 Returns a QJsonObject containing all IR data in a format suitable for
126 passing to the Inja template engine via InjaBridge.
127*/
129{
130 QJsonObject json;
131
132 // Classification (as {id, label} objects for template convenience)
133 // nodeType and genus are omitted when unclassified (NoType/DontCare),
134 // allowing templates to use `if defined` checks.
135 if (const auto t = nodeTypeToJson(nodeType))
136 json["nodeType"_L1] = *t;
137 if (const auto g = genusToJson(genus))
138 json["genus"_L1] = *g;
139 json["status"_L1] = statusToJson(status);
140 json["access"_L1] = accessToJson(access);
141
142 // Identity
143 json["title"_L1] = title;
144 json["fullTitle"_L1] = fullTitle;
145 json["url"_L1] = url;
146 if (!since.isEmpty())
147 json["since"_L1] = since;
148 if (!deprecatedSince.isEmpty())
149 json["deprecatedSince"_L1] = deprecatedSince;
150 json["brief"_L1] = brief;
151
152 // Content
153 if (!contentJson.isEmpty())
154 json["content"_L1] = contentJson;
155
156 return json;
157}
158
159} // namespace IR
160
161QT_END_NAMESPACE
Access
Definition access.h:11
Status
Specifies the status of the QQmlIncubator.
NodeType
Definition genustypes.h:150
Definition builder.cpp:14
static std::optional< QJsonObject > nodeTypeToJson(NodeType t)
Definition document.cpp:45
static QJsonObject accessToJson(Access a)
Definition document.cpp:104
static QJsonObject classificationObject(const QString &id, const QString &label)
Definition document.cpp:36
static std::optional< QJsonObject > genusToJson(Genus g)
Definition document.cpp:78
static QJsonObject statusToJson(Status s)
Definition document.cpp:91
Intermediate representation for a documentation topic.
Definition document.h:19
QJsonObject toJson() const
Converts the Document to a QJsonObject for template rendering.
Definition document.cpp:128