diff --git a/src/generator.rs b/src/generator.rs index 0b57591..5d315f8 100644 --- a/src/generator.rs +++ b/src/generator.rs @@ -26,7 +26,7 @@ impl SiteGenerator { println!("Output directory: {}", config.output_dir().display()); } - println!("{}", ACCENT_STYLE.apply_to("\nGenerating site...")); + println!("{}", Style::new().cyan().apply_to("\nGenerating site...")); // Create output directory if it doesn't exist fs::create_dir_all(config.output_dir())?; @@ -60,20 +60,7 @@ impl SiteGenerator { .filter_map(|e| e.ok()) .filter(|e| e.path().extension().map_or(false, |ext| ext == "md")) { - let content = fs::read_to_string(entry.path())?; - let file_name = entry.path().display().to_string(); - - let doc: Document = - YamlFrontMatter::parse(&content).map_err(|e| Error::Frontmatter { - file: file_name, - message: e.to_string(), - })?; - - posts.push(Post { - metadata: doc.metadata, - content: doc.content.clone(), - html_content: self.markdown.render(&doc.content), - }); + posts.push(Post::new_from_path(entry.path(), &self.markdown)?); } Ok(posts) diff --git a/src/post.rs b/src/post.rs index 78128d3..18a99f7 100644 --- a/src/post.rs +++ b/src/post.rs @@ -1,9 +1,12 @@ use crate::config::Config; use crate::errors::Error; +use crate::markdown::MarkdownProcessor; use chrono::{Local, NaiveDate}; use serde::{Deserialize, Serialize}; use std::fs; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; +use yaml_front_matter::Document; +use yaml_front_matter::YamlFrontMatter; #[derive(Debug, Serialize)] pub struct Post { @@ -13,6 +16,22 @@ pub struct Post { } impl Post { + pub fn new_from_path(path: &Path, md_proc: &MarkdownProcessor) -> Result { + let content = fs::read_to_string(path)?; + let file_name = path.display().to_string(); + + let doc: Document = + YamlFrontMatter::parse(&content).map_err(|e| Error::Frontmatter { + file: file_name, + message: e.to_string(), + })?; + + Ok(Self { + metadata: doc.metadata, + content: doc.content.clone(), + html_content: md_proc.render(&content), + }) + } // Get the assets directory for this post pub fn assets_dir(&self, config: &Config) -> PathBuf { config @@ -56,7 +75,6 @@ impl Post { ); } } - #[derive(Debug, Deserialize, Serialize, Clone)] pub struct PostMetadata { pub title: String,