From bdb18657b64d88e33912337a2b1a0f9c927142f9 Mon Sep 17 00:00:00 2001 From: Clar Fon <15850505+clarfonthey@users.noreply.github.com> Date: Mon, 22 Jan 2024 06:57:42 -0500 Subject: [PATCH] Refactor: copy_assets instead of copy_asset (#2418) --- components/site/src/lib.rs | 39 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index ab38ddc8..dce82d81 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -676,8 +676,19 @@ impl Site { Ok(current_path) } - fn copy_asset(&self, src: &Path, dest: &Path) -> Result<()> { - copy_file_if_needed(src, dest, self.config.hard_link_static) + fn copy_assets(&self, parent: &Path, assets: &[impl AsRef], dest: &Path) -> Result<()> { + for asset in assets { + let asset_path = asset.as_ref(); + copy_file_if_needed( + asset_path, + &dest.join( + asset_path.strip_prefix(parent).expect("Couldn't get filename from page asset"), + ), + self.config.hard_link_static, + )?; + } + + Ok(()) } /// Renders a single content page @@ -689,17 +700,7 @@ impl Site { self.write_content(&components, "index.html", content, !page.assets.is_empty())?; // Copy any asset we found previously into the same directory as the index.html - for asset in &page.assets { - let asset_path = asset.as_path(); - self.copy_asset( - asset_path, - ¤t_path.join( - asset_path - .strip_prefix(page.file.path.parent().unwrap()) - .expect("Couldn't get filename from page asset"), - ), - )?; - } + self.copy_assets(page.file.path.parent().unwrap(), &page.assets, ¤t_path)?; Ok(()) } @@ -1082,17 +1083,7 @@ impl Site { } // Copy any asset we found previously into the same directory as the index.html - for asset in §ion.assets { - let asset_path = asset.as_path(); - self.copy_asset( - asset_path, - &output_path.join( - asset_path - .strip_prefix(section.file.path.parent().unwrap()) - .expect("Failed to get asset filename for section"), - ), - )?; - } + self.copy_assets(§ion.file.path.parent().unwrap(), §ion.assets, &output_path)?; if render_pages { section