diff --git a/src/common/generators/plantuml/generator.h b/src/common/generators/plantuml/generator.h index 19114727..9b62a281 100644 --- a/src/common/generators/plantuml/generator.h +++ b/src/common/generators/plantuml/generator.h @@ -200,7 +200,7 @@ inja::json generator::element_context(const E &e) const std::filesystem::relative(file, ctx["git"]["toplevel"]) .string(); - ctx["element"]["source"]["path"] = relative_path; + ctx["element"]["source"]["path"] = util::path_to_url(relative_path); ctx["element"]["source"]["full_path"] = file.string(); ctx["element"]["source"]["name"] = file.filename().string(); ctx["element"]["source"]["line"] = e.line(); diff --git a/src/util/util.cc b/src/util/util.cc index 45bb3ec7..f683c0b5 100644 --- a/src/util/util.cc +++ b/src/util/util.cc @@ -303,4 +303,20 @@ std::size_t hash_seed(std::size_t seed) return kSeedStart + (seed << kSeedShiftFirst) + (seed >> kSeedShiftSecond); } +std::string path_to_url(const std::filesystem::path& p) { + std::vector path_tokens; + auto it = p.begin(); + if(p.has_root_directory()) + it++; + + for(; it != p.end(); it++) + path_tokens.push_back(it->string()); + + if(p.has_root_directory()) + return fmt::format("/{}", fmt::join(path_tokens, "/")); + else + return fmt::format("{}", fmt::join(path_tokens, "/")); + +} + } // namespace clanguml::util diff --git a/src/util/util.h b/src/util/util.h index 2f2e47eb..48f1cc1e 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -248,4 +248,15 @@ void for_each_if(const T &collection, C &&cond, F &&func) std::size_t hash_seed(std::size_t seed); +/** + * @brief Convert filesystem path to url path + * + * The purpose of this function is to make sure that a path can + * be used in a URL, e.g. it's separators are POSIX-style. + * + * @param p Path to convert + * @return String representation of the path in URL format + */ +std::string path_to_url(const std::filesystem::path& p); + } // namespace clanguml::util \ No newline at end of file diff --git a/tests/test_util.cc b/tests/test_util.cc index 06410159..d61ebeec 100644 --- a/tests/test_util.cc +++ b/tests/test_util.cc @@ -169,3 +169,26 @@ TEST_CASE("Test parse_unexposed_template_params", "[unit-test]") CHECK(declaration_template[1].type() == "Result"); CHECK(declaration_template[2].type() == "Tail"); } + +TEST_CASE("Test path_to_url", "[unit-test]") +{ + namespace fs = std::filesystem; + using namespace clanguml::util; + + fs::path p1{""}; + p1.make_preferred(); + + CHECK(path_to_url(p1) == ""); + + fs::path p2{"a/b/c/d"}; + p2.make_preferred(); + CHECK(path_to_url(p2) == "a/b/c/d"); + + fs::path p3{"/a/b/c/d"}; + p3.make_preferred(); + CHECK(path_to_url(p3) == "/a/b/c/d"); + + fs::path p4{"/"}; + p4.make_preferred(); + CHECK(path_to_url(p4) == "/"); +}