Skip to content

Commit

Permalink
Elevate page metadata into TOC entries (#65)
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti authored Jan 3, 2025
1 parent 40453e6 commit 86e6849
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 44 deletions.
6 changes: 2 additions & 4 deletions src/enterprise/enterprise_explorer.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,19 @@ static auto file_manager(std::ostringstream &html,
html << "</a>";
html << "</td>";

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

assert(entry.defines("description"));
html << "<td>";
html << "<small>";
if (entry.at("description").is_string()) {
if (entry.defines("description")) {
html << entry.at("description").to_string();
} else {
html << "-";
Expand Down
66 changes: 32 additions & 34 deletions src/enterprise/enterprise_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,52 +33,37 @@ auto generate_toc(const sourcemeta::jsontoolkit::JSON &configuration,
entry.path().string().substr(base.string().size())};
if (entry.is_directory()) {
entry_json.assign("type", sourcemeta::jsontoolkit::JSON{"directory"});
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("schemas").defines(collection_entry_name) &&
configuration.at("schemas").at(collection_entry_name).is_object() &&
configuration.at("schemas")
.at(collection_entry_name)
.defines("title") &&
configuration.at("schemas")
.at(collection_entry_name)
.at("title")
.is_string()) {
entry_json.assign("title", sourcemeta::jsontoolkit::JSON{
configuration.at("schemas")
.at(collection_entry_name)
.at("title")
.to_string()});
}

if (configuration.at("schemas").defines(collection_entry_name) &&
configuration.at("schemas").at(collection_entry_name).is_object() &&
configuration.at("schemas")
.at(collection_entry_name)
.defines("description") &&
configuration.at("schemas")
.at(collection_entry_name)
.at("description")
.is_string()) {
entry_json.assign("description", sourcemeta::jsontoolkit::JSON{
configuration.at("schemas")
.at(collection_entry_name)
.at("description")
.to_string()});
if (configuration.defines("pages") &&
configuration.at("pages").defines(collection_entry_name)) {
const auto &page_entry{
configuration.at("pages").at(collection_entry_name)};
if (page_entry.defines("title")) {
entry_json.assign("title", sourcemeta::jsontoolkit::JSON{
configuration.at("pages")
.at(collection_entry_name)
.at("title")
.to_string()});
}

if (page_entry.defines("description")) {
entry_json.assign("description", sourcemeta::jsontoolkit::JSON{
configuration.at("pages")
.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 Down Expand Up @@ -109,6 +94,19 @@ auto generate_toc(const sourcemeta::jsontoolkit::JSON &configuration,
});

auto result{sourcemeta::jsontoolkit::JSON::make_object()};
const auto page_entry_name{
std::filesystem::relative(directory, base).string()};

// Precompute page metadata
if (configuration.defines("pages") &&
configuration.at("pages").defines(page_entry_name)) {
for (const auto &entry :
configuration.at("pages").at(page_entry_name).as_object()) {
result.assign(entry.first, entry.second);
}
}

// Store entries
result.assign("entries", std::move(entries));

// Precompute the breadcrumb
Expand Down
6 changes: 0 additions & 6 deletions test/cli/ee/index/directory-index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ cat << 'EOF' > "$TMP/index-top-level.json"
{
"name": "example",
"type": "directory",
"title": null,
"description": null,
"url": "/example"
}
],
Expand All @@ -52,8 +50,6 @@ cat << 'EOF' > "$TMP/index-example.json"
{
"name": "schemas",
"type": "directory",
"title": null,
"description": null,
"url": "/example/schemas"
}
],
Expand All @@ -72,8 +68,6 @@ cat << 'EOF' > "$TMP/index-schemas.json"
{
"name": "test.json",
"type": "schema",
"title": null,
"description": null,
"url": "/example/schemas/test.json"
}
],
Expand Down

0 comments on commit 86e6849

Please sign in to comment.