diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/layout.rs | 21 | ||||
-rw-r--r-- | src/main.rs | 6 | ||||
-rw-r--r-- | src/pages.rs | 24 |
3 files changed, 44 insertions, 7 deletions
diff --git a/src/layout.rs b/src/layout.rs index 421f50e..efab6b8 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -7,11 +7,12 @@ Copyright (C) 2023 metamuffin <metamuffin.org> use crate::blog::rocket_uri_macro_r_blog; use crate::pages::{ rocket_uri_macro_r_about, rocket_uri_macro_r_contact, rocket_uri_macro_r_pgp_key, - rocket_uri_macro_r_projects, + rocket_uri_macro_r_projects, rocket_uri_macro_r_toggle_css, }; use crate::source::rocket_uri_macro_r_source; use crate::uri; use markup::Render; +use rocket::http::CookieJar; use rocket::{ http::ContentType, response::{self, Responder}, @@ -20,11 +21,17 @@ use rocket::{ use std::io::Cursor; markup::define! { - ScaffoldImpl<Main: Render>(title: String, main: Main, noimg: bool) { + ScaffoldImpl<Main: Render>( + title: String, + main: Main, + noimg: bool, + include_css: bool + ) { @markup::doctype() html { head { title { @title " - " "metamuffin's website" } + @if *include_css { link[rel="stylesheet", href="/style.css"]; } } body { @if !noimg { img[src="https://s.metamuffin.org/avatar/default-512.webp", align="left", height=80, hspace=10]; } @@ -38,7 +45,7 @@ markup::define! { a[href=uri!(r_pgp_key())] { "PGP-Key" } " " } hr; - section { @main } + article { @main } hr; footer { p { @@ -46,6 +53,10 @@ markup::define! { "sources available on " a[href=uri!(r_source())] { "this page itself" } " and on " a[href="https://codeberg.org/metamuffin/website"] { "codeberg" } } + p { "In case you " i {"really"} " want to, you can enable stylesheets." } + form[action=uri!(r_toggle_css()), method="POST"] { + input[type="submit", value="Toggle CSS (uses a cookie)"]; + } } } } @@ -60,12 +71,14 @@ pub struct Scaffold<T> { } impl<'r, Main: Render> Responder<'r, 'static> for Scaffold<Main> { - fn respond_to(self, _req: &'r Request<'_>) -> response::Result<'static> { + fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> { + let jar = async_std::task::block_on(req.guard::<&CookieJar>()).unwrap(); let mut out = String::new(); ScaffoldImpl { main: self.content, noimg: self.title == "Source", title: self.title, + include_css: jar.get("css").map(|v| v.value() == "yes").unwrap_or(false), } .render(&mut out) .unwrap(); diff --git a/src/main.rs b/src/main.rs index 70646c3..da03c61 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,18 +4,18 @@ Copyright (C) 2023 metamuffin <metamuffin.org> */ pub mod blog; +pub mod error; pub mod layout; pub mod pages; pub mod source; pub mod wellknown; -pub mod error; use blog::*; +use error::*; use pages::*; use rocket::{catchers, fairing::AdHoc, http::Header, routes}; use source::*; use wellknown::*; -use error::*; #[tokio::main] async fn main() { @@ -39,6 +39,8 @@ async fn main() { r_blog_index, r_blog_article, r_blog_atom, + r_style, + r_toggle_css, r_wellknown_security, r_wellknown_matrix_server, r_wellknown_matrix_client, diff --git a/src/pages.rs b/src/pages.rs index 482e254..7c92c4b 100644 --- a/src/pages.rs +++ b/src/pages.rs @@ -1,5 +1,11 @@ use crate::layout::{DynScaffold, Scaffold}; -use rocket::{get, response::Redirect, uri}; +use rocket::{ + get, + http::{ContentType, Cookie, CookieJar}, + post, + response::Redirect, + uri, +}; #[get("/")] pub fn r_root() -> Redirect { @@ -95,3 +101,19 @@ pub fn r_contact() -> DynScaffold<'static> { pub fn r_pgp_key() -> &'static str { include_str!("../assets/key.asc") } + +#[get("/style.css")] +pub fn r_style() -> (ContentType, &'static str) { + (ContentType::CSS, include_str!("../assets/style.css")) +} + +#[post("/toggle_css")] +pub fn r_toggle_css(jar: &CookieJar) -> Redirect { + let has_css = jar.get("css").map(|v| v.value() == "yes").unwrap_or(false); + jar.add( + Cookie::build("css", if has_css { "no" } else { "yes" }) + .permanent() + .finish(), + ); + Redirect::to(rocket::uri!(r_root())) +} |