diff --git a/src/markdown.cpp b/src/markdown.cpp index 3049ba3..8df428d 100644 --- a/src/markdown.cpp +++ b/src/markdown.cpp @@ -1,18 +1,22 @@ #include "markdown.hpp" #include "utils.hpp" -#include +#include #include #include #include #include #include +#include +#include std::optional get_metadata(const std::string &FILE_PATH) { std::fstream file(FILE_PATH); + std::stringstream metadata_s; std::string buffer; + /* if there's no ---, then no metadata in file there is */ if (!getline(file, buffer) || buffer != "---") { - std::cerr << "File " << FILE_PATH << "does not contain metadata." + std::cerr << "File " << FILE_PATH << " does not contain metadata. " << std::endl; return std::nullopt; } @@ -23,24 +27,43 @@ std::optional get_metadata(const std::string &FILE_PATH) { if (buffer == "---") { break; } - size_t colon_pos = buffer.find(":"); - - if (colon_pos == std::string::npos) { - std::cerr << "The line: \n" - << buffer << " does not contain a colon" << std::endl; - } - - std::string key = buffer.substr(0, colon_pos); - trim(key); - - std::string value = buffer.substr(colon_pos + 1); - trim(value); - metadata_map[key] = value; + metadata_s << buffer << '\n'; } - return std::optional(Metadata{metadata_map["author"], metadata_map["date"], - metadata_map["title"], - convert_string_to_set(metadata_map["tags"])}); + toml::table toml_table = toml::parse(metadata_s); + std::vector tags; + const auto &toml_tags = toml_table["metadata"]["tags"]; + + // translate toml into normal vector of strings + if (toml_tags.is_array()) { + for (auto iter = toml_tags.as_array()->begin(); + iter < toml_tags.as_array()->end(); iter++) { + tags.emplace_back(static_cast(iter->as_string()->get())); + } + } + + // time + auto toml_time = toml_table["metadata"]["date"]; + + struct tm t = {}; + time_t timestamp = time(NULL); + + if (toml_time.is_date()) { + toml::date d = toml_time.as_date()->get(); + + // To convert to a Unix timestamp (time_t): + t.tm_year = d.year - 1900; // tm_year is years since 1900 + t.tm_mon = d.month - 1; // tm_mon is 0-11 + t.tm_mday = d.day; + t.tm_isdst = -1; + + timestamp = mktime(&t); + } + + Metadata metadata = {toml_table["metadata"]["author"].value_or(""), timestamp, + toml_table["metadata"]["title"].value_or(""), tags}; + + return std::optional(metadata); } static void html_callback(const MD_CHAR *data, MD_SIZE size, void *userdata) { diff --git a/src/markdown.hpp b/src/markdown.hpp index 3bea590..cff04b1 100644 --- a/src/markdown.hpp +++ b/src/markdown.hpp @@ -1,12 +1,14 @@ +#include #include -#include #include +#include +#include struct Metadata { std::string author; - std::string date; + time_t date; std::string title; - std::set tags; + std::vector tags; }; // Reads the metadata of the markdown file diff --git a/src/tests/post.md b/src/tests/post.md index dfeaa5f..308c92e 100644 --- a/src/tests/post.md +++ b/src/tests/post.md @@ -1,8 +1,9 @@ --- -author: "joseph" -date: "2026-01-01" -title: "Example of a blog post" -tags: ["blog", "test", "lalilulelo"] +[metadata] +author = "joseph" +date = 2026-01-01 +title = "Example of a blog post" +tags = ["blog", "test", "lalilulelo"] --- # This is an example post diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp index 214e5d8..5edeb8d 100644 --- a/src/tests/tests.cpp +++ b/src/tests/tests.cpp @@ -3,6 +3,7 @@ #include "../utils.hpp" #include #include +#include #include #include @@ -25,21 +26,22 @@ TEST_CASE("convert_string_to_set parses inline YAML string list") { } TEST_CASE("get_metadata function data correctness test") { - const std::string FILE_NAME = "./tests//post.md"; + const std::string FILE_NAME = "./tests/post.md"; Metadata result = get_metadata(FILE_NAME).value(); std::string expected_author = "joseph"; - std::string expected_date = "2026-01-01"; + struct tm datetime = {0}; + datetime.tm_year = 2026 - 1900; + datetime.tm_mon = 1 - 1; + datetime.tm_mday = 1; + time_t expected_date = mktime(&datetime); std::string expected_title = "Example of a blog post"; - std::set expected_tags = {"blog", "test", "lalilulelo"}; + std::vector expected_tags = {"blog", "test", "lalilulelo"}; REQUIRE(result.author == expected_author); REQUIRE(result.date == expected_date); REQUIRE(result.title == expected_title); - - for (const auto &tag : expected_tags) { - REQUIRE(result.tags.count(tag)); - } + REQUIRE(result.tags == expected_tags); } TEST_CASE("get_metadata function test with a file that doesn't have metadata") {