macroblog.rs @ 6a31a30b98f7febe9ac0db74211ef074aefc7ad3

 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(RustEmbed)]
15#[folder = "content/projects/"]
16pub struct ProjectsAsset;
17
18#[derive(TemplateOnce)]
19#[template(path = "index.html")]
20pub struct IndexTemplate {
21    pub posts: Vec<BlogEntry>,
22}
23
24
25#[derive(TemplateOnce)]
26#[template(path = "projects.html")]
27pub struct ProjectsTemplate {
28    pub content: String,
29}
30
31#[derive(TemplateOnce)]
32#[template(path = "post.html")]
33pub struct PostTemplate {
34    pub content: String,
35    pub title: String,
36    pub date: String,
37}
38
39#[derive(PartialEq, Eq, PartialOrd, Ord)]
40pub struct BlogEntry {
41    pub title: String,
42    pub datetime: NaiveDate,
43    pub file: String,
44}
45
46impl BlogEntry {
47    pub fn new(path: &String) -> BlogEntry {
48        let re = Regex::new(BLOG_REGEX).unwrap();
49        let caps = re.captures(path).unwrap();
50        let date = &caps["date"];
51        let title = str::replace(&caps["title"], "_", " ");
52
53        BlogEntry {
54            title: String::from(title),
55            file: String::from(path),
56            datetime: NaiveDate::parse_from_str(date, "%Y-%m-%d").unwrap(),
57        }
58    }
59
60}