From 84428c144e54170ec629b379e1daf86857da2be8 Mon Sep 17 00:00:00 2001
From: Maƫl Gassmann <mael.gassmann@students.bfh.ch>
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 <iostream>
 #include <string.h>
-#include <cctype>
-#include <vector>
-#include <list>
 #include <map>
 #include <regex>
 
@@ -18,95 +15,17 @@ 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;
 
   string get_file_content(string path);
   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);
-  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);
   
-  map<string, string> get_pages();
-  map<string, string> get_posts();
-  list<string> 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<string, string>* 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 <iostream>
+#include <string.h>
+#include <vector>
+#include <map>
+#include <regex>
+
+using namespace std;
+
+class PParser{
+
+private: 
+  map<string, string> *variables;
+  map<string, string> *templates;
+  map<string, string> *pages;
+  map<string, string> *posts;
+
+  vector<string> 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<string> get_ordered_posts_indexes();
+  static bool cmp_posts(pair<string, string>& a, pair<string, string>& 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<string, string> *variables,
+  	 map<string, string> *templates,
+  	 map<string, string> *pages,
+  	 map<string, string> *posts);
+	
+  map<string, string>* parse();
+};
+#endif
-- 
cgit v1.2.3