use pulldown_cmark::{html, Options, Parser};
use sailfish::TemplateOnce;
use std::str;
use crate::assets::{BlogEntry, PostAsset, IndexTemplate, PostTemplate};
pub fn read_assets() -> Vec {
let mut entries: Vec = 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();
}
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()
}