Improve Rustdoc scrape-examples UI#105387
Merged
bors merged 10 commits intorust-lang:masterfrom Dec 9, 2022
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR combines a few different improvements to the scrape-examples UI. See a live demo here: https://willcrichton.net/misc/scrape-examples/small-first-example/clap/struct.Arg.html
1. The first scraped example now takes up significantly less screen height.
Inserting the first scraped example takes up a lot of vertical screen space. I don't want this addition to overwhelm users, so I decided to reduce the height of the initial example in two ways: (A) the default un-expanded height is reduced from 240px (10 LOC) to 120px (5 LOC), and (B) the link to the example is now positioned over the example instead of atop the example (only on desktop though, not mobile). The changes to
scrape-examples.jsandrustdoc.cssimplement this fix.Here is what an example docblock now looks like:
2. Expanding all docblocks will not expand "More examples".
The "More examples blocks" are huge, so fully expanding everything on the page would take up too much vertical space. The changes to
main.jsimplement this fix. This is tested inscrape-examples-toggle.goml.3. Examples from binary crates are sorted higher than examples from library crates.
Code that is written as an example of an API is probably better for learning than code that happens to use an API, but isn't intended for pedagogic purposes. Unfortunately Rustc doesn't know whether a particular crate comes from an example target (only Cargo knows this). But we can at least create a proxy that prefers examples from binary crates over library crates, which we know from
--crate-type.This change is implemented by adding a new field
bin_crateinOptions(seeconfig.rs). Anis_binfield has been added to the scraped examples metadata (seescrape_examples.rs). Then the example sorting metric usesis_binas the first entry of a lexicographic sort on(is_bin, example_size, display_name)(seerender/mod.rs).Note that in the future we can consider adding another flag like
--scrape-examples-cargo-targetthat would pass target information from Cargo into the example metadata. But I'm proposing a less intrusive change for now.4. The scrape-examples help page has been updated to reflect the latest Cargo interface.
See
scrape-examples-help.md.r? @notriddle
P.S. once this PR and rust-lang/cargo#11450 are merged, then I think the scrape-examples feature is officially ready for deployment on docs.rs!