Skip to content

Commit eeb3346

Browse files
committed
[clang][clang-doc] add asset path
1 parent 9871486 commit eeb3346

File tree

1 file changed

+55
-18
lines changed

1 file changed

+55
-18
lines changed

clang-tools-extra/clang-doc/tool/ClangDocMain.cpp

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ static llvm::cl::list<std::string> UserStylesheets(
8181
llvm::cl::desc("CSS stylesheets to extend the default styles."),
8282
llvm::cl::cat(ClangDocCategory));
8383

84+
static llvm::cl::opt<std::string>
85+
UserAssetPath("asset",
86+
llvm::cl::desc("User supplied asset path for html output to "
87+
"override the default css and js files"),
88+
llvm::cl::cat(ClangDocCategory));
89+
8490
static llvm::cl::opt<std::string> SourceRoot("source-root", llvm::cl::desc(R"(
8591
Directory where processed files are stored.
8692
Links to definition locations will only be
@@ -131,12 +137,54 @@ std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
131137
return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
132138
}
133139

140+
void GetAssetFiles(clang::doc::ClangDocContext CDCtx) {
141+
std::error_code Code;
142+
for (auto DirIt = llvm::sys::fs::directory_iterator(
143+
std::string(UserAssetPath), Code),
144+
dir_end = llvm::sys::fs::directory_iterator();
145+
!Code && DirIt != dir_end; DirIt.increment(Code)) {
146+
llvm::SmallString<128> filePath = llvm::SmallString<128>(DirIt->path());
147+
if (llvm::sys::fs::is_regular_file(filePath)) {
148+
if (filePath.ends_with(".css")) {
149+
CDCtx.UserStylesheets.push_back(std::string(filePath));
150+
} else if (filePath.ends_with(".js")) {
151+
CDCtx.FilesToCopy.push_back(std::string(filePath));
152+
}
153+
}
154+
}
155+
}
156+
157+
void GetDefaultAssetFiles(const char *Argv0,
158+
clang::doc::ClangDocContext CDCtx) {
159+
void *MainAddr = (void *)(intptr_t)GetExecutablePath;
160+
std::string ClangDocPath = GetExecutablePath(Argv0, MainAddr);
161+
llvm::SmallString<128> NativeClangDocPath;
162+
llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
163+
164+
llvm::SmallString<128> AssetsPath;
165+
AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
166+
llvm::sys::path::append(AssetsPath, "..", "share", "clang");
167+
llvm::SmallString<128> DefaultStylesheet;
168+
llvm::sys::path::native(AssetsPath, DefaultStylesheet);
169+
llvm::sys::path::append(DefaultStylesheet,
170+
"clang-doc-default-stylesheet.css");
171+
llvm::SmallString<128> IndexJS;
172+
llvm::sys::path::native(AssetsPath, IndexJS);
173+
llvm::sys::path::append(IndexJS, "index.js");
174+
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
175+
std::string(DefaultStylesheet));
176+
CDCtx.FilesToCopy.emplace_back(IndexJS.str());
177+
178+
llvm::outs() << "No default asset path found using default asset path: "
179+
<< AssetsPath << "\n";
180+
}
181+
134182
int main(int argc, const char **argv) {
135183
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
136184
std::error_code OK;
137185

138186
const char *Overview =
139-
R"(Generates documentation from source code and comments.
187+
R"(Generates documentation from source code and comments.
140188
141189
Example usage for files without flags (default):
142190
@@ -182,23 +230,12 @@ Example usage for a project using a compile commands database:
182230
{"index.js", "index_json.js"}};
183231

184232
if (Format == "html") {
185-
void *MainAddr = (void *)(intptr_t)GetExecutablePath;
186-
std::string ClangDocPath = GetExecutablePath(argv[0], MainAddr);
187-
llvm::SmallString<128> NativeClangDocPath;
188-
llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
189-
llvm::SmallString<128> AssetsPath;
190-
AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
191-
llvm::sys::path::append(AssetsPath, "..", "share", "clang");
192-
llvm::SmallString<128> DefaultStylesheet;
193-
llvm::sys::path::native(AssetsPath, DefaultStylesheet);
194-
llvm::sys::path::append(DefaultStylesheet,
195-
"clang-doc-default-stylesheet.css");
196-
llvm::SmallString<128> IndexJS;
197-
llvm::sys::path::native(AssetsPath, IndexJS);
198-
llvm::sys::path::append(IndexJS, "index.js");
199-
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
200-
std::string(DefaultStylesheet));
201-
CDCtx.FilesToCopy.emplace_back(IndexJS.str());
233+
if (!UserAssetPath.empty() &&
234+
llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
235+
GetAssetFiles(CDCtx);
236+
} else {
237+
GetDefaultAssetFiles(argv[0], CDCtx);
238+
}
202239
}
203240

204241
// Mapping phase

0 commit comments

Comments
 (0)