1- use clap:: { crate_version} ;
1+ use clap:: crate_version;
22
33use std:: env;
44use std:: path:: { Path , PathBuf } ;
55
6- use clap:: { App , ArgMatches , SubCommand , AppSettings } ;
6+ use clap:: { App , AppSettings , ArgMatches , SubCommand } ;
77
8+ use mdbook:: errors:: Result as Result3 ;
89use mdbook:: MDBook ;
9- use mdbook:: errors:: { Result as Result3 } ;
1010
11+ #[ cfg( feature = "linkcheck" ) ]
12+ use failure:: Error ;
1113#[ cfg( feature = "linkcheck" ) ]
1214use mdbook:: renderer:: RenderContext ;
1315#[ cfg( feature = "linkcheck" ) ]
1416use mdbook_linkcheck:: { self , errors:: BrokenLinks } ;
15- use failure:: Error ;
1617
1718fn main ( ) {
1819 let d_message = "-d, --dest-dir=[dest-dir]
@@ -21,18 +22,22 @@ fn main() {
2122'A directory for your book{n}(Defaults to Current Directory when omitted)'" ;
2223
2324 let matches = App :: new ( "rustbook" )
24- . about ( "Build a book with mdBook" )
25- . author ( "Steve Klabnik <steve@steveklabnik.com>" )
26- . version ( & * format ! ( "v{}" , crate_version!( ) ) )
27- . setting ( AppSettings :: SubcommandRequired )
28- . subcommand ( SubCommand :: with_name ( "build" )
29- . about ( "Build the book from the markdown files" )
30- . arg_from_usage ( d_message)
31- . arg_from_usage ( dir_message) )
32- . subcommand ( SubCommand :: with_name ( "linkcheck" )
33- . about ( "Run linkcheck with mdBook 3" )
34- . arg_from_usage ( dir_message) )
35- . get_matches ( ) ;
25+ . about ( "Build a book with mdBook" )
26+ . author ( "Steve Klabnik <steve@steveklabnik.com>" )
27+ . version ( & * format ! ( "v{}" , crate_version!( ) ) )
28+ . setting ( AppSettings :: SubcommandRequired )
29+ . subcommand (
30+ SubCommand :: with_name ( "build" )
31+ . about ( "Build the book from the markdown files" )
32+ . arg_from_usage ( d_message)
33+ . arg_from_usage ( dir_message) ,
34+ )
35+ . subcommand (
36+ SubCommand :: with_name ( "linkcheck" )
37+ . about ( "Run linkcheck with mdBook 3" )
38+ . arg_from_usage ( dir_message) ,
39+ )
40+ . get_matches ( ) ;
3641
3742 // Check which subcomamnd the user ran...
3843 match matches. subcommand ( ) {
@@ -46,23 +51,44 @@ fn main() {
4651
4752 :: std:: process:: exit ( 101 ) ;
4853 }
49- } ,
54+ }
5055 ( "linkcheck" , Some ( sub_matches) ) => {
51- if let Err ( err) = linkcheck ( sub_matches) {
52- eprintln ! ( "Error: {}" , err) ;
53-
54- #[ cfg( feature = "linkcheck" ) ]
55- {
56- if let Ok ( broken_links) = err. downcast :: < BrokenLinks > ( ) {
57- for cause in broken_links. links ( ) . iter ( ) {
58- eprintln ! ( "\t Caused By: {}" , cause) ;
59- }
56+ #[ cfg( feature = "linkcheck" ) ]
57+ {
58+ if let Err ( err) = linkcheck ( sub_matches) {
59+ eprintln ! ( "Error: {}" , err) ;
60+
61+ // HACK: ignore timeouts
62+ let actually_broken = err
63+ . downcast :: < BrokenLinks > ( )
64+ . map ( |broken_links| {
65+ broken_links
66+ . links ( )
67+ . iter ( )
68+ . inspect ( |cause| eprintln ! ( "\t Caused By: {}" , cause) )
69+ . fold ( false , |already_broken, cause| {
70+ already_broken || !format ! ( "{}" , cause) . contains ( "timed out" )
71+ } )
72+ } )
73+ . unwrap_or ( false ) ;
74+
75+ if actually_broken {
76+ std:: process:: exit ( 101 ) ;
77+ } else {
78+ std:: process:: exit ( 0 ) ;
6079 }
6180 }
81+ }
6282
63- :: std:: process:: exit ( 101 ) ;
83+ #[ cfg( not( feature = "linkcheck" ) ) ]
84+ {
85+ // This avoids the `unused_binding` lint.
86+ println ! (
87+ "mdbook-linkcheck is disabled, but arguments were passed: {:?}" ,
88+ sub_matches
89+ ) ;
6490 }
65- } ,
91+ }
6692 ( _, _) => unreachable ! ( ) ,
6793 } ;
6894}
@@ -77,12 +103,6 @@ pub fn linkcheck(args: &ArgMatches<'_>) -> Result<(), Error> {
77103 mdbook_linkcheck:: check_links ( & render_ctx)
78104}
79105
80- #[ cfg( not( feature = "linkcheck" ) ) ]
81- pub fn linkcheck ( _args : & ArgMatches < ' _ > ) -> Result < ( ) , Error > {
82- println ! ( "mdbook-linkcheck is disabled." ) ;
83- Ok ( ( ) )
84- }
85-
86106// Build command implementation
87107pub fn build ( args : & ArgMatches < ' _ > ) -> Result3 < ( ) > {
88108 let book_dir = get_book_dir ( args) ;
0 commit comments