aboutsummaryrefslogtreecommitdiff
path: root/src/blog.rs
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2022-06-11 00:00:27 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2022-06-11 00:00:27 +0200
commit4fb323f69c11557a51c7da0b2031029f63edf789 (patch)
tree0d08ce52abbe0c5d123b4051f50ec5bc386652ae /src/blog.rs
parent0e147a780e74b54afbd56ff7438077d855d5c1c2 (diff)
downloadmacroblog.rs-4fb323f69c11557a51c7da0b2031029f63edf789.tar.gz
macroblog.rs-4fb323f69c11557a51c7da0b2031029f63edf789.tar.bz2
macroblog.rs-4fb323f69c11557a51c7da0b2031029f63edf789.zip
feat: Handle 404 result
Now gracefully handle 404, so instead of just panic now it will return a proper http 404 response.
Diffstat (limited to 'src/blog.rs')
-rw-r--r--src/blog.rs63
1 files changed, 10 insertions, 53 deletions
diff --git a/src/blog.rs b/src/blog.rs
index c877303..eaa314a 100644
--- a/src/blog.rs
+++ b/src/blog.rs
@@ -1,62 +1,18 @@
-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;
+use crate::assets::{BlogEntry, PostAsset, IndexTemplate, PostTemplate};
-const BLOG_REGEX: &str = r"(?P<date>[\d]{4}-[\d]{2}-[\d]{2})(?P<title>[a-zA-Z0-9-_]*)";
-#[derive(RustEmbed)]
-#[folder = "content/posts/"]
-struct PostAsset;
+pub fn read_assets() -> Vec<BlogEntry> {
+ let mut entries: Vec<BlogEntry> = PostAsset::iter()
+ .map(|e| format!("{}", e))
+ .map(|e| BlogEntry::new(&e))
+ .collect();
-#[derive(TemplateOnce)]
-#[template(path = "index.html")]
-struct IndexTemplate {
- posts: Vec<BlogEntry>,
-}
-
-#[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"], "_", " ");
+ entries.sort_by(|a, b| b.datetime.cmp(&a.datetime));
- BlogEntry {
- title: String::from(title),
- file: String::from(path),
- datetime: NaiveDate::parse_from_str(date, "%Y-%m-%d").unwrap(),
- }
- }
-
- pub fn read_assets() -> Vec<BlogEntry> {
- let mut entries: Vec<BlogEntry> = PostAsset::iter()
- .map(|e| format!("{}", e))
- .map(|e| BlogEntry::new(&e))
- .collect();
-
- entries.sort_by(|a, b| b.datetime.cmp(&a.datetime));
-
- entries
- }
+ entries
}
fn get_file_content(path: &str) -> String {
@@ -68,6 +24,7 @@ fn get_file_content(path: &str) -> String {
return html_output.to_string();
}
+
pub fn render_post_page(path: &String) -> String {
let blog = BlogEntry::new(path);
@@ -82,7 +39,7 @@ pub fn render_post_page(path: &String) -> String {
pub fn render_index_page() -> String {
IndexTemplate {
- posts: BlogEntry::read_assets(),
+ posts: read_assets(),
}
.render_once()
.unwrap()