1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
mod gemini;
mod gopher;
mod raw;
use crate::template::{Context, Value};
use std::collections::HashMap;
use std::fs::read_dir;
use std::io::{Error, Result};
use std::path::{Path, PathBuf};
use time::{OffsetDateTime, format_description::FormatItem, macros::format_description};
const DATE_FORMAT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]");
struct ArchiveEntry {
id: String,
slug: String,
}
type Archiver = fn(&Path, &Path, &Path, &Context) -> Result<()>;
impl ArchiveEntry {
pub fn to_template_context(archive_entries: &[ArchiveEntry]) -> Result<Context> {
let mut context = HashMap::new();
let archive_entries_collection = archive_entries
.iter()
.map(ArchiveEntry::to_template_value)
.collect();
let archive_length: u64 = archive_entries
.len()
.try_into()
.map_err(|_| Error::other("Too many items in the archive."))?;
context.insert(
"archive_length".to_string(),
Value::Unsigned(archive_length),
);
context.insert(
"posts".to_string(),
Value::Collection(archive_entries_collection),
);
Ok(context)
}
pub fn to_template_value(&self) -> Context {
let mut context = HashMap::new();
context.insert("id".to_string(), Value::String(self.id.clone()));
context.insert("slug".to_string(), Value::String(self.slug.clone()));
if let Some(title) = self.title() {
context.insert("title".to_string(), Value::String(title));
}
context
}
fn title(&self) -> Option<String> {
let date = OffsetDateTime::from_unix_timestamp_nanos(
(self.id.parse::<u64>().ok()? * 1_000_000).into(),
)
.ok()?;
let short_date = date.format(&DATE_FORMAT).ok()?;
let title = self.slug.replace('-', " ");
Some(format!("{short_date} {title}"))
}
}
fn read_archive(archive_directory: &PathBuf) -> Vec<ArchiveEntry> {
let mut archive_entries = Vec::new();
if let Ok(entries) = read_dir(archive_directory) {
for entry in entries.filter_map(Result::ok) {
let entry_path = entry.path();
let post_id = entry.file_name();
if let Ok(entry_type) = entry.file_type() {
if entry_type.is_dir() {
if let Ok(candidates) = read_dir(&entry_path) {
for candidate in candidates.filter_map(Result::ok) {
let candidate_path = candidate.path();
if let Some(extension) = candidate_path.extension() {
if extension == "gmi" {
if let Some(slug) = candidate_path.file_stem() {
if let (Some(post_id), Some(slug)) =
(post_id.to_str(), slug.to_str())
{
archive_entries.push(ArchiveEntry {
id: post_id.to_string(),
slug: slug.to_string(),
});
}
}
}
}
}
}
}
}
}
}
archive_entries.sort_by(|a, b| b.id.cmp(&a.id));
archive_entries
}
pub fn archive(
archive_directory: &PathBuf,
template_directory: &Path,
output_directory: &Path,
) -> Result<()> {
let archivers = available_archivers();
let archive_entries = read_archive(archive_directory);
let context = ArchiveEntry::to_template_context(&archive_entries)?;
for archiver in archivers {
archiver(
archive_directory,
template_directory,
output_directory,
&context,
)?;
}
Ok(())
}
fn available_archivers() -> Vec<Archiver> {
vec![raw::archive, gemini::archive, gopher::archive]
}
#[cfg(test)]
mod tests {
use std::fs::create_dir_all;
use super::*;
use crate::template::Value;
use test_utilities::*;
// Archive template values
#[test]
fn test_creates_context_with_empty_archive_entries() {
if let Ok(context) = ArchiveEntry::to_template_context(&[]) {
assert_eq!(context.get("archive_length"), Some(&Value::Unsigned(0)));
if let Some(Value::Collection(posts)) = &context.get("posts") {
assert!(posts.is_empty());
} else {
panic!("The posts context was not the right type.");
}
} else {
panic!("Could not generate template context.");
}
}
#[test]
fn test_creates_context_with_post_slice() {
let archive_entry = ArchiveEntry {
id: "you-know-what-is-cool".to_string(),
slug: "the-archiving-shelves-with-a-spinning-lever-to-move-them".to_string(),
};
if let Ok(context) = ArchiveEntry::to_template_context(&[archive_entry]) {
assert_eq!(context.get("archive_length"), Some(&Value::Unsigned(1)));
if let Some(Value::Collection(posts)) = &context.get("posts") {
if let Some(post) = posts.first() {
assert_eq!(
post["id"],
Value::String("you-know-what-is-cool".to_string())
);
} else {
panic!("The template context had no posts");
}
} else {
panic!("The posts context was not the right type.");
}
} else {
panic!("Could not generate context");
}
}
#[test]
fn test_converts_archive_entry_without_title_to_context() {
let archive_entry = ArchiveEntry {
id: "they-always-show-them-in-spy-films".to_string(),
slug: "or-like-universities".to_string(),
};
let context = archive_entry.to_template_value();
assert_eq!(
context.get("id"),
Some(&Value::String(
"they-always-show-them-in-spy-films".to_string()
))
);
assert_eq!(
context.get("slug"),
Some(&Value::String("or-like-universities".to_string()))
);
assert!(!context.contains_key("title"));
}
#[test]
fn test_converts_archive_entry_with_title_to_context() {
let archive_entry = ArchiveEntry {
id: "1736035200000".to_string(),
slug: "need-a-warehouse".to_string(),
};
let context = archive_entry.to_template_value();
assert_eq!(
context.get("id"),
Some(&Value::String("1736035200000".to_string()))
);
assert_eq!(
context.get("slug"),
Some(&Value::String("need-a-warehouse".to_string()))
);
assert_eq!(
context.get("title"),
Some(&Value::String("2025-01-05 need a warehouse".to_string()))
);
}
// Archive Finder
#[test]
fn test_finds() {
let test_dir = setup_test_dir();
let archive_dir = test_dir.join("archive");
let template_dir = test_dir.join("templates");
let output_dir = test_dir.join("output");
let first_post_dir = archive_dir.join("1736035200000");
create_dir_all(&first_post_dir).expect("Could not first create post test directory");
create_test_file(
&first_post_dir.join("my-very-cool-post.gmi"),
"is full of flowers",
);
let second_post_dir = archive_dir.join("1736121600000");
create_dir_all(&second_post_dir).expect("Could not create second post test directory");
create_test_file(
&second_post_dir.join("my-very-sad-post.gmi"),
"is full of regrets",
);
let bad_file_dir = archive_dir.join("1736121600001");
create_dir_all(&bad_file_dir).expect("Could not create bad file test directory");
create_test_file(&bad_file_dir.join("my-very-bad-file"), "is full of lies");
create_dir_all(&template_dir).expect("Could not create template test directory");
create_dir_all(&output_dir).expect("Could not create output test directory");
archive(&archive_dir, &template_dir, &output_dir).expect("Archive failed");
assert_file_contains(
&output_dir.join("index.gmi"),
"\n=> ./1736035200000/my-very-cool-post.gmi 2025-01-05 my very cool post",
);
assert_file_contains(
&output_dir.join("index.gmi"),
"\n=> ./1736121600000/my-very-sad-post.gmi 2025-01-06 my very sad post",
);
assert_file_contents(
&output_dir.join("1736035200000/my-very-cool-post.gmi"),
"is full of flowers",
);
assert_file_contents(
&output_dir.join("1736121600000/my-very-sad-post.gmi"),
"is full of regrets",
);
assert!(&output_dir.join("1736121600001/my-very-bad-file").exists());
assert_file_does_not_contain(
&output_dir.join("index.gmi"),
"1736121600001/my-very-bad-file",
);
cleanup_test_dir(&test_dir);
}
}
|