aboutsummaryrefslogtreecommitdiff
path: root/src/blog.rs
blob: 8c3af524d120c63ff807cb6769f029aef797ad66 (plain)
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
use crate::assets::{BlogEntry, IndexTemplate, PostAsset, PostTemplate, ProjectsAsset, ProjectsTemplate};
use pulldown_cmark::{html, Options, Parser};
use sailfish::TemplateOnce;
use std::str;

pub fn read_assets() -> Vec<BlogEntry> {
    let mut entries: Vec<BlogEntry> = PostAsset::iter()
        .map(|e| format!("{}", e))
        .map(|e| BlogEntry::new(&e))
        .collect();

    entries.sort_by(|a, b| b.datetime.cmp(&a.datetime));

    entries
}

fn get_file_content(path: &str) -> String {
    let buffer = PostAsset::get(path).unwrap().data.into_owned();
    let md = String::from_utf8(buffer).unwrap();
    let mut options = Options::empty();
    options.insert(Options::ENABLE_FOOTNOTES);
    let parser = Parser::new_ext(&md, options);
    let mut html_output = &mut String::new();
    html::push_html(&mut html_output, parser);
    return html_output.to_string();
}

fn get_projects_content() -> String {
    let buffer = ProjectsAsset::get("index.md").unwrap().data.into_owned();
    let md = String::from_utf8(buffer).unwrap();
    let mut options = Options::empty();
    options.insert(Options::ENABLE_FOOTNOTES);
    let parser = Parser::new_ext(&md, options);
    let mut html_output = &mut String::new();
    html::push_html(&mut html_output, parser);
    return html_output.to_string();
}

pub fn render_projects() -> String {
    ProjectsTemplate {
        content: get_projects_content(),
    }
    .render_once()
    .unwrap()
}

pub fn render_post_page(path: &String) -> String {
    let blog = BlogEntry::new(path);

    PostTemplate {
        content: get_file_content(path),
        title: blog.title,
        date: blog.datetime.format("%Y-%m-%d").to_string(),
    }
    .render_once()
    .unwrap()
}

pub fn render_index_page() -> String {
    IndexTemplate {
        posts: read_assets(),
    }
    .render_once()
    .unwrap()
}