2024-03-31 20:08:43 +02:00
|
|
|
use markdown::{to_html_with_options, CompileOptions, Options};
|
|
|
|
use regex::Regex;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::{env, fs::read_to_string};
|
2024-03-31 22:10:07 +02:00
|
|
|
use tera::{Context, Tera};
|
2024-03-31 20:08:43 +02:00
|
|
|
fn main() {
|
|
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
if args.len() < 2 {
|
|
|
|
panic!("Please provide md file");
|
|
|
|
}
|
|
|
|
|
2024-03-31 22:10:07 +02:00
|
|
|
let markdown = read_to_string(args.get(1).unwrap()).expect("File does not exist");
|
|
|
|
|
|
|
|
println!("{}", generate_blog_entry(markdown));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_blog_entry(markdown: String) -> String {
|
|
|
|
let markdown = markdown.clone();
|
2024-03-31 20:08:43 +02:00
|
|
|
|
2024-03-31 21:06:26 +02:00
|
|
|
let mut tera = Tera::new("templates/*").unwrap();
|
|
|
|
tera.autoescape_on(vec![]);
|
|
|
|
|
2024-03-31 22:10:07 +02:00
|
|
|
let (mut key_value, markdown) = get_kv(markdown);
|
|
|
|
|
|
|
|
let html_markdown = to_html_with_options(
|
|
|
|
&markdown,
|
|
|
|
&Options {
|
|
|
|
compile: CompileOptions {
|
|
|
|
allow_dangerous_html: true,
|
|
|
|
allow_dangerous_protocol: true,
|
|
|
|
..CompileOptions::default()
|
|
|
|
},
|
|
|
|
..Options::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2024-03-31 20:08:43 +02:00
|
|
|
|
2024-03-31 22:10:07 +02:00
|
|
|
key_value.insert("blog_content".to_string(), html_markdown);
|
|
|
|
|
|
|
|
let context = Context::from_serialize(&key_value).unwrap();
|
|
|
|
|
|
|
|
tera.render(key_value.get("template").unwrap(), &context)
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_kv(markdown: String) -> (HashMap<String, String>, String) {
|
|
|
|
let re_key_value = Regex::new(r"(?ms)---(.*)---(?:\n)").unwrap();
|
2024-03-31 20:08:43 +02:00
|
|
|
|
|
|
|
let key_value_string = re_key_value
|
2024-03-31 22:10:07 +02:00
|
|
|
.find(markdown.as_str())
|
2024-03-31 20:08:43 +02:00
|
|
|
.expect("Can't find key value map in markdown");
|
|
|
|
|
2024-03-31 22:10:07 +02:00
|
|
|
let content_markdown = re_key_value
|
2024-03-31 20:14:09 +02:00
|
|
|
.replace(markdown.clone().as_str(), "")
|
|
|
|
.to_string();
|
2024-03-31 20:08:43 +02:00
|
|
|
|
2024-03-31 22:10:07 +02:00
|
|
|
let mut key_value: HashMap<String, String> = HashMap::new();
|
2024-03-31 20:08:43 +02:00
|
|
|
|
|
|
|
for line in key_value_string.as_str().lines() {
|
|
|
|
if line == "---" {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
key_value.insert(
|
2024-03-31 22:10:07 +02:00
|
|
|
line.split(':')
|
|
|
|
.collect::<Vec<&str>>().first()
|
2024-03-31 20:14:09 +02:00
|
|
|
.unwrap()
|
2024-03-31 22:10:07 +02:00
|
|
|
.trim()
|
|
|
|
.to_string(),
|
|
|
|
line.split(':')
|
2024-03-31 20:14:09 +02:00
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.get(1)
|
|
|
|
.unwrap()
|
2024-03-31 22:10:07 +02:00
|
|
|
.trim()
|
|
|
|
.to_string(),
|
2024-03-31 20:08:43 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-03-31 22:10:07 +02:00
|
|
|
(key_value, content_markdown)
|
2024-03-31 20:08:43 +02:00
|
|
|
}
|