Skip to content

Commit 57c7cf8

Browse files
ilovepiPeterChou1
andcommitted
[clang-doc] Add HTMLMustacheGenerator methods
Split from #133161. This patch fills in the implementation for a number of the MustacheHTMLGenerator methods. Many of these APIs are just stubbed out, and will have their implementation filled in by later patches. Co-authored-by: Peter Chou <peter.chou@mail.utoronto.ca>
1 parent 397d5dc commit 57c7cf8

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,119 @@ class MustacheTemplateFile : public Template {
5656

5757
MustacheTemplateFile(StringRef TemplateStr) : Template(TemplateStr) {}
5858
};
59+
60+
static std::unique_ptr<MustacheTemplateFile> NamespaceTemplate = nullptr;
61+
62+
static std::unique_ptr<MustacheTemplateFile> RecordTemplate = nullptr;
63+
64+
static Error setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) {
65+
return Error::success();
66+
}
67+
5968
Error MustacheHTMLGenerator::generateDocs(
6069
StringRef RootDir, StringMap<std::unique_ptr<doc::Info>> Infos,
6170
const clang::doc::ClangDocContext &CDCtx) {
71+
if (auto Err = setupTemplateFiles(CDCtx))
72+
return Err;
73+
// Track which directories we already tried to create.
74+
StringSet<> CreatedDirs;
75+
// Collect all output by file name and create the necessary directories.
76+
StringMap<std::vector<doc::Info *>> FileToInfos;
77+
for (const auto &Group : Infos) {
78+
doc::Info *Info = Group.getValue().get();
79+
80+
SmallString<128> Path;
81+
sys::path::native(RootDir, Path);
82+
sys::path::append(Path, Info->getRelativeFilePath(""));
83+
if (!CreatedDirs.contains(Path)) {
84+
if (std::error_code Err = sys::fs::create_directories(Path);
85+
Err != std::error_code())
86+
return createStringError(Err, "Failed to create directory '%s'.",
87+
Path.c_str());
88+
CreatedDirs.insert(Path);
89+
}
90+
91+
sys::path::append(Path, Info->getFileBaseName() + ".html");
92+
FileToInfos[Path].push_back(Info);
93+
}
94+
95+
for (const auto &Group : FileToInfos) {
96+
std::error_code FileErr;
97+
raw_fd_ostream InfoOS(Group.getKey(), FileErr, sys::fs::OF_None);
98+
if (FileErr)
99+
return createStringError(FileErr, "Error opening file '%s'",
100+
Group.getKey().data());
101+
102+
for (const auto &Info : Group.getValue()) {
103+
if (Error Err = generateDocForInfo(Info, InfoOS, CDCtx))
104+
return Err;
105+
}
106+
}
62107
return Error::success();
63108
}
109+
110+
static json::Value extractValue(const NamespaceInfo &I,
111+
const ClangDocContext &CDCtx) {
112+
Object NamespaceValue = Object();
113+
return NamespaceValue;
114+
}
115+
116+
static json::Value extractValue(const RecordInfo &I,
117+
const ClangDocContext &CDCtx) {
118+
Object RecordValue = Object();
119+
return RecordValue;
120+
}
121+
122+
static void setupTemplateValue(const ClangDocContext &CDCtx, json::Value &V,
123+
Info *I) {}
124+
64125
Error MustacheHTMLGenerator::generateDocForInfo(Info *I, raw_ostream &OS,
65126
const ClangDocContext &CDCtx) {
127+
switch (I->IT) {
128+
case InfoType::IT_namespace: {
129+
json::Value V =
130+
extractValue(*static_cast<clang::doc::NamespaceInfo *>(I), CDCtx);
131+
setupTemplateValue(CDCtx, V, I);
132+
NamespaceTemplate->render(V, OS);
133+
break;
134+
}
135+
case InfoType::IT_record: {
136+
json::Value V =
137+
extractValue(*static_cast<clang::doc::RecordInfo *>(I), CDCtx);
138+
setupTemplateValue(CDCtx, V, I);
139+
// Serialize the JSON value to the output stream in a readable format.
140+
outs() << "Visit: " << I->Name << "\n";
141+
// outs() << formatv("{0:2}", V) << "\n";
142+
RecordTemplate->render(V, outs());
143+
break;
144+
}
145+
case InfoType::IT_enum:
146+
outs() << "IT_enum\n";
147+
break;
148+
case InfoType::IT_function:
149+
outs() << "IT_Function\n";
150+
break;
151+
case InfoType::IT_typedef:
152+
outs() << "IT_typedef\n";
153+
break;
154+
case InfoType::IT_default:
155+
return createStringError(inconvertibleErrorCode(), "unexpected InfoType");
156+
}
66157
return Error::success();
67158
}
159+
68160
Error MustacheHTMLGenerator::createResources(ClangDocContext &CDCtx) {
69161
Error Err = Error::success();
162+
for (const auto &FilePath : CDCtx.UserStylesheets) {
163+
Err = copyFile(FilePath, CDCtx.OutDirectory);
164+
if (Err)
165+
return Err;
166+
}
167+
for (const auto &FilePath : CDCtx.JsScripts) {
168+
Err = copyFile(FilePath, CDCtx.OutDirectory);
169+
if (Err)
170+
return Err;
171+
}
70172
return Error::success();
71173
}
72174

0 commit comments

Comments
 (0)