Fixed path separators in diagram link URL's in Windows

This commit is contained in:
Bartek Kryza
2023-01-11 00:29:43 +01:00
parent c7d201eef9
commit 3fbf3da27f
4 changed files with 51 additions and 1 deletions

View File

@@ -200,7 +200,7 @@ inja::json generator<C, D>::element_context(const E &e) const
std::filesystem::relative(file, ctx["git"]["toplevel"]) std::filesystem::relative(file, ctx["git"]["toplevel"])
.string(); .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"]["full_path"] = file.string();
ctx["element"]["source"]["name"] = file.filename().string(); ctx["element"]["source"]["name"] = file.filename().string();
ctx["element"]["source"]["line"] = e.line(); ctx["element"]["source"]["line"] = e.line();

View File

@@ -303,4 +303,20 @@ std::size_t hash_seed(std::size_t seed)
return kSeedStart + (seed << kSeedShiftFirst) + (seed >> kSeedShiftSecond); return kSeedStart + (seed << kSeedShiftFirst) + (seed >> kSeedShiftSecond);
} }
std::string path_to_url(const std::filesystem::path& p) {
std::vector<std::string> 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 } // namespace clanguml::util

View File

@@ -248,4 +248,15 @@ void for_each_if(const T &collection, C &&cond, F &&func)
std::size_t hash_seed(std::size_t seed); 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 } // namespace clanguml::util

View File

@@ -169,3 +169,26 @@ TEST_CASE("Test parse_unexposed_template_params", "[unit-test]")
CHECK(declaration_template[1].type() == "Result"); CHECK(declaration_template[1].type() == "Result");
CHECK(declaration_template[2].type() == "Tail"); 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) == "/");
}