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