diff options
Diffstat (limited to 'inc')
-rw-r--r-- | inc/FileAssembler.h | 53 |
1 files changed, 52 insertions, 1 deletions
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 <iostream> #include <string.h> #include <cctype> +#include <vector> #include <list> #include <map> #include <regex> @@ -17,6 +18,7 @@ private: map<string, string> variables; map<string, string> templates; map<string, string> pages; + vector<string> ordered_posts_indexes; map<string, string> posts; list<string> cached_res; @@ -24,7 +26,56 @@ private: void parse_variables(); map<string, string> assemble_from_iterator(map<string, string>::iterator it, map<string, string>::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<string> get_ordered_posts_indexes(){ + vector<pair<string,string>> ordered_posts; + for (auto& it : posts) { + ordered_posts.push_back(it); + } + sort(ordered_posts.begin(), ordered_posts.end(), cmp_posts); + + vector<string> titles; + transform(ordered_posts.begin(), ordered_posts.end(), std::back_inserter(titles), + [](const std::pair<string, string>& p) { return p.first; }); + return titles; + } + static bool cmp_posts(pair<string, string>& a, pair<string, string>& 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); |