@@ -7,6 +7,7 @@ use crate::teams::{encode_zulip_stream, load_rust_teams};
77use anyhow:: Context ;
88use handlebars:: { DirectorySourceOptions , Handlebars } ;
99use handlebars_fluent:: FluentHelper ;
10+ use std:: ffi:: OsStr ;
1011use std:: path:: { Path , PathBuf } ;
1112
1213mod assets;
@@ -94,7 +95,7 @@ fn main() -> anyhow::Result<()> {
9495 rust_version,
9596 teams,
9697 handlebars,
97- output_dir,
98+ output_dir : output_dir . clone ( ) ,
9899 base_url,
99100 } ;
100101 ctx. copy_asset_dir ( "static" , "static" ) ?;
@@ -113,5 +114,42 @@ fn main() -> anyhow::Result<()> {
113114 ctx. page ( "404" , "" , & ( ) , ENGLISH ) . render ( "404.html" ) ?;
114115 create_redirects ( & ctx) ?;
115116
117+ sanity_check_index_pages ( & output_dir) ?;
118+
119+ Ok ( ( ) )
120+ }
121+
122+ /// Make sure that there are no instances where we would have both `<page>.html` and
123+ /// a `<page>` directory.
124+ fn sanity_check_index_pages ( directory : & Path ) -> anyhow:: Result < ( ) > {
125+ // Find all .html files
126+ let mut html_files = vec ! [ ] ;
127+ gather_html_files ( directory, & mut html_files) ?;
128+
129+ for file in html_files {
130+ let basename = file. file_stem ( ) . unwrap ( ) ;
131+ let dir = file. parent ( ) . unwrap ( ) . join ( basename) ;
132+ if dir. is_dir ( ) {
133+ return Err ( anyhow:: anyhow!(
134+ "Both the `{file}` file and the `{dir}` directory exist, move `{file}` to `{dir_index}` instead" ,
135+ file = file. display( ) ,
136+ dir = dir. display( ) ,
137+ dir_index = dir. join( "index.html" ) . display( )
138+ ) ) ;
139+ }
140+ }
141+
142+ Ok ( ( ) )
143+ }
144+
145+ fn gather_html_files ( path : & Path , files : & mut Vec < PathBuf > ) -> anyhow:: Result < ( ) > {
146+ if path. is_file ( ) && path. extension ( ) == Some ( OsStr :: new ( "html" ) ) {
147+ files. push ( path. to_path_buf ( ) ) ;
148+ } else if path. is_dir ( ) {
149+ for entry in path. read_dir ( ) ? {
150+ let entry = entry?;
151+ gather_html_files ( & entry. path ( ) , files) ?;
152+ }
153+ }
116154 Ok ( ( ) )
117155}
0 commit comments