-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[rustdoc] Move line numbers into the <code>
directly
#136829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[rustdoc] Move line numbers into the <code>
directly
#136829
Conversation
Some changes occurred in HTML/CSS/JS. cc @GuillaumeGomez, @jsha |
This comment has been minimized.
This comment has been minimized.
383ccee
to
cec6ec1
Compare
This comment has been minimized.
This comment has been minimized.
cec6ec1
to
27cd471
Compare
This comment has been minimized.
This comment has been minimized.
27cd471
to
456c680
Compare
This comment has been minimized.
This comment has been minimized.
456c680
to
7742ad9
Compare
src/librustdoc/html/highlight.rs
Outdated
fn write_line_number(out: &mut impl Write, line: u32, extra: &'static str) { | ||
// https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr | ||
// Do not show "1 2 3 4 5 ..." in web search results. | ||
write!(out, "{extra}<a href=\"#{line}\" id={line} data-nosnippet>{line}</a>",).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... I think?
write!(out, "{extra}<a href=\"#{line}\" id={line} data-nosnippet>{line}</a>",).unwrap(); | |
write!(out, "{extra}<a href=\"#{line}\" id=\"{line}\" data-nosnippet>{line}</a>",).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a number so I'm cheating here. That allows to get 2 characters x number of lines, so it becomes interesting for big crates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, didn't know that was valid in HTML5!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's valid in all HTML as far as I know. 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do that with the href, too. Unquoted attributes are allowed to contain hashes.
It would probably also be good to add a Rust comment pointing out what you're doing and why.
text-align: right; | ||
display: inline-block; | ||
margin-right: 20px; | ||
user-select: none; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to https://caniuse.com/?search=user-select, this needs prefixes for Safari (and IE).
(I have confirmed that on Safari, for the two linked examples, the line numbers currently get selected).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't know, thanks for that!
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
d8cda50
to
9222b05
Compare
Finally fixed the tests and made tidy happy. ^^' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This look really nice. It fixes the problem, and it also fixes #84242
lib.rs - source
Files
scrape_examples/
lib.rs
1//@ run-flags:-Zrustdoc-scrape-examples
2/// # Examples
3///
4/// ```
5/// test();
6/// test();
7/// ```
8pub fn test() {}
9
10pub fn test_many() {}
Commands: Use arrow keys to move, '?' for help, 'q' to quit, '<-' to go back.
Arrow keys: Up and Down to move. Right to follow a link; Left to go back.
H)elp O)ptions P)rint G)o M)ain screen Q)uit /=search [delete]=history list
You can mention that in the PR description and we can finally close that issue out.
src/librustdoc/html/highlight.rs
Outdated
fn write_line_number(out: &mut impl Write, line: u32, extra: &'static str) { | ||
// https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr | ||
// Do not show "1 2 3 4 5 ..." in web search results. | ||
write!(out, "{extra}<a href=\"#{line}\" id={line} data-nosnippet>{line}</a>",).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do that with the href, too. Unquoted attributes are allowed to contain hashes.
It would probably also be good to add a Rust comment pointing out what you're doing and why.
r=me after removing the quotes from the href attribute (assuming there's not some reason I wasn't aware of to keep them). |
Wow nice, didn't know about the hashtag in the attributes. Thanks! |
@bors r=notriddle rollup |
…llaumeGomez Rollup of 8 pull requests Successful merges: - rust-lang#134981 ( Explain that in paths generics can't be set on both the enum and the variant) - rust-lang#136698 (Replace i686-unknown-redox target with i586-unknown-redox) - rust-lang#136767 (improve host/cross target checking) - rust-lang#136829 ([rustdoc] Move line numbers into the `<code>` directly) - rust-lang#136875 (Rustc dev guide subtree update) - rust-lang#136900 (compiler: replace `ExternAbi::name` calls with formatters) - rust-lang#136913 (Put kobzol back on review rotation) - rust-lang#136915 (documentation fix: `f16` and `f128` are not double-precision) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#136829 - GuillaumeGomez:move-line-numbers-into-code, r=notriddle [rustdoc] Move line numbers into the `<code>` directly Fixes rust-lang#84242. This is the first for adding support for rust-lang#127334 and also for another feature I'm working on. A side-effect of this change is that it also fixes source code pages display in lynx since they're not directly in the source code. To allow having code wrapping, the grid approach doesn't work as the line numbers are in their own container, so we need to move them into the code. Now with this, it becomes much simpler to do what we want (with CSS mostly). One downside: the highlighting became more complex and slow as we need to generate some extra HTML tags directly into the highlighting process. However that also allows to not have a huge HTML size increase. You can test the result [here](https://rustdoc.crud.net/imperio/move-line-numbers-into-code/scrape_examples/fn.test_many.html) and [here](https://rustdoc.crud.net/imperio/move-line-numbers-into-code/src/scrape_examples/lib.rs.html#10). The appearance should have close to no changes. r? ``@notriddle``
@rust-timer build 60306e7 |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (60306e7): comparison URL. Overall result: ❌ regressions - please read the text belowBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @bors rollup=never Instruction countThis is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.
Max RSS (memory usage)Results (primary 2.2%, secondary 2.2%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResults (primary 1.8%, secondary 2.5%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 789.14s -> 789.514s (0.05%) |
This PR caused a number of |
I knew it would happen. The underlying cause is that this new format pushes more bytes of HTML than the old one. It's a hack that's imperfect both for producing non-bloated HTML and for a11y. This change also makes #136991 possible (which is ultimately what pushed it over the edge).
The new version pushed in this PR does an imperfect job of making HTML both small and accessible at the same time. This is vividly shown in these Lynx screenshots:
First of all, the visible-on-screen line numbers are also where the anchors for links are found, so if someone shares a URL pointing at a particular line, the "After" version will jump you to that line. So that's much better than what we did before. Too bad there isn't any way to fix this without making the HTML bigger, because we have to mark up each line separately instead of doing it all at once. The "Before" screenshot is clearly busted, but the "After" screenshot is also imperfect. Notice how the word |
…llaumeGomez Rollup of 8 pull requests Successful merges: - rust-lang#134981 ( Explain that in paths generics can't be set on both the enum and the variant) - rust-lang#136698 (Replace i686-unknown-redox target with i586-unknown-redox) - rust-lang#136767 (improve host/cross target checking) - rust-lang#136829 ([rustdoc] Move line numbers into the `<code>` directly) - rust-lang#136875 (Rustc dev guide subtree update) - rust-lang#136900 (compiler: replace `ExternAbi::name` calls with formatters) - rust-lang#136913 (Put kobzol back on review rotation) - rust-lang#136915 (documentation fix: `f16` and `f128` are not double-precision) r? `@ghost` `@rustbot` modify labels: rollup
…llaumeGomez Rollup of 8 pull requests Successful merges: - rust-lang#134981 ( Explain that in paths generics can't be set on both the enum and the variant) - rust-lang#136698 (Replace i686-unknown-redox target with i586-unknown-redox) - rust-lang#136767 (improve host/cross target checking) - rust-lang#136829 ([rustdoc] Move line numbers into the `<code>` directly) - rust-lang#136875 (Rustc dev guide subtree update) - rust-lang#136900 (compiler: replace `ExternAbi::name` calls with formatters) - rust-lang#136913 (Put kobzol back on review rotation) - rust-lang#136915 (documentation fix: `f16` and `f128` are not double-precision) r? `@ghost` `@rustbot` modify labels: rollup
Fixes #84242.
This is the first for adding support for #127334 and also for another feature I'm working on.
A side-effect of this change is that it also fixes source code pages display in lynx since they're not directly in the source code.
To allow having code wrapping, the grid approach doesn't work as the line numbers are in their own container, so we need to move them into the code. Now with this, it becomes much simpler to do what we want (with CSS mostly). One downside: the highlighting became more complex and slow as we need to generate some extra HTML tags directly into the highlighting process. However that also allows to not have a huge HTML size increase.
You can test the result here and here.
The appearance should have close to no changes.
r? @notriddle