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