Skip to content

Commit 68d3cfa

Browse files
Rollup merge of #101166 - GuillaumeGomez:error-index-mdbook, r=notriddle
Generate error index with mdbook instead of raw HTML pages This is a follow-up of #100922. This comes from a remark from ````@estebank```` who said that the search was a nice thing on the previous version and that it wasn't possible anymore. An easy way to come around this limitation was to use `mdbook`, which is what I did here. Now some explanations on the code. I'll explain how I developed this and why I reached this solution. First I did it very basically by simply setting the source directory and the output directory, generated a `SUMMARY.md` manually which listed all error codes and that was it. Two problems arose from this: 1. A lot of new HTML files were generated at the top level 2. An `index.html` file was generated at the top-level (the summary in short). So for `1.`, it's not great to have too many files at the top-level as it could create file conflicts more easily. And for `2.`, this is actually a huge issue because <doc.rust-lang.org> generates an `index.html` file with a links to a few different resources, so it should never be overwritten. <s>Unfortunately, `mdbook` **always** generates an `index.html` file so the only solution I could see (except for sending them a contribution, I'll maybe do that later) was to temporaly move a potentially existing `index.html` file and then puts it back once done. For this last part, to ensure that we don't return *before* it has been put back, I wrapped the `mdbook` generation code inside `render_html_inner` which is called from `render_html` which in turn handle the "save" of `index.html`.</s> EDIT: `mdbook` completely deletes ALL the content in the target directory so I instead generate into a sub directory and then I move the files to the real target directory. To keep compatibility with the old version, I also put the `<script>` ````@notriddle```` nicely provided in the previous PR only into the `error-index.html` file to prevent unneeded repetition. I didn't use `mdbook` `additional-js` option because the JS is included at the end of all HTML files, which we don't want for two reasons: 1. It's slow. 2. We only want it to be run in `error-index.html` (even if we also ensure in the JS itself too!). <s>Now the last part: why we generate the summary twice. We actually generate it once to tell `mdbook` what the book will look like and a second time because a create a new chapter content which will actually list all the error codes (with the updated paths).</s> EDIT: I removed the need for two summaries. You can test it [here](https://rustdoc.crud.net/imperio/error-index-mdbook/error-index.html). r? ````@notriddle````
2 parents 203526e + f5857d5 commit 68d3cfa

File tree

8 files changed

+196
-142
lines changed

8 files changed

+196
-142
lines changed

Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ dependencies = [
12591259
name = "error_index_generator"
12601260
version = "0.0.0"
12611261
dependencies = [
1262-
"rustdoc",
1262+
"mdbook",
12631263
]
12641264

12651265
[[package]]

src/bootstrap/doc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ impl Step for ErrorIndex {
793793
t!(fs::create_dir_all(&out));
794794
let mut index = tool::ErrorIndex::command(builder);
795795
index.arg("html");
796-
index.arg(out.join("error-index.html"));
796+
index.arg(out);
797797
index.arg(&builder.version);
798798

799799
builder.run(&mut index);

src/tools/error_index_generator/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.0.0"
44
edition = "2021"
55

66
[dependencies]
7-
rustdoc = { path = "../../librustdoc" }
7+
mdbook = { version = "0.4", default-features = false, features = ["search"] }
88

99
[[bin]]
1010
name = "error_index_generator"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[book]
2+
title = "Error codes index"
3+
description = "Book listing all Rust error codes"
4+
src = ""
5+
6+
[output.html]
7+
git-repository-url = "https://github.com/rust-lang/rust/"
8+
additional-css = ["error-index.css"]
9+
additional-js = ["error-index.js"]
10+
11+
[output.html.search]
12+
enable = true
13+
limit-results = 20
14+
use-boolean-and = true
15+
boost-title = 2
16+
boost-hierarchy = 2
17+
boost-paragraph = 1
18+
expand = true
19+
heading-split-level = 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
code.compile_fail {
2+
border-left: 2px solid red;
3+
}
4+
5+
pre .tooltip {
6+
position: absolute;
7+
left: -25px;
8+
top: 0;
9+
z-index: 1;
10+
color: red;
11+
cursor: pointer;
12+
}
13+
pre .tooltip::after {
14+
display: none;
15+
content: "This example deliberately fails to compile";
16+
background-color: #000;
17+
color: #fff;
18+
border-color: #000;
19+
text-align: center;
20+
padding: 5px 3px 3px 3px;
21+
border-radius: 6px;
22+
margin-left: 5px;
23+
}
24+
pre .tooltip::before {
25+
display: none;
26+
border-color: transparent black transparent transparent;
27+
content: " ";
28+
position: absolute;
29+
top: 50%;
30+
left: 16px;
31+
margin-top: -5px;
32+
border-width: 5px;
33+
border-style: solid;
34+
}
35+
36+
pre .tooltip:hover::before, pre .tooltip:hover::after {
37+
display: inline;
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
for (const elem of document.querySelectorAll("pre.playground")) {
2+
if (elem.querySelector(".compile_fail") === null) {
3+
continue;
4+
}
5+
const child = document.createElement("div");
6+
child.className = "tooltip";
7+
child.textContent = "ⓘ";
8+
elem.appendChild(child);
9+
}

0 commit comments

Comments
 (0)