Basic single post rendering
This commit is contained in:
@@ -9,11 +9,16 @@ use super::models::post::Post;
|
||||
use super::models::section::Section;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct TemplateData<'a> {
|
||||
struct TemplateSectionData<'a> {
|
||||
section: &'a Section,
|
||||
post: Option<&'a Post>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct TemplateSingleData<'a> {
|
||||
post: &'a Post
|
||||
}
|
||||
|
||||
pub fn landing_page() {
|
||||
let mut handlebars = Handlebars::new();
|
||||
|
||||
@@ -40,11 +45,31 @@ pub fn landing_page() {
|
||||
}
|
||||
|
||||
pub fn sections() {
|
||||
let singles = loader::singles();
|
||||
let sections = loader::sections();
|
||||
let mut handlebars = Handlebars::new();
|
||||
|
||||
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 §ions {
|
||||
let section_path = Path::new("./generated").join(section.url.to_owned());
|
||||
match create_dir_all(section_path) {
|
||||
@@ -54,7 +79,7 @@ pub fn sections() {
|
||||
|
||||
// Generate posts
|
||||
for post in §ion.posts {
|
||||
let data = TemplateData {
|
||||
let data = TemplateSectionData {
|
||||
post: Some(post),
|
||||
section: section,
|
||||
};
|
||||
@@ -72,8 +97,7 @@ pub fn sections() {
|
||||
}
|
||||
|
||||
// Generate section index
|
||||
|
||||
let data = TemplateData {
|
||||
let data = TemplateSectionData {
|
||||
post: None,
|
||||
section: section,
|
||||
};
|
||||
|
||||
@@ -8,9 +8,30 @@ use crate::markdown::marky::Marky;
|
||||
use super::models::section::Section;
|
||||
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> {
|
||||
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,
|
||||
};
|
||||
|
||||
@@ -23,7 +44,7 @@ pub fn sections() -> Vec<Section> {
|
||||
};
|
||||
|
||||
if entry.path().is_file() {
|
||||
|
||||
// TODO: treat file in a single run
|
||||
} else {
|
||||
sections.push(load_section(&entry));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user