Skip to content

Commit 7e1399b

Browse files
committed
[clang-doc] add a mustache backend init implementation
1 parent 08a9f99 commit 7e1399b

File tree

4 files changed

+65
-15
lines changed

4 files changed

+65
-15
lines changed

clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class MustacheTemplateFile : public Template {
5454
std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(BufferOrError.get());
5555
llvm::StringRef FileContent = Buffer->getBuffer();
5656
registerPartial(Name, FileContent);
57+
return llvm::Error::success();
5758
}
5859

5960
MustacheTemplateFile(StringRef TemplateStr) : Template(TemplateStr) {}
@@ -72,6 +73,7 @@ setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) {
7273
return llvm::createFileError("cannot open file", EC);
7374
}
7475
NamespaceTemplate = std::move(Template.get());
76+
return llvm::Error::success();
7577
}
7678

7779
llvm::Error
@@ -118,6 +120,7 @@ MustacheHTMLGenerator::generateDocs(llvm::StringRef RootDir,
118120
}
119121
}
120122
}
123+
return llvm::Error::success();
121124
}
122125

123126
Value extractValue(const Location &L,
@@ -130,6 +133,8 @@ Value extractValue(const Location &L,
130133
SmallString<128> FileURL(*RepositoryUrl);
131134
llvm::sys::path::append(FileURL, llvm::sys::path::Style::posix, L.Filename);
132135
Obj.insert({"FileURL", FileURL});
136+
137+
return Obj;
133138
}
134139

135140
Value extractValue(const Reference &I, StringRef CurrentDirectory) {
@@ -280,29 +285,39 @@ Value extractValue(const NamespaceInfo &I, const ClangDocContext &CDCtx) {
280285
Value ArrNamespace = Array();
281286
for (const Reference& Child : I.Children.Namespaces)
282287
ArrNamespace.getAsArray()->emplace_back(extractValue(Child, BasePath));
283-
NamespaceValue.insert({"Namespace", ArrNamespace});
288+
289+
if (!ArrNamespace.getAsArray()->empty())
290+
NamespaceValue.insert({"Namespace", Object{{"Links", ArrNamespace}}});
284291

285292
Value ArrRecord = Array();
286293
for (const Reference& Child : I.Children.Records)
287294
ArrRecord.getAsArray()->emplace_back(extractValue(Child, BasePath));
288-
NamespaceValue.insert({"Record", ArrRecord});
295+
296+
if (!ArrRecord.getAsArray()->empty())
297+
NamespaceValue.insert({"Record", Object{{"Links", ArrRecord}}});
289298

290299
Value ArrFunction = Array();
291300
for (const FunctionInfo& Child : I.Children.Functions)
292301
ArrFunction.getAsArray()->emplace_back(extractValue(Child, BasePath,
293302
CDCtx));
294-
NamespaceValue.insert({"Function", ArrRecord});
303+
if (!ArrFunction.getAsArray()->empty())
304+
NamespaceValue.insert({"Function", Object{{"Obj", ArrFunction}}});
295305

296306
Value ArrEnum = Array();
297307
for (const EnumInfo& Child : I.Children.Enums)
298308
ArrEnum.getAsArray()->emplace_back(extractValue(Child, CDCtx));
299-
NamespaceValue.insert({"Enums", ArrEnum });
309+
310+
if (!ArrEnum.getAsArray()->empty())
311+
NamespaceValue.insert({"Enums", Object{{"Obj", ArrEnum }}});
300312

301313
Value ArrTypedefs = Array();
302314
for (const TypedefInfo& Child : I.Children.Typedefs)
303315
ArrTypedefs.getAsArray()->emplace_back(extractValue(Child));
304-
NamespaceValue.insert({"Typedefs", ArrTypedefs });
305316

317+
if (!ArrTypedefs.getAsArray()->empty())
318+
NamespaceValue.insert({"Typedefs", Object{{"Obj", ArrTypedefs }}});
319+
320+
return NamespaceValue;
306321
}
307322

308323

@@ -313,6 +328,7 @@ MustacheHTMLGenerator::generateDocForInfo(Info *I, llvm::raw_ostream &OS,
313328
switch (I->IT) {
314329
case InfoType::IT_namespace: {
315330
Value V = extractValue(*static_cast<clang::doc::NamespaceInfo *>(I), CDCtx);
331+
llvm::outs() << V << "\n";
316332
OS << NamespaceTemplate->render(V);
317333
break;
318334
}
@@ -332,18 +348,18 @@ MustacheHTMLGenerator::generateDocForInfo(Info *I, llvm::raw_ostream &OS,
332348
}
333349

334350
llvm::Error MustacheHTMLGenerator::createResources(ClangDocContext &CDCtx) {
335-
351+
return llvm::Error::success();
336352
}
337353

338-
const char *MustacheHTMLGenerator::Format = "mustache";
354+
const char *MustacheHTMLGenerator::Format = "mhtml";
339355

340356

341357
static GeneratorRegistry::Add<MustacheHTMLGenerator> MHTML(MustacheHTMLGenerator::Format,
342358
"Generator for mustache HTML output.");
343359

344360
// This anchor is used to force the linker to link in the generated object
345361
// file and thus register the generator.
346-
volatile int HTMLGeneratorAnchorSource = 0;
362+
volatile int MHTMLGeneratorAnchorSource = 0;
347363

348364
} // namespace doc
349365
} // namespace clang

clang-tools-extra/clang-doc/assets/template.mustache

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,50 @@
33
See https://llvm.org/LICENSE.txt for license information.
44
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55
6-
This file defines the template
6+
This file defines the template for generating Namespaces
77
}}
88
<!DOCTYPE html>
99
<html lang="en-US">
1010
<head>
1111
<meta charset="utf-8"/>
1212
<title>{{NamespaceTitle}}</title>
1313
</head>
14+
<h1>{{NamespaceTitle}}</h1>
1415
{{#NamespaceComments}}
15-
<p>Namespace Comment Present!</p>
16+
<p>Namespace Comment</p>
1617
{{/NamespaceComments}}
1718
{{#Namespace}}
18-
<p>Namespace Present!</p>
19+
<h2 id="Namespace">Namespace</h2>
20+
<ul>
21+
{{#Links}}
22+
<li>
23+
<a href="{{Link}}">{{Name}}</a>
24+
</li>
25+
{{/Links}}
26+
</ul>
1927
{{/Namespace}}
2028
{{#Record}}
21-
<p>Record Present!</p>
29+
<h2 id="Class">Class</h2>
30+
<ul>
31+
{{#Links}}
32+
<li>
33+
<a href="{{Link}}">{{Name}}</a>
34+
</li>
35+
{{/Links}}
36+
</ul>
2237
{{/Record}}
38+
{{#Function}}
39+
<h2 id="Function">Function</h2>
40+
<div>
41+
{{#Obj}}
42+
{{/Obj}}
43+
</div>
44+
{{/Function}}
45+
{{#Enums}}
46+
<h2 id="Enums">Enums</h2>
47+
<div>
48+
{{#Obj}}
49+
{{/Obj}}
50+
</div>
51+
{{/Enums}}
2352
</html>

clang-tools-extra/clang-doc/tool/ClangDocMain.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ enum OutputFormatTy {
103103
md,
104104
yaml,
105105
html,
106+
mhtml
106107
};
107108

108109
static llvm::cl::opt<OutputFormatTy>
@@ -112,7 +113,9 @@ static llvm::cl::opt<OutputFormatTy>
112113
clEnumValN(OutputFormatTy::md, "md",
113114
"Documentation in MD format."),
114115
clEnumValN(OutputFormatTy::html, "html",
115-
"Documentation in HTML format.")),
116+
"Documentation in HTML format."),
117+
clEnumValN(OutputFormatTy::mhtml, "mhtml",
118+
"Documentation in mHTML format")),
116119
llvm::cl::init(OutputFormatTy::yaml),
117120
llvm::cl::cat(ClangDocCategory));
118121

@@ -124,6 +127,8 @@ std::string getFormatString() {
124127
return "md";
125128
case OutputFormatTy::html:
126129
return "html";
130+
case OutputFormatTy::mhtml:
131+
return "mhtml";
127132
}
128133
llvm_unreachable("Unknown OutputFormatTy");
129134
}
@@ -227,6 +232,8 @@ llvm::Error getMustacheHtmlFiles(const char *Argv0,
227232
llvm::sys::path::native(AssetsPath, MustacheTemplate);
228233
llvm::sys::path::append(MustacheTemplate, "template.mustache");
229234
CDCtx.MustacheTemplates.insert({"template", MustacheTemplate.c_str()});
235+
236+
return llvm::Error::success();
230237
}
231238

232239
/// Make the output of clang-doc deterministic by sorting the children of

clang-tools-extra/test/clang-doc/Inputs/basic-project/include/Shape.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,3 @@ class Shape {
2626
*/
2727
virtual double perimeter() const = 0;
2828
};
29-
30-

0 commit comments

Comments
 (0)