From 36b3422daed37549e405ed4a5bb37b601012ca21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gassmann?= Date: Wed, 2 Mar 2022 14:37:17 +0100 Subject: [+] Now ordering posts by date, possiblity to show a limited number of posts, date now prints the date --- src/FileAssembler.cpp | 58 +++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 30 deletions(-) (limited to 'src/FileAssembler.cpp') 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 FileAssembler::get_pages(){ +map FileAssembler::get_pages(){ return assemble_from_iterator(pages.begin(), pages.end(), false); } -map FileAssembler::get_posts(){ +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){ +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); - }/* 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 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::iterator it = posts.begin(); - while (it != posts.end()){ - string date = parse_arg("date", it->second); + + 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->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; -} -- cgit v1.2.3