From 647ad56349df90762b3966019b812c9db7977f8f Mon Sep 17 00:00:00 2001 From: luca0N! Date: Wed, 27 Apr 2022 20:51:39 -0300 Subject: [PATCH] Add custom localized timestamp override Add support for a custom key in the article metadata that allows users to override the default date string (e.g. January 1, 1970). This is useful for other locales. --- src/Article.cxx | 5 ++++- src/Article.hxx | 7 ++++--- src/WebsiteBuilder.cxx | 17 ++++++++++------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Article.cxx b/src/Article.cxx index 239d9fc..6b59367 100644 --- a/src/Article.cxx +++ b/src/Article.cxx @@ -124,13 +124,16 @@ namespace Article { if (reading_article_metadata) { std::string k_title = "Title=", k_authors = "Authors=", - k_published = "Published="; + k_published = "Published=", + k_published_str = "PublishedString="; if (line.find(k_title) == 0) strncpy(m->title, line.substr(k_title.length()).c_str(), sizeof(m->title)); else if (line.find(k_authors) == 0) strncpy(m->authors, line.substr(k_authors.length()).c_str(), sizeof(m->authors)); else if (line.find(k_published) == 0) m->publish_ts = atol(line.substr(k_published.length()).c_str()); + else if (line.find(k_published_str) == 0) + strncpy(m->publish_str, line.substr(k_published_str.length()).c_str(), sizeof(m->publish_str)); else std::cerr << "warning: ignoring unknown key/value due to unknown key: " << line << std::endl; } else if (line[0] == '[') { diff --git a/src/Article.hxx b/src/Article.hxx index 702f9d1..98f0850 100644 --- a/src/Article.hxx +++ b/src/Article.hxx @@ -30,9 +30,10 @@ namespace Article { char path[256]; // Path to this article // Epoch timestamps - long original_ts; // Original article timestamp (for "Originally written on [...]") - long publish_ts; // (for "Published on [...]") - long update_ts; // (for "Last updated on [...]") + long original_ts; // Original article timestamp (for "Originally written on [...]") + long publish_ts; // (for "Published on [...]") + long update_ts; // (for "Last updated on [...]") + char publish_str[32]; // Override localized publish timestamps // Article flags bool partially_obsoleted, diff --git a/src/WebsiteBuilder.cxx b/src/WebsiteBuilder.cxx index 1f59044..36d1635 100644 --- a/src/WebsiteBuilder.cxx +++ b/src/WebsiteBuilder.cxx @@ -138,16 +138,19 @@ void compile_markdown(std::string const &path, std::string const &md, std::strin 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); + if (metadata->publish_str[0] == '\0') { + struct tm *publish_tm = gmtime(&(metadata->publish_ts)); + //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); + } else + article_html += metadata->publish_str; article_html += "
\nWritten by "; article_html += metadata->authors; article_html += "
";