From 9f9685170c89ecd1c9f6c8613309aaa4528dbad3 Mon Sep 17 00:00:00 2001 From: Szymon P Date: Sat, 3 Jan 2026 21:21:09 +0100 Subject: [PATCH] added markdown metadata parsing --- src/markdown.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/markdown.hpp | 20 ++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/markdown.cpp create mode 100644 src/markdown.hpp diff --git a/src/markdown.cpp b/src/markdown.cpp new file mode 100644 index 0000000..fdb7f86 --- /dev/null +++ b/src/markdown.cpp @@ -0,0 +1,43 @@ +#include "markdown.hpp" +#include "utils.hpp" +#include +#include +#include +#include +#include + +std::optional get_metadata(const std::string &FILE_PATH) { + std::fstream file(FILE_PATH); + std::string buffer; + + if (!getline(file, buffer) || buffer != "---") { + std::cerr << "File " << FILE_PATH << "does not contain metadata." + << std::endl; + return std::nullopt; + } + + std::map metadata_map; + + while (getline(file, buffer)) { + 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; + } + + return std::optional(Metadata{metadata_map["author"], metadata_map["date"], + metadata_map["title"], + convert_string_to_set(metadata_map["tags"])}); +} diff --git a/src/markdown.hpp b/src/markdown.hpp new file mode 100644 index 0000000..8df17fd --- /dev/null +++ b/src/markdown.hpp @@ -0,0 +1,20 @@ +#include +#include +#include + +struct Metadata { + std::string author; + std::string date; + std::string title; + std::set tags; +}; + +// Reads the metadata of the markdown file +// Metadata are in yaml format at the top of the file +// They are separated from the rest of the file with `---` at the beginning and +// at the end. +std::optional get_metadata(const std::string &FILE_PATH); + +// Reads the markdown file and converts it to html. +// Return everything in one string +std::string get_html(const std::string &FILE_PATH);