2 // AGPL-3.0 License (see LICENSE)
4 use chrono::{Datelike, NaiveDateTime};
5 use sailfish::Template;
6 use std::path::PathBuf;
8 use crate::site::helper::Helper;
11 #[template(path = "blog_post.stpl")]
12 #[derive(Clone, Debug)]
17 pub published: NaiveDateTime,
18 pub published_for_feed: String,
19 pub updated: NaiveDateTime,
20 pub updated_for_feed: String,
21 pub topics_comma_separated: String,
22 pub topics: Vec<String>,
23 pub topics_sanitized: Vec<String>,
24 pub topic_base_dir: String,
32 pub fn new(base_dir: String) -> BlogPost {
36 author: String::new(),
37 published: NaiveDateTime::parse_from_str("2000-01-01 23:56:04", "%Y-%m-%d %H:%M:%S")
39 published_for_feed: String::new(),
40 updated: NaiveDateTime::parse_from_str("2000-01-01 23:56:04", "%Y-%m-%d %H:%M:%S")
42 updated_for_feed: String::new(),
43 topics_comma_separated: String::new(),
45 topics_sanitized: Vec::new(),
46 topic_base_dir: String::from("topic"),
48 snippet: String::new(),
54 pub fn get_topics(&self) -> &Vec<String> {
58 pub fn get_year(&self) -> String {
59 return self.published.year().to_string();
62 pub fn get_published_date(&self) -> &NaiveDateTime {
63 return &self.published;
66 fn get_date_for_feed(&self, date: &NaiveDateTime) -> String {
67 let month = date.month();
70 let mut month_str = month.to_string();
71 let mut day_str = day.to_string();
74 month_str = format!("0{}", month);
78 day_str = format!("0{}", day);
90 pub fn create_output_dir(&self) {
91 Helper::create_dir_all(
92 &Helper::get_output_dir()
98 pub fn generate(&self) {
99 Helper::write_file_sync(
100 &Helper::get_output_dir()
101 .join(&self.base_dir)
104 &self.render().unwrap().as_bytes(),
110 pub async fn parse_markdown_file(path: PathBuf, base_dir: String) -> BlogPost {
111 let contents = tokio::fs::read_to_string(path).await.unwrap();
112 let mut post_start_found = false;
113 let mut markdown = String::new();
114 let mut blog_post = BlogPost::new(base_dir.clone());
116 for line in contents.lines() {
117 if !post_start_found && line.len() > 0 {
119 post_start_found = true;
123 let v: Vec<&str> = line.splitn(2, ':').collect();
124 assert_eq!(v.len(), 2);
126 if let Some(key) = v.get(0) {
127 if *key == "author" {
128 if let Some(value) = v.get(1) {
129 blog_post.author = String::from(value.trim());
131 panic!("Unable to parse field: 'author'.");
133 } else if *key == "published" {
134 if let Some(value) = v.get(1) {
135 blog_post.published =
136 NaiveDateTime::parse_from_str(value, "%Y-%m-%d %H:%M:%S").unwrap();
137 blog_post.published_for_feed =
138 blog_post.get_date_for_feed(&blog_post.published);
140 panic!("Unable to parse field: 'published'.");
142 } else if *key == "updated" {
143 if let Some(value) = v.get(1) {
145 NaiveDateTime::parse_from_str(value, "%Y-%m-%d %H:%M:%S").unwrap();
146 blog_post.updated_for_feed =
147 blog_post.get_date_for_feed(&blog_post.updated);
149 panic!("Unable to parse field: 'updated'.");
151 } else if *key == "topics" {
152 if let Some(value) = v.get(1) {
153 blog_post.topics_comma_separated = String::from(value.trim());
154 let v = blog_post.topics_comma_separated.split(",");
156 let topic_trimmed = topic.trim();
157 blog_post.topics.push(String::from(topic_trimmed));
160 .push(Helper::sanitize_string(topic_trimmed));
163 panic!("Unable to parse field: 'topics'.");
165 } else if *key == "title" {
166 if let Some(value) = v.get(1) {
167 blog_post.title = String::from(value.trim());
168 blog_post.url = Helper::sanitize_string(&blog_post.title);
170 panic!("Unable to parse field: 'title'.");
172 } else if *key == "snippet" {
173 if let Some(value) = v.get(1) {
174 blog_post.snippet = String::from(value.trim());
176 panic!("Unable to parse field: 'snippet'.");
181 if markdown.len() > 0 {
188 // convert markdown to html:
189 blog_post.html = markdown::to_html(&markdown);