clang-tools 22.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(
48 StringRef RootDir, llvm::StringMap<std::unique_ptr<doc::Info>> Infos,
49 const ClangDocContext &CDCtx, std::string DirName) override;
50};
51
53 // Template files need to use the native path when they're opened,
54 // but have to be used in POSIX style when used in HTML.
55 auto ConvertToNative = [](std::string &&Path) -> std::string {
56 SmallString<128> PathBuf(Path);
57 llvm::sys::path::native(PathBuf);
58 return PathBuf.str().str();
59 };
60
61 std::string NamespaceFilePath =
62 ConvertToNative(CDCtx.MustacheTemplates.lookup("namespace-template"));
63 std::string ClassFilePath =
64 ConvertToNative(CDCtx.MustacheTemplates.lookup("class-template"));
65 std::string IndexFilePath =
66 ConvertToNative(CDCtx.MustacheTemplates.lookup("index-template"));
67 std::string CommentFilePath =
68 ConvertToNative(CDCtx.MustacheTemplates.lookup("comment-template"));
69 std::string FunctionFilePath =
70 ConvertToNative(CDCtx.MustacheTemplates.lookup("function-template"));
71 std::string EnumFilePath =
72 ConvertToNative(CDCtx.MustacheTemplates.lookup("enum-template"));
73 std::string HeadFilePath =
74 ConvertToNative(CDCtx.MustacheTemplates.lookup("head-template"));
75 std::string NavbarFilePath =
76 ConvertToNative(CDCtx.MustacheTemplates.lookup("navbar-template"));
77 std::vector<std::pair<StringRef, StringRef>> Partials = {
78 {"Comments", CommentFilePath},
79 {"FunctionPartial", FunctionFilePath},
80 {"EnumPartial", EnumFilePath},
81 {"HeadPartial", HeadFilePath},
82 {"NavbarPartial", NavbarFilePath}};
83
84 if (Error Err = setupTemplate(NamespaceTemplate, NamespaceFilePath, Partials))
85 return Err;
86
87 if (Error Err = setupTemplate(RecordTemplate, ClassFilePath, Partials))
88 return Err;
89
90 if (Error Err = setupTemplate(IndexTemplate, IndexFilePath, Partials))
91 return Err;
92
93 return Error::success();
94}
95
97 json::Value &V,
98 SmallString<128> RelativeRootPath) {
99 V.getAsObject()->insert({"ProjectName", CDCtx.ProjectName});
100 json::Value StylesheetArr = Array();
101 sys::path::native(RelativeRootPath, sys::path::Style::posix);
102
103 auto *SSA = StylesheetArr.getAsArray();
104 SSA->reserve(CDCtx.UserStylesheets.size());
105 for (const auto &FilePath : CDCtx.UserStylesheets) {
106 SmallString<128> StylesheetPath = RelativeRootPath;
107 sys::path::append(StylesheetPath, sys::path::Style::posix,
108 sys::path::filename(FilePath));
109 SSA->emplace_back(StylesheetPath);
110 }
111 V.getAsObject()->insert({"Stylesheets", StylesheetArr});
112
113 json::Value ScriptArr = Array();
114 auto *SCA = ScriptArr.getAsArray();
115 SCA->reserve(CDCtx.JsScripts.size());
116 for (auto Script : CDCtx.JsScripts) {
117 SmallString<128> JsPath = RelativeRootPath;
118 sys::path::append(JsPath, sys::path::Style::posix,
119 sys::path::filename(Script));
120 SCA->emplace_back(JsPath);
121 }
122 V.getAsObject()->insert({"Scripts", ScriptArr});
123 if (RelativeRootPath.empty()) {
124 RelativeRootPath = "";
125 } else {
126 sys::path::append(RelativeRootPath, "/index.html");
127 sys::path::native(RelativeRootPath, sys::path::Style::posix);
128 }
129 V.getAsObject()->insert({"Homepage", RelativeRootPath});
130 return Error::success();
131}
132
133Error HTMLGenerator::generateDocForJSON(json::Value &JSON, raw_fd_ostream &OS,
134 const ClangDocContext &CDCtx,
135 StringRef ObjTypeStr,
136 StringRef RelativeRootPath) {
137 if (ObjTypeStr == "namespace") {
138 if (auto Err = setupTemplateResources(CDCtx, JSON, RelativeRootPath))
139 return Err;
140 assert(NamespaceTemplate && "NamespaceTemplate is nullptr.");
141 NamespaceTemplate->render(JSON, OS);
142 } else if (ObjTypeStr == "record") {
143 if (auto Err = setupTemplateResources(CDCtx, JSON, RelativeRootPath))
144 return Err;
145 assert(RecordTemplate && "RecordTemplate is nullptr.");
146 RecordTemplate->render(JSON, OS);
147 } else if (ObjTypeStr == "index") {
148 if (auto Err = setupTemplateResources(CDCtx, JSON, RelativeRootPath))
149 return Err;
150 assert(IndexTemplate && "IndexTemplate is nullptr.");
151 IndexTemplate->render(JSON, OS);
152 }
153 return Error::success();
154}
155
156Error HTMLGenerator::generateDocForInfo(Info *I, raw_ostream &OS,
157 const ClangDocContext &CDCtx) {
158 switch (I->IT) {
167 break;
169 return createStringError(inconvertibleErrorCode(), "unexpected InfoType");
170 }
171 return Error::success();
172}
173
175 std::string ResourcePath(CDCtx.OutDirectory + "/html");
176 for (const auto &FilePath : CDCtx.UserStylesheets)
177 if (Error Err = copyFile(FilePath, ResourcePath))
178 return Err;
179 for (const auto &FilePath : CDCtx.JsScripts)
180 if (Error Err = copyFile(FilePath, ResourcePath))
181 return Err;
182 return Error::success();
183}
184
186 StringRef RootDir, llvm::StringMap<std::unique_ptr<doc::Info>> Infos,
187 const ClangDocContext &CDCtx, std::string DirName) {
188 return MustacheGenerator::generateDocumentation(RootDir, std::move(Infos),
189 CDCtx, "html");
190}
191
192const char *HTMLGenerator::Format = "html";
193
194static GeneratorRegistry::Add<HTMLGenerator>
195 HTML(HTMLGenerator::Format, "Generator for mustache HTML output.");
196
197// This anchor is used to force the linker to link in the generated object
198// file and thus register the generator.
200
201} // namespace doc
202} // namespace clang
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
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 generateDocumentation(StringRef RootDir, llvm::StringMap< std::unique_ptr< doc::Info > > Infos, const ClangDocContext &CDCtx, std::string DirName) override
llvm::Error copyFile(llvm::StringRef FilePath, llvm::StringRef OutDirectory)
Definition File.cpp:15
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:145
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< std::unique_ptr< 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.