From 36b3422daed37549e405ed4a5bb37b601012ca21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gassmann?= Date: Wed, 2 Mar 2022 14:37:17 +0100 Subject: [+] Now ordering posts by date, possiblity to show a limited number of posts, date now prints the date --- inc/FileAssembler.h | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) (limited to 'inc') diff --git a/inc/FileAssembler.h b/inc/FileAssembler.h index 3eaab35..1a37207 100644 --- a/inc/FileAssembler.h +++ b/inc/FileAssembler.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -17,6 +18,7 @@ private: map variables; map templates; map pages; + vector ordered_posts_indexes; map posts; list cached_res; @@ -24,7 +26,56 @@ private: void parse_variables(); map assemble_from_iterator(map::iterator it, map::iterator end, bool is_post); string parse(string title, string content, bool is_post); - string parse_arg(string arg_name, string to_parse); + static string parse_arg(string to_parse){ + static std::regex rgx("\\w+\\((.*)\\)"); + std::smatch match; + if (std::regex_search(to_parse, match, rgx)) + return match[1]; + return ""; + } + vector get_ordered_posts_indexes(){ + vector> ordered_posts; + for (auto& it : posts) { + ordered_posts.push_back(it); + } + sort(ordered_posts.begin(), ordered_posts.end(), cmp_posts); + + vector titles; + transform(ordered_posts.begin(), ordered_posts.end(), std::back_inserter(titles), + [](const std::pair& p) { return p.first; }); + return titles; + } + static bool cmp_posts(pair& a, pair& b){ + string a_date = parse_arg(a.second); + string b_date = parse_arg(b.second); + if(a_date == ""){ + cerr << "Error: swg: Variable 'date' of post '" << a.first << "' is not defined." << endl; + exit(5); + }else if (b_date == ""){ + cerr << "Error: swg: Variable 'date' of post '" << b.first << "' is not defined." << endl; + exit(5); + } + static std::regex rgx("(\\d{1,2})\\.(\\d{1,2})\\.(\\d{4})"); + std::smatch a_match; + std::smatch b_match; + if (std::regex_search(a_date, a_match, rgx)){ // For now only european swiss format handled + if (std::regex_search(b_date, b_match, rgx)){ // For now only european swiss format handled + if(a_match[3] != b_match[3]) // Trying to differentiate by year + return stoi(a_match[3]) > stoi(b_match[3]); + if(a_match[2] != b_match[2]) // Trying to differentiate by month + return stoi(a_match[2]) > stoi(b_match[2]); + if(a_match[1] != b_match[1]) // Trying to differentiate by day + return stoi(a_match[1]) > stoi(b_match[1]); + return false; // or if equal return false + }else{ + cerr << "Error: swg: Variable 'date' of post '" << b.first << "' is not following the format dd.mm.yyyy." << endl; + exit(5); + } + }else{ + cerr << "Error: swg: Variable 'date' of post '" << a.first << "' is not following the format dd.mm.yyyy." << endl; + exit(5); + } + } public: FileAssembler(string path); -- cgit v1.2.3