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 mut options = Options::empty();
22 options.insert(Options::ENABLE_FOOTNOTES);
23 let parser = Parser::new_ext(&md, options);
24 let mut html_output = &mut String::new();
25 html::push_html(&mut html_output, parser);
26 return html_output.to_string();
27}
28
29
30pub fn render_post_page(path: &String) -> String {
31 let blog = BlogEntry::new(path);
32
33 PostTemplate {
34 content: get_file_content(path),
35 title: blog.title,
36 date: blog.datetime.format("%Y-%m-%d").to_string(),
37 }
38 .render_once()
39 .unwrap()
40}
41
42pub fn render_index_page() -> String {
43 IndexTemplate {
44 posts: read_assets(),
45 }
46 .render_once()
47 .unwrap()
48}