Skip to content

Commit f5857d5

Browse files
Move error code book into a sub folder
1 parent 1171697 commit f5857d5

File tree

2 files changed

+40
-59
lines changed

2 files changed

+40
-59
lines changed

src/tools/error_index_generator/main.rs

+28-53
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::error_codes::error_codes;
77

88
use std::env;
99
use std::error::Error;
10-
use std::fs::{self, create_dir_all, File};
10+
use std::fs::{self, File};
1111
use std::io::Write;
1212
use std::path::Path;
1313
use std::path::PathBuf;
@@ -65,44 +65,6 @@ fn render_markdown(output_path: &Path) -> Result<(), Box<dyn Error>> {
6565
Ok(())
6666
}
6767

68-
fn move_folder(source: &Path, target: &Path) -> Result<(), Box<dyn Error>> {
69-
let entries =
70-
fs::read_dir(source)?.map(|res| res.map(|e| e.path())).collect::<Result<Vec<_>, _>>()?;
71-
72-
for entry in entries {
73-
let file_name = entry.file_name().expect("file_name() failed").to_os_string();
74-
let output = target.join(file_name);
75-
if entry.is_file() {
76-
fs::rename(entry, output)?;
77-
} else {
78-
if !output.exists() {
79-
create_dir_all(&output)?;
80-
}
81-
move_folder(&entry, &output)?;
82-
}
83-
}
84-
85-
fs::remove_dir(&source)?;
86-
87-
Ok(())
88-
}
89-
90-
fn render_html(output_path: &Path) -> Result<(), Box<dyn Error>> {
91-
// We need to render into a temporary folder to prevent `mdbook` from removing everything
92-
// in the output folder (including other completely unrelated things).
93-
let tmp_output = output_path.join("tmp");
94-
95-
if !tmp_output.exists() {
96-
create_dir_all(&tmp_output)?;
97-
}
98-
99-
render_html_inner(&tmp_output)?;
100-
101-
move_folder(&tmp_output, output_path)?;
102-
103-
Ok(())
104-
}
105-
10668
// By default, mdbook doesn't consider code blocks as Rust ones contrary to rustdoc so we have
10769
// to manually add `rust` attribute whenever needed.
10870
fn add_rust_attribute_on_codeblock(explanation: &str) -> String {
@@ -134,26 +96,22 @@ fn add_rust_attribute_on_codeblock(explanation: &str) -> String {
13496
})
13597
}
13698

137-
fn render_html_inner(output_path: &Path) -> Result<(), Box<dyn Error>> {
138-
// We need to have a little difference between `summary` and `introduction` because the "draft"
139-
// chapters (the ones looking like `[a]()`) are not handled correctly when being put into a
140-
// `Chapter` directly: they generate a link whereas they shouldn't.
99+
fn render_html(output_path: &Path) -> Result<(), Box<dyn Error>> {
141100
let mut introduction = format!(
142-
"<script>{}</script>
101+
"<script src='redirect.js'></script>
143102
# Rust error codes index
144103
145104
This page lists all the error codes emitted by the Rust compiler.
146105
147-
",
148-
include_str!("redirect.js")
106+
"
149107
);
150108

151109
let err_codes = error_codes();
152110
let mut chapters = Vec::with_capacity(err_codes.len());
153111

154112
for (err_code, explanation) in err_codes.iter() {
155113
if let Some(explanation) = explanation {
156-
introduction.push_str(&format!(" * [{0}](./error_codes/{0}.html)\n", err_code));
114+
introduction.push_str(&format!(" * [{0}](./{0}.html)\n", err_code));
157115

158116
let content = add_rust_attribute_on_codeblock(explanation);
159117
chapters.push(BookItem::Chapter(Chapter {
@@ -162,7 +120,7 @@ This page lists all the error codes emitted by the Rust compiler.
162120
number: None,
163121
sub_items: Vec::new(),
164122
// We generate it into the `error_codes` folder.
165-
path: Some(PathBuf::from(&format!("error_codes/{}.html", err_code))),
123+
path: Some(PathBuf::from(&format!("{}.html", err_code))),
166124
source_path: None,
167125
parent_names: Vec::new(),
168126
}));
@@ -172,7 +130,7 @@ This page lists all the error codes emitted by the Rust compiler.
172130
}
173131

174132
let mut config = Config::from_str(include_str!("book_config.toml"))?;
175-
config.build.build_dir = output_path.to_path_buf();
133+
config.build.build_dir = output_path.join("error_codes").to_path_buf();
176134
let mut book = MDBook::load_with_config_and_summary(
177135
env!("CARGO_MANIFEST_DIR"),
178136
config,
@@ -191,10 +149,27 @@ This page lists all the error codes emitted by the Rust compiler.
191149
book.book.sections.push(BookItem::Chapter(chapter));
192150
book.build()?;
193151

194-
// We don't need this file since it's handled by doc.rust-lang.org directly.
195-
let _ = fs::remove_file(output_path.join("404.html"));
196-
// We don't want this file either because it would overwrite the already existing `index.html`.
197-
let _ = fs::remove_file(output_path.join("index.html"));
152+
// We can't put this content into another file, otherwise `mbdbook` will also put it into the
153+
// output directory, making a duplicate.
154+
fs::write(
155+
output_path.join("error-index.html"),
156+
r#"<!DOCTYPE html>
157+
<html>
158+
<head>
159+
<title>Rust error codes index - Error codes index</title>
160+
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
161+
<meta name="description" content="Book listing all Rust error codes">
162+
<script src="error_codes/redirect.js"></script>
163+
</head>
164+
<body>
165+
<div>If you are not automatically redirected to the error code index, please <a id="index-link" href="./error_codes/error-index.html">here</a>.
166+
<script>document.getElementById("index-link").click()</script>
167+
</body>
168+
</html>"#,
169+
)?;
170+
171+
// No need for a 404 file, it's already handled by the server.
172+
fs::remove_file(output_path.join("error_codes/404.html"))?;
198173

199174
Ok(())
200175
}
+12-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
(function() {{
2-
if (window.location.hash) {{
1+
(function() {
2+
if (window.location.hash) {
33
let code = window.location.hash.replace(/^#/, '');
44
// We have to make sure this pattern matches to avoid inadvertently creating an
55
// open redirect.
6-
if (/^E[0-9]+$/.test(code)) {{
6+
if (!/^E[0-9]+$/.test(code)) {
7+
return;
8+
}
9+
if (window.location.pathname.indexOf("/error_codes/") !== -1) {
10+
// We're not at the top level, so we don't prepend with "./error_codes/".
11+
window.location = './' + code + '.html';
12+
} else {
713
window.location = './error_codes/' + code + '.html';
8-
}}
9-
}}
10-
}})()
14+
}
15+
}
16+
})()

0 commit comments

Comments
 (0)