Skip to content

Commit

Permalink
Support directory title/description in file manager (#51)
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 14, 2024
1 parent b94eafa commit 43a4c37
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 19 deletions.
6 changes: 6 additions & 0 deletions schemas/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
"path"
],
"properties": {
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"base": {
"type": "string",
"format": "url"
Expand Down
71 changes: 53 additions & 18 deletions src/enterprise/enterprise_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,74 @@ static auto trim(const std::string &input) -> std::string {

namespace sourcemeta::registry::enterprise {

auto generate_toc(const std::filesystem::path &base,
auto generate_toc(const sourcemeta::jsontoolkit::JSON &configuration,
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()};

assert(configuration.is_object());
assert(configuration.defines("collections"));
assert(configuration.at("collections").is_object());

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().filename()});
const auto entry_relative_path{
entry.path().string().substr(base.string().size())};
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())});

const auto collection_entry_name{entry_relative_path.substr(1)};
if (configuration.at("collections").defines(collection_entry_name) &&
configuration.at("collections")
.at(collection_entry_name)
.is_object() &&
configuration.at("collections")
.at(collection_entry_name)
.defines("title") &&
configuration.at("collections")
.at(collection_entry_name)
.at("title")
.is_string()) {
entry_json.assign("title", sourcemeta::jsontoolkit::JSON{
configuration.at("collections")
.at(collection_entry_name)
.at("title")
.to_string()});
}

if (configuration.at("collections").defines(collection_entry_name) &&
configuration.at("collections")
.at(collection_entry_name)
.is_object() &&
configuration.at("collections")
.at(collection_entry_name)
.defines("description") &&
configuration.at("collections")
.at(collection_entry_name)
.at("description")
.is_string()) {
entry_json.assign("description", sourcemeta::jsontoolkit::JSON{
configuration.at("collections")
.at(collection_entry_name)
.at("description")
.to_string()});
}

entries.push_back(std::move(entry_json));
} else if (entry.path().extension() == ".json" &&
!entry.path().stem().string().starts_with(".")) {
const auto schema{sourcemeta::jsontoolkit::from_file(entry.path())};
entry_json.assign("type", sourcemeta::jsontoolkit::JSON{"schema"});
entry_json.assign("title", sourcemeta::jsontoolkit::JSON{nullptr});
entry_json.assign("description", sourcemeta::jsontoolkit::JSON{nullptr});
if (schema.is_object() && schema.defines("title") &&
schema.at("title").is_string()) {
entry_json.assign("title", sourcemeta::jsontoolkit::JSON{
Expand All @@ -55,18 +100,8 @@ auto generate_toc(const std::filesystem::path &base,
trim(schema.at("description").to_string())});
}

if (!entry_json.defines("title")) {
entry_json.assign("title", sourcemeta::jsontoolkit::JSON{nullptr});
}

if (!entry_json.defines("description")) {
entry_json.assign("description",
sourcemeta::jsontoolkit::JSON{nullptr});
}

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

entries.push_back(std::move(entry_json));
}
Expand All @@ -85,9 +120,9 @@ auto generate_toc(const std::filesystem::path &base,
result.assign("entries", std::move(entries));

// Precompute the breadcrumb
result.assign("breadcrumb", sourcemeta::jsontoolkit::JSON::make_array());
const std::filesystem::path relative_path{directory.string().substr(
std::min(base.string().size() + 1, directory.string().size()))};
result.assign("breadcrumb", sourcemeta::jsontoolkit::JSON::make_array());
std::filesystem::path current_path{"/"};
for (const auto &part : relative_path) {
current_path = current_path / part;
Expand All @@ -106,12 +141,12 @@ auto generate_toc(const std::filesystem::path &base,
stream.close();
}

auto attach(const sourcemeta::jsontoolkit::JSON &,
auto attach(const sourcemeta::jsontoolkit::JSON &configuration,
const std::filesystem::path &, const std::filesystem::path &output)
-> int {
std::cerr << "-- Indexing directory: " << output.string() << "\n";
const auto base{std::filesystem::canonical(output / "schemas")};
generate_toc(base, base);
generate_toc(configuration, base, base);

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

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

return EXIT_SUCCESS;
Expand Down
3 changes: 2 additions & 1 deletion src/index/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_executable(schema_registry_index index.cc configure.h.in)
add_executable(schema_registry_index index.cc configure.h.in
"${PROJECT_SOURCE_DIR}/schemas/configuration.json")

noa_add_default_options(PRIVATE schema_registry_index)
set_target_properties(schema_registry_index PROPERTIES OUTPUT_NAME sourcemeta-registry-index)
Expand Down
2 changes: 2 additions & 0 deletions test/sandbox/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"path": "./schemas/example/bundling"
},
"doc": {
"title": "A sample schema folder",
"description": "For testing purposes",
"base": "https://example.com/doc",
"path": "./schemas/doc"
}
Expand Down

0 comments on commit 43a4c37

Please sign in to comment.