diff options
-rw-r--r-- | inc/FileAssembler.h | 53 | ||||
-rw-r--r-- | res/template-website/pages/Home | 3 | ||||
-rw-r--r-- | res/template-website/posts/day later post | 2 | ||||
-rw-r--r-- | res/template-website/posts/month later post | 2 | ||||
-rw-r--r-- | res/template-website/posts/new year post | 2 | ||||
-rw-r--r-- | src/FileAssembler.cpp | 58 | ||||
-rw-r--r-- | src/main.cpp | 4 |
7 files changed, 89 insertions, 35 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); diff --git a/res/template-website/pages/Home b/res/template-website/pages/Home index d35cb13..12a2b27 100644 --- a/res/template-website/pages/Home +++ b/res/template-website/pages/Home @@ -1,3 +1,6 @@ ## Welcome Hi, enjoy my website. + +### Latest posts: +<ul>$list_post(2)$</ul> diff --git a/res/template-website/posts/day later post b/res/template-website/posts/day later post new file mode 100644 index 0000000..1652e65 --- /dev/null +++ b/res/template-website/posts/day later post @@ -0,0 +1,2 @@ +$date(06.10.2021)$ +first post diff --git a/res/template-website/posts/month later post b/res/template-website/posts/month later post new file mode 100644 index 0000000..69ec43e --- /dev/null +++ b/res/template-website/posts/month later post @@ -0,0 +1,2 @@ +$date(05.11.2021)$ +first post diff --git a/res/template-website/posts/new year post b/res/template-website/posts/new year post new file mode 100644 index 0000000..49d7ea8 --- /dev/null +++ b/res/template-website/posts/new year post @@ -0,0 +1,2 @@ +$date(01.01.2022)$ +first post of the year 2022 diff --git a/src/FileAssembler.cpp b/src/FileAssembler.cpp index b224a0c..cc8cc98 100644 --- a/src/FileAssembler.cpp +++ b/src/FileAssembler.cpp @@ -33,13 +33,14 @@ FileAssembler::FileAssembler(string path): path(path){ pages[l2.path().filename()] = content.substr(0,content.length()-1); //keeping plaintext link else pages[l2.path().filename()] = parser->Parse(ss); //parsing md to html - }else{ + }else{ // Posts ss.str(content); posts[l2.path().filename()] = parser->Parse(ss); //parsing md to html } } } } + ordered_posts_indexes = get_ordered_posts_indexes(); // Only used when a listing of posts in a page is necessary } void FileAssembler::parse_variables(){ @@ -76,28 +77,22 @@ string FileAssembler::get_file_content(string path){ return content; } -map <string, string> FileAssembler::get_pages(){ +map<string, string> FileAssembler::get_pages(){ return assemble_from_iterator(pages.begin(), pages.end(), false); } -map <string, string> FileAssembler::get_posts(){ +map<string, string> FileAssembler::get_posts(){ return assemble_from_iterator(posts.begin(), posts.end(), true); } -map <string, string> FileAssembler::assemble_from_iterator(map<string, string>::iterator it, map<string, string>::iterator end, bool is_post){ +map<string, string> FileAssembler::assemble_from_iterator(map<string, string>::iterator it, map<string, string>::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); - }/* else if(templates.find("menu_listing.html") == templates.end()){ - cerr << "Error: swg: menu_listing.html is not present in the sourced folder." << endl; - exit(2); - } else if(templates.find("post_listing.html") == templates.end()){ - cerr << "Error: swg: post_listing.html is not present in the sourced folder." << endl; - exit(2); - }*/ + } map<string, string> p_it; while (it != end){ if(it->first.substr(0, 5) != "link_"){ // Ignoring link pages @@ -143,8 +138,8 @@ string FileAssembler::parse(string title, string to_parse, bool is_post){ output = title; else if(input.substr(0, 5) == "date("){ - variables["date"] = parse_arg("date",to_parse); - output = " "; // I have to fill the variable to erase the $ section + variables["date"] = parse_arg(input); + output = variables["date"]; }else if(variables.find(input) != variables.end()) //VARIABLES output = variables[input]; @@ -171,8 +166,15 @@ string FileAssembler::parse(string title, string to_parse, bool is_post){ } else if(input.substr(0,5) == "list_"){ //LISTINGS - - string name = input.substr(5,input.length()-1-4); + string name = ""; + int arg = -1; + static std::regex rgx("\\_([a-z]*)(\\((\\d*)\\))?"); + std::smatch match; + if (std::regex_search(input, match, rgx)){ + name = match[1]; + if(match[3] != "") + arg = stoi(match[3]); + } string list_template = name + "_listing.html"; if(templates.find(list_template) == templates.end()){ @@ -190,20 +192,25 @@ string FileAssembler::parse(string title, string to_parse, bool is_post){ variables["link"] = current_link; }else if(name == "post"){ string current_date = ""; - string current_link = variables["link"]; + string current_link = variables["link"]; if(variables.find("date") != templates.end()) current_date = variables["date"]; - map<string, string>::iterator it = posts.begin(); - while (it != posts.end()){ - string date = parse_arg("date", it->second); + + int i = 0; + vector<string>::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->first << "' is not defined." << endl; + cerr << "Error: swg: Variable 'date' of post '" << *it << "' is not defined." << endl; exit(5); } - output += parse(it->first,templates[list_template], true); + output += parse(*it,templates[list_template], true); it ++; + i ++; } if(current_date != "") variables["date"] = current_date; @@ -221,12 +228,3 @@ string FileAssembler::parse(string title, string to_parse, bool is_post){ return parsed; } -string FileAssembler::parse_arg(string arg_name, string to_parse){ - if(to_parse.substr(3,6) != "$"+arg_name+"(") - return ""; - size_t end = to_parse.find(")"); - if (end == string::npos) - return ""; - string parsed = to_parse.substr(9, end-9); - return parsed; -} diff --git a/src/main.cpp b/src/main.cpp index 333a841..634d6fc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,10 +21,6 @@ void write_file(string path, string content){ } } -void recursive_directory_copy(const fs::path& src, const fs::path& dst) noexcept -{ -} - int generateWebsite(string arg, string config){ // cout << arg << " " << config << endl; if(config[config.length()-1] != '/') |