mlem/src/main.rs

61 lines
1.8 KiB
Rust
Raw Normal View History

2024-04-01 19:05:53 +02:00
use std::fs::read_to_string;
2024-04-08 17:45:35 +02:00
pub mod config;
pub mod emoji;
pub mod index;
2024-04-01 19:05:53 +02:00
use mlem::*;
2024-04-09 17:23:37 +02:00
use std::env;
2024-04-09 11:55:24 +02:00
2024-04-09 17:28:56 +02:00
fn main() {
2024-04-09 17:23:37 +02:00
let args: Vec<String> = env::args().collect();
if args.len() == 1 {
generate();
} else if args[1] == "create" {
create_default_env()
} else {
eprintln!("Option not recognized");
std::process::exit(1);
}
}
fn generate() {
2024-04-01 19:05:53 +02:00
let config = config::read_config();
let raw_files = read_src_files(&config.src_dir);
2024-04-06 21:39:27 +02:00
let mut post_index: Vec<index::BlogPost> = Vec::new();
2024-04-01 19:05:53 +02:00
for file in raw_files {
2024-04-02 21:48:02 +02:00
let mut markdown = read_to_string(file.path).expect("File does not exist");
2024-04-09 17:28:56 +02:00
markdown = emoji::emoji_pass(&markdown, &config.emoji_config);
2024-04-06 21:39:27 +02:00
let (html, index_content) = generate_blog_entry(markdown, &config.templates_dir);
2024-04-02 21:48:02 +02:00
write_to_fs(
html,
&config.output_dir,
2024-04-06 21:39:27 +02:00
&file.file_name.clone().into_string().unwrap(),
2024-04-02 21:48:02 +02:00
);
2024-04-06 21:39:27 +02:00
post_index.push(index::BlogPost::new(
file.title,
file.date.format("%Y-%m-%d").to_string(),
file.date.timestamp(),
index_content,
format!("{}.html", file.file_name.to_str().unwrap()),
));
2024-03-31 20:08:43 +02:00
}
2024-04-06 21:39:27 +02:00
let index = index::generate(post_index, &config.templates_dir);
std::fs::write(format!("{}/index.html", config.output_dir), index)
.unwrap_or_else(|_| panic!("Error writing index"));
2024-03-31 20:08:43 +02:00
}
2024-04-09 17:23:37 +02:00
fn create_default_env() {
println!("Writing a default mlem.toml");
let config = config::Config::default();
let toml_config = toml::to_string(&config).unwrap();
std::fs::write("mlem.toml", toml_config).unwrap();
println!("Creating default directorys");
std::fs::create_dir(config.output_dir).unwrap();
std::fs::create_dir(config.src_dir).unwrap();
std::fs::create_dir(config.templates_dir).unwrap();
}