Skip to content
Snippets Groups Projects
Commit c8bf7385 authored by Stephen D's avatar Stephen D
Browse files

allow custom css

parent d7075542
No related branches found
No related tags found
No related merge requests found
......@@ -2,13 +2,16 @@ use anyhow::Error;
use std::collections::HashMap;
use warp::hyper::StatusCode;
use crate::{image::MyImage, load::load_from_path, misc::Misc, post::Post, resp::Response};
use crate::{
image::MyImage, load::load_from_path, misc::Misc, post::Post, resp::Response, style::Style,
};
pub struct Blog {
//pages: Vec<Page>,
posts: Vec<Post>,
imgs: Vec<MyImage>,
misc: Vec<Misc>,
custom_style: Option<Style>,
}
impl Blog {
......@@ -23,7 +26,14 @@ impl Blog {
let misc = load_from_path("assets/misc", "misc")?;
Ok(Self { posts, imgs, misc })
let custom_style = Style::load_if_exists("assets/style.css")?;
Ok(Self {
posts,
imgs,
misc,
custom_style,
})
}
fn home(&mut self) -> Result<String, Error> {
......@@ -70,10 +80,12 @@ impl TryFrom<Blog> for RenderedBlog {
pages.insert(p.url.clone(), Response::html(body));
}
pages.insert(
"/style.css".to_string(),
Response::css(include_str!("assets/style.css").to_string()),
);
let mut style = include_str!("assets/style.css").to_string();
if let Some(s) = &b.custom_style {
style.push_str(&s.content);
}
pages.insert("/style.css".to_string(), Response::css(style));
let home = b.home()?;
pages.insert("/".to_string(), Response::html(home));
......
......@@ -17,6 +17,7 @@ mod misc;
mod parser;
mod post;
mod resp;
mod style;
mod util;
async fn handle_request(
......
use std::{fs, io::ErrorKind, path::Path};
pub struct Style {
pub content: String,
}
impl Style {
pub fn load_if_exists<P: AsRef<Path>>(path: P) -> anyhow::Result<Option<Self>> {
let content = match fs::read_to_string(path) {
Ok(x) => x,
Err(e) => {
if e.kind() == ErrorKind::NotFound {
return Ok(None);
}
return Err(e.into());
}
};
Ok(Some(Self { content }))
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment