Static site generator + my presonnal website written in rust for some reason.

rewrite to be impromptu Saait

+74 -12
+1 -1
Cargo.toml
··· 12 12 13 13 [dependencies] 14 14 askama = { version = "0.12.1" } 15 - comrak = "0.27.0" 15 + comrak = { version = "0.27.0", features = ["syntect"] } 16 16 markdown-parser = "0.1.2" 17 17 rand = "0.8.5" 18 18 serde = { version = "1.0.209", features = ["derive"] }
+67 -5
posts/005-regenesis.md
··· 12 12 [theprimeagen](https://www.youtube.com/watch?v=rcZSOLAI1lM), and everpresent desire to rewrite everything in Rust, 13 13 the new website is a complete rewrite using rust and adjacent techniques. 14 14 15 - Using only 15 + Using only 16 16 ``` 17 17 [dependencies] 18 18 askama = { version = "0.12.1", features = ["with-axum"] } ··· 26 26 tokio = { version = "1.39.3", features = ["macros", "rt-multi-thread"] } 27 27 tower-http = { version = "0.5.2", features = ["fs"] } 28 28 ``` 29 + as dependencies in the end we get a clean~ish, 2.8MB executable. 30 + 31 + All markdown compilation gets done every time page is loaded, which is sub-optimal, 32 + But reading time for all posts is non-significant compared to other loading times. 33 + 34 + And voila, there is the new post. 35 + 36 + ## Update 37 + 38 + This approach however had few issues. 39 + As fun as it is writing your own routing logic, It feels purely unnecessary. 40 + 41 + Even scaling down from `axum` to `tiny_http`, it doesn't change the binary size, and because of the limited resources and location of the VPS, 42 + it does not affect the load times. 43 + 44 + Short of rewriting it all in [Yew](yew.rs) and loading the wasm as a SPA (which comes with it's own complexity) loading times would not improve. 45 + 46 + Therefore the goal has been slightly changed. 47 + From serving the files the goal is now using askama like a sort of static site generator. 48 + 49 + # DIY Hugo? 50 + 51 + Not exactly. In the current state it is basically Saait again, but with extra steps. 52 + Additional pages require source code intervention, which isn't hard, but tidious. 53 + 54 + //TODO: use some enum for pages, with derive as EnumString, so new pages can be added simply by adding a template. 55 + 56 + 57 + But for now, the new dependencies now look like this: 58 + 59 + ```toml 60 + [dependencies] 61 + askama = { version = "0.12.1" } 62 + comrak = "0.27.0" 63 + markdown-parser = "0.1.2" 64 + rand = "0.8.5" 65 + serde = { version = "1.0.209", features = ["derive"] } 66 + serde_yaml = "0.9.34" 67 + syntect = "5.2.0" 68 + ``` 69 + 70 + Syntect providing oh so nice code highlighting in the markdown. 71 + 29 72 30 73 ```json 31 74 { ··· 35 78 } 36 79 ``` 37 80 38 - as dependancies in the end we get a clean~ish, 2.8MB executable. 81 + ```rust 82 + 83 + std::fs::write(format!("{output_path}/index.html"), handlers::index().as_bytes()).expect("Couldnt write index file"); 84 + std::fs::write(format!("{output_path}/about.html"), handlers::about().as_bytes()).expect("Couldnt write about file"); 85 + std::fs::write(format!("{output_path}/404.html"), handlers::not_found().as_bytes()).expect("Couldnt write 404 file"); 39 86 40 - All markdown compilation gets done every time page is loaded, which is sub-optimal, 41 - But reading time for all posts is non-significant compared to other loading times. 42 87 43 - And voila, there is the new post. 88 + for entry in post_dir_iter { 89 + if let Ok(entry) = entry { 90 + 91 + let filename = entry.file_name().into_string().unwrap().split(".").collect::<Vec<_>>()[0].to_string(); 92 + 93 + fs::write(format!("{output_path}/blog/{filename}.html"), handlers::blog(filename).as_bytes()).expect("Couldnt write blog entry"); 94 + }; 95 + 96 + } 97 + 98 + ``` 99 + 100 + For now, I am rather happy with the result. 101 + 102 + Built for release profile, binary is just 3.1M (majority of it is syntact), and produces a output folder with it's contents in a rather pleasant amount of time. 103 + > ./target/release/rusty_duck 0.03s user 0.01s system 97% cpu 0.035 total 104 + 105 + This is no **BLAZING** speed, but it's honest work. 44 106
+1 -1
src/handlers.rs
··· 42 42 options.extension.strikethrough = true; 43 43 let mut plugins = comrak::Plugins::default(); 44 44 let adapter = comrak::plugins::syntect::SyntectAdapterBuilder::new() 45 - .theme("base16-ocean.dark") 45 + .theme("base16-mocha.dark") 46 46 .build(); 47 47 48 48 plugins.render.codefence_syntax_highlighter = Some(&adapter);
+5 -5
src/main.rs
··· 1 - use std::{fs::{self, read_dir, DirBuilder},path::Path}; 1 + use std::{fs::{write, read_dir, DirBuilder},path::Path}; 2 2 3 3 mod handlers; 4 4 mod structs; ··· 23 23 24 24 25 25 26 - fs::write(format!("{output_path}/index.html"), handlers::index().as_bytes()).expect("Couldnt write index file"); 27 - fs::write(format!("{output_path}/about.html"), handlers::about().as_bytes()).expect("Couldnt write about file"); 28 - fs::write(format!("{output_path}/404.html"), handlers::not_found().as_bytes()).expect("Couldnt write 404 file"); 26 + write(format!("{output_path}/index.html"), handlers::index().as_bytes()).expect("Couldnt write index file"); 27 + write(format!("{output_path}/about.html"), handlers::about().as_bytes()).expect("Couldnt write about file"); 28 + write(format!("{output_path}/404.html"), handlers::not_found().as_bytes()).expect("Couldnt write 404 file"); 29 29 30 30 match DirBuilder::new() 31 31 .create(format!("{output_path}/blog")) { ··· 44 44 45 45 let filename = entry.file_name().into_string().unwrap().split(".").collect::<Vec<_>>()[0].to_string(); 46 46 47 - fs::write(format!("{output_path}/blog/{filename}.html"), handlers::blog(filename).as_bytes()).expect("Couldnt write blog entry"); 47 + write(format!("{output_path}/blog/{filename}.html"), handlers::blog(filename).as_bytes()).expect("Couldnt write blog entry"); 48 48 }; 49 49 50 50 }