Skip to content

Commit

Permalink
Add links to file explorer (#40)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Nov 13, 2024
1 parent 841349d commit b2cea4b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
20 changes: 16 additions & 4 deletions src/enterprise/enterprise_explorer.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,34 @@ static auto file_manager(std::ostringstream &html,
assert(entry.defines("name"));
assert(entry.at("name").is_string());
html << "<td class=\"font-monospace text-nowrap\">";
assert(entry.defines("url"));
assert(entry.at("url").is_string());
html << "<a href=\"" << entry.at("url").to_string() << "\">";
html << entry.at("name").to_string();
html << "</a>";
html << "</td>";

assert(entry.defines("title"));
html << "<td>";
html << "<small>";
if (entry.at("title").is_string()) {
html << "<td>" << entry.at("title").to_string() << "</td>";
html << entry.at("title").to_string();
} else {
html << "<td>-</td>";
html << "-";
}
html << "</small>";
html << "</td>";

assert(entry.defines("description"));
html << "<td>";
html << "<small>";
if (entry.at("description").is_string()) {
html << "<td>" << entry.at("description").to_string() << "</td>";
html << entry.at("description").to_string();
} else {
html << "<td>-</td>";
html << "-";
}
html << "</small>";
html << "</td>";

html << "</tr>";
}
Expand Down
19 changes: 15 additions & 4 deletions src/enterprise/enterprise_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,24 @@ static auto trim(const std::string &input) -> std::string {

namespace sourcemeta::registry::enterprise {

auto generate_toc(const std::filesystem::path &directory) -> void {
auto generate_toc(const std::filesystem::path &base,
const std::filesystem::path &directory) -> void {
assert(directory.string().starts_with(base.string()));
auto entries{sourcemeta::jsontoolkit::JSON::make_array()};

for (const auto &entry : std::filesystem::directory_iterator{directory}) {
auto entry_json{sourcemeta::jsontoolkit::JSON::make_object()};
entry_json.assign("name",
sourcemeta::jsontoolkit::JSON{entry.path().stem()});
sourcemeta::jsontoolkit::JSON{entry.path().filename()});
if (entry.is_directory()) {
entry_json.assign("type", sourcemeta::jsontoolkit::JSON{"directory"});
// TODO: Read these from "pages" in the configuration
entry_json.assign("title", sourcemeta::jsontoolkit::JSON{nullptr});
entry_json.assign("description", sourcemeta::jsontoolkit::JSON{nullptr});
entry_json.assign(
"url", sourcemeta::jsontoolkit::JSON{
entry.path().string().substr(base.string().size())});

entries.push_back(std::move(entry_json));
} else if (entry.path().extension() == ".json" &&
!entry.path().stem().string().starts_with(".")) {
Expand Down Expand Up @@ -58,6 +64,10 @@ auto generate_toc(const std::filesystem::path &directory) -> void {
sourcemeta::jsontoolkit::JSON{nullptr});
}

entry_json.assign(
"url", sourcemeta::jsontoolkit::JSON{
entry.path().string().substr(base.string().size())});

entries.push_back(std::move(entry_json));
}
}
Expand Down Expand Up @@ -87,7 +97,8 @@ auto attach(const sourcemeta::jsontoolkit::JSON &,
const std::filesystem::path &, const std::filesystem::path &output)
-> int {
std::cerr << "-- Indexing directory: " << output.string() << "\n";
generate_toc(output / "schemas");
const auto base{std::filesystem::canonical(output / "schemas")};
generate_toc(base, base);

for (const auto &entry :
std::filesystem::recursive_directory_iterator{output / "schemas"}) {
Expand All @@ -96,7 +107,7 @@ auto attach(const sourcemeta::jsontoolkit::JSON &,
}

std::cerr << "-- Processing: " << entry.path().string() << "\n";
generate_toc(entry.path());
generate_toc(base, std::filesystem::canonical(entry.path()));
}

return EXIT_SUCCESS;
Expand Down

0 comments on commit b2cea4b

Please sign in to comment.