From fbdc97a6c8960094f8c537fa8ba72d30c8f9350b Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Mon, 11 Dec 2023 22:14:57 -0500 Subject: [PATCH] Add options to configure the source url paths --- .github/workflows/subdoc.yml | 3 +++ .github/workflows/try.yml | 3 +++ subdoc/lib/run_options.h | 13 +++++++++++++ subdoc/lib/visit.cc | 14 ++++++++++++++ subdoc/subdoc_main.cc | 21 +++++++++++++++++++++ tools/run_subdoc.bat | 2 ++ tools/run_subdoc.sh | 2 ++ 7 files changed, 58 insertions(+) diff --git a/.github/workflows/subdoc.yml b/.github/workflows/subdoc.yml index e9ad9b80f..8f149b697 100644 --- a/.github/workflows/subdoc.yml +++ b/.github/workflows/subdoc.yml @@ -12,6 +12,7 @@ jobs: env: clang_version: 16 installed_clang_version: 14 + source_url: "https://github.com/chromium/subspace/blob/main" steps: - uses: actions/checkout@v3 @@ -169,6 +170,8 @@ jobs: --project-logo logo.png \ --project-md sus/project.md \ --project-name Subspace \ + --remove-source-path-prefix $PWD \ + --add-source-path-prefix ${source_url} \ /home/runner/work/subspace/subspace/sus - name: Deploy diff --git a/.github/workflows/try.yml b/.github/workflows/try.yml index 0edb79cb8..6e8543ccf 100644 --- a/.github/workflows/try.yml +++ b/.github/workflows/try.yml @@ -270,6 +270,7 @@ jobs: env: clang_version: 16 installed_clang_version: 14 + source_url: "https://github.com/chromium/subspace/blob/main" steps: - uses: actions/checkout@v3 @@ -417,6 +418,8 @@ jobs: --project-logo logo.png \ --project-md sus/project.md \ --project-name Subspace \ + --remove-source-path-prefix $PWD \ + --add-source-path-prefix ${source_url} \ /home/runner/work/subspace/subspace/sus bench: diff --git a/subdoc/lib/run_options.h b/subdoc/lib/run_options.h index 03076229c..e50fedd64 100644 --- a/subdoc/lib/run_options.h +++ b/subdoc/lib/run_options.h @@ -49,6 +49,14 @@ struct RunOptions { macro_prefixes = sus::move(prefixes); return sus::move(*this); } + RunOptions set_remove_path_prefix(sus::Option prefix) && { + remove_path_prefix = sus::move(prefix); + return sus::move(*this); + } + RunOptions set_add_path_prefix(sus::Option prefix) && { + add_path_prefix = sus::move(prefix); + return sus::move(*this); + } /// Whether to print progress while collecting documentation from souce files. bool show_progress = true; @@ -69,6 +77,11 @@ struct RunOptions { /// global namespace/project overview page. This is the raw markdown text, not /// parsed to html yet. std::string project_overview_text; + /// A prefix to remove from all paths in source links. + sus::Option remove_path_prefix; + /// A prefix to add to all paths in source links, after removing the prefix + /// specified by `remove_path_prefix`. + sus::Option add_path_prefix; }; } // namespace subdoc diff --git a/subdoc/lib/visit.cc b/subdoc/lib/visit.cc index 7b0f82914..8cd52e868 100644 --- a/subdoc/lib/visit.cc +++ b/subdoc/lib/visit.cc @@ -1579,6 +1579,20 @@ class Visitor : public clang::RecursiveASTVisitor { auto canonical_path = std::string(sm.getFilename(loc)); std::replace(canonical_path.begin(), canonical_path.end(), '\\', '/'); + if (cx_.options.remove_path_prefix.is_some()) { + const auto& prefix = cx_.options.remove_path_prefix.as_value(); + if (canonical_path.starts_with(prefix)) { + canonical_path = canonical_path.substr(prefix.size()); + if (canonical_path.starts_with("/")) + canonical_path = canonical_path.substr(1u); + } + } + if (cx_.options.add_path_prefix.is_some()) { + const auto& prefix = cx_.options.add_path_prefix.as_value(); + if (!prefix.ends_with("/")) canonical_path.insert(0u, "/"); + canonical_path.insert(0u, prefix); + } + auto link = sus::Option(SourceLink{ .quality = quality, .file_path = sus::move(canonical_path), diff --git a/subdoc/subdoc_main.cc b/subdoc/subdoc_main.cc index fc102f6c0..09138d6e0 100644 --- a/subdoc/subdoc_main.cc +++ b/subdoc/subdoc_main.cc @@ -106,6 +106,18 @@ int main(int argc, const char** argv) { "multiple times for multiple prefixes."), llvm::cl::cat(option_category)); + llvm::cl::opt option_remove_path_prefix( + "remove-source-path-prefix", + llvm::cl::desc("A path prefix to remove from all source code links."), + llvm::cl::cat(option_category)); + + llvm::cl::opt option_add_path_prefix( + "add-source-path-prefix", + llvm::cl::desc( + "A path prefix to add to all source code links, after any prefix " + "specified by `--remove-source-path-prefix` is removed."), + llvm::cl::cat(option_category)); + llvm::cl::opt option_ignore_bad_code_links( "ignore-bad-code-links", llvm::cl::desc("Ignore bad code links, don't generate an error. Useful " @@ -208,6 +220,15 @@ int main(int argc, const char** argv) { sus::iter::from_range(option_include_macro_prefixes) .cloned() .collect>(); + if (option_remove_path_prefix.getNumOccurrences() > 0) { + // Canonicalize the path to use `/` instead of `\`. + std::string canonical_path = option_remove_path_prefix.getValue(); + std::replace(canonical_path.begin(), canonical_path.end(), '\\', '/'); + run_options.remove_path_prefix = sus::some(sus::move(canonical_path)); + } + if (option_add_path_prefix.getNumOccurrences() > 0) { + run_options.add_path_prefix = sus::some(option_add_path_prefix.getValue()); + } auto fs = llvm::vfs::getRealFileSystem(); auto result = subdoc::run_files(comp_db, sus::move(run_against_files), diff --git a/tools/run_subdoc.bat b/tools/run_subdoc.bat index 8f41e031c..036aaf1f6 100644 --- a/tools/run_subdoc.bat +++ b/tools/run_subdoc.bat @@ -13,4 +13,6 @@ out\subdoc\subdoc -p out --out docs ^ --project-logo logo.png ^ --project-name Subspace ^ --ignore-bad-code-links ^ + --remove-source-path-prefix %cd% ^ + --add-source-path-prefix .. ^ subspace/sus/num/uptr_uni reverse_uni diff --git a/tools/run_subdoc.sh b/tools/run_subdoc.sh index 73fcc81c0..98f58756a 100755 --- a/tools/run_subdoc.sh +++ b/tools/run_subdoc.sh @@ -15,4 +15,6 @@ out/subdoc/subdoc -p out --out docs \ --project-logo logo.png \ --project-name Subspace \ --ignore-bad-code-links \ + --remove-source-path-prefix $PWD \ + --add-source-path-prefix .. \ i8_unittest $*