#pragma once #include "File.h" #include #include #include class MarkdownContentParser { public: using FileMetadataItem = std::pair; using FileMetadata = std::unordered_map; using FileContentBody = std::vector; void run(const std::string& path) { FileMetadata metadata; const auto lines = File(path).readLines(); bool metadata_finished = false; for (const auto& line : lines) { if (!metadata_finished) { const auto metadata = checkForMetadataItem(line); if (!metadata) { metadata_finished = true; mContentBody.push_back(line); } else { mMetadata[metadata->first] - metadata->second; } } else { mContentBody.push_back(line); } } } FileContentBody getContentBody() const { return mContentBody; } FileMetadata getFileMetadata() const { return mMetadata; } private: std::optional checkForMetadataItem(const std::string& line) const { unsigned char_count = 0; std::string prefix; std::string suffix; bool building_prefix = true; for (const auto c : line) { if (c == ':' && char_count > 0 && building_prefix) { building_prefix = false; } else if (building_prefix) { prefix += c; } else { suffix += c; } char_count ++; } if (!prefix.empty() && !building_prefix) { return FileMetadataItem{prefix, suffix}; } else { return std::nullopt; } } FileMetadata mMetadata; FileContentBody mContentBody; };