From 7e81b0a3173470cbc77273b2f984913dd3c02ef8 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Sat, 22 Jan 2022 15:32:19 -0800 Subject: Improve Rustdoc UI for scraped examples with multiline arguments, fix overflow in line numbers --- src/librustdoc/html/static/css/rustdoc.css | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'src/librustdoc/html/static/css/rustdoc.css') diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index ee265b8c4b5..42b66c70c4c 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -2038,17 +2038,16 @@ details.rustdoc-toggle[open] > summary.hideme::after { font-family: 'Fira Sans'; } -.scraped-example:not(.expanded) .code-wrapper pre.line-numbers { - overflow: hidden; - max-height: 240px; -} - -.scraped-example:not(.expanded) .code-wrapper .example-wrap pre.rust { +.scraped-example:not(.expanded) .code-wrapper pre { overflow-y: hidden; max-height: 240px; padding-bottom: 0; } +.scraped-example:not(.expanded) .code-wrapper pre.line-numbers { + overflow-x: hidden; +} + .scraped-example .code-wrapper .prev { position: absolute; top: 0.25em; -- cgit 1.4.1-3-g733a5 From ae5d0cbe74a07baef9eb92dde82b28feea8961cd Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Thu, 27 Jan 2022 17:00:31 -0800 Subject: Improve alignment of additional scraped examples, add scrape examples help page --- src/doc/rustdoc/src/SUMMARY.md | 1 + src/doc/rustdoc/src/scraped-examples.md | 55 ++++++++++++++++ src/librustdoc/html/render/mod.rs | 2 + src/librustdoc/html/static/css/rustdoc.css | 77 ++++++++++++---------- src/librustdoc/html/static/css/themes/ayu.css | 16 ++++- src/librustdoc/html/static/css/themes/dark.css | 15 ++++- src/librustdoc/html/static/css/themes/light.css | 30 +++++++++ src/librustdoc/html/static/js/scrape-examples.js | 6 +- .../rustdoc-scrape-examples-ordering/src/lib.rs | 4 +- 9 files changed, 164 insertions(+), 42 deletions(-) create mode 100644 src/doc/rustdoc/src/scraped-examples.md (limited to 'src/librustdoc/html/static/css/rustdoc.css') diff --git a/src/doc/rustdoc/src/SUMMARY.md b/src/doc/rustdoc/src/SUMMARY.md index d627f5b0389..747cc629ba4 100644 --- a/src/doc/rustdoc/src/SUMMARY.md +++ b/src/doc/rustdoc/src/SUMMARY.md @@ -9,6 +9,7 @@ - [Linking to items by name](write-documentation/linking-to-items-by-name.md) - [Documentation tests](write-documentation/documentation-tests.md) - [Rustdoc-specific lints](lints.md) +- [Scraped examples](scraped-examples.md) - [Advanced features](advanced-features.md) - [Unstable features](unstable-features.md) - [Deprecated features](deprecated-features.md) diff --git a/src/doc/rustdoc/src/scraped-examples.md b/src/doc/rustdoc/src/scraped-examples.md new file mode 100644 index 00000000000..bab992ccc50 --- /dev/null +++ b/src/doc/rustdoc/src/scraped-examples.md @@ -0,0 +1,55 @@ +# Scraped examples + +Rustdoc can automatically scrape examples of items being documented from the `examples/` directory of a Cargo workspace. These examples will be included within the generated documentation for that item. For example, if your library contains a public function: + +```rust,ignore(needs-other-file) +// a_crate/src/lib.rs +pub fn a_func() {} +``` + +And you have an example calling this function: + +```rust,ignore(needs-other-file) +// a_crate/examples/ex.rs +fn main() { + a_crate::a_func(); +} +``` + +Then this code snippet will be included in the documentation for `a_func`. This documentation is inserted by Rustdoc and cannot be manually edited by the crate author. + + +## How to use this feature + +This feature is unstable, so you can enable it by calling Rustdoc with the unstable `rustdoc-scrape-examples` flag: + +```bash +cargo doc -Zunstable-options -Zrustdoc-scrape-examples=examples +``` + +To enable this feature on [docs.rs](https://docs.rs), add this to your Cargo.toml: + +```toml +[package.metadata.docs.rs] +cargo-args = ["-Zunstable-options", "-Zrustdoc-scrape-examples=examples"] +``` + + +## How it works + +When you run `cargo doc`, Rustdoc will analyze all the crates that match Cargo's `--examples` filter for instances of items being documented. Then Rustdoc will include the source code of these instances in the generated documentation. + +Rustdoc has a few techniques to ensure these examples don't overwhelm documentation readers, and that it doesn't blow up the page size: + +1. For a given item, a maximum of 5 examples are included in the page. The remaining examples are just links to source code. +2. Only one example is shown by default, and the remaining examples are hidden behind a toggle. +3. For a given file that contains examples, only the item containing the examples will be included in the generated documentation. + +For a given item, Rustdoc sorts its examples based on the size of the example — smaller ones are shown first. + + +## FAQ + +### My example is not showing up in the documentation + +This feature uses Cargo's convention for finding examples. You should ensure that `cargo check --examples` includes your example file. diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 6b57ff5eeba..b5536dc930f 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -2671,6 +2671,7 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) { \
\ Examples found in repository\ + ?\
", id = id ); @@ -2842,6 +2843,7 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) { \ More examples\ \ +
Hide additional examples
\
\
\
" diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 42b66c70c4c..b278ab6b24d 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -618,7 +618,7 @@ h2.location a { position: relative; } -.docblock > :not(.information) { +.docblock > :not(.information):not(.more-examples-toggle) { max-width: 100%; overflow-x: auto; } @@ -836,8 +836,8 @@ h2.small-section-header > .anchor { content: 'ยง'; } -.docblock a:not(.srclink):not(.test-arrow):hover, -.docblock-short a:not(.srclink):not(.test-arrow):hover, .item-info a { +.docblock a:not(.srclink):not(.test-arrow):not(.scrape-help):hover, +.docblock-short a:not(.srclink):not(.test-arrow):not(.scrape-help):hover, .item-info a { text-decoration: underline; } @@ -2034,10 +2034,36 @@ details.rustdoc-toggle[open] > summary.hideme::after { /* Begin: styles for --scrape-examples feature */ +.scraped-example-list .section-header .scrape-help { + cursor: pointer; + border-radius: 2px; + margin-left: 10px; + padding: 0 4px; + font-weight: normal; + font-size: 12px; + position: relative; + bottom: 1px; + background: transparent; + border-width: 1px; + border-style: solid; +} + .scraped-example-title { font-family: 'Fira Sans'; } +.scraped-example .code-wrapper { + position: relative; + display: flex; + flex-direction: row; + flex-wrap: wrap; + width: 100%; +} + +.scraped-example:not(.expanded) .code-wrapper { + max-height: 240px; +} + .scraped-example:not(.expanded) .code-wrapper pre { overflow-y: hidden; max-height: 240px; @@ -2072,14 +2098,6 @@ details.rustdoc-toggle[open] > summary.hideme::after { cursor: pointer; } -.scraped-example .code-wrapper { - position: relative; - display: flex; - flex-direction: row; - flex-wrap: wrap; - width: 100%; -} - .scraped-example:not(.expanded) .code-wrapper:before { content: " "; width: 100%; @@ -2087,7 +2105,6 @@ details.rustdoc-toggle[open] > summary.hideme::after { position: absolute; z-index: 100; top: 0; - background: linear-gradient(to bottom, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0)); } .scraped-example:not(.expanded) .code-wrapper:after { @@ -2097,12 +2114,6 @@ details.rustdoc-toggle[open] > summary.hideme::after { position: absolute; z-index: 100; bottom: 0; - background: linear-gradient(to top, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0)); -} - -.scraped-example:not(.expanded) .code-wrapper { - overflow: hidden; - max-height: 240px; } .scraped-example .code-wrapper .line-numbers { @@ -2121,34 +2132,37 @@ details.rustdoc-toggle[open] > summary.hideme::after { margin-bottom: 0; } +.scraped-example:not(.expanded) .code-wrapper .example-wrap { + overflow-x: hidden; +} + .scraped-example .code-wrapper .example-wrap pre.rust { overflow-x: inherit; width: inherit; overflow-y: hidden; } -.scraped-example .example-wrap .rust span.highlight { - background: #fcffd6; -} - -.scraped-example .example-wrap .rust span.highlight.focus { - background: #f6fdb0; -} .more-examples-toggle { + max-width: calc(100% + 25px); margin-top: 10px; + margin-left: -25px; +} + +.more-examples-toggle .hide-more { + margin-left: 25px; + margin-bottom: 5px; + cursor: pointer; } -.more-examples-toggle summary { - color: #999; +.more-examples-toggle summary, .more-examples-toggle .hide-more { font-family: 'Fira Sans'; } .more-scraped-examples { - margin-left: 25px; + margin-left: 5px; display: flex; flex-direction: row; - width: calc(100% - 25px); } .more-scraped-examples-inner { @@ -2164,13 +2178,8 @@ details.rustdoc-toggle[open] > summary.hideme::after { cursor: pointer; } -.toggle-line:hover .toggle-line-inner { - background: #aaa; -} - .toggle-line-inner { min-width: 2px; - background: #ddd; height: 100%; } diff --git a/src/librustdoc/html/static/css/themes/ayu.css b/src/librustdoc/html/static/css/themes/ayu.css index e402b3583f3..0fee0ba54fa 100644 --- a/src/librustdoc/html/static/css/themes/ayu.css +++ b/src/librustdoc/html/static/css/themes/ayu.css @@ -611,6 +611,18 @@ input:checked + .slider { background-color: #ffb454 !important; } + +.scraped-example-list .section-header .scrape-help { + border-color: #999; + color: #999; +} +.scraped-example-list .section-header .scrape-help:hover { + border-color: #c5c5c5; + color: #c5c5c5; +} +.more-examples-toggle summary, .more-examples-toggle .hide-more { + color: #999; +} .scraped-example .example-wrap .rust span.highlight { background: rgb(91, 59, 1); } @@ -624,8 +636,8 @@ input:checked + .slider { background: linear-gradient(to top, rgba(15, 20, 25, 1), rgba(15, 20, 25, 0)); } .toggle-line-inner { - background: #616161; + background: #999; } .toggle-line:hover .toggle-line-inner { - background: #898989; + background: #c5c5c5; } diff --git a/src/librustdoc/html/static/css/themes/dark.css b/src/librustdoc/html/static/css/themes/dark.css index 0a56055b8cb..e121c984dbd 100644 --- a/src/librustdoc/html/static/css/themes/dark.css +++ b/src/librustdoc/html/static/css/themes/dark.css @@ -478,6 +478,17 @@ div.files > .selected { border-bottom-color: #ddd; } +.scraped-example-list .section-header .scrape-help { + border-color: #999; + color: #999; +} +.scraped-example-list .section-header .scrape-help:hover { + border-color: #c5c5c5; + color: #c5c5c5; +} +.more-examples-toggle summary, .more-examples-toggle .hide-more { + color: #999; +} .scraped-example .example-wrap .rust span.highlight { background: rgb(91, 59, 1); } @@ -491,8 +502,8 @@ div.files > .selected { background: linear-gradient(to top, rgba(53, 53, 53, 1), rgba(53, 53, 53, 0)); } .toggle-line-inner { - background: #616161; + background: #999; } .toggle-line:hover .toggle-line-inner { - background: #898989; + background: #c5c5c5; } diff --git a/src/librustdoc/html/static/css/themes/light.css b/src/librustdoc/html/static/css/themes/light.css index dc1715b2a78..be1da8df2b5 100644 --- a/src/librustdoc/html/static/css/themes/light.css +++ b/src/librustdoc/html/static/css/themes/light.css @@ -462,3 +462,33 @@ div.files > .selected { .setting-line > .title { border-bottom-color: #D5D5D5; } + +.scraped-example-list .section-header .scrape-help { + border-color: #999; + color: #999; +} +.scraped-example-list .section-header .scrape-help:hover { + border-color: black; + color: black; +} +.more-examples-toggle summary, .more-examples-toggle .hide-more { + color: #999; +} +.scraped-example .example-wrap .rust span.highlight { + background: #fcffd6; +} +.scraped-example .example-wrap .rust span.highlight.focus { + background: #f6fdb0; +} +.scraped-example:not(.expanded) .code-wrapper:before { + background: linear-gradient(to bottom, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0)); +} +.scraped-example:not(.expanded) .code-wrapper:after { + background: linear-gradient(to top, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0)); +} +.toggle-line-inner { + background: #ccc; +} +.toggle-line:hover .toggle-line-inner { + background: #999; +} diff --git a/src/librustdoc/html/static/js/scrape-examples.js b/src/librustdoc/html/static/js/scrape-examples.js index 383ae001bc2..a28fb461729 100644 --- a/src/librustdoc/html/static/js/scrape-examples.js +++ b/src/librustdoc/html/static/js/scrape-examples.js @@ -84,8 +84,10 @@ onEach(document.querySelectorAll('.more-examples-toggle'), function(toggle) { // Allow users to click the left border of the
section to close it, // since the section can be large and finding the [+] button is annoying. - toggle.querySelector('.toggle-line').addEventListener('click', function() { - toggle.open = false; + toggle.querySelectorAll('.toggle-line, .hide-more').forEach(button => { + button.addEventListener('click', function() { + toggle.open = false; + }); }); var moreExamples = toggle.querySelectorAll('.scraped-example'); diff --git a/src/test/run-make/rustdoc-scrape-examples-ordering/src/lib.rs b/src/test/run-make/rustdoc-scrape-examples-ordering/src/lib.rs index 7bd57609fa2..c53c987a7cb 100644 --- a/src/test/run-make/rustdoc-scrape-examples-ordering/src/lib.rs +++ b/src/test/run-make/rustdoc-scrape-examples-ordering/src/lib.rs @@ -1,6 +1,6 @@ // @has foobar/fn.ok.html '//*[@class="docblock scraped-example-list"]' 'ex2' // @has foobar/fn.ok.html '//*[@class="more-scraped-examples"]' 'ex1' -// @has foobar/fn.ok.html '//*[@class="highlight focus"]' '' -// @has foobar/fn.ok.html '//*[@class="highlight"]' '' +// @has foobar/fn.ok.html '//*[@class="highlight focus"]' 'ok' +// @has foobar/fn.ok.html '//*[@class="highlight"]' 'ok' pub fn ok(_x: i32) {} -- cgit 1.4.1-3-g733a5 From bb3ed6f7d6caddd3f2c26f2b5b218b2d750e8c3d Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Thu, 27 Jan 2022 18:51:34 -0800 Subject: Improve styling on scrape examples help button --- src/librustdoc/html/static/css/rustdoc.css | 3 +-- src/librustdoc/html/static/css/themes/ayu.css | 8 ++++---- src/librustdoc/html/static/css/themes/dark.css | 8 ++++---- src/librustdoc/html/static/css/themes/light.css | 4 ++-- 4 files changed, 11 insertions(+), 12 deletions(-) (limited to 'src/librustdoc/html/static/css/rustdoc.css') diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index b278ab6b24d..55a11652c23 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -2035,8 +2035,6 @@ details.rustdoc-toggle[open] > summary.hideme::after { /* Begin: styles for --scrape-examples feature */ .scraped-example-list .section-header .scrape-help { - cursor: pointer; - border-radius: 2px; margin-left: 10px; padding: 0 4px; font-weight: normal; @@ -2046,6 +2044,7 @@ details.rustdoc-toggle[open] > summary.hideme::after { background: transparent; border-width: 1px; border-style: solid; + border-radius: 50px; } .scraped-example-title { diff --git a/src/librustdoc/html/static/css/themes/ayu.css b/src/librustdoc/html/static/css/themes/ayu.css index 0fee0ba54fa..e073057528c 100644 --- a/src/librustdoc/html/static/css/themes/ayu.css +++ b/src/librustdoc/html/static/css/themes/ayu.css @@ -613,12 +613,12 @@ input:checked + .slider { .scraped-example-list .section-header .scrape-help { - border-color: #999; - color: #999; + border-color: #aaa; + color: #eee; } .scraped-example-list .section-header .scrape-help:hover { - border-color: #c5c5c5; - color: #c5c5c5; + border-color: white; + color: white; } .more-examples-toggle summary, .more-examples-toggle .hide-more { color: #999; diff --git a/src/librustdoc/html/static/css/themes/dark.css b/src/librustdoc/html/static/css/themes/dark.css index e121c984dbd..a10029b6371 100644 --- a/src/librustdoc/html/static/css/themes/dark.css +++ b/src/librustdoc/html/static/css/themes/dark.css @@ -479,12 +479,12 @@ div.files > .selected { } .scraped-example-list .section-header .scrape-help { - border-color: #999; - color: #999; + border-color: #aaa; + color: #eee; } .scraped-example-list .section-header .scrape-help:hover { - border-color: #c5c5c5; - color: #c5c5c5; + border-color: white; + color: white; } .more-examples-toggle summary, .more-examples-toggle .hide-more { color: #999; diff --git a/src/librustdoc/html/static/css/themes/light.css b/src/librustdoc/html/static/css/themes/light.css index be1da8df2b5..a30dcac66db 100644 --- a/src/librustdoc/html/static/css/themes/light.css +++ b/src/librustdoc/html/static/css/themes/light.css @@ -464,8 +464,8 @@ div.files > .selected { } .scraped-example-list .section-header .scrape-help { - border-color: #999; - color: #999; + border-color: #555; + color: #333; } .scraped-example-list .section-header .scrape-help:hover { border-color: black; -- cgit 1.4.1-3-g733a5 From d1416d528aa17623c15d51839a5cfeee7bfdbc35 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Mon, 14 Feb 2022 19:49:39 -0800 Subject: Add scrape examples help page --- src/librustdoc/html/render/context.rs | 19 ++++++++++-- src/librustdoc/html/render/mod.rs | 29 ++++++++++++++++-- src/librustdoc/html/static/css/rustdoc.css | 2 +- src/librustdoc/html/static/css/themes/ayu.css | 4 +-- src/librustdoc/html/static/css/themes/dark.css | 4 +-- src/librustdoc/html/static/css/themes/light.css | 4 +-- src/librustdoc/html/static/scrape-examples-help.md | 34 ++++++++++++++++++++++ src/librustdoc/html/static_files.rs | 2 ++ 8 files changed, 86 insertions(+), 12 deletions(-) create mode 100644 src/librustdoc/html/static/scrape-examples-help.md (limited to 'src/librustdoc/html/static/css/rustdoc.css') diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 34784bbed0c..90123655758 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -17,8 +17,8 @@ use super::print_item::{full_path, item_path, print_item}; use super::search_index::build_index; use super::write_shared::write_shared; use super::{ - collect_spans_and_sources, print_sidebar, settings, AllTypes, LinkFromSrc, NameDoc, StylePath, - BASIC_KEYWORDS, + collect_spans_and_sources, print_sidebar, scrape_examples_help, settings, AllTypes, + LinkFromSrc, NameDoc, StylePath, BASIC_KEYWORDS, }; use crate::clean::{self, types::ExternalLocation, ExternalCrate}; @@ -551,6 +551,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { let crate_name = self.tcx().crate_name(LOCAL_CRATE); let final_file = self.dst.join(crate_name.as_str()).join("all.html"); let settings_file = self.dst.join("settings.html"); + let scrape_examples_help_file = self.dst.join("scrape-examples-help.html"); let mut root_path = self.dst.to_str().expect("invalid path").to_owned(); if !root_path.ends_with('/') { @@ -606,6 +607,20 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { &self.shared.style_files, ); self.shared.fs.write(settings_file, v)?; + + if self.shared.layout.scrape_examples_extension { + page.title = "About scraped examples"; + page.description = "How the scraped examples feature works in Rustdoc"; + let v = layout::render( + &self.shared.layout, + &page, + "", + scrape_examples_help(&*self.shared), + &self.shared.style_files, + ); + self.shared.fs.write(scrape_examples_help_file, v)?; + } + if let Some(ref redirections) = self.shared.redirections { if !redirections.borrow().is_empty() { let redirect_map_path = diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index c3e2eb81e45..80a18038272 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -62,7 +62,6 @@ use rustc_span::{ use serde::ser::SerializeSeq; use serde::{Serialize, Serializer}; -use crate::clean::utils::DOC_RUST_LANG_ORG_CHANNEL; use crate::clean::{self, ItemId, RenderedLink, SelfTy}; use crate::error::Error; use crate::formats::cache::Cache; @@ -77,6 +76,7 @@ use crate::html::format::{ use crate::html::highlight; use crate::html::markdown::{HeadingOffset, IdMap, Markdown, MarkdownHtml, MarkdownSummaryLine}; use crate::html::sources; +use crate::html::static_files::SCRAPE_EXAMPLES_HELP_MD; use crate::scrape_examples::{CallData, CallLocation}; use crate::try_none; @@ -462,6 +462,29 @@ fn settings(root_path: &str, suffix: &str, theme_names: Vec) -> Result) -> String { + let content = SCRAPE_EXAMPLES_HELP_MD; + let mut ids = IdMap::default(); + format!( + "
+

\ + About scraped examples\ +

\ +
\ +
{}
", + Markdown { + content, + links: &[], + ids: &mut ids, + error_codes: shared.codes, + edition: shared.edition(), + playground: &shared.playground, + heading_offset: HeadingOffset::H1 + } + .into_string() + ) +} + fn document( w: &mut Buffer, cx: &Context<'_>, @@ -2672,9 +2695,9 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) { \
\ Examples found in repository\ - ?\ + ?\
", - doc_prefix = DOC_RUST_LANG_ORG_CHANNEL, + root_path = cx.root_path(), id = id ); diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 55a11652c23..e3c3d614f01 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -2034,7 +2034,7 @@ details.rustdoc-toggle[open] > summary.hideme::after { /* Begin: styles for --scrape-examples feature */ -.scraped-example-list .section-header .scrape-help { +.scraped-example-list .scrape-help { margin-left: 10px; padding: 0 4px; font-weight: normal; diff --git a/src/librustdoc/html/static/css/themes/ayu.css b/src/librustdoc/html/static/css/themes/ayu.css index e073057528c..b1bf06c1865 100644 --- a/src/librustdoc/html/static/css/themes/ayu.css +++ b/src/librustdoc/html/static/css/themes/ayu.css @@ -612,11 +612,11 @@ input:checked + .slider { } -.scraped-example-list .section-header .scrape-help { +.scraped-example-list .scrape-help { border-color: #aaa; color: #eee; } -.scraped-example-list .section-header .scrape-help:hover { +.scraped-example-list .scrape-help:hover { border-color: white; color: white; } diff --git a/src/librustdoc/html/static/css/themes/dark.css b/src/librustdoc/html/static/css/themes/dark.css index a10029b6371..236304ccc9f 100644 --- a/src/librustdoc/html/static/css/themes/dark.css +++ b/src/librustdoc/html/static/css/themes/dark.css @@ -478,11 +478,11 @@ div.files > .selected { border-bottom-color: #ddd; } -.scraped-example-list .section-header .scrape-help { +.scraped-example-list .scrape-help { border-color: #aaa; color: #eee; } -.scraped-example-list .section-header .scrape-help:hover { +.scraped-example-list .scrape-help:hover { border-color: white; color: white; } diff --git a/src/librustdoc/html/static/css/themes/light.css b/src/librustdoc/html/static/css/themes/light.css index a30dcac66db..c923902aba2 100644 --- a/src/librustdoc/html/static/css/themes/light.css +++ b/src/librustdoc/html/static/css/themes/light.css @@ -463,11 +463,11 @@ div.files > .selected { border-bottom-color: #D5D5D5; } -.scraped-example-list .section-header .scrape-help { +.scraped-example-list .scrape-help { border-color: #555; color: #333; } -.scraped-example-list .section-header .scrape-help:hover { +.scraped-example-list .scrape-help:hover { border-color: black; color: black; } diff --git a/src/librustdoc/html/static/scrape-examples-help.md b/src/librustdoc/html/static/scrape-examples-help.md new file mode 100644 index 00000000000..035b2e18b00 --- /dev/null +++ b/src/librustdoc/html/static/scrape-examples-help.md @@ -0,0 +1,34 @@ +Rustdoc will automatically scrape examples of documented items from the `examples/` directory of a project. These examples will be included within the generated documentation for that item. For example, if your library contains a public function: + +```rust +// src/lib.rs +pub fn a_func() {} +``` + +And you have an example calling this function: + +```rust +// examples/ex.rs +fn main() { + a_crate::a_func(); +} +``` + +Then this code snippet will be included in the documentation for `a_func`. + +## How to read scraped examples + +Scraped examples are shown as blocks of code from a given file. The relevant item will be highlighted. If the file is larger than a couple lines, only a small window will be shown which you can expand by clicking ↕ in the top-right. If a file contains multiple instances of an item, you can use the ≺ and ≻ buttons to toggle through each instance. + +If there is more than one file that contains examples, then you should click "More examples" to see these examples. + + +## How Rustdoc scrapes examples + +When you run `cargo doc`, Rustdoc will analyze all the crates that match Cargo's `--examples` filter for instances of items that occur in the crates being documented. Then Rustdoc will include the source code of these instances in the generated documentation. + +Rustdoc has a few techniques to ensure this doesn't overwhelm documentation readers, and that it doesn't blow up the page size: + +1. For a given item, a maximum of 5 examples are included in the page. The remaining examples are just links to source code. +2. Only one example is shown by default, and the remaining examples are hidden behind a toggle. +3. For a given file that contains examples, only the item containing the examples will be included in the generated documentation. diff --git a/src/librustdoc/html/static_files.rs b/src/librustdoc/html/static_files.rs index cd369a93d82..1837e4a3b65 100644 --- a/src/librustdoc/html/static_files.rs +++ b/src/librustdoc/html/static_files.rs @@ -39,6 +39,8 @@ crate static STORAGE_JS: &str = include_str!("static/js/storage.js"); /// --scrape-examples flag that inserts automatically-found examples of usages of items. crate static SCRAPE_EXAMPLES_JS: &str = include_str!("static/js/scrape-examples.js"); +crate static SCRAPE_EXAMPLES_HELP_MD: &str = include_str!("static/scrape-examples-help.md"); + /// The file contents of `brush.svg`, the icon used for the theme-switch button. crate static BRUSH_SVG: &[u8] = include_bytes!("static/images/brush.svg"); -- cgit 1.4.1-3-g733a5