From f5857d5c5e1d2fde302f330d11c5cdea8005eb2a Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 31 Aug 2022 21:00:05 +0200 Subject: [PATCH] Move error code book into a sub folder --- src/tools/error_index_generator/main.rs | 81 +++++++-------------- src/tools/error_index_generator/redirect.js | 18 +++-- 2 files changed, 40 insertions(+), 59 deletions(-) diff --git a/src/tools/error_index_generator/main.rs b/src/tools/error_index_generator/main.rs index 5451e45b28b..1bde8e00782 100644 --- a/src/tools/error_index_generator/main.rs +++ b/src/tools/error_index_generator/main.rs @@ -7,7 +7,7 @@ use crate::error_codes::error_codes; use std::env; use std::error::Error; -use std::fs::{self, create_dir_all, File}; +use std::fs::{self, File}; use std::io::Write; use std::path::Path; use std::path::PathBuf; @@ -65,44 +65,6 @@ fn render_markdown(output_path: &Path) -> Result<(), Box> { Ok(()) } -fn move_folder(source: &Path, target: &Path) -> Result<(), Box> { - let entries = - fs::read_dir(source)?.map(|res| res.map(|e| e.path())).collect::, _>>()?; - - for entry in entries { - let file_name = entry.file_name().expect("file_name() failed").to_os_string(); - let output = target.join(file_name); - if entry.is_file() { - fs::rename(entry, output)?; - } else { - if !output.exists() { - create_dir_all(&output)?; - } - move_folder(&entry, &output)?; - } - } - - fs::remove_dir(&source)?; - - Ok(()) -} - -fn render_html(output_path: &Path) -> Result<(), Box> { - // We need to render into a temporary folder to prevent `mdbook` from removing everything - // in the output folder (including other completely unrelated things). - let tmp_output = output_path.join("tmp"); - - if !tmp_output.exists() { - create_dir_all(&tmp_output)?; - } - - render_html_inner(&tmp_output)?; - - move_folder(&tmp_output, output_path)?; - - Ok(()) -} - // By default, mdbook doesn't consider code blocks as Rust ones contrary to rustdoc so we have // to manually add `rust` attribute whenever needed. fn add_rust_attribute_on_codeblock(explanation: &str) -> String { @@ -134,18 +96,14 @@ fn add_rust_attribute_on_codeblock(explanation: &str) -> String { }) } -fn render_html_inner(output_path: &Path) -> Result<(), Box> { - // We need to have a little difference between `summary` and `introduction` because the "draft" - // chapters (the ones looking like `[a]()`) are not handled correctly when being put into a - // `Chapter` directly: they generate a link whereas they shouldn't. +fn render_html(output_path: &Path) -> Result<(), Box> { let mut introduction = format!( - " + " # Rust error codes index This page lists all the error codes emitted by the Rust compiler. -", - include_str!("redirect.js") +" ); let err_codes = error_codes(); @@ -153,7 +111,7 @@ This page lists all the error codes emitted by the Rust compiler. for (err_code, explanation) in err_codes.iter() { if let Some(explanation) = explanation { - introduction.push_str(&format!(" * [{0}](./error_codes/{0}.html)\n", err_code)); + introduction.push_str(&format!(" * [{0}](./{0}.html)\n", err_code)); let content = add_rust_attribute_on_codeblock(explanation); chapters.push(BookItem::Chapter(Chapter { @@ -162,7 +120,7 @@ This page lists all the error codes emitted by the Rust compiler. number: None, sub_items: Vec::new(), // We generate it into the `error_codes` folder. - path: Some(PathBuf::from(&format!("error_codes/{}.html", err_code))), + path: Some(PathBuf::from(&format!("{}.html", err_code))), source_path: None, parent_names: Vec::new(), })); @@ -172,7 +130,7 @@ This page lists all the error codes emitted by the Rust compiler. } let mut config = Config::from_str(include_str!("book_config.toml"))?; - config.build.build_dir = output_path.to_path_buf(); + config.build.build_dir = output_path.join("error_codes").to_path_buf(); let mut book = MDBook::load_with_config_and_summary( env!("CARGO_MANIFEST_DIR"), config, @@ -191,10 +149,27 @@ This page lists all the error codes emitted by the Rust compiler. book.book.sections.push(BookItem::Chapter(chapter)); book.build()?; - // We don't need this file since it's handled by doc.rust-lang.org directly. - let _ = fs::remove_file(output_path.join("404.html")); - // We don't want this file either because it would overwrite the already existing `index.html`. - let _ = fs::remove_file(output_path.join("index.html")); + // We can't put this content into another file, otherwise `mbdbook` will also put it into the + // output directory, making a duplicate. + fs::write( + output_path.join("error-index.html"), + r#" + + + Rust error codes index - Error codes index + + + + + +
If you are not automatically redirected to the error code index, please here. + + +"#, + )?; + + // No need for a 404 file, it's already handled by the server. + fs::remove_file(output_path.join("error_codes/404.html"))?; Ok(()) } diff --git a/src/tools/error_index_generator/redirect.js b/src/tools/error_index_generator/redirect.js index e6e910658e4..8c907f5795d 100644 --- a/src/tools/error_index_generator/redirect.js +++ b/src/tools/error_index_generator/redirect.js @@ -1,10 +1,16 @@ -(function() {{ - if (window.location.hash) {{ +(function() { + if (window.location.hash) { let code = window.location.hash.replace(/^#/, ''); // We have to make sure this pattern matches to avoid inadvertently creating an // open redirect. - if (/^E[0-9]+$/.test(code)) {{ + if (!/^E[0-9]+$/.test(code)) { + return; + } + if (window.location.pathname.indexOf("/error_codes/") !== -1) { + // We're not at the top level, so we don't prepend with "./error_codes/". + window.location = './' + code + '.html'; + } else { window.location = './error_codes/' + code + '.html'; - }} - }} -}})() + } + } +})()