Newer
Older
use fnv::FnvHashMap;
favicon::Favicon, image::MyImage, load::load_from_path, misc::Misc, path::UrlPath, post::Post,
resp::Response, style::Style,
let posts = load_from_path("posts", "posts")?;
let imgs = load_from_path("assets/img", "")?;
let custom_style = Style::load_if_exists("assets/style.css")?;
r#"<div class="centered"><h1>Stephen's Site</h1></div>Welcome to my corner of the Internet. Occasionally I will do something kind of interesting and post about it here. Bear with me - I'm a developer, not a writer, so I'm bad at keeping this place up to date! For a more complete list of my work, please check out my <a href="https://gitlab.scd31.com/stephen">Git repository</a>.<h2>Recent Posts</h2><table>"#
Ok(dress_page(
"Stephen's Site",
&content,
&self.pages,
&self.favicon,
))
pages: FnvHashMap<UrlPath, Response>,
.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 pages = FnvHashMap::default();
let mut style = include_str!("assets/style.css").to_string();
if let Some(s) = &b.custom_style {
style.push_str(&s.content);
}
for img in b.imgs {
let (original_url, original, thumbnail_url, thumbnail) = img.into_responses()?;
insert_path(&mut pages, &thumbnail_url, thumbnail)?;
insert_path(&mut pages, &original_url, original)?;
let not_found = Response::html(dress_page(
"Page not found",
include_str!("assets/404.html"),
if let Some(fav) = b.favicon {
for (p, r) in fav.responses {
insert_path(&mut pages, p, r)?;
}
}
pages: &mut FnvHashMap<UrlPath, Response>,
path: &str,
value: Response,
) -> anyhow::Result<()> {
let path = UrlPath::new(path);
if pages.contains_key(&path) {
bail!(
"Cannot serve {} - already serving an asset with the same path",
path
);
}
pages.insert(path, value);
Ok(())
}
fn dress_page(title: &str, content: &str, pages: &[Post], favicon: &Option<Favicon>) -> String {
let favicons = match favicon {
Some(f) => f.html(),
None => "",
};
let mut page_links = String::new();
for p in pages {
page_links.push_str(&p.link_short());
}
r#"<!DOCTYPE html><html lang="en"><head><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="/style.css" />{favicons}<title>{title}</title></head><body><div><a href="/">Home</a>{page_links}<img src="/img/logo.png" class="align-right" /></div><hr>{content}</body></html>"#