forked from googleapis/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doxygen2toc.cc
349 lines (320 loc) · 12.7 KB
/
doxygen2toc.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "docfx/doxygen2toc.h"
#include "docfx/doxygen2markdown.h"
#include "docfx/doxygen2yaml.h"
#include "docfx/doxygen_groups.h"
#include "docfx/doxygen_pages.h"
#include "docfx/function_classifiers.h"
#include "docfx/node_name.h"
#include "docfx/public_docs.h"
#include <yaml-cpp/yaml.h>
#include <functional>
#include <map>
#include <sstream>
namespace docfx {
namespace {
std::shared_ptr<TocEntry> NamedEntry(std::string_view name) {
auto entry = std::make_shared<TocEntry>();
entry->name = name;
return entry;
}
std::shared_ptr<TocEntry> CompoundEntry(pugi::xml_node node) {
auto const id = std::string_view{node.attribute("id").as_string()};
auto overview = NamedEntry("Overview");
overview->attr.emplace("uid", id);
auto entry = NamedEntry(NodeName(node));
entry->items.push_back(std::move(overview));
return entry;
}
TocItems MemberEntry(YamlContext const& ctx, pugi::xml_node node) {
// Skip MOCK_METHOD() functions.
auto const name = NodeName(node);
if (name.rfind("MOCK_METHOD", 0) == 0) return {};
auto actual = MockingNode(ctx, node);
auto const id = std::string_view{actual.attribute("id").as_string()};
auto entry = NamedEntry(name);
entry->attr.emplace("uid", id);
return {entry};
}
std::shared_ptr<TocEntry> EnumValueEntry(pugi::xml_node node) {
auto const id = std::string_view{node.attribute("id").as_string()};
auto entry = NamedEntry(NodeName(node));
entry->attr.emplace("uid", id);
return entry;
}
bool IsNamespace(pugi::xml_node node) {
return std::string_view{node.attribute("kind").as_string()} == "namespace";
}
bool IsClass(pugi::xml_node node) {
return std::string_view{node.attribute("kind").as_string()} == "class";
}
bool IsStruct(pugi::xml_node node) {
return std::string_view{node.attribute("kind").as_string()} == "struct";
}
bool IsEnum(pugi::xml_node node) {
return std::string_view{node.attribute("kind").as_string()} == "enum";
}
bool IsTypedef(pugi::xml_node node) {
return std::string_view{node.attribute("kind").as_string()} == "typedef";
}
TocItems NamespaceToc(YamlContext const& ctx, pugi::xml_document const& doc,
pugi::xml_node node);
TocItems ClassToc(YamlContext const& ctx, pugi::xml_document const& doc,
pugi::xml_node node);
TocItems EnumToc(YamlContext const& ctx, pugi::xml_document const& doc,
pugi::xml_node node);
// Generate ToC entries for any elements
TocItems GenericToc(YamlContext const& ctx, pugi::xml_document const& doc,
pugi::xml_node node) {
if (!IncludeInPublicDocuments(ctx.config, node)) return {};
if (IsClass(node)) return ClassToc(ctx, doc, node);
if (IsStruct(node)) return ClassToc(ctx, doc, node);
if (IsEnum(node)) return EnumToc(ctx, doc, node);
if (IsNamespace(node)) return NamespaceToc(ctx, doc, node);
auto const element = std::string_view{node.name()};
if (element == "memberdef") return MemberEntry(ctx, node);
if (element == "enumvalue") return {EnumValueEntry(node)};
return {};
}
// Recursively build the ToC.
//
// This function only generates a ToC for some elements in the first level of
// recursion. This makes it possible to "group" the elements by some predicate,
// e.g. all the "Constructors" are grouped. The filtering does not recurse
// for things like "classes" we want to list all the attributes of the matching
// classes.
using Predicate = std::function<bool(pugi::xml_node node)>;
TocItems Recurse(YamlContext const& ctx, pugi::xml_document const& doc,
pugi::xml_node node, Predicate const& pred) {
if (!IncludeInPublicDocuments(ctx.config, node)) return {};
TocItems items;
for (auto const child : node) {
if (!IncludeInPublicDocuments(ctx.config, child)) continue;
auto const element = std::string_view{child.name()};
// A <sectiondef> element defines groups of members, such as, "public
// functions", or "private member variables". They currently do not get
// a representation in the ToC.
if (element == "sectiondef") {
items.splice(items.end(),
Recurse(NestedYamlContext(ctx, node), doc, child, pred));
continue;
}
// In the Doxygen XML file classes are referenced, but not defined, as
// a child element of the namespace element. That is, the XML structure is:
//
// clang-format off
// <doxygen>
// <compounddef kind="namespace" id="namespacefoo">
// <innerclass refid="classfoo_1_1Bar">foo::Bar</innerclass>
// </compounddef>
// <compounddef kind="class" id="classfoo_1_1Bar">
// </compounddef>
// </doxygen>
// clang-format on
//
// We want the classes to appear inside the namespace, so we need to lookup
// the referenced class and generate its ToC recursively.
if (element == "innerclass") {
pugi::xpath_variable_set vars;
vars.add("id", pugi::xpath_type_string);
vars.set("id", child.attribute("refid").as_string());
auto query = pugi::xpath_query("//compounddef[@id = string($id)]", &vars);
auto child = doc.select_node(query);
// Skip the referenced element if it does not match the predicate.
if (!child || !pred(child.node())) continue;
items.splice(items.end(), GenericToc(ctx, doc, child.node()));
continue;
}
// Skip the element if it does not match the predicate.
if (!pred(child)) continue;
items.splice(items.end(), GenericToc(ctx, doc, child));
}
return items;
}
using TocItemsProducer = std::function<TocItems()>;
TocItems NamespaceToc(YamlContext const& ctx, pugi::xml_document const& doc,
pugi::xml_node node) {
if (!IncludeInPublicDocuments(ctx.config, node)) return {};
auto entry = CompoundEntry(node);
struct Node {
std::string_view name;
TocItemsProducer producer;
} nodes[] = {
{"Classes", [&] { return Recurse(ctx, doc, node, IsClass); }},
{"Structs", [&] { return Recurse(ctx, doc, node, IsStruct); }},
{"Functions", [&] { return Recurse(ctx, doc, node, IsPlainFunction); }},
{"Operators", [&] { return Recurse(ctx, doc, node, IsOperator); }},
{"Enums", [&] { return Recurse(ctx, doc, node, IsEnum); }},
{"Types", [&] { return Recurse(ctx, doc, node, IsTypedef); }},
};
for (auto const& [name, generator] : nodes) {
auto items = generator();
if (items.empty()) continue;
auto node = NamedEntry(name);
node->items = std::move(items);
entry->items.push_back(node);
}
return {std::move(entry)};
}
TocItems ClassToc(YamlContext const& ctx, pugi::xml_document const& doc,
pugi::xml_node node) {
if (!IncludeInPublicDocuments(ctx.config, node)) return {};
auto const nested = NestedYamlContext(ctx, node);
auto entry = CompoundEntry(node);
struct Node {
std::string_view name;
TocItemsProducer producer;
} nodes[] = {
// Skip these. They also appear as `<innerclass>` elements in the
// namespace.
// {"Classes", [&] { return Recurse(cfg, doc, node, IsClass); }},
// {"Structs", [&] { return Recurse(cfg, doc, node, IsStruct); }},
{"Constructors",
[&] { return Recurse(nested, doc, node, IsConstructor); }},
{"Operators", [&] { return Recurse(nested, doc, node, IsOperator); }},
{"Functions",
[&] { return Recurse(nested, doc, node, IsPlainFunction); }},
{"Enums", [&] { return Recurse(nested, doc, node, IsEnum); }},
{"Types", [&] { return Recurse(nested, doc, node, IsTypedef); }},
};
for (auto const& [name, generator] : nodes) {
auto items = generator();
if (items.empty()) continue;
auto node = NamedEntry(name);
node->items = std::move(items);
entry->items.push_back(node);
}
return {std::move(entry)};
}
TocItems EnumToc(YamlContext const& ctx, pugi::xml_document const& doc,
pugi::xml_node node) {
if (!IncludeInPublicDocuments(ctx.config, node)) return {};
auto const nested = NestedYamlContext(ctx, node);
auto const id = std::string_view{node.attribute("id").as_string()};
auto entry = NamedEntry(NodeName(node));
auto overview = NamedEntry("Overview");
overview->attr.emplace("uid", id);
entry->items.push_back(std::move(overview));
for (auto const child : node) {
if (!IncludeInPublicDocuments(ctx.config, child)) continue;
auto const element = std::string_view{child.name()};
if (element == "enumvalue") {
entry->items.splice(entry->items.end(), GenericToc(nested, doc, child));
continue;
}
}
return {std::move(entry)};
}
TocItems Indexpage(Config const& /*config*/, pugi::xml_document const& doc) {
pugi::xpath_variable_set vars;
vars.add("id", pugi::xpath_type_string);
vars.set("id", "indexpage");
auto query = pugi::xpath_query("//compounddef[@id = string($id)]", &vars);
auto child = doc.select_node(query);
if (!child) return {};
std::ostringstream title;
AppendTitle(title, MarkdownContext{}, child.node());
auto entry = NamedEntry(title.str());
entry->attr.emplace("href", "index.md");
entry->attr.emplace("uid", "indexpage");
return {std::move(entry)};
}
TocItems Pages(Config const& config, pugi::xml_document const& doc) {
TocItems items;
for (auto const& i : doc.select_nodes("//*[@kind='page']")) {
auto const page = i.node();
if (!IncludeInPublicDocuments(config, page)) continue;
auto const id = std::string_view{page.attribute("id").as_string()};
// Skip endpoint and authorization override snippets.
if (id.find("-endpoint-snippet") != std::string_view::npos) continue;
if (id.find("-account-snippet") != std::string_view::npos) continue;
if (id == "indexpage") continue;
std::ostringstream title;
AppendTitle(title, MarkdownContext{}, page);
auto entry = NamedEntry(title.str());
entry->attr.emplace("href", std::string{id} + ".md");
entry->attr.emplace("uid", id);
items.push_back(std::move(entry));
}
return items;
}
TocItems Groups(Config const& /*config*/, pugi::xml_document const& doc) {
TocItems items;
for (auto const& i : doc.select_nodes("//*[@kind='group']")) {
auto const group = i.node();
auto const id = std::string{group.attribute("id").as_string()};
std::ostringstream title;
AppendTitle(title, MarkdownContext{}, group);
auto entry = NamedEntry(title.str());
entry->attr.emplace("href", std::string{id} + ".yml");
entry->attr.emplace("uid", id);
items.push_back(std::move(entry));
}
return items;
}
TocItems Namespaces(Config const& config, pugi::xml_document const& doc) {
YamlContext ctx;
ctx.config = config;
TocItems items;
for (auto const& i : doc.select_nodes("//compounddef[@kind='namespace']")) {
if (!IncludeInPublicDocuments(config, i.node())) continue;
items.splice(items.end(), NamespaceToc(ctx, doc, i.node()));
}
return items;
}
void Toc2Yaml(YAML::Emitter& out, TocEntry const& e) {
out << YAML::BeginMap;
out << YAML::Key << "name" << YAML::Value << YAML::DoubleQuoted << e.name;
for (auto const& [key, value] : e.attr) {
out << YAML::Key << key << YAML::Value << value;
}
if (!e.items.empty()) {
out << YAML::Key << "items" << YAML::Value << YAML::BeginSeq;
for (auto const& entry : e.items) Toc2Yaml(out, *entry);
out << YAML::EndSeq;
}
out << YAML::EndMap;
}
TocEntry BuildToc(Config const& config, pugi::xml_document const& doc) {
TocEntry toc;
toc.name = std::string{"cloud.google.com/cpp/"} + config.library;
toc.items.splice(toc.items.end(), Indexpage(config, doc));
struct Node {
std::string_view name;
std::function<TocItems(Config const&, pugi::xml_document const&)> generator;
} nodes[] = {
{"In-Depth Topics", Pages},
{"Modules", Groups},
{"Namespaces", Namespaces},
};
for (auto const& [name, generator] : nodes) {
auto items = generator(config, doc);
if (items.empty()) continue;
auto node = NamedEntry(name);
node->items = std::move(items);
toc.items.push_back(node);
}
return toc;
}
} // namespace
std::string Doxygen2Toc(Config const& config, pugi::xml_document const& doc) {
auto const toc = BuildToc(config, doc);
YAML::Emitter out;
Toc2Yaml(out, toc);
std::ostringstream os;
os << "### YamlMime:TableOfContent\n" << out.c_str() << "\n";
return std::move(os).str();
}
} // namespace docfx