Post date & tags

This commit is contained in:
Thomas
2021-03-29 16:59:17 +02:00
parent b0ee8e2c23
commit a315f5eb24
24 changed files with 70 additions and 28 deletions

View File

@@ -54,6 +54,8 @@ fn load_section(dir_entry: &DirEntry) -> Section {
let section_name = dir_entry.file_name().into_string().unwrap();
let section_url = section_name.to_lowercase().trim().replace(" ", "_");
section_posts.sort_by(|a, b| b.date_compare.partial_cmp(&a.date_compare).unwrap());
println!("[LOADER] Loaded section {} with {} posts", section_name, section_posts.len());
return Section {
@@ -70,4 +72,4 @@ fn load_post(path: &Path) -> Post {
};
let marky = Marky::from_file(&file);
return Post::from_marky(marky);
}
}

View File

@@ -8,8 +8,9 @@ use crate::markdown::marky::Marky;
pub struct Post {
pub title: String,
pub date: String,
pub date_compare: i32,
pub date_printable: String,
pub url: String,
pub description: String,
pub tags: Vec<String>,
pub body: String,
pub is_year_only: bool,
@@ -17,11 +18,30 @@ pub struct Post {
impl Post {
pub fn from_marky(marky: Marky) -> Post {
let date = marky.metadata.get("date").map_or(String::from("none"), String::from);
let is_year_only = marky.metadata.get("yearonly").map_or(false, |a| a == "true");
let (year, month, day) = to_date_number(&date);
let date_compare = year * 1200 + month * 100 + day;
let mut date_printable = String::new();
if is_year_only {
date_printable.push_str(&year.to_string());
} else {
date_printable.push_str(&day.to_string());
date_printable.push_str("-");
date_printable.push_str(&month.to_string());
date_printable.push_str("-");
date_printable.push_str(&year.to_string());
}
Post {
title: marky.metadata.get("title").map_or(String::from("none"), String::from),
date: marky.metadata.get("date").map_or(String::from("none"), String::from),
date: date,
date_compare: date_compare,
date_printable: date_printable,
url: marky.metadata.get("url").map_or(String::from("none"), String::from),
description: marky.metadata.get("description").map_or(String::from("none"), String::from),
tags: tags_string_to_vec(marky.metadata.get("tags").map_or(String::from("none"), String::from)),
is_year_only: marky.metadata.get("yearonly").map_or(false, |a| a == "true"),
body: to_html(&marky.content),
@@ -30,5 +50,20 @@ impl Post {
}
fn tags_string_to_vec(tags: String) -> Vec<String> {
return tags.split(",").map(String::from).collect();
return tags.split_whitespace().map(String::from).collect();
}
// Convert yyyy-MM-dd to yyyy, MM, dd
fn to_date_number(date: &String) -> (i32, i32, i32) {
let split: Vec<&str> = date.split("-").collect();
if split.len() == 0 {
return (0, 0, 0);
}
let year = split[0].parse::<i32>().unwrap();
let month = split[1].parse::<i32>().unwrap();
let day = split[2].parse::<i32>().unwrap();
return (year, month, day);
}