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

View file

@ -24,10 +24,13 @@ fn generate() {
let mut post_index: Vec<index::BlogPost> = Vec::new(); let mut post_index: Vec<index::BlogPost> = Vec::new();
for file in raw_files { 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); 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( write_to_fs(
html, html,
&config.output_dir, &config.output_dir,