18#include "llvm/Support/Error.h"
19#include "llvm/Support/Path.h"
22using namespace llvm::json;
23using namespace llvm::mustache;
29static std::unique_ptr<MustacheTemplateFile>
RecordTemplate =
nullptr;
30static std::unique_ptr<MustacheTemplateFile>
IndexTemplate =
nullptr;
34 static StringRef Format;
35 Error createResources(ClangDocContext &CDCtx)
override;
36 Error generateDocForInfo(Info *I, raw_ostream &OS,
37 const ClangDocContext &CDCtx)
override;
38 Error setupTemplateFiles(
const ClangDocContext &CDCtx)
override;
39 Error generateDocForJSON(json::Value &
JSON, raw_fd_ostream &OS,
40 const ClangDocContext &CDCtx, StringRef ObjTypeStr,
41 StringRef RelativeRootPath)
override;
43 Error setupTemplateResources(
const ClangDocContext &CDCtx, json::Value &V,
44 SmallString<128> RelativeRootPath);
45 llvm::Error generateDocumentation(StringRef RootDir,
46 llvm::StringMap<Info *> Infos,
47 const ClangDocContext &CDCtx,
48 std::string DirName)
override;
56 auto ConvertToNative = [](std::string &&
Path) -> std::string {
57 SmallString<128> PathBuf(Path);
58 llvm::sys::path::native(PathBuf);
59 return PathBuf.str().str();
62 std::string NamespaceFilePath =
64 std::string ClassFilePath =
66 std::string IndexFilePath =
68 std::string CommentFilePath =
70 std::string FunctionFilePath =
72 std::string EnumFilePath =
74 std::string HeadFilePath =
76 std::string NavbarFilePath =
78 std::string AliasFilePath =
80 std::vector<std::pair<StringRef, StringRef>> Partials = {
81 {
"Comments", CommentFilePath}, {
"FunctionPartial", FunctionFilePath},
82 {
"EnumPartial", EnumFilePath}, {
"HeadPartial", HeadFilePath},
83 {
"NavbarPartial", NavbarFilePath}, {
"AliasPartial", AliasFilePath}};
88 if (Error Err = setupTemplate(
RecordTemplate, ClassFilePath, Partials))
91 if (Error Err = setupTemplate(
IndexTemplate, IndexFilePath, Partials))
94 return Error::success();
97Error HTMLGenerator::setupTemplateResources(
const ClangDocContext &CDCtx,
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);
104 auto *SSA = StylesheetArr.getAsArray();
107 SmallString<128> StylesheetPath = RelativeRootPath;
108 sys::path::append(StylesheetPath, sys::path::Style::posix,
109 sys::path::filename(FilePath));
110 SSA->emplace_back(StylesheetPath);
112 V.getAsObject()->insert({
"Stylesheets", StylesheetArr});
114 json::Value ScriptArr =
Array();
115 auto *SCA = ScriptArr.getAsArray();
118 SmallString<128> JsPath = RelativeRootPath;
119 sys::path::append(JsPath, sys::path::Style::posix,
120 sys::path::filename(Script));
121 SCA->emplace_back(JsPath);
123 V.getAsObject()->insert({
"Scripts", ScriptArr});
124 if (RelativeRootPath.empty()) {
125 RelativeRootPath =
"";
127 sys::path::append(RelativeRootPath,
"/index.html");
128 sys::path::native(RelativeRootPath, sys::path::Style::posix);
130 V.getAsObject()->insert({
"Homepage", RelativeRootPath});
131 return Error::success();
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))
143 }
else if (ObjTypeStr ==
"record") {
144 if (
auto Err = setupTemplateResources(CDCtx,
JSON, RelativeRootPath))
148 }
else if (ObjTypeStr ==
"index") {
149 if (
auto Err = setupTemplateResources(CDCtx,
JSON, RelativeRootPath))
154 return Error::success();
157Error HTMLGenerator::generateDocForInfo(Info *I, raw_ostream &OS,
158 const ClangDocContext &CDCtx) {
160 case InfoType::IT_enum:
161 case InfoType::IT_function:
162 case InfoType::IT_typedef:
163 case InfoType::IT_namespace:
164 case InfoType::IT_record:
165 case InfoType::IT_concept:
166 case InfoType::IT_variable:
167 case InfoType::IT_friend:
169 case InfoType::IT_default:
170 return createStringError(inconvertibleErrorCode(),
"unexpected InfoType");
172 return Error::success();
175Error HTMLGenerator::createResources(ClangDocContext &CDCtx) {
178 if (Error Err =
copyFile(FilePath, ResourcePath))
180 for (
const auto &FilePath : CDCtx.
JsScripts)
181 if (Error Err =
copyFile(FilePath, ResourcePath))
183 return Error::success();
186Error HTMLGenerator::generateDocumentation(StringRef RootDir,
187 llvm::StringMap<Info *> Infos,
188 const ClangDocContext &CDCtx,
189 std::string DirName) {
194StringRef HTMLGenerator::Format =
"html";
196static GeneratorRegistry::Add<HTMLGenerator>
197 HTML(HTMLGenerator::Format,
"Generator for mustache HTML output.");
This file contains file I/O utility functions used in clang-doc, such as creating directories and wri...
static GeneratorRegistry::Add< HTMLGenerator > HTML(HTMLGenerator::Format, "Generator for mustache HTML output.")
static GeneratorRegistry::Add< JSONGenerator > JSON(JSONGenerator::Format, "Generator for JSON output.")
static std::unique_ptr< MustacheTemplateFile > RecordTemplate
static std::unique_ptr< MustacheTemplateFile > NamespaceTemplate
static std::unique_ptr< MustacheTemplateFile > IndexTemplate
std::string Path
A typedef to represent a file path.
llvm::Error copyFile(llvm::StringRef FilePath, llvm::StringRef OutDirectory)
volatile int HTMLGeneratorAnchorSource
Some operations such as code completion produce a set of candidates.
std::vector< std::string > UserStylesheets
llvm::StringMap< std::string > MustacheTemplates
std::vector< std::string > JsScripts
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.