diff --git a/src/WebsiteBuilder.cxx b/src/WebsiteBuilder.cxx index 8e8afa0..1f59044 100644 --- a/src/WebsiteBuilder.cxx +++ b/src/WebsiteBuilder.cxx @@ -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(""); - 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 = "

"; + article_html += metadata->title; + article_html += "

\nPublished 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 += "
\nWritten by "; + article_html += metadata->authors; + article_html += "
"; + } + + // 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 = "