Skip to content

Commit 0f772fa

Browse files
committed
Add submodule for document translation
1 parent 83965ef commit 0f772fa

File tree

7 files changed

+79
-11
lines changed

7 files changed

+79
-11
lines changed

Diff for: .gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,6 @@
5555
path = src/gcc
5656
url = https://github.com/rust-lang/gcc.git
5757
shallow = true
58+
[submodule "src/doc/translations"]
59+
path = src/doc/translations
60+
url = https://github.com/dalance/translations

Diff for: src/bootstrap/src/core/build_steps/doc.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ use std::io::{self, Write};
1111
use std::path::{Path, PathBuf};
1212
use std::{env, fs, mem};
1313

14-
use crate::Mode;
1514
use crate::core::build_steps::compile;
16-
use crate::core::build_steps::tool::{self, SourceType, Tool, prepare_tool_cargo};
15+
use crate::core::build_steps::tool::{self, prepare_tool_cargo, SourceType, Tool};
1716
use crate::core::builder::{
18-
self, Alias, Builder, Compiler, Kind, RunConfig, ShouldRun, Step, crate_description,
17+
self, crate_description, Alias, Builder, Compiler, Kind, RunConfig, ShouldRun, Step,
1918
};
2019
use crate::core::config::{Config, TargetSelection};
2120
use crate::utils::helpers::{symlink_dir, t, up_to_date};
21+
use crate::Mode;
2222

2323
macro_rules! submodule_helper {
2424
($path:expr, submodule) => {
@@ -177,6 +177,8 @@ impl<P: Step> Step for RustbookSrc<P> {
177177
.arg(&out)
178178
.arg("--rust-root")
179179
.arg(&builder.src)
180+
.arg("-n")
181+
.arg(&name)
180182
.run(builder);
181183

182184
for lang in &self.languages {
@@ -191,6 +193,10 @@ impl<P: Step> Step for RustbookSrc<P> {
191193
.arg(&src)
192194
.arg("-d")
193195
.arg(&out)
196+
.arg("--rust-root")
197+
.arg(&builder.src)
198+
.arg("-n")
199+
.arg(&name)
194200
.arg("-l")
195201
.arg(lang)
196202
.run(builder);

Diff for: src/doc/rust-by-example

Diff for: src/doc/translations

Submodule translations added at 3afda44

Diff for: src/tools/rustbook/Cargo.lock

+5-4
Original file line numberDiff line numberDiff line change
@@ -1146,13 +1146,14 @@ dependencies = [
11461146
"mdbook-spec",
11471147
"mdbook-trpl-listing",
11481148
"mdbook-trpl-note",
1149+
"toml 0.5.11",
11491150
]
11501151

11511152
[[package]]
11521153
name = "rustix"
1153-
version = "0.38.38"
1154+
version = "0.38.41"
11541155
source = "registry+https://github.com/rust-lang/crates.io-index"
1155-
checksum = "aa260229e6538e52293eeb577aabd09945a09d6d9cc0fc550ed7529056c2e32a"
1156+
checksum = "d7f649912bc1495e167a6edee79151c84b1bad49748cb4f1f1167f459f6224f6"
11561157
dependencies = [
11571158
"bitflags 2.6.0",
11581159
"errno",
@@ -1323,9 +1324,9 @@ dependencies = [
13231324

13241325
[[package]]
13251326
name = "tempfile"
1326-
version = "3.13.0"
1327+
version = "3.14.0"
13271328
source = "registry+https://github.com/rust-lang/crates.io-index"
1328-
checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b"
1329+
checksum = "28cce251fcbc87fac86a866eeb0d6c2d536fc16d06f184bb61aeae11aa4cee0c"
13291330
dependencies = [
13301331
"cfg-if",
13311332
"fastrand",

Diff for: src/tools/rustbook/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mdbook-trpl-listing = { path = "../../doc/book/packages/mdbook-trpl-listing" }
1313
mdbook-trpl-note = { path = "../../doc/book/packages/mdbook-trpl-note" }
1414
mdbook-i18n-helpers = "0.3.3"
1515
mdbook-spec = { path = "../../doc/reference/mdbook-spec" }
16+
toml = "0.5.11"
1617

1718
[dependencies.mdbook]
1819
version = "0.4.37"

Diff for: src/tools/rustbook/src/main.rs

+59-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::env;
22
use 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};
65
use mdbook::errors::Result as Result3;
6+
use mdbook::MDBook;
77
use mdbook_i18n_helpers::preprocessors::Gettext;
88
use mdbook_spec::Spec;
99
use 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<()> {
139191
fn 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

Comments
 (0)