From f6250699a5464796693d6bb637e8cf6de26bbef5 Mon Sep 17 00:00:00 2001 From: radhitya Date: Thu, 25 Jun 2026 12:23:26 +0700 Subject: site config, frontmatter, readme --- src/main.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/main.cpp (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..bffbe5f --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +#include + +using namespace std::string_literals; +struct MainConfig { + std::string title; + std::string url; + std::string author; +}; + +struct FrontmatterConfig { + std::string title; + std::string date; + std::string categories; + std::string tags; + bool draft; +}; + +struct Config { + MainConfig main; + FrontmatterConfig frontmatter; +}; + +Config +parse_config(const std::string& filepath) +{ + toml::table tbl; + Config cfg; + + try { + tbl = toml::parse_file(filepath); + } catch (const toml::parse_error& err) { + std::cerr << "parsing failed: " << err << std::endl; + throw; + } + + if (auto* main = tbl["main"].as_table()) { + cfg.main.title = (*main)["title"].value_or("untitled's blog"); + cfg.main.url = (*main)["url"].value_or("localhost"); + cfg.main.author = (*main)["author"].value_or("linus"); + } + return cfg; +} + +Config +frontmatter(const std::string& filepath) +{ + Config cfg; + std::ifstream file(filepath); + std::string content((std::istreambuf_iterator(file)), + std::istreambuf_iterator()); + + const size_t first_fence = content.find("+++"); + const size_t second_fence = (first_fence == std::string::npos) + ? std::string::npos + : content.find("+++", first_fence + 3); + + /* TIL: npos == no position */ + if (first_fence != std::string::npos && second_fence != std::string::npos) { + std::string fm = content.substr(first_fence + 3, + second_fence - (first_fence+3)); + try { + toml::parse_result result = toml::parse(fm); + const toml::table& tbl = result; + cfg.frontmatter.title = tbl["title"].value_or(""s); + cfg.frontmatter.date = tbl["date"].value_or(""s); + cfg.frontmatter.categories = tbl["categories"].value_or(""s); + cfg.frontmatter.tags = tbl["tags"].value_or(""s); + cfg.frontmatter.draft = tbl["draft"].value_or(true); + } catch(const toml::parse_error& err) { + std::cerr << "parsing failed: " << err << std::endl; + throw; + } + } + return cfg; +} + +int main(int argc, char *argv[]) { + try { + Config my_config = parse_config("config.toml"); + + std::cout << "title name: " << my_config.main.title << std::endl; + std::cout << "url name: " << my_config.main.url << std::endl; + std::cout << "author: " << my_config.main.author << std::endl; + + Config front = frontmatter(argv[1]); + std::cout << "title page: " << front.frontmatter.title << std::endl; + std::cout << "title date: " << front.frontmatter.title << std::endl; + std::cout << "title categories: " << front.frontmatter.categories << std::endl; + std::cout << "title tags: " << front.frontmatter.tags << std::endl; + std::cout << "title draft: " << front.frontmatter.draft << std::endl; + + } catch (const std::exception& e) { + std::cerr << e.what() << std::endl; + return 1; + } + return 0; +} -- cgit v1.2.3