From 7010360f049c066d83f1557128b90cdeb79bbfc0 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Mon, 8 Jan 2024 10:57:52 +0300 Subject: [PATCH] Add Markdown page creator --- src/page/mod.rs | 10 ++++++++ src/page/text/mod.rs | 2 +- src/routes/about.rs | 17 ------------- src/routes/mdpage.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++ src/routes/mod.rs | 4 +-- 5 files changed, 72 insertions(+), 20 deletions(-) delete mode 100644 src/routes/about.rs create mode 100644 src/routes/mdpage.rs diff --git a/src/page/mod.rs b/src/page/mod.rs index 202f2d2..fdfa99f 100644 --- a/src/page/mod.rs +++ b/src/page/mod.rs @@ -41,6 +41,16 @@ impl Page { Self::Other => "other", } } + + pub fn from_str(s: &str) -> Self { + match s { + "home" => Self::Home, + "about" => Self::About, + "blog" => Self::Blog, + "contact" => Self::Contact, + _ => Self::Other, + } + } } /// Creates a page with the given head and body. /// diff --git a/src/page/text/mod.rs b/src/page/text/mod.rs index 870dfb8..c6f7344 100644 --- a/src/page/text/mod.rs +++ b/src/page/text/mod.rs @@ -13,7 +13,7 @@ use crate::page::{ }; /// Creates a simple text page. -pub fn create(title: Option<&str>, page: Page, body: Markup) -> Markup { +pub fn create(title: Option<&str>, page: Page, body: &Markup) -> Markup { crate::page::create( title, html! { diff --git a/src/routes/about.rs b/src/routes/about.rs deleted file mode 100644 index 932845f..0000000 --- a/src/routes/about.rs +++ /dev/null @@ -1,17 +0,0 @@ -use maud::Markup; - -use crate::{ - markdown, - page::{ - text, - Page, - }, -}; - -pub async fn handler() -> Markup { - text::create( - Some("About"), - Page::About, - markdown::parse(embed::string!("about.md").as_ref()), - ) -} diff --git a/src/routes/mdpage.rs b/src/routes/mdpage.rs new file mode 100644 index 0000000..c275d6e --- /dev/null +++ b/src/routes/mdpage.rs @@ -0,0 +1,59 @@ +use std::{ + collections::HashMap, + path, + sync::LazyLock, +}; + +use axum::{ + body::Body, + extract::Path, + http::{ + Response, + StatusCode, + }, + response::{ + Html, + IntoResponse, + }, +}; +use maud::Markup; + +use crate::{ + markdown, + page::{ + text, + Page, + }, +}; + +static PAGES: LazyLock> = LazyLock::new(|| { + let mut pages = HashMap::new(); + + for file in embed::dir!(".").flatten() { + let path = path::Path::new(file.path().as_ref()) + .file_name() + .unwrap() + .to_str() + .unwrap(); + + if !path.ends_with(".md") { + continue; + } + + log::info!("Adding page {path}"); + pages.insert( + path.to_string().strip_suffix(".md").unwrap().to_string(), + markdown::parse(&String::from_utf8(file.content().to_vec()).unwrap()), + ); + } + + pages +}); + +pub async fn handler(Path(path): Path) -> Response { + if let Some(body) = PAGES.get(&path) { + Html(text::create(Some("test"), Page::from_str(&path), &body).into_string()).into_response() + } else { + StatusCode::NOT_FOUND.into_response() + } +} diff --git a/src/routes/mod.rs b/src/routes/mod.rs index eaa7fae..d3bc2a2 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -3,13 +3,13 @@ use axum::{ Router, }; -mod about; mod assets; mod index; +mod mdpage; pub fn router() -> Router { Router::new() .route("/", get(index::handler)) - .route("/about", get(about::handler)) + .route("/:page", get(mdpage::handler)) .route("/assets/*path", get(assets::handler)) }