Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use anyhow::{Context, Error};
use std::{collections::HashMap, fs};
use warp::hyper::StatusCode;
use crate::{
parser::{parse, parse_org, parse_org_summary},
post::{Post, PostParser},
};
pub struct Blog {
//pages: Vec<Page>,
posts: Vec<(String, Post)>,
}
impl Blog {
pub fn new() -> Result<Self, Error> {
/*let pages = fs::read_dir("pages")?
.map(|path| Page::new(path?)?)
.collect();*/
let posts = fs::read_dir("posts")?
.map(|path| {
let path = path?.path();
let file_name = path
.file_stem()
.context("Could not get file name")?
.to_str()
.context("Could not convert filename into string")?
.to_owned();
let slug = format!("/posts/{}", file_name.replace('-', "_").replace(' ', "-"));
Ok((
slug,
parse(PostParser::default(), &fs::read_to_string(path)?)?,
))
})
.collect::<Result<_, anyhow::Error>>()?;
Ok(Self { posts })
}
fn home(&self) -> Result<String, Error> {
let mut content =
r#"<div class="centered"><h1>Stephen's Site</h1>Rewritten in Rust!</div>"#.to_string();
for (slug, post) in &self.posts {
content.push_str(&post.link(slug));
content.push_str("<br>");
}
Ok(dress_page("Stephen's Site", &content))
}
}
pub struct RenderedBlog {
pages: HashMap<String, String>,
not_found: String,
}
impl RenderedBlog {
pub fn get(&self, path: &str) -> (StatusCode, &str) {
self.pages
.get(path)
.map(|x| (StatusCode::OK, x.as_str()))
.unwrap_or((StatusCode::NOT_FOUND, &self.not_found))
}
}
impl TryFrom<Blog> for RenderedBlog {
type Error = Error;
fn try_from(b: Blog) -> Result<Self, Error> {
let mut pages = HashMap::new();
for (slug, p) in &b.posts {
let body = dress_page(&p.title, &p.html()?);
pages.insert(slug.to_string(), body);
}
pages.insert(
"/style.css".to_string(),
include_str!("assets/style.css").to_string(),
);
let home = b.home()?;
pages.insert("/".to_string(), home);
let not_found = 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#"<html><head><link rel="stylesheet" href="/style.css" /><title>{title}</title></head><body><a href="/">Home</a><hr>{content}</body></html>"#
)
}