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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use glob::glob;
use handlebars::Handlebars;
use handlebars::JsonValue;
use serde_json::json;
use std::env;
use std::fs::{self, File};
use std::io::Read;
use std::io::Write;
use std::path::PathBuf;

mod theme;
use theme::{TEMPLATE_INDEX, TEMPLATE_RECIPE};

use crate::parser::{parse_recipe, Recipe};
mod parser;

// check if theme folder exists, if it does register handlerbars from it, otherwise use default theme
fn register_theme(handlebars: &mut Handlebars) {
    let theme_path = PathBuf::from("theme");
    if theme_path.exists() {
        println!("Using theme from {}", theme_path.display());
        let mut theme_files = Vec::new();
        for entry in glob("theme/*.hbs").expect("Failed to read glob pattern") {
            match entry {
                Ok(path) => {
                    theme_files.push(path);
                }
                Err(e) => println!("{:?}", e),
            }
        }
        for file in theme_files.iter() {
            let mut f = File::open(file).expect("Unable to open file");
            let mut contents = String::new();
            f.read_to_string(&mut contents)
                .expect("Unable to read string");
            handlebars
                .register_template_string(file.to_str().unwrap(), contents)
                .expect("Unable to register template");
        }
    } else {
        println!("No theme folder found, using default theme");
        let template_index_string = String::from_utf8_lossy(TEMPLATE_INDEX);
        let template_recipe_string = String::from_utf8_lossy(TEMPLATE_RECIPE);
        handlebars
            .register_template_string("index.hbs", template_index_string)
            .expect("Unable to register template");
        handlebars
            .register_template_string("recipe.hbs", template_recipe_string)
            .expect("Unable to register template");
    }
}

// iterate over all recipes and generate a list of recipes
fn parse_folder() -> Vec<Recipe> {
    let mut recipes = Vec::new();
    let mut next_id = 0;
    for entry in glob("./recipes/*.md").expect("Failed to read glob pattern") {
        match entry {
            Ok(path) => {
                let recipe = parse_recipe(path, next_id);
                recipes.push(recipe);
                next_id += 1;
            }
            Err(e) => println!("{:?}", e),
        }
    }
    recipes
}

fn collect_output_folder_from_args() -> PathBuf {
    let args: Vec<String> = env::args().collect();
    let output_folder = match args.len() {
        1 => PathBuf::from("./docs"),
        2 => PathBuf::from(&args[1]),
        _ => panic!("Too many arguments"),
    };
    output_folder
}

fn generate_recipe_pages(
    handlebars: &Handlebars,
    recipes: &Vec<Recipe>,
    config: &serde_json::Value,
) {
    let output_folder = PathBuf::from(config["output_folder"].as_str().unwrap());

    // check if output folder exists
    if !output_folder.exists() {
        fs::create_dir_all(&output_folder).unwrap();
    }

    for recipe in recipes {
        let mut file = File::create(
            output_folder
                .join(recipe.id.to_string())
                .with_extension("html"),
        )
        .expect("Unable to create file");
        let data = json!({
            "name": recipe.name,
            "ingredients": recipe.ingredients,
            "instructions": recipe.instructions,
        });

        let full_data = json!({
            "config": config,
            "recipe": data,
        });

        let rendered = handlebars.render("recipe.hbs", &full_data).unwrap();
        file.write_all(rendered.as_bytes())
            .expect("Unable to write data");
    }
}

fn generate_index_page(handlebars: &Handlebars, recipes: &Vec<Recipe>, config: &serde_json::Value) {
    let output_folder = PathBuf::from(config["output_folder"].as_str().unwrap());

    // check if output folder exists
    if !output_folder.exists() {
        fs::create_dir_all(&output_folder).unwrap();
    }

    let mut file = File::create(output_folder.join("index.html")).expect("Unable to create file");

    let full_data = json!({
        "config": config,
        "recipes": recipes,
    });

    let rendered = handlebars.render("index.hbs", &full_data).unwrap();
    file.write_all(rendered.as_bytes())
        .expect("Unable to write data");
}

fn read_config() -> JsonValue {
    let mut file = File::open("./config.json").expect("Unable to open file");
    let mut contents = String::new();
    file.read_to_string(&mut contents)
        .expect("Unable to read string");
    let config: JsonValue = serde_json::from_str(&contents).unwrap();
    config
}

fn check_config_file_present() {
    if !PathBuf::from("./config.json").exists() {
        println!("Do you want to create a config file? [y/n]");
        let mut input = String::new();
        std::io::stdin()
            .read_line(&mut input)
            .expect("Failed to read line");
        if input.trim() != "y" {
            println!("No prandium without a config file!");
            std::process::exit(0);
        }
        let mut file = File::create("./config.json").expect("Unable to create file");
        let config = json!({
            "title": "My Cookbook",
            "author": "John Doe",
            "author_url": "https://google.com",
            "output_folder": "./docs",
            "description": "A cookbook for my recipes",
            "translations" : {
                "ingredients": "Ingredients",
                "instructions": "Instructions",
            }
        });
        let config_string = serde_json::to_string_pretty(&config).unwrap();
        file.write_all(config_string.as_bytes())
            .expect("Unable to write data");
    }
}

fn create_output_folder(config: &serde_json::Value) {
    let output_folder = PathBuf::from(config["output_folder"].as_str().unwrap());
    if !output_folder.exists() {
        fs::create_dir_all(&output_folder).unwrap();
    }
}

fn main() {
    println!("Hello from Prandium");

    check_config_file_present();

    let today_date = chrono::Local::today().format("%Y-%m-%d").to_string();
    let mut config = read_config();
    config["date"] = json!(today_date);

    create_output_folder(&config);

    let mut hbs = Handlebars::new();
    register_theme(&mut hbs);

    let recipes = parse_folder();

    generate_recipe_pages(&mut hbs, &recipes, &config);

    generate_index_page(&mut hbs, &recipes, &config);
}