@@ -3,12 +3,11 @@ extern crate staticfile;
3
3
extern crate ws;
4
4
5
5
use std;
6
- use std:: path:: PathBuf ;
7
6
use self :: iron:: { status, AfterMiddleware , Chain , Iron , IronError , IronResult , Request , Response ,
8
7
Set } ;
9
8
use clap:: { App , ArgMatches , SubCommand } ;
10
9
use mdbook:: MDBook ;
11
- use mdbook:: errors:: Result ;
10
+ use mdbook:: errors:: * ;
12
11
use { get_book_dir, open} ;
13
12
#[ cfg( feature = "watch" ) ]
14
13
use watch;
@@ -18,28 +17,21 @@ struct ErrorRecover;
18
17
// Create clap subcommand arguments
19
18
pub fn make_subcommand < ' a , ' b > ( ) -> App < ' a , ' b > {
20
19
SubCommand :: with_name ( "serve" )
21
- . about (
22
- "Serve the book at http://localhost:3000. Rebuild and reload on change." ,
23
- )
24
- . arg_from_usage (
25
- "[dir] 'A directory for your book{n}(Defaults to \
26
- Current Directory when omitted)'",
27
- )
20
+ . about ( "Serve the book at http://localhost:3000. Rebuild and reload on change." )
28
21
. arg_from_usage (
29
- "-d, --dest-dir=[dest-dir] 'The output directory for \
30
- your book{n}(Defaults to ./book when omitted)'",
22
+ "[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'" ,
31
23
)
32
24
. arg_from_usage ( "-p, --port=[port] 'Use another port{n}(Defaults to 3000)'" )
33
25
. arg_from_usage (
34
- "-w, --websocket-port=[ws-port] 'Use another port for the \
35
- websocket connection (livereload){n}(Defaults to 3001)'",
26
+ "-w, --websocket-port=[ws-port] 'Use another port for the websocket connection \
27
+ (livereload){n}(Defaults to 3001)'",
36
28
)
37
29
. arg_from_usage (
38
30
"-i, --interface=[interface] 'Interface to listen on{n}(Defaults to localhost)'" ,
39
31
)
40
32
. arg_from_usage (
41
- "-a, --address=[address] 'Address that the browser can reach the \
42
- websocket server from{n}(Defaults to the interface address)'",
33
+ "-a, --address=[address] 'Address that the browser can reach the websocket server \
34
+ from{n}(Defaults to the interface address)'",
43
35
)
44
36
. arg_from_usage ( "-o, --open 'Open the book server in a web browser'" )
45
37
}
@@ -51,10 +43,6 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
51
43
let book_dir = get_book_dir ( args) ;
52
44
let mut book = MDBook :: load ( & book_dir) ?;
53
45
54
- if let Some ( dest_dir) = args. value_of ( "dest-dir" ) {
55
- book. config . build . build_dir = PathBuf :: from ( dest_dir) ;
56
- }
57
-
58
46
let port = args. value_of ( "port" ) . unwrap_or ( "3000" ) ;
59
47
let ws_port = args. value_of ( "websocket-port" ) . unwrap_or ( "3001" ) ;
60
48
let interface = args. value_of ( "interface" ) . unwrap_or ( "localhost" ) ;
@@ -80,18 +68,19 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
80
68
}}
81
69
</script>
82
70
"# ,
83
- public_address,
84
- ws_port,
85
- RELOAD_COMMAND
71
+ public_address, ws_port, RELOAD_COMMAND
86
72
) ) ;
87
73
88
74
book. build ( ) ?;
89
75
90
76
let mut chain = Chain :: new ( staticfile:: Static :: new ( book. get_destination ( ) ) ) ;
91
77
chain. link_after ( ErrorRecover ) ;
92
- let _iron = Iron :: new ( chain) . http ( & * address) . unwrap ( ) ;
78
+ let _iron = Iron :: new ( chain)
79
+ . http ( & * address)
80
+ . chain_err ( || "Unable to launch the server" ) ?;
93
81
94
- let ws_server = ws:: WebSocket :: new ( |_| |_| Ok ( ( ) ) ) . unwrap ( ) ;
82
+ let ws_server =
83
+ ws:: WebSocket :: new ( |_| |_| Ok ( ( ) ) ) . chain_err ( || "Unable to start the websocket" ) ?;
95
84
96
85
let broadcaster = ws_server. broadcaster ( ) ;
97
86
@@ -107,9 +96,9 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
107
96
}
108
97
109
98
#[ cfg( feature = "watch" ) ]
110
- watch:: trigger_on_change ( & mut book, move |path, book | {
99
+ watch:: trigger_on_change ( & mut book, move |path, book_dir | {
111
100
println ! ( "File changed: {:?}\n Building book...\n " , path) ;
112
- match book . build ( ) {
101
+ match MDBook :: load ( & book_dir ) . and_then ( | mut b| b . build ( ) ) {
113
102
Err ( e) => println ! ( "Error while building: {:?}" , e) ,
114
103
_ => broadcaster. send ( RELOAD_COMMAND ) . unwrap ( ) ,
115
104
}
0 commit comments