]>
Commit | Line | Data |
---|---|---|
6352ebb0 RBR |
1 | mod gemini; |
2 | mod gopher; | |
d7fef30a | 3 | mod raw; |
6352ebb0 | 4 | |
b17907fa | 5 | use crate::template::{Context, Value}; |
6352ebb0 RBR |
6 | use std::collections::HashMap; |
7 | use std::fs::read_dir; | |
8 | use std::io::Result; | |
d7fef30a RBR |
9 | use std::path::{Path, PathBuf}; |
10 | use time::{format_description::FormatItem, macros::format_description, OffsetDateTime}; | |
6352ebb0 RBR |
11 | |
12 | const DATE_FORMAT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]"); | |
13 | ||
14 | struct ArchiveEntry { | |
15 | id: String, | |
d7fef30a | 16 | slug: String, |
6352ebb0 RBR |
17 | } |
18 | ||
b17907fa | 19 | type Archiver = fn(&Path, &Path, &Path, &Context) -> Result<()>; |
d7fef30a | 20 | |
6352ebb0 | 21 | impl ArchiveEntry { |
b17907fa | 22 | pub fn to_template_context(archive_entries: &[ArchiveEntry]) -> Context { |
6352ebb0 RBR |
23 | let mut context = HashMap::new(); |
24 | ||
25 | let archive_entries_collection = archive_entries | |
26 | .iter() | |
d7fef30a | 27 | .map(ArchiveEntry::to_template_value) |
6352ebb0 RBR |
28 | .collect(); |
29 | ||
30 | context.insert( | |
31 | "archive_length".to_string(), | |
b17907fa | 32 | Value::Unsigned(archive_entries.len().try_into().unwrap()), |
6352ebb0 RBR |
33 | ); |
34 | context.insert( | |
35 | "posts".to_string(), | |
b17907fa | 36 | Value::Collection(archive_entries_collection), |
6352ebb0 RBR |
37 | ); |
38 | ||
39 | context | |
40 | } | |
41 | ||
b17907fa | 42 | pub fn to_template_value(&self) -> Context { |
6352ebb0 RBR |
43 | let mut context = HashMap::new(); |
44 | ||
b17907fa | 45 | context.insert("id".to_string(), Value::String(self.id.clone())); |
6352ebb0 | 46 | |
b17907fa | 47 | context.insert("slug".to_string(), Value::String(self.slug.clone())); |
6352ebb0 RBR |
48 | |
49 | if let Some(title) = self.title() { | |
b17907fa | 50 | context.insert("title".to_string(), Value::String(title)); |
6352ebb0 RBR |
51 | } |
52 | ||
53 | context | |
54 | } | |
55 | ||
56 | fn title(&self) -> Option<String> { | |
57 | let date = OffsetDateTime::from_unix_timestamp_nanos( | |
d7fef30a RBR |
58 | (self.id.parse::<u64>().ok()? * 1_000_000).into(), |
59 | ) | |
60 | .ok()?; | |
6352ebb0 | 61 | let short_date = date.format(&DATE_FORMAT).ok()?; |
d7fef30a RBR |
62 | let title = self.slug.replace('-', " "); |
63 | Some(format!("{short_date} {title}")) | |
6352ebb0 RBR |
64 | } |
65 | } | |
66 | ||
67 | fn read_archive(archive_directory: &PathBuf) -> Vec<ArchiveEntry> { | |
68 | let mut archive_entries = Vec::new(); | |
d7fef30a | 69 | if let Ok(entries) = read_dir(archive_directory) { |
6352ebb0 RBR |
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() { | |
79 | Some(extension) => { | |
80 | if extension == "gmi" { | |
81 | if let Some(slug) = candidate_path.file_stem() { | |
d7fef30a RBR |
82 | if let (Some(post_id), Some(slug)) = |
83 | (post_id.to_str(), slug.to_str()) | |
84 | { | |
6352ebb0 RBR |
85 | archive_entries.push(ArchiveEntry { |
86 | id: post_id.to_string(), | |
d7fef30a RBR |
87 | slug: slug.to_string(), |
88 | }); | |
6352ebb0 RBR |
89 | } |
90 | } | |
91 | } | |
d7fef30a RBR |
92 | } |
93 | _ => continue, | |
6352ebb0 RBR |
94 | } |
95 | } | |
96 | } | |
97 | } | |
98 | } | |
99 | } | |
100 | } | |
101 | ||
d7fef30a | 102 | archive_entries.sort_by(|a, b| b.id.cmp(&a.id)); |
6352ebb0 RBR |
103 | archive_entries |
104 | } | |
105 | ||
d7fef30a RBR |
106 | pub fn archive( |
107 | archive_directory: &PathBuf, | |
108 | template_directory: &Path, | |
109 | output_directory: &Path, | |
110 | ) -> Result<()> { | |
6352ebb0 RBR |
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 { | |
d7fef30a RBR |
115 | archiver( |
116 | archive_directory, | |
117 | template_directory, | |
118 | output_directory, | |
119 | &context, | |
120 | )?; | |
6352ebb0 | 121 | } |
d7fef30a | 122 | Ok(()) |
6352ebb0 RBR |
123 | } |
124 | ||
d7fef30a RBR |
125 | fn available_archivers() -> Vec<Archiver> { |
126 | vec![raw::archive, gemini::archive, gopher::archive] | |
6352ebb0 | 127 | } |