Use rfc3339 for date

with this commit mlem now requires nightly to build
This commit is contained in:
viridian 2024-04-20 15:01:28 +02:00
parent adb3b15d29
commit 9337db2a0a
Signed by: viridian
GPG key ID: DCD4DF95CE23FE8C
2 changed files with 29 additions and 22 deletions

View file

@ -1,3 +1,4 @@
#![feature(str_split_remainder)]
use chrono::prelude::*;
use derive_more::Constructor;
use markdown::{to_html_with_options, CompileOptions, Options};
@ -13,13 +14,16 @@ pub fn read_src_files(src_dir: &str) -> Vec<SrcMD> {
for file in read_dir(src_dir).expect("Cant read src dir") {
let file = file.unwrap();
let frontmatter = get_frontmatter(read_to_string(file.path()).unwrap()).0;
let title = frontmatter.get("title").expect("Error getting field: title from frontmatter");
let date = frontmatter.get("date").expect("Error getting field: date from frontmatter");
let title = frontmatter
.get("title")
.expect("Error getting field: title from frontmatter");
let date = frontmatter
.get("date")
.expect("Error getting field: date from frontmatter");
files.push(SrcMD::new(
file.path(),
title.to_string(),
DateTime::parse_from_str(&format!("{date} 00:00:00 +00:00"), "%Y-%m-%d %H:%M:%S %z")
.unwrap(),
DateTime::parse_from_rfc3339(date).unwrap(),
file.path().file_stem().unwrap().to_os_string(),
))
}
@ -40,14 +44,15 @@ pub struct SrcMD {
pub file_name: OsString,
}
pub fn generate_blog_entry(markdown: String, template_dir: &String) -> Option<(String, String)> {
pub fn generate_blog_entry(
markdown: String,
template_dir: &String,
frontmatter: &mut HashMap<String, String>,
) -> Option<(String, String)> {
let markdown = markdown.clone();
let mut tera = Tera::new(&format!("{template_dir}/*")).unwrap();
tera.autoescape_on(vec![]);
let (mut key_value, markdown) = get_frontmatter(markdown);
let html_markdown = to_html_with_options(
&markdown,
&Options {
@ -61,13 +66,11 @@ pub fn generate_blog_entry(markdown: String, template_dir: &String) -> Option<(S
)
.unwrap();
key_value.insert("blog_content".to_string(), html_markdown.clone());
frontmatter.insert("blog_content".to_string(), html_markdown.clone());
let context = Context::from_serialize(&key_value).unwrap();
let context = Context::from_serialize(&frontmatter).unwrap();
let templated_html = tera
.render(key_value.get("template")?, &context)
.unwrap();
let templated_html = tera.render(frontmatter.get("template")?, &context).unwrap();
Some((templated_html, html_markdown))
}
@ -88,6 +91,12 @@ pub fn get_frontmatter(markdown: String) -> (HashMap<String, String>, String) {
if line == "---" {
continue;
}
let second_value = {
let mut line_iter = line.split(':');
line_iter.next();
line_iter.remainder().unwrap().trim().to_string()
};
key_value.insert(
line.split(':')
.collect::<Vec<&str>>()
@ -95,12 +104,7 @@ pub fn get_frontmatter(markdown: String) -> (HashMap<String, String>, String) {
.unwrap()
.trim()
.to_string(),
line.split(':')
.collect::<Vec<&str>>()
.get(1)
.unwrap()
.trim()
.to_string(),
second_value,
);
}

View file

@ -24,10 +24,13 @@ fn generate() {
let mut post_index: Vec<index::BlogPost> = Vec::new();
for file in raw_files {
let mut markdown = read_to_string(file.path).expect("File does not exist");
let markdown = read_to_string(file.path).expect("File does not exist");
let (mut frontmatter, mut markdown) = get_frontmatter(markdown);
markdown = emoji::emoji_pass(&markdown, &config.emoji_config);
let (html, index_content) = generate_blog_entry(markdown, &config.templates_dir).expect(&format!("Error generating entry {}",&file.title));
let (html, index_content) =
generate_blog_entry(markdown, &config.templates_dir, &mut frontmatter)
.unwrap_or_else(|| panic!("Error generating entry {}", &file.title));
write_to_fs(
html,
&config.output_dir,