clang-tools 23.0.0git
HTMLGenerator.cpp
Go to the documentation of this file.
1//===-- HTMLGenerator.cpp - HTML Generator ----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file contains the implementation of the HTMLGenerator class,
11/// which is a Clang-Doc generator for HTML using Mustache templates.
12///
13//===----------------------------------------------------------------------===//
14
15#include "Generators.h"
16#include "Representation.h"
17#include "support/File.h"
18#include "llvm/Support/Error.h"
19#include "llvm/Support/Path.h"
20
21using namespace llvm;
22using namespace llvm::json;
23using namespace llvm::mustache;
24
25namespace clang {
26namespace doc {
27
28static std::unique_ptr<MustacheTemplateFile> NamespaceTemplate = nullptr;
29
30static std::unique_ptr<MustacheTemplateFile> RecordTemplate = nullptr;
31
32static std::unique_ptr<MustacheTemplateFile> IndexTemplate = nullptr;
33
35public:
36 static const char *Format;
37 Error createResources(ClangDocContext &CDCtx) override;
38 Error generateDocForInfo(Info *I, raw_ostream &OS,
39 const ClangDocContext &CDCtx) override;
40 Error setupTemplateFiles(const ClangDocContext &CDCtx) override;
41 Error generateDocForJSON(json::Value &JSON, raw_fd_ostream &OS,
42 const ClangDocContext &CDCtx, StringRef ObjTypeStr,
43 StringRef RelativeRootPath) override;
44 // Populates templates with CSS stylesheets, JS scripts paths.
45 Error setupTemplateResources(const ClangDocContext &CDCtx, json::Value &V,
46 SmallString<128> RelativeRootPath);
47 llvm::Error generateDocumentation(StringRef RootDir,
48 llvm::StringMap<doc::Info *> Infos,
49 const ClangDocContext &CDCtx,
50 std::string DirName) override;
51};
52
54 // Template files need to use the native path when they're opened,
55 // but have to be used in POSIX style when used in HTML.
56 auto ConvertToNative = [](std::string &&Path) -> std::string {
57 SmallString<128> PathBuf(Path);
58 llvm::sys::path::native(PathBuf);
59 return PathBuf.str().str();
60 };
61
62 std::string NamespaceFilePath =
63 ConvertToNative(CDCtx.MustacheTemplates.lookup("namespace-template"));
64 std::string ClassFilePath =
65 ConvertToNative(CDCtx.MustacheTemplates.lookup("class-template"));
66 std::string IndexFilePath =
67 ConvertToNative(CDCtx.MustacheTemplates.lookup("index-template"));
68 std::string CommentFilePath =
69 ConvertToNative(CDCtx.MustacheTemplates.lookup("comment-template"));
70 std::string FunctionFilePath =
71 ConvertToNative(CDCtx.MustacheTemplates.lookup("function-template"));
72 std::string EnumFilePath =
73 ConvertToNative(CDCtx.MustacheTemplates.lookup("enum-template"));
74 std::string HeadFilePath =
75 ConvertToNative(CDCtx.MustacheTemplates.lookup("head-template"));
76 std::string NavbarFilePath =
77 ConvertToNative(CDCtx.MustacheTemplates.lookup("navbar-template"));
78 std::string AliasFilePath =
79 ConvertToNative(CDCtx.MustacheTemplates.lookup("alias-template"));
80 std::vector<std::pair<StringRef, StringRef>> Partials = {
81 {"Comments", CommentFilePath}, {"FunctionPartial", FunctionFilePath},
82 {"EnumPartial", EnumFilePath}, {"HeadPartial", HeadFilePath},
83 {"NavbarPartial", NavbarFilePath}, {"AliasPartial", AliasFilePath}};
84
85 if (Error Err = setupTemplate(NamespaceTemplate, NamespaceFilePath, Partials))
86 return Err;
87
88 if (Error Err = setupTemplate(RecordTemplate, ClassFilePath, Partials))
89 return Err;
90
91 if (Error Err = setupTemplate(IndexTemplate, IndexFilePath, Partials))
92 return Err;
93
94 return Error::success();
95}
96
98 json::Value &V,
99 SmallString<128> RelativeRootPath) {
100 V.getAsObject()->insert({"ProjectName", CDCtx.ProjectName});
101 json::Value StylesheetArr = Array();
102 sys::path::native(RelativeRootPath, sys::path::Style::posix);
103
104 auto *SSA = StylesheetArr.getAsArray();
105 SSA->reserve(CDCtx.UserStylesheets.size());
106 for (const auto &FilePath : CDCtx.UserStylesheets) {
107 SmallString<128> StylesheetPath = RelativeRootPath;
108 sys::path::append(StylesheetPath, sys::path::Style::posix,
109 sys::path::filename(FilePath));
110 SSA->emplace_back(StylesheetPath);
111 }
112 V.getAsObject()->insert({"Stylesheets", StylesheetArr});
113
114 json::Value ScriptArr = Array();
115 auto *SCA = ScriptArr.getAsArray();
116 SCA->reserve(CDCtx.JsScripts.size());
117 for (auto Script : CDCtx.JsScripts) {
118 SmallString<128> JsPath = RelativeRootPath;
119 sys::path::append(JsPath, sys::path::Style::posix,
120 sys::path::filename(Script));
121 SCA->emplace_back(JsPath);
122 }
123 V.getAsObject()->insert({"Scripts", ScriptArr});
124 if (RelativeRootPath.empty()) {
125 RelativeRootPath = "";
126 } else {
127 sys::path::append(RelativeRootPath, "/index.html");
128 sys::path::native(RelativeRootPath, sys::path::Style::posix);
129 }
130 V.getAsObject()->insert({"Homepage", RelativeRootPath});
131 return Error::success();
132}
133
134Error HTMLGenerator::generateDocForJSON(json::Value &JSON, raw_fd_ostream &OS,
135 const ClangDocContext &CDCtx,
136 StringRef ObjTypeStr,
137 StringRef RelativeRootPath) {
138 if (ObjTypeStr == "namespace") {
139 if (auto Err = setupTemplateResources(CDCtx, JSON, RelativeRootPath))
140 return Err;
141 assert(NamespaceTemplate && "NamespaceTemplate is nullptr.");
142 NamespaceTemplate->render(JSON, OS);
143 } else if (ObjTypeStr == "record") {
144 if (auto Err = setupTemplateResources(CDCtx, JSON, RelativeRootPath))
145 return Err;
146 assert(RecordTemplate && "RecordTemplate is nullptr.");
147 RecordTemplate->render(JSON, OS);
148 } else if (ObjTypeStr == "index") {
149 if (auto Err = setupTemplateResources(CDCtx, JSON, RelativeRootPath))
150 return Err;
151 assert(IndexTemplate && "IndexTemplate is nullptr.");
152 IndexTemplate->render(JSON, OS);
153 }
154 return Error::success();
155}
156
157Error HTMLGenerator::generateDocForInfo(Info *I, raw_ostream &OS,
158 const ClangDocContext &CDCtx) {
159 switch (I->IT) {
168 break;
170 return createStringError(inconvertibleErrorCode(), "unexpected InfoType");
171 }
172 return Error::success();
173}
174
176 std::string ResourcePath(CDCtx.OutDirectory + "/html");
177 for (const auto &FilePath : CDCtx.UserStylesheets)
178 if (Error Err = copyFile(FilePath, ResourcePath))
179 return Err;
180 for (const auto &FilePath : CDCtx.JsScripts)
181 if (Error Err = copyFile(FilePath, ResourcePath))
182 return Err;
183 return Error::success();
184}
185
187 llvm::StringMap<doc::Info *> Infos,
188 const ClangDocContext &CDCtx,
189 std::string DirName) {
190 return MustacheGenerator::generateDocumentation(RootDir, std::move(Infos),
191 CDCtx, "html");
192}
193
194const char *HTMLGenerator::Format = "html";
195
196static GeneratorRegistry::Add<HTMLGenerator>
197 HTML(HTMLGenerator::Format, "Generator for mustache HTML output.");
198
199// This anchor is used to force the linker to link in the generated object
200// file and thus register the generator.
202
203} // namespace doc
204} // namespace clang
This file contains file I/O utility functions used in clang-doc, such as creating directories and wri...
Error generateDocForJSON(json::Value &JSON, raw_fd_ostream &OS, const ClangDocContext &CDCtx, StringRef ObjTypeStr, StringRef RelativeRootPath) override
static const char * Format
Error createResources(ClangDocContext &CDCtx) override
Error generateDocForInfo(Info *I, raw_ostream &OS, const ClangDocContext &CDCtx) override
llvm::Error generateDocumentation(StringRef RootDir, llvm::StringMap< doc::Info * > Infos, const ClangDocContext &CDCtx, std::string DirName) override
Error setupTemplateFiles(const ClangDocContext &CDCtx) override
Initializes the template files from disk and calls setupTemplate to register partials.
Error setupTemplateResources(const ClangDocContext &CDCtx, json::Value &V, SmallString< 128 > RelativeRootPath)
llvm::Error copyFile(llvm::StringRef FilePath, llvm::StringRef OutDirectory)
Definition File.cpp:21
static std::unique_ptr< MustacheTemplateFile > RecordTemplate
static GeneratorRegistry::Add< JSONGenerator > JSON(JSONGenerator::Format, "Generator for JSON output.")
static std::unique_ptr< MustacheTemplateFile > IndexTemplate
volatile int HTMLGeneratorAnchorSource
static std::unique_ptr< MustacheTemplateFile > NamespaceTemplate
static GeneratorRegistry::Add< HTMLGenerator > HTML(HTMLGenerator::Format, "Generator for mustache HTML output.")
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Some operations such as code completion produce a set of candidates.
Definition Generators.h:152
std::vector< std::string > UserStylesheets
llvm::StringMap< std::string > MustacheTemplates
std::vector< std::string > JsScripts
A base struct for Infos.
llvm::Error generateDocumentation(StringRef RootDir, llvm::StringMap< doc::Info * > Infos, const clang::doc::ClangDocContext &CDCtx, std::string DirName) override
The main orchestrator for Mustache-based documentation.
llvm::Error setupTemplate(std::unique_ptr< MustacheTemplateFile > &Template, StringRef TemplatePath, std::vector< std::pair< StringRef, StringRef > > Partials)
Registers partials to templates.