1
- use clap:: { crate_version} ;
1
+ use clap:: crate_version;
2
2
3
3
use std:: env;
4
4
use std:: path:: { Path , PathBuf } ;
5
5
6
- use clap:: { App , ArgMatches , SubCommand , AppSettings } ;
6
+ use clap:: { App , AppSettings , ArgMatches , SubCommand } ;
7
7
8
+ use mdbook:: errors:: Result as Result3 ;
8
9
use mdbook:: MDBook ;
9
- use mdbook:: errors:: { Result as Result3 } ;
10
10
11
+ #[ cfg( feature = "linkcheck" ) ]
12
+ use failure:: Error ;
11
13
#[ cfg( feature = "linkcheck" ) ]
12
14
use mdbook:: renderer:: RenderContext ;
13
15
#[ cfg( feature = "linkcheck" ) ]
14
16
use mdbook_linkcheck:: { self , errors:: BrokenLinks } ;
15
- use failure:: Error ;
16
17
17
18
fn main ( ) {
18
19
let d_message = "-d, --dest-dir=[dest-dir]
@@ -21,18 +22,22 @@ fn main() {
21
22
'A directory for your book{n}(Defaults to Current Directory when omitted)'" ;
22
23
23
24
let matches = App :: new ( "rustbook" )
24
- . about ( "Build a book with mdBook" )
25
- . author ( "Steve Klabnik <[email protected] >" )
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 <[email protected] >" )
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 ( ) ;
36
41
37
42
// Check which subcomamnd the user ran...
38
43
match matches. subcommand ( ) {
@@ -46,23 +51,44 @@ fn main() {
46
51
47
52
:: std:: process:: exit ( 101 ) ;
48
53
}
49
- } ,
54
+ }
50
55
( "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 ) ;
60
79
}
61
80
}
81
+ }
62
82
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
+ ) ;
64
90
}
65
- } ,
91
+ }
66
92
( _, _) => unreachable ! ( ) ,
67
93
} ;
68
94
}
@@ -77,12 +103,6 @@ pub fn linkcheck(args: &ArgMatches<'_>) -> Result<(), Error> {
77
103
mdbook_linkcheck:: check_links ( & render_ctx)
78
104
}
79
105
80
- #[ cfg( not( feature = "linkcheck" ) ) ]
81
- pub fn linkcheck ( _args : & ArgMatches < ' _ > ) -> Result < ( ) , Error > {
82
- println ! ( "mdbook-linkcheck is disabled." ) ;
83
- Ok ( ( ) )
84
- }
85
-
86
106
// Build command implementation
87
107
pub fn build ( args : & ArgMatches < ' _ > ) -> Result3 < ( ) > {
88
108
let book_dir = get_book_dir ( args) ;
0 commit comments