Add basic article metadata to article files

Print basic metadata (title, authors and timestamp) to HTML article
files.
This commit is contained in:
luca0N! 2022-04-27 20:18:38 -03:00
parent e2aa7f4ce8
commit 44cd6dc1d6
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
1 changed files with 42 additions and 19 deletions

View File

@ -35,6 +35,21 @@
#include "MarkdownParser.hxx"
#include "Article.hxx"
static std::string HR_MONTH[] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
std::string blog_relative_path(std::string const &pathPrefix, std::string const &path) {
return path.substr(pathPrefix.length() - 1);
}
@ -112,14 +127,36 @@ std::string get_template(std::string const &path) {
return htmlTemplate;
}
void compile_markdown(std::string const &path, std::string const &md, std::string const &to) {
void compile_markdown(std::string const &path, std::string const &md, std::string const &to, Article::Metadata *metadata = NULL) {
FILE *articleOutput = fopen(to.c_str(), "w");
std::string htmlTemplate = get_template(path);
// NOTE: std::regex requires the C++11 standard.
std::regex contentPlaceholder("<!--\\[_SWG: \\$CONTENT\\]-->");
std::string articleHtml = MarkdownParser::make_html(md);
std::string articleContents = std::regex_replace(htmlTemplate, contentPlaceholder, articleHtml);
// Write basic article metadata to article if this is an article.
std::string article_html;
if (metadata != NULL) {
// Get time information.
struct tm *publish_tm = gmtime(&(metadata->publish_ts));
article_html = "<h1>";
article_html += metadata->title;
article_html += "</h1>\n<span><b>Published on ";
//article_html += ctime(&(metadata->publish_ts));
article_html += HR_MONTH[publish_tm->tm_mon];
article_html += " ";
article_html += std::to_string(publish_tm->tm_mday);
article_html += ", ";
article_html += std::to_string(publish_tm->tm_year + 1900);
article_html += "</b></span><br/>\n<span><b>Written by ";
article_html += metadata->authors;
article_html += "</b></span><br/>";
}
// Concat article to main final content.
article_html += MarkdownParser::make_html(md);
std::string articleContents = std::regex_replace(htmlTemplate, contentPlaceholder, article_html);
fputs(articleContents.c_str(), articleOutput);
fclose(articleOutput);
@ -177,7 +214,7 @@ void build_blog_structure(std::string const &path, std::string const &prefix, st
new_article_filename += ".html";
oad /= new_article_filename;
rap /= new_article_filename;
compile_markdown(path, a, oad);
compile_markdown(path, a, oad, articleMetadata);
strncpy(articleMetadata->path, rap.c_str(), sizeof(articleMetadata->path));
am.push_back(articleMetadata);
} catch (std::filesystem::filesystem_error const &e) {
@ -191,24 +228,10 @@ void build_blog_structure(std::string const &path, std::string const &prefix, st
// Generate blog catalog.
std::string blog_html_catalog = "<ul>\n",
last_date = "";
std::string hr_month[] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
for (Article::Metadata *m : am) {
struct tm *article_time = gmtime(&(m->publish_ts));
std::string hr_date = hr_month[article_time->tm_mon];
std::string hr_date = HR_MONTH[article_time->tm_mon];
hr_date += " ";
hr_date += std::to_string(article_time->tm_year + 1900);
// Check if this article belongs to the same "month + year" group as