diff --git a/.gitmodules b/.gitmodules index 33ea0f53cf132..be6e636bf4206 100644 --- a/.gitmodules +++ b/.gitmodules @@ -55,3 +55,6 @@ path = src/gcc url = https://github.com/rust-lang/gcc.git shallow = true +[submodule "src/doc/translations"] + path = src/doc/translations + url = https://github.com/dalance/translations diff --git a/src/bootstrap/src/core/build_steps/doc.rs b/src/bootstrap/src/core/build_steps/doc.rs index 8a9321f8e79b6..a901593360c85 100644 --- a/src/bootstrap/src/core/build_steps/doc.rs +++ b/src/bootstrap/src/core/build_steps/doc.rs @@ -177,6 +177,8 @@ impl Step for RustbookSrc

{ .arg(&out) .arg("--rust-root") .arg(&builder.src) + .arg("-n") + .arg(&name) .run(builder); for lang in &self.languages { @@ -191,6 +193,10 @@ impl Step for RustbookSrc

{ .arg(&src) .arg("-d") .arg(&out) + .arg("--rust-root") + .arg(&builder.src) + .arg("-n") + .arg(&name) .arg("-l") .arg(lang) .run(builder); diff --git a/src/doc/translations b/src/doc/translations new file mode 160000 index 0000000000000..3afda44c8301d --- /dev/null +++ b/src/doc/translations @@ -0,0 +1 @@ +Subproject commit 3afda44c8301d32e2519e9447aff3e81389a6789 diff --git a/src/tools/rustbook/Cargo.lock b/src/tools/rustbook/Cargo.lock index 400eb7c5e0d7b..c6cfc1822e224 100644 --- a/src/tools/rustbook/Cargo.lock +++ b/src/tools/rustbook/Cargo.lock @@ -1146,6 +1146,7 @@ dependencies = [ "mdbook-spec", "mdbook-trpl-listing", "mdbook-trpl-note", + "toml 0.5.11", ] [[package]] diff --git a/src/tools/rustbook/Cargo.toml b/src/tools/rustbook/Cargo.toml index 854c454733759..41c977bf68bad 100644 --- a/src/tools/rustbook/Cargo.toml +++ b/src/tools/rustbook/Cargo.toml @@ -13,6 +13,7 @@ mdbook-trpl-listing = { path = "../../doc/book/packages/mdbook-trpl-listing" } mdbook-trpl-note = { path = "../../doc/book/packages/mdbook-trpl-note" } mdbook-i18n-helpers = "0.3.3" mdbook-spec = { path = "../../doc/reference/mdbook-spec" } +toml = "0.5.11" [dependencies.mdbook] version = "0.4.37" diff --git a/src/tools/rustbook/src/main.rs b/src/tools/rustbook/src/main.rs index a1ef18610b000..a112017aa5e19 100644 --- a/src/tools/rustbook/src/main.rs +++ b/src/tools/rustbook/src/main.rs @@ -22,6 +22,11 @@ fn main() { .required(false) .value_parser(clap::value_parser!(String)); + let n_arg = arg!(-n --"name" +"The book name") + .required(false) + .value_parser(clap::value_parser!(String)); + let root_arg = arg!(--"rust-root" "Path to the root of the rust source tree") .required(false) @@ -56,6 +61,7 @@ fn main() { .about("Build the book from the markdown files") .arg(d_arg) .arg(l_arg) + .arg(n_arg) .arg(root_arg) .arg(&dir_arg), ) @@ -121,8 +127,54 @@ pub fn build(args: &ArgMatches) -> Result3<()> { book.with_preprocessor(Spec::new(rust_root)?); } + let mut cleanup = Vec::new(); + if let Some(rust_root) = args.get_one::("rust-root") { + if let Some(name) = args.get_one::("name") { + let translation_path = rust_root.join(format!("src/doc/translations/{name}")); + if translation_path.exists() { + let css_file = "theme/language-picker.css"; + let js_file = "theme/language-picker.js"; + + let css_path = translation_path.join(css_file); + let js_path = translation_path.join(js_file); + let po_path = translation_path.join("po"); + + let theme_dir = book_dir.join("theme"); + if !theme_dir.exists() { + std::fs::create_dir(theme_dir)?; + } + let css_target = book_dir.join(css_file); + let js_target = book_dir.join(js_file); + std::fs::copy(css_path, &css_target)?; + std::fs::copy(js_path, &js_target)?; + cleanup.push(css_target); + cleanup.push(js_target); + + let css_file: toml::Value = css_file.into(); + let js_file: toml::Value = js_file.into(); + let po_path: toml::Value = po_path.to_string_lossy().as_ref().into(); + + if let Some(additional_css) = book.config.get_mut("output.html.additional-css") { + additional_css.as_array_mut().unwrap().push(css_file.into()); + } else { + book.config.set("output.html.additional-css", vec![css_file])?; + } + if let Some(additional_js) = book.config.get_mut("output.html.additional-js") { + additional_js.as_array_mut().unwrap().push(js_file.into()); + } else { + book.config.set("output.html.additional-js", vec![js_file])?; + } + book.config.set("preprocessor.gettext.po-dir", po_path)?; + } + } + } + book.build()?; + for file in cleanup { + std::fs::remove_file(file)?; + } + Ok(()) }