From 84428c144e54170ec629b379e1daf86857da2be8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gassmann?= Date: Thu, 17 Mar 2022 16:14:17 +0100 Subject: [~] Refactoring of the FileAssembler, Extraction of methods in PParser --- inc/FileAssembler.h | 87 ++--------------------------------------------------- inc/PParser.h | 45 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 84 deletions(-) create mode 100644 inc/PParser.h (limited to 'inc') diff --git a/inc/FileAssembler.h b/inc/FileAssembler.h index 0ae61df..8126d51 100644 --- a/inc/FileAssembler.h +++ b/inc/FileAssembler.h @@ -3,9 +3,6 @@ #include #include -#include -#include -#include #include #include @@ -18,95 +15,17 @@ private: map variables; map templates; map pages; - vector ordered_posts_indexes; map posts; - list cached_res; string get_file_content(string path); 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); - 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); - map get_pages(); - map get_posts(); - list get_cached_resources(){ - return cached_res; - } - string get_target(){ - string w = variables["website"]; - static std::regex rgx("\\w+\\.\\w+"); - std::smatch match; - if (std::regex_search(w, match, rgx)) - return match[0]; - cerr << "Error: swg: website attribute is badly configured!" << endl; - exit(7); - } - string get_index(){ - if(variables.find("index") == variables.end()){ - return ""; - } - return variables["index"]; - } + map* get_website(); + string get_target(); + string get_index(); - static string lowercase(string s){ - string low = ""; - for (int i = 0; i < s.length(); i++) { - low += tolower(s[i]); - } - return low; - } }; #endif diff --git a/inc/PParser.h b/inc/PParser.h new file mode 100644 index 0000000..dad379b --- /dev/null +++ b/inc/PParser.h @@ -0,0 +1,45 @@ +#ifndef PP_H +#define PP_H + +#include +#include +#include +#include +#include + +using namespace std; + +class PParser{ + +private: + map *variables; + map *templates; + map *pages; + map *posts; + + vector ordered_posts_indexes; + + string parse_text(string title, string content, bool is_post); + static string* parse_function(string to_parse); + static string* parse_separator(string to_parse, string separator); + vector get_ordered_posts_indexes(); + static bool cmp_posts(pair& a, pair& b); + + static string lowercase(string s){ + string low = ""; + for (int i = 0; i < s.length(); i++) { + low += tolower(s[i]); + } + return low; + } + +public: + PParser( + map *variables, + map *templates, + map *pages, + map *posts); + + map* parse(); +}; +#endif -- cgit v1.2.3