~/website

website/src/main.rs -rw-r--r-- 2.50 kB
2fb4d353 — arthurmelton format pages 2 years ago

            
2e51be12 arthurmelton
2fb4d353 arthurmelton
d6b497e2 Arthur Melton
2e51be12 arthurmelton
2fb4d353 arthurmelton
2e51be12 arthurmelton
2fb4d353 arthurmelton
2e51be12 arthurmelton
5b96ebd5 arthurmelton
2e51be12 arthurmelton
5b96ebd5 arthurmelton
2e51be12 arthurmelton
5b96ebd5 arthurmelton
0f8635bc arthurmelton
d9fbf244 Arthur Melton
d6b497e2 Arthur Melton
d9fbf244 Arthur Melton
4ea5743d arthurmelton
2fb4d353 arthurmelton
d6b497e2 Arthur Melton
d9fbf244 Arthur Melton
2fb4d353 arthurmelton
0f8635bc arthurmelton
188f6383 Arthur Melton
9226e0e3 Arthur Melton
188f6383 Arthur Melton
2e51be12 arthurmelton
0f8635bc arthurmelton
2fb4d353 arthurmelton
2e51be12 arthurmelton
2fb4d353 arthurmelton
2e51be12 arthurmelton
2fb4d353 arthurmelton
2e51be12 arthurmelton
9226e0e3 Arthur Melton
2e51be12 arthurmelton
2fb4d353 arthurmelton
5b96ebd5 arthurmelton
2e51be12 arthurmelton
073f1644 Arthur Melton
2e51be12 arthurmelton
073f1644 Arthur Melton
2e51be12 arthurmelton
073f1644 Arthur Melton
2e51be12 arthurmelton
073f1644 Arthur Melton
2e51be12 arthurmelton
073f1644 Arthur Melton
2e51be12 arthurmelton
073f1644 Arthur Melton
2e51be12 arthurmelton
d6b497e2 Arthur Melton
77beca8c Arthur Melton
d6b497e2 Arthur Melton
d9fbf244 Arthur Melton
d6b497e2 Arthur Melton
8bbbbea3 Arthur Melton
d6b497e2 Arthur Melton
d9fbf244 Arthur Melton
c0811f75 Arthur Melton
d6b497e2 Arthur Melton
8bbbbea3 Arthur Melton
d6b497e2 Arthur Melton
d9fbf244 Arthur Melton
d6b497e2 Arthur Melton
d9fbf244 Arthur Melton
d6b497e2 Arthur Melton
d9fbf244 Arthur Melton
0fd9e0c1 Arthur Melton
2e51be12 arthurmelton
5b96ebd5 arthurmelton
2fb4d353 arthurmelton
cd00e4d6 Arthur Melton
2fb4d353 arthurmelton
4ea5743d arthurmelton





















































































































































































































































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
use walkdir::WalkDir;
use std::fs::{self, File};
use std::path::Path;
use handlebars::Handlebars;
use std::io::Write;
use toml::{Table, Value};

fn main() {
    // make public folder
    let _ = fs::remove_dir_all("public");
    fs::create_dir("public").expect("could not make the folder \"public\"");

    //copy all static
    for e in WalkDir::new("static").into_iter().filter_map(|e| e.ok()) {
        if e.metadata().unwrap().is_file() {
            let mut path = e.path();
            path = path.strip_prefix("static/").unwrap(); // should never fail
            fs::copy(e.path(), Path::new("public").join(path)).expect(&format!("failed to copy static/{} to public/{}", path.display(), path.display()));
        }
    }

    // make template
    let mut reg = Handlebars::new();
    reg.register_template_string("main", fs::read_to_string("template.html.hbs").expect("cant read template.html.hbs")).expect("cant make template");

    //format all pages
    for e in WalkDir::new("pages").into_iter().filter_map(|e| e.ok()) {
        if e.metadata().unwrap().is_file() {
            let path = e.path();
            let contents = fs::read_to_string(path).expect(&format!("Cant read file {}", path.display()));
            let mut config = format!("{}\ncontent = \"\"", contents.split("+++").nth(1).expect(&format!("{} does not have a +++ section", path.display()))).parse::<Table>().unwrap();
            let content = contents.split("+++").nth(2).expect(&format!("{} does not have a +++ section", path.display()));
            let config_content = config.get_mut("content").unwrap();
            *config_content = Value::try_from(md_to_html(content)).unwrap();
            let path = Path::new("public").join(path.strip_prefix("pages").unwrap()).with_extension("html");
            let mut file = File::create(path.clone()).expect(&format!("Cant make file {}", path.display()));
            file.write_all(reg.render("main", &config).expect(&format!("Cant render {}", path.display())).as_bytes()).expect(&format!("Cant write to file {}", path.display()))
        }
    }
}

use emojicons::EmojiFormatter;
use pulldown_cmark::{html, Options, Parser};

pub fn md_to_html(input: &str) -> String {
    let input = EmojiFormatter(input).to_string();
    let mut options = Options::empty();
    options.insert(Options::ENABLE_STRIKETHROUGH);
    let parser = Parser::new_ext(&input, options);
    let mut html_output = String::new();
    html::push_html(&mut html_output, parser);
    html_output
}