5 use std::collections::HashMap;
8 use std::path::PathBuf;
9 use time::{OffsetDateTime, format_description::FormatItem, macros::format_description};
10 use crate::template::{TemplateContext, TemplateValue};
12 const DATE_FORMAT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]");
20 pub fn to_template_context(archive_entries: &Vec<ArchiveEntry>) -> TemplateContext {
21 let mut context = HashMap::new();
23 let archive_entries_collection = archive_entries
25 .map(|archive_entry| archive_entry.to_template_value())
29 "archive_length".to_string(),
30 TemplateValue::Unsigned(
31 archive_entries.len().try_into().unwrap()
36 TemplateValue::Collection(archive_entries_collection)
42 pub fn to_template_value(&self) -> TemplateContext {
43 let mut context = HashMap::new();
47 TemplateValue::String(self.id.clone())
52 TemplateValue::String(self.slug.clone())
55 if let Some(title) = self.title() {
58 TemplateValue::String(title)
65 fn title(&self) -> Option<String> {
66 let date = OffsetDateTime::from_unix_timestamp_nanos(
67 (self.id.parse::<u64>().ok()? * 1_000_000).into()
69 let short_date = date.format(&DATE_FORMAT).ok()?;
70 let title = self.slug.replace("-", " ");
71 Some(format!("{} {}", short_date, title))
75 fn read_archive(archive_directory: &PathBuf) -> Vec<ArchiveEntry> {
76 let mut archive_entries = Vec::new();
77 if let Ok(entries) = read_dir(&archive_directory) {
78 for entry in entries.filter_map(Result::ok) {
79 let entry_path = entry.path();
80 let post_id = entry.file_name();
81 if let Ok(entry_type) = entry.file_type() {
82 if entry_type.is_dir() {
83 if let Ok(candidates) = read_dir(&entry_path) {
84 for candidate in candidates.filter_map(Result::ok) {
85 let candidate_path = candidate.path();
86 match candidate_path.extension() {
88 if extension == "gmi" {
89 if let Some(slug) = candidate_path.file_stem() {
90 if let (Some(post_id), Some(slug)) = (post_id.to_str(), slug.to_str()) {
91 archive_entries.push(ArchiveEntry {
92 id: post_id.to_string(),
93 slug: slug.to_string()
109 .sort_by(|a, b| b.id.cmp(&a.id));
114 pub fn archive(archive_directory: &PathBuf, template_directory: &PathBuf, output_directory: &PathBuf) -> Result<()> {
115 let archivers = available_archivers();
116 let archive_entries = read_archive(archive_directory);
117 let context = ArchiveEntry::to_template_context(&archive_entries);
118 for archiver in archivers {
119 archiver(archive_directory, template_directory, output_directory, &context)?;
124 fn available_archivers() -> Vec<fn(&PathBuf, &PathBuf, &PathBuf, &TemplateContext) -> Result<()>> {