Skip to content

Commit

Permalink
Add submodule for document translation
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Nov 28, 2024
1 parent 83965ef commit 5b172f4
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 9 additions & 3 deletions src/bootstrap/src/core/build_steps/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::{env, fs, mem};

use crate::Mode;
use crate::core::build_steps::compile;
use crate::core::build_steps::tool::{self, SourceType, Tool, prepare_tool_cargo};
use crate::core::build_steps::tool::{self, prepare_tool_cargo, SourceType, Tool};
use crate::core::builder::{
self, Alias, Builder, Compiler, Kind, RunConfig, ShouldRun, Step, crate_description,
self, crate_description, Alias, Builder, Compiler, Kind, RunConfig, ShouldRun, Step,
};
use crate::core::config::{Config, TargetSelection};
use crate::utils::helpers::{symlink_dir, t, up_to_date};
use crate::Mode;

macro_rules! submodule_helper {
($path:expr, submodule) => {
Expand Down Expand Up @@ -177,6 +177,8 @@ impl<P: Step> Step for RustbookSrc<P> {
.arg(&out)
.arg("--rust-root")
.arg(&builder.src)
.arg("-n")
.arg(&name)
.run(builder);

for lang in &self.languages {
Expand All @@ -191,6 +193,10 @@ impl<P: Step> Step for RustbookSrc<P> {
.arg(&src)
.arg("-d")
.arg(&out)
.arg("--rust-root")
.arg(&builder.src)
.arg("-n")
.arg(&name)
.arg("-l")
.arg(lang)
.run(builder);
Expand Down
1 change: 1 addition & 0 deletions src/doc/translations
Submodule translations added at 3afda4
9 changes: 5 additions & 4 deletions src/tools/rustbook/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1146,13 +1146,14 @@ dependencies = [
"mdbook-spec",
"mdbook-trpl-listing",
"mdbook-trpl-note",
"toml 0.5.11",
]

[[package]]
name = "rustix"
version = "0.38.38"
version = "0.38.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa260229e6538e52293eeb577aabd09945a09d6d9cc0fc550ed7529056c2e32a"
checksum = "d7f649912bc1495e167a6edee79151c84b1bad49748cb4f1f1167f459f6224f6"
dependencies = [
"bitflags 2.6.0",
"errno",
Expand Down Expand Up @@ -1323,9 +1324,9 @@ dependencies = [

[[package]]
name = "tempfile"
version = "3.13.0"
version = "3.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b"
checksum = "28cce251fcbc87fac86a866eeb0d6c2d536fc16d06f184bb61aeae11aa4cee0c"
dependencies = [
"cfg-if",
"fastrand",
Expand Down
1 change: 1 addition & 0 deletions src/tools/rustbook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
52 changes: 52 additions & 0 deletions src/tools/rustbook/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ fn main() {
.required(false)
.value_parser(clap::value_parser!(String));

let n_arg = arg!(-n --"name" <NAME>
"The book name")
.required(false)
.value_parser(clap::value_parser!(String));

let root_arg = arg!(--"rust-root" <ROOT_DIR>
"Path to the root of the rust source tree")
.required(false)
Expand Down Expand Up @@ -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),
)
Expand Down Expand Up @@ -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::<PathBuf>("rust-root") {
if let Some(name) = args.get_one::<String>("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(())
}

Expand Down

0 comments on commit 5b172f4

Please sign in to comment.