1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15+ use std:: fs:: File ;
16+ use std:: io:: Read ;
1517use std:: path:: { Path , PathBuf } ;
1618use std:: sync:: Arc ;
1719
20+ use anyhow:: Ok ;
1821use log:: debug;
1922
2023/// a slide is a page in the book
@@ -33,17 +36,26 @@ pub struct Book {
3336
3437impl Book {
3538 /// create a book from all html files in the source_dir
36- pub fn from_html_slides ( source_dir : PathBuf ) -> anyhow:: Result < Book > {
39+ pub fn from_html_slides (
40+ source_dir : PathBuf ,
41+ ignore_redirects : bool ,
42+ ) -> anyhow:: Result < Book > {
3743 let mut slides = vec ! [ ] ;
3844 let files = glob:: glob ( & format ! (
3945 "{}/**/*.html" ,
4046 source_dir. to_str( ) . expect( "invalid path" )
4147 ) ) ?;
4248 for file in files {
43- let slide = Slide { filename : file?. into ( ) } ;
49+ let file = file?;
50+ if ignore_redirects && file_is_redirect ( & file) ? {
51+ debug ! ( "slide {file:?} is a redirect page" ) ;
52+ continue ;
53+ }
54+ let slide = Slide { filename : file. into ( ) } ;
4455 debug ! ( "add {:?}" , slide) ;
4556 slides. push ( slide) ;
4657 }
58+ debug ! ( "processing {} slides" , slides. len( ) ) ;
4759 Ok ( Book { _source_dir : source_dir, slides } )
4860 }
4961
@@ -52,3 +64,20 @@ impl Book {
5264 & self . slides
5365 }
5466}
67+
68+ const HTML_REDIRECT_PAGE : & str = r#"<!DOCTYPE html>
69+ <html lang="en">
70+ <head>
71+ <meta charset="utf-8">
72+ <title>Redirecting...</title>"# ;
73+
74+ /// check if the file is starting with the mdbook redirect page.
75+ /// This method is optimized to not read the entire file but only the start
76+ fn file_is_redirect ( filename : & PathBuf ) -> anyhow:: Result < bool > {
77+ let mut file = File :: open ( filename) ?;
78+ // create a buffer with the exact length of the text that is checked
79+ let mut file_start_buffer = [ 0u8 ; HTML_REDIRECT_PAGE . len ( ) ] ;
80+ // read only the part that is relevant
81+ file. read_exact ( & mut file_start_buffer) ?;
82+ Ok ( file_start_buffer. eq ( HTML_REDIRECT_PAGE . as_bytes ( ) ) )
83+ }
0 commit comments