From 884215a7e1aec9d78e02c6e4e7a7817b1c40434a Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 17 Dec 2023 13:46:47 -0500 Subject: [PATCH] Writing the stylesheets to the output folder. --- src/command/build/render.rs | 21 +++++++++++++++++++-- src/command/build/runner.rs | 7 ++++++- src/intermediate/convert.rs | 9 +++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/command/build/render.rs b/src/command/build/render.rs index 501b4ab..4420ddc 100644 --- a/src/command/build/render.rs +++ b/src/command/build/render.rs @@ -11,21 +11,26 @@ use crate::intermediate::BlogPost; use crate::render::DusterRenderer; use crate::render::RendererIntegration; +use super::stylesheet::Stylesheet; + static MAIN_TEMPLATES: Dir = include_dir!("$CARGO_MANIFEST_DIR/default_environment/templates/html"); pub(crate) struct SiteRenderer { output_directory: PathBuf, blog_posts: Vec, + stylesheets: Vec, } impl SiteRenderer { pub(crate) fn new>( output_directory: P, blog_posts: Vec, + stylesheets: Vec, ) -> SiteRenderer { SiteRenderer { output_directory: output_directory.into(), blog_posts, + stylesheets, } } @@ -84,8 +89,20 @@ impl SiteRenderer { Ok(()) } - pub(crate) async fn copy_stylesheets(&self, config: &Config) -> Result<(), CustomError> { - todo!() + pub(crate) async fn render_stylesheets(&self) -> Result<(), CustomError> { + let stylesheet_output_directory = self.output_directory.join("stylesheet"); + if !stylesheet_output_directory.exists() { + tokio::fs::create_dir(&stylesheet_output_directory).await?; + } + for stylesheet in &self.stylesheets { + let file_output_path = stylesheet_output_directory.join(&stylesheet.path); + let parent_directory = file_output_path + .parent() + .ok_or("Output file should have a containing directory.")?; + tokio::fs::create_dir_all(parent_directory).await?; + tokio::fs::write(file_output_path, stylesheet.contents.as_bytes()).await?; + } + Ok(()) } } diff --git a/src/command/build/runner.rs b/src/command/build/runner.rs index 4033d1d..f41cf74 100644 --- a/src/command/build/runner.rs +++ b/src/command/build/runner.rs @@ -17,8 +17,13 @@ pub(crate) async fn build_site(args: BuildArgs) -> Result<(), CustomError> { let config = Config::load_from_file(args.config).await?; let blog_posts = load_blog_posts(&config).await?; let stylesheets = load_stylesheets().await?; - let renderer = SiteRenderer::new(get_output_directory(&config).await?, blog_posts); + let renderer = SiteRenderer::new( + get_output_directory(&config).await?, + blog_posts, + stylesheets, + ); renderer.render_blog_posts(&config).await?; + renderer.render_stylesheets().await?; Ok(()) } diff --git a/src/intermediate/convert.rs b/src/intermediate/convert.rs index d600b1e..747fce8 100644 --- a/src/intermediate/convert.rs +++ b/src/intermediate/convert.rs @@ -22,8 +22,13 @@ pub(crate) fn convert_blog_post_page_to_render_context, F: AsRef< let output_directory = output_directory.as_ref(); let output_file = output_file.as_ref(); let css_files = vec![ - get_web_path(config, output_directory, output_file, "reset.css")?, - get_web_path(config, output_directory, output_file, "main.css")?, + get_web_path( + config, + output_directory, + output_file, + "stylesheet/reset.css", + )?, + get_web_path(config, output_directory, output_file, "stylesheet/main.css")?, ]; let js_files = vec![get_web_path( config,