11use std:: env;
22use std:: path:: { Path , PathBuf } ;
33
4- use clap:: { ArgMatches , Command , arg, crate_version} ;
5- use mdbook:: MDBook ;
4+ use clap:: { arg, crate_version, ArgMatches , Command } ;
65use mdbook:: errors:: Result as Result3 ;
6+ use mdbook:: MDBook ;
77use mdbook_i18n_helpers:: preprocessors:: Gettext ;
88use mdbook_spec:: Spec ;
99use mdbook_trpl_listing:: TrplListing ;
@@ -22,6 +22,11 @@ fn main() {
2222 . required ( false )
2323 . value_parser ( clap:: value_parser!( String ) ) ;
2424
25+ let n_arg = arg ! ( -n --"name" <NAME >
26+ "The book name" )
27+ . required ( false )
28+ . value_parser ( clap:: value_parser!( String ) ) ;
29+
2530 let root_arg = arg ! ( --"rust-root" <ROOT_DIR >
2631"Path to the root of the rust source tree" )
2732 . required ( false )
@@ -56,6 +61,7 @@ fn main() {
5661 . about ( "Build the book from the markdown files" )
5762 . arg ( d_arg)
5863 . arg ( l_arg)
64+ . arg ( n_arg)
5965 . arg ( root_arg)
6066 . arg ( & dir_arg) ,
6167 )
@@ -121,8 +127,54 @@ pub fn build(args: &ArgMatches) -> Result3<()> {
121127 book. with_preprocessor ( Spec :: new ( rust_root) ?) ;
122128 }
123129
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+
124172 book. build ( ) ?;
125173
174+ for file in cleanup {
175+ std:: fs:: remove_file ( file) ?;
176+ }
177+
126178 Ok ( ( ) )
127179}
128180
@@ -139,7 +191,11 @@ fn test(args: &ArgMatches) -> Result3<()> {
139191fn get_book_dir ( args : & ArgMatches ) -> PathBuf {
140192 if let Some ( p) = args. get_one :: < PathBuf > ( "dir" ) {
141193 // 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+ }
143199 } else {
144200 env:: current_dir ( ) . unwrap ( )
145201 }
0 commit comments