macroblog.rs @ 4fb323f69c11557a51c7da0b2031029f63edf789

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