stamdStatic Markdown Page Generator |
git clone git://git.dimitrijedobrota.com/stamd.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING | |
commit | c5c5d039b7a8cad01a685a4f022cce5114fee540 |
parent | cb91fd3a2930f24272da2bd9a260bc1ace838322 |
author | Dimitrije Dobrota <mail@dimitrijedobrota.com> |
date | Sun, 19 Jan 2025 19:40:11 +0100 |
Offload time calculation to hemplate library
Diffstat:M | CMakeLists.txt | | | ++-- |
M | source/indexer.cpp | | | ++++++++++-------------------------------------------------------- |
2 files changed, 12 insertions(+), 58 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -4,7 +4,7 @@ include(cmake/prelude.cmake)
project(
stamd
VERSION 0.3.1
VERSION 0.3.2
DESCRIPTION "Static Markdown Page Generator"
HOMEPAGE_URL "https://git.dimitrijedobrota.com/stamd.git"
LANGUAGES CXX
@@ -17,7 +17,7 @@ include(cmake/variables.cmake)
find_package(md4c CONFIG REQUIRED)
find_package(poafloc 1 CONFIG REQUIRED)
find_package(hemplate 0.1 CONFIG REQUIRED)
find_package(hemplate 0.2.2 CONFIG REQUIRED)
# ---- Declare library ----
diff --git a/source/indexer.cpp b/source/indexer.cpp
@@ -1,9 +1,10 @@
#include <algorithm>
#include <chrono>
#include <ctime>
#include <format>
#include <iomanip>
#include <iterator>
#include <ostream>
#include <sstream>
#include <string>
#include "indexer.hpp"
@@ -33,56 +34,12 @@ void Indexer::sort()
{ return lft->get_date() > rht->get_date(); });
}
std::tm get_time(const std::string& date)
int64_t parse_time(const std::string& date)
{
int year = 0;
int month = 0;
int day = 0;
std::sscanf(date.c_str(), "%d-%d-%d", &year, &month, &day); // NOLINT
tm time = {.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = day,
.tm_mon = month - 1,
.tm_year = year - 1900,
.tm_wday = 0,
.tm_yday = 0,
.tm_isdst = 0,
.tm_gmtoff = 0,
.tm_zone = nullptr};
return time;
}
#define rfc882_f "{:%a, %d %b %Y %H:%M:%S %z}" // NOLINT
#define rfc3339_f "{:%FT%H:%M:%SZ}" // NOLINT
std::string to_rfc882(const std::string& date)
{
using namespace std::chrono; // NOLINT
tm time = get_time(date);
const auto tmp = std::mktime(&time);
const auto chrono_time =
time_point_cast<seconds>(system_clock::from_time_t(tmp));
return std::format(rfc882_f, chrono_time);
}
std::string to_rfc3339(const std::string& date)
{
using namespace std::chrono; // NOLINT
tm time = get_time(date);
const auto tmp = std::mktime(&time);
const auto chrono_time =
time_point_cast<seconds>(system_clock::from_time_t(tmp));
return std::format(rfc3339_f, chrono_time);
std::tm tms = {};
std::stringstream stream(date);
stream >> std::get_time(&tms, "%Y-%m-%d");
return std::mktime(&tms);
}
void Indexer::create_index(std::ostream& ost, const std::string& name)
@@ -116,14 +73,11 @@ void Indexer::create_atom(std::ostream& ost, const std::string& name) const
const std::string& base_url = m_options.base_url;
auto const time =
std::chrono::current_zone()->to_local(std::chrono::system_clock::now());
ost << xml();
ost << atom::feed();
ost << atom::title(name);
ost << atom::id(base_url);
ost << atom::updated(std::format(rfc3339_f, time));
ost << atom::updated(atom::format_time_now());
ost << atom::author().add(atom::name(name));
ost << atom::link(" ",
{{"rel", "self"}, {"href", base_url + "blog/atom.xml"}});
@@ -141,7 +95,7 @@ void Indexer::create_atom(std::ostream& ost, const std::string& name) const
.add(atom::title(title))
.add(atom::id(base_url + filename))
.add(atom::link(" ").set("href", base_url + filename))
.add(atom::updated(to_rfc3339(date)))
.add(atom::updated(atom::format_time(parse_time((date)))))
.add(atom::summary(summary));
}
@@ -177,7 +131,7 @@ void Indexer::create_rss(std::ostream& ost, const std::string& name) const
.add(rss::title(filename))
.add(rss::link(base_url + filename))
.add(rss::guid(base_url + filename))
.add(rss::pubDate(to_rfc882(date)))
.add(rss::pubDate(rss::format_time(parse_time(date))))
.add(rss::author(std::format("{} ({})", email, author)));
}