1use chrono::NaiveDate;
2use regex::Regex;
3use rust_embed::RustEmbed;
4use sailfish::TemplateOnce;
5use std::cmp::{Eq, Ord, PartialEq, PartialOrd};
6use std::str;
7
8pub const BLOG_REGEX: &str = r"(?P<date>[\d]{4}-[\d]{2}-[\d]{2})(?P<title>[a-zA-Z0-9-_]*)";
9
10#[derive(RustEmbed)]
11#[folder = "content/posts/"]
12pub struct PostAsset;
13
14#[derive(TemplateOnce)]
15#[template(path = "index.html")]
16pub struct IndexTemplate {
17 pub posts: Vec<BlogEntry>,
18}
19
20#[derive(TemplateOnce)]
21#[template(path = "post.html")]
22pub struct PostTemplate {
23 pub content: String,
24 pub title: String,
25 pub date: String,
26}
27
28#[derive(PartialEq, Eq, PartialOrd, Ord)]
29pub struct BlogEntry {
30 pub title: String,
31 pub datetime: NaiveDate,
32 pub file: String,
33}
34
35impl BlogEntry {
36 pub fn new(path: &String) -> BlogEntry {
37 let re = Regex::new(BLOG_REGEX).unwrap();
38 let caps = re.captures(path).unwrap();
39 let date = &caps["date"];
40 let title = str::replace(&caps["title"], "_", " ");
41
42 BlogEntry {
43 title: String::from(title),
44 file: String::from(path),
45 datetime: NaiveDate::parse_from_str(date, "%Y-%m-%d").unwrap(),
46 }
47 }
48
49}