1
1
use std:: env;
2
2
use std:: path:: { Path , PathBuf } ;
3
3
4
- use clap:: { ArgMatches , Command , arg, crate_version} ;
5
- use mdbook:: MDBook ;
4
+ use clap:: { arg, crate_version, ArgMatches , Command } ;
6
5
use mdbook:: errors:: Result as Result3 ;
6
+ use mdbook:: MDBook ;
7
7
use mdbook_i18n_helpers:: preprocessors:: Gettext ;
8
8
use mdbook_spec:: Spec ;
9
9
use mdbook_trpl_listing:: TrplListing ;
@@ -22,6 +22,11 @@ fn main() {
22
22
. required ( false )
23
23
. value_parser ( clap:: value_parser!( String ) ) ;
24
24
25
+ let n_arg = arg ! ( -n --"name" <NAME >
26
+ "The book name" )
27
+ . required ( false )
28
+ . value_parser ( clap:: value_parser!( String ) ) ;
29
+
25
30
let root_arg = arg ! ( --"rust-root" <ROOT_DIR >
26
31
"Path to the root of the rust source tree" )
27
32
. required ( false )
@@ -56,6 +61,7 @@ fn main() {
56
61
. about ( "Build the book from the markdown files" )
57
62
. arg ( d_arg)
58
63
. arg ( l_arg)
64
+ . arg ( n_arg)
59
65
. arg ( root_arg)
60
66
. arg ( & dir_arg) ,
61
67
)
@@ -121,8 +127,54 @@ pub fn build(args: &ArgMatches) -> Result3<()> {
121
127
book. with_preprocessor ( Spec :: new ( rust_root) ?) ;
122
128
}
123
129
130
+ let mut cleanup = Vec :: new ( ) ;
131
+ if let Some ( rust_root) = args. get_one :: < PathBuf > ( "rust-root" ) {
132
+ if let Some ( name) = args. get_one :: < String > ( "name" ) {
133
+ let translation_path = rust_root. join ( format ! ( "src/doc/translations/{name}" ) ) ;
134
+ if translation_path. exists ( ) {
135
+ let css_file = "theme/language-picker.css" ;
136
+ let js_file = "theme/language-picker.js" ;
137
+
138
+ let css_path = translation_path. join ( css_file) ;
139
+ let js_path = translation_path. join ( js_file) ;
140
+ let po_path = translation_path. join ( "po" ) ;
141
+
142
+ let theme_dir = book_dir. join ( "theme" ) ;
143
+ if !theme_dir. exists ( ) {
144
+ std:: fs:: create_dir ( theme_dir) ?;
145
+ }
146
+ let css_target = book_dir. join ( css_file) ;
147
+ let js_target = book_dir. join ( js_file) ;
148
+ std:: fs:: copy ( css_path, & css_target) ?;
149
+ std:: fs:: copy ( js_path, & js_target) ?;
150
+ cleanup. push ( css_target) ;
151
+ cleanup. push ( js_target) ;
152
+
153
+ let css_file: toml:: Value = css_file. into ( ) ;
154
+ let js_file: toml:: Value = js_file. into ( ) ;
155
+ let po_path: toml:: Value = po_path. to_string_lossy ( ) . as_ref ( ) . into ( ) ;
156
+
157
+ if let Some ( additional_css) = book. config . get_mut ( "output.html.additional-css" ) {
158
+ additional_css. as_array_mut ( ) . unwrap ( ) . push ( css_file. into ( ) ) ;
159
+ } else {
160
+ book. config . set ( "output.html.additional-css" , vec ! [ css_file] ) ?;
161
+ }
162
+ if let Some ( additional_js) = book. config . get_mut ( "output.html.additional-js" ) {
163
+ additional_js. as_array_mut ( ) . unwrap ( ) . push ( js_file. into ( ) ) ;
164
+ } else {
165
+ book. config . set ( "output.html.additional-js" , vec ! [ js_file] ) ?;
166
+ }
167
+ book. config . set ( "preprocessor.gettext.po-dir" , po_path) ?;
168
+ }
169
+ }
170
+ }
171
+
124
172
book. build ( ) ?;
125
173
174
+ for file in cleanup {
175
+ std:: fs:: remove_file ( file) ?;
176
+ }
177
+
126
178
Ok ( ( ) )
127
179
}
128
180
@@ -139,7 +191,11 @@ fn test(args: &ArgMatches) -> Result3<()> {
139
191
fn get_book_dir ( args : & ArgMatches ) -> PathBuf {
140
192
if let Some ( p) = args. get_one :: < PathBuf > ( "dir" ) {
141
193
// Check if path is relative from current dir, or absolute...
142
- if p. is_relative ( ) { env:: current_dir ( ) . unwrap ( ) . join ( p) } else { p. to_path_buf ( ) }
194
+ if p. is_relative ( ) {
195
+ env:: current_dir ( ) . unwrap ( ) . join ( p)
196
+ } else {
197
+ p. to_path_buf ( )
198
+ }
143
199
} else {
144
200
env:: current_dir ( ) . unwrap ( )
145
201
}
0 commit comments