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::metadata::Metadata;
14 pub fn new() -> Self {
18 fn read_posts(&self, posts_directory: &PathBuf, max_posts: u8) -> Vec<Post> {
19 let mut posts = Vec::new();
21 for i in 0..max_posts - 1 {
22 let post_directory = posts_directory.join(i.to_string());
23 match self.read_post(&post_directory, i) {
24 Some(post) => posts.push(post),
32 fn find_blog_content(&self, post_directory: &PathBuf) -> Option<String> {
33 let entries = read_dir(&post_directory).ok()?;
34 for entry in entries.filter_map(Result::ok) {
35 let entry_path = entry.path();
36 match entry_path.extension() {
38 if extension == "gmi" {
39 let mut file = File::open(entry_path).ok()?;
40 let mut contents = String::new();
41 file.read_to_string(&mut contents).ok()?;
42 return Some(contents);
51 fn read_post(&self, post_directory: &PathBuf, index: u8) -> Option<Post> {
52 let metadata_path = post_directory.join(METADATA_FILENAME);
53 let metadata = Metadata::read_or_create(&metadata_path);
54 let raw = self.find_blog_content(&post_directory)?;
55 let html = parse(&raw);
66 impl super::Command for Generate {
67 fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
71 fn execute(&self, _: Option<&String>, configuration: &Configuration, _: &String) -> Result<()> {
72 let _ = remove_dir_all(&configuration.blog_output_directory);
73 create_dir_all(&configuration.blog_output_directory)?;
75 let posts = self.read_posts(&configuration.posts_directory, configuration.max_posts);
77 &configuration.static_directory,
78 &configuration.templates_directory,
79 &configuration.blog_output_directory,
83 let _ = remove_dir_all(&configuration.archive_output_directory);
84 create_dir_all(&configuration.archive_output_directory)?;
88 fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
92 fn command(&self) -> &'static str {
96 fn help(&self) -> &'static str {
97 "\t\t\t\tGenerates the blog assets"