Sort articles by timestamp

Sort articles in std::list by timestamp to generate blog catalogs.
This commit is contained in:
luca0N! 2022-04-26 23:29:13 -03:00
parent 1e8660fb86
commit 4753920f3d
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
4 changed files with 17 additions and 4 deletions

View File

@ -103,7 +103,6 @@ namespace Article {
}
bool reading_article_metadata = false;
for (std::string const &line : cfg_lines) {
std::cout << "[!] Line: " << line << "\n";
// Check namespace
@ -131,5 +130,11 @@ namespace Article {
fclose(a);
}
bool Comparator::comp(Metadata *a, Metadata *b) {
return a->publish_ts < b->publish_ts;
}
bool Comparator::equiv(Metadata *a, Metadata *b) {
return a->publish_ts == b->publish_ts;
}
};

View File

@ -44,6 +44,11 @@ namespace Article {
mature_content_reason[256];
};
class Comparator {
public:
static bool comp(Metadata *a, Metadata *b);
static bool equiv(Metadata *a, Metadata *b);
};
void get_metadata(std::string const &path, Metadata *m);
}

View File

@ -35,4 +35,3 @@
#define RETURN_FAILED_WEBSITE_BUILD_EXISTS 4
#define RETURN_FAILED_UNKNOWN_ERROR 10

View File

@ -24,8 +24,8 @@
#include <string>
#include <list>
#include <regex>
#include <filesystem>
#include "time.h"
#include "SwgRuntime.hxx"
#include "SwgContext.hxx"
@ -138,6 +138,7 @@ void build_blog_structure(std::string const &path, std::string const &prefix, st
}
std::list<Article::Metadata*> am;
//std::map<std::string, std::list<std::string>> sorted_articles;
for (std::string const &a : articles) {
std::string articlePath = blog_relative_path(prefix, a);
@ -150,7 +151,8 @@ void build_blog_structure(std::string const &path, std::string const &prefix, st
// Go ahead and parse article metadata.
Article::Metadata *articleMetadata = (Article::Metadata*) malloc(sizeof(Article::Metadata));
Article::get_metadata(a, articleMetadata);
std::cout << "Parsed metadata for article \"" << articleMetadata->title << "\"\n";
std::cout << "Parsed metadata for article \"" << articleMetadata->title << "\"\n\tPublished on "
<< ctime(&(articleMetadata->publish_ts)) << "\n";
am.push_back(articleMetadata);
// TODO: This code could be optimized by removing directory
@ -178,6 +180,8 @@ void build_blog_structure(std::string const &path, std::string const &prefix, st
exit(RETURN_FAILED_UNKNOWN_ERROR);
}
}
// Sort am list.
am.sort(Article::Comparator::comp);
}
/**