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 --- src/FileAssembler.cpp | 165 +++++--------------------------------------------- 1 file changed, 16 insertions(+), 149 deletions(-) (limited to 'src/FileAssembler.cpp') diff --git a/src/FileAssembler.cpp b/src/FileAssembler.cpp index 7a64846..3949ca7 100644 --- a/src/FileAssembler.cpp +++ b/src/FileAssembler.cpp @@ -1,12 +1,9 @@ #include "../inc/FileAssembler.h" +#include "../inc/PParser.h" #include "../inc/maddy/parser.h" #include -#include -#include #include #include -#include -#include using namespace std; namespace fs = filesystem; @@ -40,7 +37,6 @@ FileAssembler::FileAssembler(string path): path(path){ } } } - ordered_posts_indexes = get_ordered_posts_indexes(); // Only used when a listing of posts in a page is necessary } void FileAssembler::parse_variables(){ @@ -77,154 +73,25 @@ string FileAssembler::get_file_content(string path){ return content; } -map FileAssembler::get_pages(){ - return assemble_from_iterator(pages.begin(), pages.end(), false); +map* FileAssembler::get_website(){ + PParser p = PParser(&variables, &templates, &pages, &posts); + return p.parse(); } -map FileAssembler::get_posts(){ - return assemble_from_iterator(posts.begin(), posts.end(), true); -} -map FileAssembler::assemble_from_iterator(map::iterator it, map::iterator end, bool is_post){ - if(templates.find("header.html") == templates.end()){ - cerr << "Error: swg: header.html is not present in the sourced folder." << endl; - exit(2); - } else if(templates.find("footer.html") == templates.end()){ - cerr << "Error: swg: footer.html is not present in the sourced folder." << endl; - exit(2); - } - map p_it; - while (it != end){ - if(it->first.substr(0, 5) != "link_"){ // Ignoring link pages - p_it[it->first] = parse(it->first, templates["header.html"] + it->second + templates["footer.html"], is_post); - } - it ++; - } - return p_it; +string FileAssembler::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 FileAssembler::parse(string title, string to_parse, bool is_post){ - string parsed = to_parse; - string url = variables["website"]; - - if(is_post) - variables["link"] = url + "posts/" + lowercase(title) + ".html"; - else{ - if(title == variables["index"]){ - variables["link"] = url + "index.html"; - }else if(title.substr(0, 5) == "link_"){ // Link - variables["link"] = pages[title]; - }else{ - variables["link"] = url + lowercase(title) + ".html"; - } - variables.erase("date"); // we are not doing it if it's a post, because it could be a listing - } - // Parsing variables and functions - size_t pos_first = 0; - size_t pos_second = 0; - static std::regex r_expression("\\$([^\\$]*)\\$"); - std::smatch m_expression; - while (regex_search(parsed, m_expression, r_expression)){ - string input = m_expression[1]; //Group 1 ie. without the dollars - string output = ""; - - if(input == "title") //TITLE - if(title.substr(0, 5) == "link_") // Link - output = title.substr(5,title.length()-5); - else - output = title; - - else if(input.substr(0, 5) == "date("){ - variables["date"] = parse_arg(input); - output = variables["date"]; - }else if(variables.find(input) != variables.end()) //VARIABLES - output = variables[input]; - - else if(input.substr(0,4) == "res("){ //RESOURCES - string full_path = input.substr(4,input.length()-1-4); - output = url + full_path; - - if(full_path.find("/") != string::npos){ - size_t s; - - string tok; - string last_tok; - - while ((s = full_path.find("/")) != string::npos){ // Adding the path progressively - tok = full_path.substr(0,s-1); - if(find(cached_res.begin(), cached_res.end(), last_tok+tok) == cached_res.end()) - cached_res.push_back(last_tok+tok); // Not found, adding it - last_tok += tok+"/"; - full_path.erase(0,s); - } - } - if(find(cached_res.begin(), cached_res.end(), input.substr(4,input.length()-1-4)) == cached_res.end()) - cached_res.push_back(input.substr(4,input.length()-1-4)); // Not found, adding it - - - } else if(input.substr(0,5) == "list_"){ //LISTINGS - string name = ""; - int arg = -1; - static std::regex r_listing("\\_([a-z]*)(\\((\\d*)\\))?"); - std::smatch m_listing; - if (std::regex_search(input, m_listing, r_listing)){ - name = m_listing[1]; - if(m_listing[3] != "") - arg = stoi(m_listing[3]); - } - string list_template = name + "_listing.html"; - - if(templates.find(list_template) == templates.end()){ - cerr << "Error: swg: Listing template '" << list_template << "' does not exist." << endl; - exit(2); - } - - if(name == "menu"){ - string current_link = variables["link"]; - map::iterator it = pages.begin(); - while (it != pages.end()){ - output += parse(it->first,templates[list_template], false); - it ++; - } - variables["link"] = current_link; - }else if(name == "post"){ - string current_date = ""; - string current_link = variables["link"]; - if(variables.find("date") != templates.end()) - current_date = variables["date"]; - - int i = 0; - vector::iterator it = ordered_posts_indexes.begin(); - while (it != ordered_posts_indexes.end() && - (arg == -1 || i < arg) // Limiting the number of posts shown if arg is set - ){ - string date = parse_arg(posts[*it]); - if(date != "") - variables["date"] = date; - else{ - cerr << "Error: swg: Variable 'date' of post '" << *it << "' is not defined." << endl; - exit(5); - } - output += parse(*it,templates[list_template], true); - it ++; - i ++; - } - if(current_date != "") - variables["date"] = current_date; - else - variables.erase("date"); - variables["link"] = current_link; - } - } - if(output.length() == 0){ - cerr << "Error: swg: Invalid swg text section: \"" << input << "\"." << endl; - exit(4); - } - //cout << "BEFORE:" << endl << parsed << endl << endl; - parsed = regex_replace(parsed, r_expression, output, regex_constants::format_first_only); - //cout << "AFTER:" << endl << parsed << endl ; - //cout << endl << "----------------------------------------------------" << endl << endl; +string FileAssembler::get_index(){ + if(variables.find("index") == variables.end()){ + return ""; } - return parsed; + return variables["index"]; } - -- cgit v1.2.3