use chrono::NaiveDate;
use pulldown_cmark::{html, Options, Parser};
use regex::Regex;
use rust_embed::RustEmbed;
use sailfish::TemplateOnce;
use std::cmp::{Eq, Ord, PartialEq, PartialOrd};
use std::str;
const BLOG_REGEX: &str = r"(?P[\d]{4}-[\d]{2}-[\d]{2})(?P[a-zA-Z0-9-_]*)";
#[derive(RustEmbed)]
#[folder = "content/posts/"]
struct PostAsset;
#[derive(TemplateOnce)]
#[template(path = "index.html")]
struct IndexTemplate {
posts: Vec,
}
#[derive(TemplateOnce)]
#[template(path = "post.html")]
struct PostTemplate {
content: String,
title: String,
date: String,
}
#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct BlogEntry {
pub title: String,
pub datetime: NaiveDate,
pub file: String,
}
impl BlogEntry {
pub fn new(path: &String) -> BlogEntry {
let re = Regex::new(BLOG_REGEX).unwrap();
let caps = re.captures(path).unwrap();
let date = &caps["date"];
let title = str::replace(&caps["title"], "_", " ");
BlogEntry {
title: String::from(title),
file: String::from(path),
datetime: NaiveDate::parse_from_str(date, "%Y-%m-%d").unwrap(),
}
}
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 parser = Parser::new_ext(&md, Options::empty());
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: BlogEntry::read_assets(),
}
.render_once()
.unwrap()
}