1use rust_embed::RustEmbed;
2use sailfish::TemplateOnce;
3use chrono::NaiveDate;
4use regex::{Regex};
5use std::str;
6
7const BLOG_REGEX: &str = r"(?P<date>[\d]{4}-[\d]{2}-[\d]{2})(?P<title>[a-zA-Z0-9_]*)";
8
9#[derive(RustEmbed)]
10#[folder = "content/posts/"]
11struct PostAsset;
12
13
14#[derive(TemplateOnce)]
15#[template(path = "index.html")]
16struct IndexTemplate {
17 posts: Vec<BlogEntry>,
18}
19
20#[derive(TemplateOnce)]
21#[template(path = "post.html")]
22struct PostTemplate {
23 content: String,
24}
25
26pub struct BlogEntry {
27 pub title: String,
28 pub datetime: NaiveDate,
29 pub file: String,
30}
31
32impl BlogEntry {
33 pub fn new(path: &String) -> BlogEntry {
34 let re = Regex::new(BLOG_REGEX).unwrap();
35 let caps = re.captures(path).unwrap();
36 let date = &caps["date"];
37 let title = str::replace(&caps["title"], "_", " ");
38
39 BlogEntry {
40 title: String::from(title),
41 file: String::from(path),
42 datetime: NaiveDate::parse_from_str(date, "%Y-%m-%d").unwrap()
43 }
44 }
45
46 pub fn read_assets() -> Vec<BlogEntry> {
47 PostAsset::iter()
48 .map(|e| format!("{}", e))
49 .map(|e| BlogEntry::new(&e))
50 .collect()
51 }
52}
53
54fn get_file_content(path: &str) -> String {
55 let buffer = PostAsset::get(path)
56 .unwrap()
57 .data
58 .into_owned();
59
60 return String::from_utf8(buffer).unwrap();
61}
62
63
64pub fn render_post_page(path: &String) -> String {
65 PostTemplate { content: get_file_content(path) }
66 .render_once()
67 .unwrap()
68}
69
70pub fn render_index_page() -> String {
71 IndexTemplate { posts: BlogEntry::read_assets() }
72 .render_once()
73 .unwrap()
74}