]> git.r.bdr.sh - rbdr/blog/blob - src/archiver/mod.rs
Generate and archive blog, allow publishing
[rbdr/blog] / src / archiver / mod.rs
1 mod raw;
2 mod gemini;
3 mod gopher;
4
5 use std::collections::HashMap;
6 use std::fs::read_dir;
7 use std::io::Result;
8 use std::path::PathBuf;
9 use time::{OffsetDateTime, format_description::FormatItem, macros::format_description};
10 use crate::template::{TemplateContext, TemplateValue};
11
12 const DATE_FORMAT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]");
13
14 struct ArchiveEntry {
15 id: String,
16 slug: String
17 }
18
19 impl ArchiveEntry {
20 pub fn to_template_context(archive_entries: &Vec<ArchiveEntry>) -> TemplateContext {
21 let mut context = HashMap::new();
22
23 let archive_entries_collection = archive_entries
24 .iter()
25 .map(|archive_entry| archive_entry.to_template_value())
26 .collect();
27
28 context.insert(
29 "archive_length".to_string(),
30 TemplateValue::Unsigned(
31 archive_entries.len().try_into().unwrap()
32 )
33 );
34 context.insert(
35 "posts".to_string(),
36 TemplateValue::Collection(archive_entries_collection)
37 );
38
39 context
40 }
41
42 pub fn to_template_value(&self) -> TemplateContext {
43 let mut context = HashMap::new();
44
45 context.insert(
46 "id".to_string(),
47 TemplateValue::String(self.id.clone())
48 );
49
50 context.insert(
51 "slug".to_string(),
52 TemplateValue::String(self.slug.clone())
53 );
54
55 if let Some(title) = self.title() {
56 context.insert(
57 "title".to_string(),
58 TemplateValue::String(title)
59 );
60 }
61
62 context
63 }
64
65 fn title(&self) -> Option<String> {
66 let date = OffsetDateTime::from_unix_timestamp_nanos(
67 (self.id.parse::<u64>().ok()? * 1_000_000).into()
68 ).ok()?;
69 let short_date = date.format(&DATE_FORMAT).ok()?;
70 let title = self.slug.replace("-", " ");
71 Some(format!("{} {}", short_date, title))
72 }
73 }
74
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() {
87 Some(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()
94 })
95 }
96 }
97 }
98 },
99 _ => continue
100 }
101 }
102 }
103 }
104 }
105 }
106 }
107
108 archive_entries
109 .sort_by(|a, b| b.id.cmp(&a.id));
110 archive_entries
111 }
112
113
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)?;
120 }
121 return Ok(())
122 }
123
124 fn available_archivers() -> Vec<fn(&PathBuf, &PathBuf, &PathBuf, &TemplateContext) -> Result<()>> {
125 vec![
126 raw::archive,
127 gemini::archive,
128 gopher::archive
129 ]
130 }