Skip to content

Commit

Permalink
Rollup merge of rust-lang#101166 - GuillaumeGomez:error-index-mdbook,…
Browse files Browse the repository at this point in the history
… r=notriddle

Generate error index with mdbook instead of raw HTML pages

This is a follow-up of rust-lang#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```
  • Loading branch information
Dylan-DPC committed Sep 2, 2022
2 parents 4c05e77 + f5857d5 commit f865d5a
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 142 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ dependencies = [
name = "error_index_generator"
version = "0.0.0"
dependencies = [
"rustdoc",
"mdbook",
]

[[package]]
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ impl Step for ErrorIndex {
t!(fs::create_dir_all(&out));
let mut index = tool::ErrorIndex::command(builder);
index.arg("html");
index.arg(out.join("error-index.html"));
index.arg(out);
index.arg(&builder.version);

builder.run(&mut index);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/error_index_generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.0.0"
edition = "2021"

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

[[bin]]
name = "error_index_generator"
Expand Down
19 changes: 19 additions & 0 deletions src/tools/error_index_generator/book_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[book]
title = "Error codes index"
description = "Book listing all Rust error codes"
src = ""

[output.html]
git-repository-url = "https://github.com/rust-lang/rust/"
additional-css = ["error-index.css"]
additional-js = ["error-index.js"]

[output.html.search]
enable = true
limit-results = 20
use-boolean-and = true
boost-title = 2
boost-hierarchy = 2
boost-paragraph = 1
expand = true
heading-split-level = 0
38 changes: 38 additions & 0 deletions src/tools/error_index_generator/error-index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
code.compile_fail {
border-left: 2px solid red;
}

pre .tooltip {
position: absolute;
left: -25px;
top: 0;
z-index: 1;
color: red;
cursor: pointer;
}
pre .tooltip::after {
display: none;
content: "This example deliberately fails to compile";
background-color: #000;
color: #fff;
border-color: #000;
text-align: center;
padding: 5px 3px 3px 3px;
border-radius: 6px;
margin-left: 5px;
}
pre .tooltip::before {
display: none;
border-color: transparent black transparent transparent;
content: " ";
position: absolute;
top: 50%;
left: 16px;
margin-top: -5px;
border-width: 5px;
border-style: solid;
}

pre .tooltip:hover::before, pre .tooltip:hover::after {
display: inline;
}
9 changes: 9 additions & 0 deletions src/tools/error_index_generator/error-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
for (const elem of document.querySelectorAll("pre.playground")) {
if (elem.querySelector(".compile_fail") === null) {
continue;
}
const child = document.createElement("div");
child.className = "tooltip";
child.textContent = "ⓘ";
elem.appendChild(child);
}
Loading

0 comments on commit f865d5a

Please sign in to comment.