Skip to content

Commit

Permalink
Add options to configure the source url paths
Browse files Browse the repository at this point in the history
  • Loading branch information
danakj committed Dec 12, 2023
1 parent 4376854 commit fbdc97a
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/subdoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/try.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions subdoc/lib/run_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ struct RunOptions {
macro_prefixes = sus::move(prefixes);
return sus::move(*this);
}
RunOptions set_remove_path_prefix(sus::Option<std::string> prefix) && {
remove_path_prefix = sus::move(prefix);
return sus::move(*this);
}
RunOptions set_add_path_prefix(sus::Option<std::string> 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;
Expand All @@ -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<std::string> remove_path_prefix;
/// A prefix to add to all paths in source links, after removing the prefix
/// specified by `remove_path_prefix`.
sus::Option<std::string> add_path_prefix;
};

} // namespace subdoc
14 changes: 14 additions & 0 deletions subdoc/lib/visit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,20 @@ class Visitor : public clang::RecursiveASTVisitor<Visitor> {
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>(SourceLink{
.quality = quality,
.file_path = sus::move(canonical_path),
Expand Down
21 changes: 21 additions & 0 deletions subdoc/subdoc_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ int main(int argc, const char** argv) {
"multiple times for multiple prefixes."),
llvm::cl::cat(option_category));

llvm::cl::opt<std::string> 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<std::string> 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<bool> option_ignore_bad_code_links(
"ignore-bad-code-links",
llvm::cl::desc("Ignore bad code links, don't generate an error. Useful "
Expand Down Expand Up @@ -208,6 +220,15 @@ int main(int argc, const char** argv) {
sus::iter::from_range(option_include_macro_prefixes)
.cloned()
.collect<sus::Vec<std::string>>();
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),
Expand Down
2 changes: 2 additions & 0 deletions tools/run_subdoc.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions tools/run_subdoc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 $*

0 comments on commit fbdc97a

Please sign in to comment.