@@ -10,16 +10,30 @@ use crate::navigation::Navigation;
1010use crate :: post_note:: PostNote ;
1111use crate :: settings:: Settings ;
1212
13+ /// Builds the static site by rendering templates and copying assets.
14+ ///
15+ /// Steps:
16+ /// - Initializes the Tera template engine with HTML templates
17+ /// - Creates the output directory structure
18+ /// - Copies all static asset directories to output
19+ /// - Copies media files referenced in notes
20+ /// - Writes the content map index
21+ /// - Renders all notes using templates
22+ ///
23+ /// # Errors
24+ ///
25+ /// Returns an error if template loading, directory creation, file copying, or rendering fails.
1326pub fn build (
14- notes : & Vec < PostNote > ,
27+ notes : & [ PostNote ] ,
1528 content_map : ContentMap ,
1629 navigation : Navigation ,
1730 settings : & Settings ,
1831) -> anyhow:: Result < ( ) > {
19- let tera = Tera :: new ( & format ! ( "{}/**/*.html" , & settings. path. template. display( ) ) ) ?;
20-
21- fs:: create_dir_all ( & settings. path . output ) ?;
22- copy_static_dir ( & settings. path . asset , & settings. path . output ) ?;
32+ let template_pattern = format ! ( "{}/**/*.html" , settings. path. template. display( ) ) ;
33+ let tera = Tera :: new ( & template_pattern) ?;
34+ for asset_path in & settings. path . assets {
35+ copy_static_dir ( asset_path, & settings. path . output ) ?;
36+ }
2337 copy_media_files ( notes, & settings. path . input , & settings. path . output ) ?;
2438 write_content_map ( content_map, & settings. path . output ) ?;
2539 render_notes ( notes, & navigation, & tera, & settings. path . output ) ?;
@@ -28,7 +42,7 @@ pub fn build(
2842}
2943
3044fn render_notes (
31- notes : & Vec < PostNote > ,
45+ notes : & [ PostNote ] ,
3246 navigation : & Navigation ,
3347 tera : & Tera ,
3448 output_path : & Path ,
@@ -69,37 +83,47 @@ fn render_notes(
6983 Ok ( ( ) )
7084}
7185
72- fn copy_static_dir ( src : & Path , destination : & Path ) -> io:: Result < ( ) > {
73- fs:: create_dir_all ( destination) ?;
74-
75- for entry in fs:: read_dir ( src) ? {
86+ /// Recursively copies a directory tree from source to destination.
87+ ///
88+ /// Creates the destination directory if it doesn't exist. For each entry in the source:
89+ /// - Directories are recursively copied
90+ /// - Files are copied directly
91+ ///
92+ /// If destination already exists, contents are merged (existing files are overwritten).
93+ ///
94+ /// # Errors
95+ ///
96+ /// Returns an error if any filesystem operation fails (reading, creating directories, copying).
97+ fn copy_static_dir ( from : & Path , to : & Path ) -> io:: Result < ( ) > {
98+ // Ensure the destination directory exists before copying contents.
99+ fs:: create_dir_all ( to) ?;
100+ // Iterate through all entries in the source directory.
101+ for entry in fs:: read_dir ( from) ? {
76102 let entry = entry?;
77- let file_type = entry. file_type ( ) ?;
78-
79- if file_type. is_dir ( ) {
80- copy_static_dir ( & entry. path ( ) , & destination. join ( entry. file_name ( ) ) ) ?;
103+ let from = entry. path ( ) ;
104+ let to = to. join ( entry. file_name ( ) ) ;
105+ if entry. file_type ( ) ?. is_dir ( ) {
106+ // Recursively copy subdirectories.
107+ copy_static_dir ( & from, & to) ?;
81108 } else {
82- fs:: copy ( entry . path ( ) , destination . join ( entry . file_name ( ) ) ) ?;
109+ fs:: copy ( & from , & to ) ?;
83110 }
84111 }
85112
86113 Ok ( ( ) )
87114}
88115
89- fn copy_media_files ( notes : & Vec < PostNote > , src : & Path , destination : & Path ) -> anyhow:: Result < ( ) > {
116+ fn copy_media_files ( notes : & [ PostNote ] , src : & Path , destination : & Path ) -> anyhow:: Result < ( ) > {
90117 fs:: create_dir_all ( destination) ?;
91-
92118 notes. par_iter ( ) . for_each ( |note| {
93119 note. media_links . par_iter ( ) . for_each ( |media_link| {
94120 let media_path = PathBuf :: from ( media_link. to_string ( ) ) ;
95121 let output_media_path = PathBuf :: from ( media_link. to_string ( ) ) ;
96-
97122 if let Some ( parent) = media_path. parent ( )
98123 && let Err ( err) = fs:: create_dir_all ( destination. join ( parent) )
99124 {
100125 log:: warn!( "Could not create parent directory: {}" , err) ;
101126 } ;
102-
103127 if let Err ( err) = fs:: copy ( src. join ( & media_path) , destination. join ( & output_media_path) )
104128 {
105129 log:: warn!(
0 commit comments