aboutsummaryrefslogtreecommitdiff
path: root/src/command/generate.rs
blob: b74b33b3dbb31abf80c7481e8fcf0443f7129819 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use crate::archiver::archive;
use crate::configuration::Configuration;
use crate::constants::METADATA_FILENAME;
use crate::generator::generate;
use crate::metadata::Metadata;
use crate::post::Post;
use gema_texto::{gemini_parser::parse, html_renderer::render_html};
use std::fs::{create_dir_all, read_dir, remove_dir_all, File};
use std::io::{Read, Result};
use std::path::Path;

pub struct Generate;

impl Generate {
    pub fn new() -> Self {
        Generate
    }

    fn read_posts(posts_directory: &Path, max_posts: u8) -> Vec<Post> {
        let mut posts = Vec::new();

        for i in 0..max_posts {
            let post_path = posts_directory.join(i.to_string());
            match Generate::read_post(&post_path, i) {
                Some(post) => posts.push(post),
                None => continue,
            }
        }

        posts
    }

    fn find_blog_content(post_path: &Path) -> Option<String> {
        let entries = read_dir(post_path).ok()?;
        for entry in entries.filter_map(Result::ok) {
            let entry_path = entry.path();
            match entry_path.extension() {
                Some(extension) => {
                    if extension == "gmi" {
                        let mut file = File::open(entry_path).ok()?;
                        let mut contents = String::new();
                        file.read_to_string(&mut contents).ok()?;
                        return Some(contents);
                    }
                }
                None => continue,
            }
        }
        None
    }

    fn read_post(post_directory: &Path, index: u8) -> Option<Post> {
        let metadata_path = post_directory.join(METADATA_FILENAME);
        let metadata = Metadata::read_or_create(&metadata_path);
        let raw = Generate::find_blog_content(post_directory)?;
        let html = render_html(&parse(&raw));

        Some(Post {
            metadata,
            index,
            html,
            raw,
        })
    }
}

impl super::Command for Generate {
    fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
        vec![]
    }

    fn execute(&self, _: Option<&String>, configuration: &Configuration, _: &str) -> Result<()> {
        let _ = remove_dir_all(&configuration.blog_output_directory);
        create_dir_all(&configuration.blog_output_directory)?;

        let posts = Generate::read_posts(&configuration.posts_directory, configuration.max_posts);
        generate(
            &configuration.static_directory,
            &configuration.templates_directory,
            &configuration.blog_output_directory,
            &posts,
        )?;

        let _ = remove_dir_all(&configuration.archive_output_directory);
        create_dir_all(&configuration.archive_output_directory)?;
        archive(
            &configuration.archive_directory,
            &configuration.templates_directory,
            &configuration.archive_output_directory,
        )?;
        Ok(())
    }

    fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
        vec![]
    }

    fn command(&self) -> &'static str {
        "generate"
    }

    fn help(&self) -> &'static str {
        "\t\t\t\tGenerates the blog assets"
    }
}