Skip to content

Commit 77bb241

Browse files
ilovepiPeterChou1
andcommitted
[clang-doc] Add helpers for Template config
This patch adds or fills in some helper functions related to template setup when initializing the mustache backend. It was split from #133161. Co-authored-by: Peter Chou <peter.chou@mail.utoronto.ca>
1 parent 57c7cf8 commit 77bb241

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

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

+34
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,41 @@ static std::unique_ptr<MustacheTemplateFile> NamespaceTemplate = nullptr;
6161

6262
static std::unique_ptr<MustacheTemplateFile> RecordTemplate = nullptr;
6363

64+
static Error
65+
setupTemplate(std::unique_ptr<MustacheTemplateFile> &Template,
66+
StringRef TemplatePath,
67+
std::vector<std::pair<StringRef, StringRef>> Partials) {
68+
auto T = MustacheTemplateFile::createMustacheFile(TemplatePath);
69+
if (auto EC = T.getError())
70+
return createFileError("cannot open file", EC);
71+
Template = std::move(T.get());
72+
for (const auto [Name, FileName] : Partials) {
73+
if (auto Err = Template->registerPartialFile(Name, FileName))
74+
return Err;
75+
}
76+
return Error::success();
77+
}
78+
6479
static Error setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) {
80+
std::string NamespaceFilePath =
81+
CDCtx.MustacheTemplates.lookup("namespace-template");
82+
std::string ClassFilePath = CDCtx.MustacheTemplates.lookup("class-template");
83+
std::string CommentFilePath =
84+
CDCtx.MustacheTemplates.lookup("comments-template");
85+
std::string FunctionFilePath =
86+
CDCtx.MustacheTemplates.lookup("function-template");
87+
std::string EnumFilePath = CDCtx.MustacheTemplates.lookup("enum-template");
88+
std::vector<std::pair<StringRef, StringRef>> Partials = {
89+
{"Comments", CommentFilePath},
90+
{"FunctionPartial", FunctionFilePath},
91+
{"EnumPartial", EnumFilePath}};
92+
93+
if (Error Err = setupTemplate(NamespaceTemplate, NamespaceFilePath, Partials))
94+
return Err;
95+
96+
if (Error Err = setupTemplate(RecordTemplate, ClassFilePath, Partials))
97+
return Err;
98+
6599
return Error::success();
66100
}
67101

0 commit comments

Comments
 (0)