From bb50e3b2e5075f0cea730de3e2e1033fa00bb98f Mon Sep 17 00:00:00 2001 From: Technoduck Date: Sat, 31 Aug 2024 15:56:15 -0400 Subject: [PATCH] rewrite to be impromptu Saait --- Cargo.toml | 2 +- posts/005-regenesis.md | 72 +++++++++++++++++++++++++++++++++++++++--- src/handlers.rs | 2 +- src/main.rs | 10 +++--- 4 files changed, 74 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 010432a..2a3c8cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ strip = "symbols" # Strip symbols from binary [dependencies] askama = { version = "0.12.1" } -comrak = "0.27.0" +comrak = { version = "0.27.0", features = ["syntect"] } markdown-parser = "0.1.2" rand = "0.8.5" serde = { version = "1.0.209", features = ["derive"] } diff --git a/posts/005-regenesis.md b/posts/005-regenesis.md index ef9bf46..10874a2 100644 --- a/posts/005-regenesis.md +++ b/posts/005-regenesis.md @@ -12,7 +12,7 @@ This time inspied by [this article](https://blog.transrights.art/blogs/2024_Scre [theprimeagen](https://www.youtube.com/watch?v=rcZSOLAI1lM), and everpresent desire to rewrite everything in Rust, the new website is a complete rewrite using rust and adjacent techniques. -Using only +Using only ``` [dependencies] askama = { version = "0.12.1", features = ["with-axum"] } @@ -26,6 +26,49 @@ serde_yaml = "0.9.34" tokio = { version = "1.39.3", features = ["macros", "rt-multi-thread"] } tower-http = { version = "0.5.2", features = ["fs"] } ``` +as dependencies in the end we get a clean~ish, 2.8MB executable. + +All markdown compilation gets done every time page is loaded, which is sub-optimal, +But reading time for all posts is non-significant compared to other loading times. + +And voila, there is the new post. + +## Update + +This approach however had few issues. +As fun as it is writing your own routing logic, It feels purely unnecessary. + +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, +it does not affect the load times. + +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. + +Therefore the goal has been slightly changed. +From serving the files the goal is now using askama like a sort of static site generator. + +# DIY Hugo? + +Not exactly. In the current state it is basically Saait again, but with extra steps. +Additional pages require source code intervention, which isn't hard, but tidious. + +//TODO: use some enum for pages, with derive as EnumString, so new pages can be added simply by adding a template. + + +But for now, the new dependencies now look like this: + +```toml +[dependencies] +askama = { version = "0.12.1" } +comrak = "0.27.0" +markdown-parser = "0.1.2" +rand = "0.8.5" +serde = { version = "1.0.209", features = ["derive"] } +serde_yaml = "0.9.34" +syntect = "5.2.0" +``` + +Syntect providing oh so nice code highlighting in the markdown. + ```json { @@ -35,10 +78,29 @@ tower-http = { version = "0.5.2", features = ["fs"] } } ``` -as dependancies in the end we get a clean~ish, 2.8MB executable. +```rust -All markdown compilation gets done every time page is loaded, which is sub-optimal, -But reading time for all posts is non-significant compared to other loading times. +std::fs::write(format!("{output_path}/index.html"), handlers::index().as_bytes()).expect("Couldnt write index file"); +std::fs::write(format!("{output_path}/about.html"), handlers::about().as_bytes()).expect("Couldnt write about file"); +std::fs::write(format!("{output_path}/404.html"), handlers::not_found().as_bytes()).expect("Couldnt write 404 file"); -And voila, there is the new post. + +for entry in post_dir_iter { + if let Ok(entry) = entry { + + let filename = entry.file_name().into_string().unwrap().split(".").collect::>()[0].to_string(); + + fs::write(format!("{output_path}/blog/{filename}.html"), handlers::blog(filename).as_bytes()).expect("Couldnt write blog entry"); + }; + +} + +``` + +For now, I am rather happy with the result. + +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. +> ./target/release/rusty_duck 0.03s user 0.01s system 97% cpu 0.035 total + +This is no **BLAZING** speed, but it's honest work. diff --git a/src/handlers.rs b/src/handlers.rs index 5a567e9..a55dc02 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -42,7 +42,7 @@ fn parse_markdown(content: &str) -> String { options.extension.strikethrough = true; let mut plugins = comrak::Plugins::default(); let adapter = comrak::plugins::syntect::SyntectAdapterBuilder::new() - .theme("base16-ocean.dark") + .theme("base16-mocha.dark") .build(); plugins.render.codefence_syntax_highlighter = Some(&adapter); diff --git a/src/main.rs b/src/main.rs index daf2e5e..afc8c47 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::{fs::{self, read_dir, DirBuilder},path::Path}; +use std::{fs::{write, read_dir, DirBuilder},path::Path}; mod handlers; mod structs; @@ -23,9 +23,9 @@ fn main() { - fs::write(format!("{output_path}/index.html"), handlers::index().as_bytes()).expect("Couldnt write index file"); - fs::write(format!("{output_path}/about.html"), handlers::about().as_bytes()).expect("Couldnt write about file"); - fs::write(format!("{output_path}/404.html"), handlers::not_found().as_bytes()).expect("Couldnt write 404 file"); + write(format!("{output_path}/index.html"), handlers::index().as_bytes()).expect("Couldnt write index file"); + write(format!("{output_path}/about.html"), handlers::about().as_bytes()).expect("Couldnt write about file"); + write(format!("{output_path}/404.html"), handlers::not_found().as_bytes()).expect("Couldnt write 404 file"); match DirBuilder::new() .create(format!("{output_path}/blog")) { @@ -44,7 +44,7 @@ fn main() { let filename = entry.file_name().into_string().unwrap().split(".").collect::>()[0].to_string(); - fs::write(format!("{output_path}/blog/{filename}.html"), handlers::blog(filename).as_bytes()).expect("Couldnt write blog entry"); + write(format!("{output_path}/blog/{filename}.html"), handlers::blog(filename).as_bytes()).expect("Couldnt write blog entry"); }; }