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;
15 pub fn new() -> Self {
19 fn read_posts(&self, posts_directory: &PathBuf, max_posts: u8) -> Vec<Post> {
20 let mut posts = Vec::new();
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),
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() {
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);
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);
67 impl super::Command for Generate {
68 fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
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)?;
76 let posts = self.read_posts(&configuration.posts_directory, configuration.max_posts);
78 &configuration.static_directory,
79 &configuration.templates_directory,
80 &configuration.blog_output_directory,
84 let _ = remove_dir_all(&configuration.archive_output_directory);
85 create_dir_all(&configuration.archive_output_directory)?;
87 &configuration.archive_directory,
88 &configuration.templates_directory,
89 &configuration.archive_output_directory
94 fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
98 fn command(&self) -> &'static str {
102 fn help(&self) -> &'static str {
103 "\t\t\t\tGenerates the blog assets"