~/website

5b96ebd533eac8ce1b36822a908fc80da05fe0a0 — arthurmelton 2fb4d353 2 years ago
working
3 files changed, 109 insertions(+), 9 deletions(-)

M Cargo.lock
M Cargo.toml
M src/main.rs
M Cargo.lock => Cargo.lock +32 -8
@@ 151,7 151,7 @@ dependencies = [

 "proc-macro2",
 "quote",
 "scratch",
 "syn",
 "syn 1.0.109",
]

[[package]]

@@ 168,7 168,7 @@ checksum = "0b75aed41bb2e6367cae39e6326ef817a851db13c13e4f3263714ca3cfb8de56"

dependencies = [
 "proc-macro2",
 "quote",
 "syn",
 "syn 1.0.109",
]

[[package]]

@@ 390,7 390,7 @@ dependencies = [

 "pest_meta",
 "proc-macro2",
 "quote",
 "syn",
 "syn 1.0.109",
]

[[package]]

@@ 629,9 629,20 @@ checksum = "1792db035ce95be60c3f8853017b3999209281c24e2ba5bc8e59bf97a0c590c1"


[[package]]
name = "serde"
version = "1.0.156"
version = "1.0.157"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "314b5b092c0ade17c00142951e50ced110ec27cea304b1037c6969246c2469a4"
checksum = "707de5fcf5df2b5788fca98dd7eab490bc2fd9b7ef1404defc462833b83f25ca"

[[package]]
name = "serde_derive"
version = "1.0.157"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78997f4555c22a7971214540c4a661291970619afd56de19f77e0de86296e1e5"
dependencies = [
 "proc-macro2",
 "quote",
 "syn 2.0.0",
]

[[package]]
name = "serde_json"

@@ 682,6 693,17 @@ dependencies = [

]

[[package]]
name = "syn"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cff13bb1732bccfe3b246f3fdb09edfd51c01d6f5299b7ccd9457c2e4e37774"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-ident",
]

[[package]]
name = "termcolor"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

@@ 707,7 729,7 @@ checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e"

dependencies = [
 "proc-macro2",
 "quote",
 "syn",
 "syn 1.0.109",
]

[[package]]

@@ 856,7 878,7 @@ dependencies = [

 "once_cell",
 "proc-macro2",
 "quote",
 "syn",
 "syn 1.0.109",
 "wasm-bindgen-shared",
]


@@ 878,7 900,7 @@ checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6"

dependencies = [
 "proc-macro2",
 "quote",
 "syn",
 "syn 1.0.109",
 "wasm-bindgen-backend",
 "wasm-bindgen-shared",
]

@@ 897,6 919,8 @@ dependencies = [

 "emojicons",
 "handlebars",
 "pulldown-cmark",
 "serde",
 "serde_derive",
 "toml",
 "walkdir",
]

M Cargo.toml => Cargo.toml +2 -0
@@ 10,5 10,7 @@ chrono = "0.4.24"

emojicons = "1.0.1"
handlebars = "4.3.6"
pulldown-cmark = "0.9.2"
serde = "1.0.157"
serde_derive = "1.0.157"
toml = "0.7.3"
walkdir = "2.3.3"

M src/main.rs => src/main.rs +75 -1
@@ 1,9 1,25 @@

use walkdir::WalkDir;
use std::fs::{self, File};
use std::path::Path;
use std::path::{Path, PathBuf};
use handlebars::Handlebars;
use std::io::Write;
use toml::{Table, Value};
use chrono::NaiveDate;
use serde_derive::Serialize;

#[derive(Clone, Serialize)]
struct Blog {
    config: Table,
    path: PathBuf
}

#[derive(Clone, Serialize)]
struct BlogPage {
    title: String,
    blogs: Vec<Blog>,
    before: Option<usize>,
    after: Option<usize>
}

fn main() {
    // make public folder

@@ 37,6 53,64 @@ fn main() {

            file.write_all(reg.render("main", &config).expect(&format!("Cant render {}", path.display())).as_bytes()).expect(&format!("Cant write to file {}", path.display()))
        }
    }

    //do the blogs
    match fs::read_dir("blogs") {
        Ok(blogs) => {
            fs::create_dir("public/blogs").expect("could not make the folder \"public/blogs\"");
            let mut all_blogs:Vec<Blog> = Vec::new();
            for i in blogs {
                if let Ok(i) = i {
                let path = i.path();
                    let contents = fs::read_to_string(path.clone()).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.strip_prefix("blogs").unwrap().with_extension("html");
                    let mut file = File::create(Path::new("public/blogs").join(path.clone())).expect(&format!("Cant make file public/blogs/{}", path.display()));
                    file.write_all(reg.render("main", &config).expect(&format!("Cant render public/blogs/{}", path.display())).as_bytes()).expect(&format!("Cant write to file public/blogs/{}", path.display()));
                    all_blogs.push(Blog {
                        config: config,
                        path: path.to_path_buf()
                    });
                }
            }
            all_blogs.sort_by(|a,b| NaiveDate::parse_from_str(b.config["date"].as_str().expect(&format!("{} does not have a date", b.path.display())), "%Y-%m-%d").expect(&format!("Cant convert {} date", b.path.display())).cmp(&NaiveDate::parse_from_str(a.config["date"].as_str().expect(&format!("{} does not have a date", a.path.display())), "%Y-%m-%d").expect(&format!("Cant convert {} date", a.path.display()))));
            let mut i = 0;
            let blog_pages = all_blogs.chunks(10).map(|x| {
                let before = if i > 0 {
                    Some(i-1)
                }
                else {
                    None
                };
                let after = if i < all_blogs.len()/10 {
                    Some(i+1)
                }
                else {
                    None
                };
                i+=1;
                BlogPage {
                    title: "Blogs".to_string(),
                    blogs: x.to_vec(),
                    before,
                    after
                }
            }).collect::<Vec<BlogPage>>();
            let mut x = 0;
            for i in blog_pages {
                let path_name = format!("public/blogs-{}.html", x);
                let path = Path::new(&path_name);
                let mut file = File::create(path.clone()).expect(&format!("Cant make file {}", path.display()));
                file.write_all(reg.render("main", &i).expect(&format!("Cant render {}", path.display())).as_bytes()).expect(&format!("Cant write to file {}", path.display()));
                x+=1;
            }

        },
        Err(_) => {}
    }
}

use emojicons::EmojiFormatter;