5 use crate::template::{Context, Value};
6 use std::collections::HashMap;
9 use std::path::{Path, PathBuf};
10 use time::{format_description::FormatItem, macros::format_description, OffsetDateTime};
12 const DATE_FORMAT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]");
19 type Archiver = fn(&Path, &Path, &Path, &Context) -> Result<()>;
22 pub fn to_template_context(archive_entries: &[ArchiveEntry]) -> Context {
23 let mut context = HashMap::new();
25 let archive_entries_collection = archive_entries
27 .map(ArchiveEntry::to_template_value)
31 "archive_length".to_string(),
32 Value::Unsigned(archive_entries.len().try_into().unwrap()),
36 Value::Collection(archive_entries_collection),
42 pub fn to_template_value(&self) -> Context {
43 let mut context = HashMap::new();
45 context.insert("id".to_string(), Value::String(self.id.clone()));
47 context.insert("slug".to_string(), Value::String(self.slug.clone()));
49 if let Some(title) = self.title() {
50 context.insert("title".to_string(), Value::String(title));
56 fn title(&self) -> Option<String> {
57 let date = OffsetDateTime::from_unix_timestamp_nanos(
58 (self.id.parse::<u64>().ok()? * 1_000_000).into(),
61 let short_date = date.format(&DATE_FORMAT).ok()?;
62 let title = self.slug.replace('-', " ");
63 Some(format!("{short_date} {title}"))
67 fn read_archive(archive_directory: &PathBuf) -> Vec<ArchiveEntry> {
68 let mut archive_entries = Vec::new();
69 if let Ok(entries) = read_dir(archive_directory) {
70 for entry in entries.filter_map(Result::ok) {
71 let entry_path = entry.path();
72 let post_id = entry.file_name();
73 if let Ok(entry_type) = entry.file_type() {
74 if entry_type.is_dir() {
75 if let Ok(candidates) = read_dir(&entry_path) {
76 for candidate in candidates.filter_map(Result::ok) {
77 let candidate_path = candidate.path();
78 match candidate_path.extension() {
80 if extension == "gmi" {
81 if let Some(slug) = candidate_path.file_stem() {
82 if let (Some(post_id), Some(slug)) =
83 (post_id.to_str(), slug.to_str())
85 archive_entries.push(ArchiveEntry {
86 id: post_id.to_string(),
87 slug: slug.to_string(),
102 archive_entries.sort_by(|a, b| b.id.cmp(&a.id));
107 archive_directory: &PathBuf,
108 template_directory: &Path,
109 output_directory: &Path,
111 let archivers = available_archivers();
112 let archive_entries = read_archive(archive_directory);
113 let context = ArchiveEntry::to_template_context(&archive_entries);
114 for archiver in archivers {
125 fn available_archivers() -> Vec<Archiver> {
126 vec![raw::archive, gemini::archive, gopher::archive]
131 use std::fs::create_dir_all;
134 use crate::template::Value;
136 use test_utilities::*;
138 // Archive template values
141 fn test_creates_context_with_empty_archive_entries() {
142 let context = ArchiveEntry::to_template_context(&[]);
144 assert_eq!(context["archive_length"], Value::Unsigned(0));
145 if let Value::Collection(posts) = &context["posts"] {
146 assert!(posts.is_empty());
148 panic!("The posts context was not the right type.");
153 fn test_creates_context_with_post_slice() {
154 let archive_entry = ArchiveEntry {
155 id: "you-know-what-is-cool".to_string(),
156 slug: "the-archiving-shelves-with-a-spinning-lever-to-move-them".to_string(),
159 let context = ArchiveEntry::to_template_context(&[archive_entry]);
161 assert_eq!(context["archive_length"], Value::Unsigned(1));
162 if let Value::Collection(posts) = &context["posts"] {
163 if let Some(post) = posts.first() {
166 Value::String("you-know-what-is-cool".to_string())
169 panic!("The template context had no posts");
172 panic!("The posts context was not the right type.");
177 fn test_converts_archive_entry_without_title_to_context() {
178 let archive_entry = ArchiveEntry {
179 id: "they-always-show-them-in-spy-films".to_string(),
180 slug: "or-like-universities".to_string(),
183 let context = archive_entry.to_template_value();
187 Value::String("they-always-show-them-in-spy-films".to_string())
191 Value::String("or-like-universities".to_string())
193 assert!(!context.contains_key("title"));
197 fn test_converts_archive_entry_with_title_to_context() {
198 let archive_entry = ArchiveEntry {
199 id: "1736035200000".to_string(),
200 slug: "need-a-warehouse".to_string(),
203 let context = archive_entry.to_template_value();
205 assert_eq!(context["id"], Value::String("1736035200000".to_string()));
208 Value::String("need-a-warehouse".to_string())
212 Value::String("2025-01-05 need a warehouse".to_string())
219 let test_dir = setup_test_dir();
220 let archive_dir = test_dir.join("archive");
221 let template_dir = test_dir.join("templates");
222 let output_dir = test_dir.join("output");
224 let first_post_dir = archive_dir.join("1736035200000");
225 create_dir_all(&first_post_dir).expect("Could not first create post test directory");
227 &first_post_dir.join("my-very-cool-post.gmi"),
228 "is full of flowers",
231 let second_post_dir = archive_dir.join("1736121600000");
232 create_dir_all(&second_post_dir).expect("Could not create second post test directory");
234 &second_post_dir.join("my-very-sad-post.gmi"),
235 "is full of regrets",
238 let bad_file_dir = archive_dir.join("1736121600001");
239 create_dir_all(&bad_file_dir).expect("Could not create bad file test directory");
240 create_test_file(&bad_file_dir.join("my-very-bad-file"), "is full of lies");
242 create_dir_all(&template_dir).expect("Could not create template test directory");
243 create_dir_all(&output_dir).expect("Could not create output test directory");
245 archive(&archive_dir, &template_dir, &output_dir).expect("Archive failed");
247 assert_file_contains(
248 &output_dir.join("index.gmi"),
249 "\n=> ./1736035200000/my-very-cool-post.gmi 2025-01-05 my very cool post",
251 assert_file_contains(
252 &output_dir.join("index.gmi"),
253 "\n=> ./1736121600000/my-very-sad-post.gmi 2025-01-06 my very sad post",
255 assert_file_contents(
256 &output_dir.join("1736035200000/my-very-cool-post.gmi"),
257 "is full of flowers",
259 assert_file_contents(
260 &output_dir.join("1736121600000/my-very-sad-post.gmi"),
261 "is full of regrets",
263 assert!(&output_dir.join("1736121600001/my-very-bad-file").exists());
264 assert_file_does_not_contain(
265 &output_dir.join("index.gmi"),
266 "1736121600001/my-very-bad-file",