macroblog.rs @ 6a31a30b98f7febe9ac0db74211ef074aefc7ad3

 1use crate::assets::{BlogEntry, IndexTemplate, PostAsset, PostTemplate, ProjectsAsset, ProjectsTemplate};
 2use pulldown_cmark::{html, Options, Parser};
 3use sailfish::TemplateOnce;
 4use std::str;
 5
 6pub fn read_assets() -> Vec<BlogEntry> {
 7    let mut entries: Vec<BlogEntry> = PostAsset::iter()
 8        .map(|e| format!("{}", e))
 9        .map(|e| BlogEntry::new(&e))
10        .collect();
11
12    entries.sort_by(|a, b| b.datetime.cmp(&a.datetime));
13
14    entries
15}
16
17fn get_file_content(path: &str) -> String {
18    let buffer = PostAsset::get(path).unwrap().data.into_owned();
19    let md = String::from_utf8(buffer).unwrap();
20    let mut options = Options::empty();
21    options.insert(Options::ENABLE_FOOTNOTES);
22    let parser = Parser::new_ext(&md, options);
23    let mut html_output = &mut String::new();
24    html::push_html(&mut html_output, parser);
25    return html_output.to_string();
26}
27
28fn get_projects_content() -> String {
29    let buffer = ProjectsAsset::get("index.md").unwrap().data.into_owned();
30    let md = String::from_utf8(buffer).unwrap();
31    let mut options = Options::empty();
32    options.insert(Options::ENABLE_FOOTNOTES);
33    let parser = Parser::new_ext(&md, options);
34    let mut html_output = &mut String::new();
35    html::push_html(&mut html_output, parser);
36    return html_output.to_string();
37}
38
39pub fn render_projects() -> String {
40    ProjectsTemplate {
41        content: get_projects_content(),
42    }
43    .render_once()
44    .unwrap()
45}
46
47pub fn render_post_page(path: &String) -> String {
48    let blog = BlogEntry::new(path);
49
50    PostTemplate {
51        content: get_file_content(path),
52        title: blog.title,
53        date: blog.datetime.format("%Y-%m-%d").to_string(),
54    }
55    .render_once()
56    .unwrap()
57}
58
59pub fn render_index_page() -> String {
60    IndexTemplate {
61        posts: read_assets(),
62    }
63    .render_once()
64    .unwrap()
65}