Skip to content

Commit 91450f1

Browse files
authored
[clang-doc] switched from using relative to absolute paths (#93281)
fixes #92867 This patches changes the way clang-doc index navigation works, previously it was based a relative path approach, this approach is error prone and lead to wrong paths for the anchor tag. The new navigation way is based on absolute paths and should work and be less confusing codewise. Because the differences with serving over a http server and viewing via file system I also added export a RootPath variable to the index_json.js file
1 parent 6ce7b1f commit 91450f1

File tree

3 files changed

+32
-38
lines changed

3 files changed

+32
-38
lines changed

clang-tools-extra/clang-doc/HTMLGenerator.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/Support/JSON.h"
1717
#include "llvm/Support/Path.h"
1818
#include "llvm/Support/raw_ostream.h"
19+
#include <algorithm>
1920
#include <optional>
2021
#include <string>
2122

@@ -979,6 +980,18 @@ static llvm::Error serializeIndex(ClangDocContext &CDCtx) {
979980
"error creating index file: " +
980981
FileErr.message());
981982
}
983+
llvm::SmallString<128> RootPath(CDCtx.OutDirectory);
984+
if (llvm::sys::path::is_relative(RootPath)) {
985+
llvm::sys::fs::make_absolute(RootPath);
986+
}
987+
// Replace the escaped characters with a forward slash. It shouldn't matter
988+
// when rendering the webpage in a web browser. This helps to prevent the
989+
// JavaScript from escaping characters incorrectly, and introducing bad paths
990+
// in the URLs.
991+
std::string RootPathEscaped = RootPath.str().str();
992+
std::replace(RootPathEscaped.begin(), RootPathEscaped.end(), '\\', '/');
993+
OS << "var RootPath = \"" << RootPathEscaped << "\";\n";
994+
982995
CDCtx.Idx.sort();
983996
llvm::json::OStream J(OS, 2);
984997
std::function<void(Index)> IndexToJSON = [&](const Index &I) {

clang-tools-extra/clang-doc/assets/index.js

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,17 @@
1-
// Append using posix-style a file name or directory to Base
2-
function append(Base, New) {
3-
if (!New)
4-
return Base;
5-
if (Base)
6-
Base += "/";
7-
Base += New;
8-
return Base;
9-
}
10-
11-
// Get relative path to access FilePath from CurrentDirectory
12-
function computeRelativePath(FilePath, CurrentDirectory) {
13-
var Path = FilePath;
14-
while (Path) {
15-
if (CurrentDirectory == Path)
16-
return FilePath.substring(Path.length + 1);
17-
Path = Path.substring(0, Path.lastIndexOf("/"));
18-
}
19-
20-
var Dir = CurrentDirectory;
21-
var Result = "";
22-
while (Dir) {
23-
if (Dir == FilePath)
24-
break;
25-
Dir = Dir.substring(0, Dir.lastIndexOf("/"));
26-
Result = append(Result, "..")
1+
function genLink(Ref) {
2+
// we treat the file paths different depending on if we're
3+
// serving via a http server or viewing from a local
4+
var Path = window.location.protocol.startsWith("file") ?
5+
`${window.location.protocol}//${window.location.host}/${Ref.Path}` :
6+
`${window.location.protocol}//${RootPath}/${Ref.Path}`;
7+
if (Ref.RefType === "namespace") {
8+
Path = `${Path}/index.html`
9+
} else if (Ref.Path === "") {
10+
Path = `${Path}${Ref.Name}.html`;
11+
} else {
12+
Path = `${Path}/${Ref.Name}.html`;
2713
}
28-
Result = append(Result, FilePath.substring(Dir.length))
29-
return Result;
30-
}
31-
32-
function genLink(Ref, CurrentDirectory) {
33-
var Path = computeRelativePath(Ref.Path, CurrentDirectory);
34-
if (Ref.RefType == "namespace")
35-
Path = append(Path, "index.html");
36-
else
37-
Path = append(Path, Ref.Name + ".html")
38-
39-
ANode = document.createElement("a");
14+
ANode = document.createElement("a");
4015
ANode.setAttribute("href", Path);
4116
var TextNode = document.createTextNode(Ref.Name);
4217
ANode.appendChild(TextNode);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: rm -rf %t && mkdir %t
2+
// RUN: clang-doc --format=html --executor=standalone %s --output=%t
3+
// RUN: FileCheck %s -input-file=%t/index_json.js -check-prefix=JSON-INDEX
4+
// RUN: rm -rf %t
5+
6+
// JSON-INDEX: var RootPath = "{{.*}}test-path-abs.cpp.tmp";

0 commit comments

Comments
 (0)