46 lines
1.3 KiB
Rust
46 lines
1.3 KiB
Rust
|
use markdown::{to_html_with_options, CompileOptions, Options};
|
||
|
use regex::Regex;
|
||
|
use std::collections::HashMap;
|
||
|
use std::{env, fs::read_to_string};
|
||
|
fn main() {
|
||
|
let args: Vec<String> = env::args().collect();
|
||
|
if args.len() < 2 {
|
||
|
panic!("Please provide md file");
|
||
|
}
|
||
|
|
||
|
let mut markdown = read_to_string(args.get(1).unwrap()).expect("File does not exist");
|
||
|
|
||
|
let re_key_value = Regex::new(r"(?ms)---(.*)---(?:\n)").unwrap();
|
||
|
|
||
|
let binding_markdown = markdown.clone();
|
||
|
|
||
|
let key_value_string = re_key_value
|
||
|
.find(binding_markdown.as_str())
|
||
|
.expect("Can't find key value map in markdown");
|
||
|
|
||
|
markdown = re_key_value.replace(markdown.clone().as_str(), "").to_string();
|
||
|
|
||
|
let mut key_value: HashMap<&str, &str> = HashMap::new();
|
||
|
|
||
|
for line in key_value_string.as_str().lines() {
|
||
|
if line == "---" {
|
||
|
continue;
|
||
|
}
|
||
|
key_value.insert(
|
||
|
line.split(":").collect::<Vec<&str>>().get(0).unwrap().trim(),
|
||
|
line.split(":").collect::<Vec<&str>>().get(1).unwrap().trim(),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
let html_markdown = to_html_with_options(&markdown, &Options {
|
||
|
compile: CompileOptions {
|
||
|
allow_dangerous_html: true,
|
||
|
allow_dangerous_protocol: true,
|
||
|
..CompileOptions::default()
|
||
|
},
|
||
|
..Options::default()}).unwrap();
|
||
|
|
||
|
println!("{}",html_markdown)
|
||
|
|
||
|
}
|