Basic single post rendering

This commit is contained in:
gltron
2021-12-11 16:22:35 +01:00
parent 3e66fca0be
commit 1115a37340
2 changed files with 51 additions and 6 deletions

View File

@@ -9,11 +9,16 @@ use super::models::post::Post;
use super::models::section::Section; use super::models::section::Section;
#[derive(Serialize)] #[derive(Serialize)]
struct TemplateData<'a> { struct TemplateSectionData<'a> {
section: &'a Section, section: &'a Section,
post: Option<&'a Post>, post: Option<&'a Post>,
} }
#[derive(Serialize)]
struct TemplateSingleData<'a> {
post: &'a Post
}
pub fn landing_page() { pub fn landing_page() {
let mut handlebars = Handlebars::new(); let mut handlebars = Handlebars::new();
@@ -40,11 +45,31 @@ pub fn landing_page() {
} }
pub fn sections() { pub fn sections() {
let singles = loader::singles();
let sections = loader::sections(); let sections = loader::sections();
let mut handlebars = Handlebars::new(); let mut handlebars = Handlebars::new();
load_templates(&mut handlebars); load_templates(&mut handlebars);
// Generation single files
for single in &singles {
let data = TemplateSingleData {
post: single
};
let post_path = Path::new("./generated").join(single.url.to_owned());
let mut output_file = match File::create(post_path) {
Err(error) => panic!("Could not create single post: {}", error),
Ok(file) => file,
};
match handlebars.render_to_write("base", &data, &mut output_file) {
Err(error) => panic!("Error rendering single post: {}", error),
Ok(_) => println!("[GEN] Rendered single post {}", single.title),
};
}
// Generate sections
for section in &sections { for section in &sections {
let section_path = Path::new("./generated").join(section.url.to_owned()); let section_path = Path::new("./generated").join(section.url.to_owned());
match create_dir_all(section_path) { match create_dir_all(section_path) {
@@ -54,7 +79,7 @@ pub fn sections() {
// Generate posts // Generate posts
for post in &section.posts { for post in &section.posts {
let data = TemplateData { let data = TemplateSectionData {
post: Some(post), post: Some(post),
section: section, section: section,
}; };
@@ -72,8 +97,7 @@ pub fn sections() {
} }
// Generate section index // Generate section index
let data = TemplateSectionData {
let data = TemplateData {
post: None, post: None,
section: section, section: section,
}; };

View File

@@ -8,9 +8,30 @@ use crate::markdown::marky::Marky;
use super::models::section::Section; use super::models::section::Section;
use super::models::post::Post; use super::models::post::Post;
pub fn singles() -> Vec<Post> {
let dir = match read_dir("./raws") {
Err(error) => panic!("Could not read raws directory: {}", error),
Ok(dir) => dir,
};
let mut singles: Vec<Post> = Vec::new();
for entry in dir {
let entry = match entry {
Err(error) => panic!("Could not read file: {}", error),
Ok(entry) => entry.path(),
};
if entry.is_file() {
singles.push(load_post(&entry));
}
}
return singles;
}
pub fn sections() -> Vec<Section> { pub fn sections() -> Vec<Section> {
let dir = match read_dir("./raws") { let dir = match read_dir("./raws") {
Err(error) => panic!("Could read raws directory: {}", error), Err(error) => panic!("Could not read raws directory: {}", error),
Ok(dir) => dir, Ok(dir) => dir,
}; };
@@ -23,7 +44,7 @@ pub fn sections() -> Vec<Section> {
}; };
if entry.path().is_file() { if entry.path().is_file() {
// TODO: treat file in a single run
} else { } else {
sections.push(load_section(&entry)); sections.push(load_section(&entry));
} }