diff options
author | Maël Gassmann <mael.gassmann@students.bfh.ch> | 2022-03-02 14:37:17 +0100 |
---|---|---|
committer | Maël Gassmann <mael.gassmann@students.bfh.ch> | 2022-03-02 14:37:17 +0100 |
commit | 36b3422daed37549e405ed4a5bb37b601012ca21 (patch) | |
tree | 7b7252d7bdbdb3dfbea45bfc743dd0460fb4e1bf /inc | |
parent | 7d3c9ec7368a5d25235e818c2c147ba7e89f6567 (diff) |
[+] Now ordering posts by date, possiblity to show a limited number of posts, date now prints the date
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); |