]>
Commit | Line | Data |
---|---|---|
1 | use std::fs::{create_dir_all, read_dir, remove_dir_all, File}; | |
2 | use std::io::{Read, Result}; | |
3 | use std::path::PathBuf; | |
4 | use crate::configuration::Configuration; | |
5 | use crate::constants::METADATA_FILENAME; | |
6 | use crate::gemini_parser::parse; | |
7 | use crate::generator::generate; | |
8 | use crate::archiver::archive; | |
9 | use crate::metadata::Metadata; | |
10 | use crate::post::Post; | |
11 | ||
12 | pub struct Generate; | |
13 | ||
14 | impl Generate { | |
15 | pub fn new() -> Self { | |
16 | Generate | |
17 | } | |
18 | ||
19 | fn read_posts(&self, posts_directory: &PathBuf, max_posts: u8) -> Vec<Post> { | |
20 | let mut posts = Vec::new(); | |
21 | ||
22 | for i in 0..max_posts { | |
23 | let post_directory = posts_directory.join(i.to_string()); | |
24 | match self.read_post(&post_directory, i) { | |
25 | Some(post) => posts.push(post), | |
26 | None => continue | |
27 | } | |
28 | } | |
29 | ||
30 | posts | |
31 | } | |
32 | ||
33 | fn find_blog_content(&self, post_directory: &PathBuf) -> Option<String> { | |
34 | let entries = read_dir(&post_directory).ok()?; | |
35 | for entry in entries.filter_map(Result::ok) { | |
36 | let entry_path = entry.path(); | |
37 | match entry_path.extension() { | |
38 | Some(extension) => { | |
39 | if extension == "gmi" { | |
40 | let mut file = File::open(entry_path).ok()?; | |
41 | let mut contents = String::new(); | |
42 | file.read_to_string(&mut contents).ok()?; | |
43 | return Some(contents); | |
44 | } | |
45 | }, | |
46 | None => continue | |
47 | } | |
48 | } | |
49 | None | |
50 | } | |
51 | ||
52 | fn read_post(&self, post_directory: &PathBuf, index: u8) -> Option<Post> { | |
53 | let metadata_path = post_directory.join(METADATA_FILENAME); | |
54 | let metadata = Metadata::read_or_create(&metadata_path); | |
55 | let raw = self.find_blog_content(&post_directory)?; | |
56 | let html = parse(&raw); | |
57 | ||
58 | Some(Post { | |
59 | metadata, | |
60 | index, | |
61 | html, | |
62 | raw | |
63 | }) | |
64 | } | |
65 | } | |
66 | ||
67 | impl super::Command for Generate { | |
68 | fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> { | |
69 | vec![] | |
70 | } | |
71 | ||
72 | fn execute(&self, _: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> { | |
73 | let _ = remove_dir_all(&configuration.blog_output_directory); | |
74 | create_dir_all(&configuration.blog_output_directory)?; | |
75 | ||
76 | let posts = self.read_posts(&configuration.posts_directory, configuration.max_posts); | |
77 | generate( | |
78 | &configuration.static_directory, | |
79 | &configuration.templates_directory, | |
80 | &configuration.blog_output_directory, | |
81 | &posts | |
82 | )?; | |
83 | ||
84 | let _ = remove_dir_all(&configuration.archive_output_directory); | |
85 | create_dir_all(&configuration.archive_output_directory)?; | |
86 | archive( | |
87 | &configuration.archive_directory, | |
88 | &configuration.templates_directory, | |
89 | &configuration.archive_output_directory | |
90 | )?; | |
91 | return Ok(()) | |
92 | } | |
93 | ||
94 | fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> { | |
95 | vec![] | |
96 | } | |
97 | ||
98 | fn command(&self) -> &'static str { | |
99 | "generate" | |
100 | } | |
101 | ||
102 | fn help(&self) -> &'static str { | |
103 | "\t\t\t\tGenerates the blog assets" | |
104 | } | |
105 | } |