mlem/src/main.rs

60 lines
1.6 KiB
Rust
Raw Normal View History

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};
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");
2024-03-31 20:14:09 +02:00
markdown = re_key_value
.replace(markdown.clone().as_str(), "")
.to_string();
2024-03-31 20:08:43 +02:00
let mut key_value: HashMap<&str, &str> = HashMap::new();
for line in key_value_string.as_str().lines() {
if line == "---" {
continue;
}
key_value.insert(
2024-03-31 20:14:09 +02:00
line.split(":")
.collect::<Vec<&str>>()
.get(0)
.unwrap()
.trim(),
line.split(":")
.collect::<Vec<&str>>()
.get(1)
.unwrap()
.trim(),
2024-03-31 20:08:43 +02:00
);
}
2024-03-31 20:14:09 +02:00
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)
2024-03-31 20:08:43 +02:00
}