From 7931c65820bf44a8974de876da1ef5163ebb2343 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Fri, 3 Jan 2025 11:41:54 -0400 Subject: [PATCH] Elevate page metadata into TOC entries Signed-off-by: Juan Cruz Viotti --- src/enterprise/enterprise_explorer.h | 6 +-- src/enterprise/enterprise_index.h | 66 ++++++++++++++-------------- test/cli/ee/index/directory-index.sh | 6 --- 3 files changed, 34 insertions(+), 44 deletions(-) diff --git a/src/enterprise/enterprise_explorer.h b/src/enterprise/enterprise_explorer.h index 212c4aa..1cab042 100644 --- a/src/enterprise/enterprise_explorer.h +++ b/src/enterprise/enterprise_explorer.h @@ -171,10 +171,9 @@ static auto file_manager(std::ostringstream &html, html << ""; html << ""; - assert(entry.defines("title")); html << ""; html << ""; - if (entry.at("title").is_string()) { + if (entry.defines("title")) { html << entry.at("title").to_string(); } else { html << "-"; @@ -182,10 +181,9 @@ static auto file_manager(std::ostringstream &html, html << ""; html << ""; - assert(entry.defines("description")); html << ""; html << ""; - if (entry.at("description").is_string()) { + if (entry.defines("description")) { html << entry.at("description").to_string(); } else { html << "-"; diff --git a/src/enterprise/enterprise_index.h b/src/enterprise/enterprise_index.h index bdcfd5f..b081dbd 100644 --- a/src/enterprise/enterprise_index.h +++ b/src/enterprise/enterprise_index.h @@ -33,43 +33,30 @@ 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)); @@ -77,8 +64,6 @@ auto generate_toc(const sourcemeta::jsontoolkit::JSON &configuration, !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{ @@ -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 diff --git a/test/cli/ee/index/directory-index.sh b/test/cli/ee/index/directory-index.sh index 24e468b..4cde488 100755 --- a/test/cli/ee/index/directory-index.sh +++ b/test/cli/ee/index/directory-index.sh @@ -37,8 +37,6 @@ cat << 'EOF' > "$TMP/index-top-level.json" { "name": "example", "type": "directory", - "title": null, - "description": null, "url": "/example" } ], @@ -52,8 +50,6 @@ cat << 'EOF' > "$TMP/index-example.json" { "name": "schemas", "type": "directory", - "title": null, - "description": null, "url": "/example/schemas" } ], @@ -72,8 +68,6 @@ cat << 'EOF' > "$TMP/index-schemas.json" { "name": "test.json", "type": "schema", - "title": null, - "description": null, "url": "/example/schemas/test.json" } ],