[clang-tools-extra] [clang-doc] switched from using relative to absolute paths (PR #93281)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jun 29 01:27:11 PDT 2024
https://github.com/PeterChou1 updated https://github.com/llvm/llvm-project/pull/93281
>From f5872e7c82d097ae3c141765d3f1d7e3d0b25b82 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Fri, 24 May 2024 04:28:08 -0400
Subject: [PATCH 1/3] clang-doc switched from using relative to absolute paths
---
clang-tools-extra/clang-doc/assets/index.js | 72 ++++++++++-----------
1 file changed, 35 insertions(+), 37 deletions(-)
diff --git a/clang-tools-extra/clang-doc/assets/index.js b/clang-tools-extra/clang-doc/assets/index.js
index a5ac90f2e06e7..fe35e706cc98d 100644
--- a/clang-tools-extra/clang-doc/assets/index.js
+++ b/clang-tools-extra/clang-doc/assets/index.js
@@ -1,48 +1,46 @@
-// Append using posix-style a file name or directory to Base
-function append(Base, New) {
- if (!New)
- return Base;
- if (Base)
- Base += "/";
- Base += New;
- return Base;
-}
-
-// Get relative path to access FilePath from CurrentDirectory
-function computeRelativePath(FilePath, CurrentDirectory) {
- var Path = FilePath;
- while (Path) {
- if (CurrentDirectory == Path)
- return FilePath.substring(Path.length + 1);
- Path = Path.substring(0, Path.lastIndexOf("/"));
- }
-
- var Dir = CurrentDirectory;
- var Result = "";
- while (Dir) {
- if (Dir == FilePath)
- break;
- Dir = Dir.substring(0, Dir.lastIndexOf("/"));
- Result = append(Result, "..")
+function genLink(Ref) {
+ var Path = `${window.location.protocol}//${window.location.host}/${Ref.Path}`;
+ if (Ref.RefType !== "namespace") {
+ if (Ref.Path === "") {
+ Path = `${Path}${Ref.Name}.html`;
+ }
+ else {
+ Path = `${Path}/${Ref.Name}.html`;
+ }
}
- Result = append(Result, FilePath.substring(Dir.length))
- return Result;
-}
-
-function genLink(Ref, CurrentDirectory) {
- var Path = computeRelativePath(Ref.Path, CurrentDirectory);
- if (Ref.RefType == "namespace")
- Path = append(Path, "index.html");
- else
- Path = append(Path, Ref.Name + ".html")
- ANode = document.createElement("a");
+ ANode = document.createElement("a");
ANode.setAttribute("href", Path);
var TextNode = document.createTextNode(Ref.Name);
ANode.appendChild(TextNode);
return ANode;
}
+function genHTMLOfIndex(Index, CurrentDirectory, IsOutermostList) {
+ // Out will store the HTML elements that Index requires to be generated
+ var Out = [];
+ if (Index.Name) {
+ var SpanNode = document.createElement("span");
+ var TextNode = document.createTextNode(Index.Name);
+ SpanNode.appendChild(genLink(Index));
+ Out.push(SpanNode);
+ }
+ if (Index.Children.length == 0)
+ return Out;
+ // Only the outermost list should use ol, the others should use ul
+ var ListNodeName = IsOutermostList ? "ol" : "ul";
+ var ListNode = document.createElement(ListNodeName);
+ for (Child of Index.Children) {
+ var LiNode = document.createElement("li");
+ ChildNodes = genHTMLOfIndex(Child, CurrentDirectory, false);
+ for (Node of ChildNodes)
+ LiNode.appendChild(Node);
+ ListNode.appendChild(LiNode);
+ }
+ Out.push(ListNode);
+ return Out;
+}
+
function genHTMLOfIndex(Index, CurrentDirectory, IsOutermostList) {
// Out will store the HTML elements that Index requires to be generated
var Out = [];
>From a22609f75064194604e476a12bc8154676dacfb9 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Fri, 24 May 2024 05:10:03 -0400
Subject: [PATCH 2/3] remove duplicate function
---
clang-tools-extra/clang-doc/assets/index.js | 24 ---------------------
1 file changed, 24 deletions(-)
diff --git a/clang-tools-extra/clang-doc/assets/index.js b/clang-tools-extra/clang-doc/assets/index.js
index fe35e706cc98d..b013418c82093 100644
--- a/clang-tools-extra/clang-doc/assets/index.js
+++ b/clang-tools-extra/clang-doc/assets/index.js
@@ -16,30 +16,6 @@ function genLink(Ref) {
return ANode;
}
-function genHTMLOfIndex(Index, CurrentDirectory, IsOutermostList) {
- // Out will store the HTML elements that Index requires to be generated
- var Out = [];
- if (Index.Name) {
- var SpanNode = document.createElement("span");
- var TextNode = document.createTextNode(Index.Name);
- SpanNode.appendChild(genLink(Index));
- Out.push(SpanNode);
- }
- if (Index.Children.length == 0)
- return Out;
- // Only the outermost list should use ol, the others should use ul
- var ListNodeName = IsOutermostList ? "ol" : "ul";
- var ListNode = document.createElement(ListNodeName);
- for (Child of Index.Children) {
- var LiNode = document.createElement("li");
- ChildNodes = genHTMLOfIndex(Child, CurrentDirectory, false);
- for (Node of ChildNodes)
- LiNode.appendChild(Node);
- ListNode.appendChild(LiNode);
- }
- Out.push(ListNode);
- return Out;
-}
function genHTMLOfIndex(Index, CurrentDirectory, IsOutermostList) {
// Out will store the HTML elements that Index requires to be generated
>From 48ceff111ef4ce2bfd6c9dd4ec7df272b35f3872 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Sat, 29 Jun 2024 04:25:13 -0400
Subject: [PATCH 3/3] [clang-doc] modify paths so its also viewable locally as
well as viewable when served from a server
---
clang-tools-extra/clang-doc/HTMLGenerator.cpp | 12 ++++++++++++
clang-tools-extra/clang-doc/assets/index.js | 12 +++++++++---
clang-tools-extra/test/clang-doc/basic-project.test | 1 +
3 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clang-doc/HTMLGenerator.cpp b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
index 4c72b329507d3..81ad607bc9ef8 100644
--- a/clang-tools-extra/clang-doc/HTMLGenerator.cpp
+++ b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
@@ -17,6 +17,7 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <optional>
+#include <algorithm>
#include <string>
using namespace llvm;
@@ -979,6 +980,17 @@ static llvm::Error serializeIndex(ClangDocContext &CDCtx) {
"error creating index file: " +
FileErr.message());
}
+ llvm::SmallString<128> RootPath(CDCtx.OutDirectory);
+ if (llvm::sys::path::is_relative(RootPath)) {
+ llvm::sys::fs::make_absolute(RootPath);
+ }
+ // replace escape character with forward slash it shouldn't matter
+ // when viewing from the browser this helps with preventing javascript
+ // from escaping unwanted characters leading to bad paths
+ std::string RootPathEscaped = RootPath.str().str();
+ std::replace(RootPathEscaped.begin(), RootPathEscaped.end(), '\\', '/');
+ OS << "var RootPath = \"" << RootPathEscaped << "\";\n";
+
CDCtx.Idx.sort();
llvm::json::OStream J(OS, 2);
std::function<void(Index)> IndexToJSON = [&](const Index &I) {
diff --git a/clang-tools-extra/clang-doc/assets/index.js b/clang-tools-extra/clang-doc/assets/index.js
index b013418c82093..eecac22b06e68 100644
--- a/clang-tools-extra/clang-doc/assets/index.js
+++ b/clang-tools-extra/clang-doc/assets/index.js
@@ -1,12 +1,19 @@
function genLink(Ref) {
var Path = `${window.location.protocol}//${window.location.host}/${Ref.Path}`;
+ var isFileProtocol = window.location.protocol.startsWith("file");
+ // we treat the file paths different depending on if we're
+ // serving via a http server or viewing from a local
+ if (isFileProtocol) {
+ Path = `${window.location.protocol}//${RootPath}/${Ref.Path}`;
+ }
if (Ref.RefType !== "namespace") {
if (Ref.Path === "") {
Path = `${Path}${Ref.Name}.html`;
- }
- else {
+ } else {
Path = `${Path}/${Ref.Name}.html`;
}
+ } else {
+ Path = `${Path}/index.html`
}
ANode = document.createElement("a");
@@ -16,7 +23,6 @@ function genLink(Ref) {
return ANode;
}
-
function genHTMLOfIndex(Index, CurrentDirectory, IsOutermostList) {
// Out will store the HTML elements that Index requires to be generated
var Out = [];
diff --git a/clang-tools-extra/test/clang-doc/basic-project.test b/clang-tools-extra/test/clang-doc/basic-project.test
index 3118c20495987..455fb28da0fb8 100644
--- a/clang-tools-extra/test/clang-doc/basic-project.test
+++ b/clang-tools-extra/test/clang-doc/basic-project.test
@@ -7,6 +7,7 @@
// RUN: FileCheck %s -input-file=%t/docs/GlobalNamespace/Rectangle.html -check-prefix=HTML-RECTANGLE
// RUN: FileCheck %s -input-file=%t/docs/GlobalNamespace/Circle.html -check-prefix=HTML-CIRCLE
+// JSON-INDEX: var RootPath = "{{.*}}";
// JSON-INDEX: var JsonIndex = `
// JSON-INDEX-NEXT: {
// JSON-INDEX-NEXT: "USR": "{{([0-9A-F]{40})}}",
More information about the cfe-commits
mailing list