Newer
Older
use crate::{
image::MyImage, load::load_from_path, misc::Misc, post::Post, resp::Response, style::Style,
};
}
impl Blog {
pub fn new() -> Result<Self, Error> {
/*let pages = fs::read_dir("pages")?
.map(|path| Page::new(path?)?)
.collect();*/
let posts = load_from_path("posts", "posts")?;
let imgs = load_from_path("assets/img", "")?;
let misc = load_from_path("assets/misc", "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> {
self.posts.sort();
self.posts.reverse();
r#"<div class="centered"><h1>Stephen's Site</h1>Rewritten in Rust!</div><h4>Recent Posts</h4><table>"#
Ok(dress_page("Stephen's Site", &content))
}
}
pub struct RenderedBlog {
.map(|x| (StatusCode::OK, x.clone()))
.unwrap_or((StatusCode::NOT_FOUND, self.not_found.clone()))
}
}
impl TryFrom<Blog> for RenderedBlog {
type Error = Error;
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));
pages.insert("/".to_string(), Response::html(home));
for img in b.imgs {
let (original_url, original, thumbnail_url, thumbnail) = img.into_responses()?;
pages.insert(thumbnail_url, thumbnail);
pages.insert(original_url, original);
}
for m in b.misc {
pages.insert(m.url.clone(), m.response());
}
let not_found = Response::html(dress_page(
"Page not found",
include_str!("assets/404.html"),
));
Ok(Self { not_found, pages })
}
}
fn dress_page(title: &str, content: &str) -> String {
format!(
r#"<!DOCTYPE html><html><head><link rel="stylesheet" href="/style.css" /><title>{title}</title></head><body><a href="/">Home</a><hr>{content}</body></html>"#