Skip to content

Commit d1416d5

Browse files
committed
Add scrape examples help page
1 parent 318e457 commit d1416d5

File tree

8 files changed

+86
-12
lines changed

8 files changed

+86
-12
lines changed

src/librustdoc/html/render/context.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use super::print_item::{full_path, item_path, print_item};
1717
use super::search_index::build_index;
1818
use super::write_shared::write_shared;
1919
use super::{
20-
collect_spans_and_sources, print_sidebar, settings, AllTypes, LinkFromSrc, NameDoc, StylePath,
21-
BASIC_KEYWORDS,
20+
collect_spans_and_sources, print_sidebar, scrape_examples_help, settings, AllTypes,
21+
LinkFromSrc, NameDoc, StylePath, BASIC_KEYWORDS,
2222
};
2323

2424
use crate::clean::{self, types::ExternalLocation, ExternalCrate};
@@ -551,6 +551,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
551551
let crate_name = self.tcx().crate_name(LOCAL_CRATE);
552552
let final_file = self.dst.join(crate_name.as_str()).join("all.html");
553553
let settings_file = self.dst.join("settings.html");
554+
let scrape_examples_help_file = self.dst.join("scrape-examples-help.html");
554555

555556
let mut root_path = self.dst.to_str().expect("invalid path").to_owned();
556557
if !root_path.ends_with('/') {
@@ -606,6 +607,20 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
606607
&self.shared.style_files,
607608
);
608609
self.shared.fs.write(settings_file, v)?;
610+
611+
if self.shared.layout.scrape_examples_extension {
612+
page.title = "About scraped examples";
613+
page.description = "How the scraped examples feature works in Rustdoc";
614+
let v = layout::render(
615+
&self.shared.layout,
616+
&page,
617+
"",
618+
scrape_examples_help(&*self.shared),
619+
&self.shared.style_files,
620+
);
621+
self.shared.fs.write(scrape_examples_help_file, v)?;
622+
}
623+
609624
if let Some(ref redirections) = self.shared.redirections {
610625
if !redirections.borrow().is_empty() {
611626
let redirect_map_path =

src/librustdoc/html/render/mod.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ use rustc_span::{
6262
use serde::ser::SerializeSeq;
6363
use serde::{Serialize, Serializer};
6464

65-
use crate::clean::utils::DOC_RUST_LANG_ORG_CHANNEL;
6665
use crate::clean::{self, ItemId, RenderedLink, SelfTy};
6766
use crate::error::Error;
6867
use crate::formats::cache::Cache;
@@ -77,6 +76,7 @@ use crate::html::format::{
7776
use crate::html::highlight;
7877
use crate::html::markdown::{HeadingOffset, IdMap, Markdown, MarkdownHtml, MarkdownSummaryLine};
7978
use crate::html::sources;
79+
use crate::html::static_files::SCRAPE_EXAMPLES_HELP_MD;
8080
use crate::scrape_examples::{CallData, CallLocation};
8181
use crate::try_none;
8282

@@ -462,6 +462,29 @@ fn settings(root_path: &str, suffix: &str, theme_names: Vec<String>) -> Result<S
462462
))
463463
}
464464

465+
fn scrape_examples_help(shared: &SharedContext<'_>) -> String {
466+
let content = SCRAPE_EXAMPLES_HELP_MD;
467+
let mut ids = IdMap::default();
468+
format!(
469+
"<div class=\"main-heading\">
470+
<h1 class=\"fqn\">\
471+
<span class=\"in-band\">About scraped examples</span>\
472+
</h1>\
473+
</div>\
474+
<div>{}</div>",
475+
Markdown {
476+
content,
477+
links: &[],
478+
ids: &mut ids,
479+
error_codes: shared.codes,
480+
edition: shared.edition(),
481+
playground: &shared.playground,
482+
heading_offset: HeadingOffset::H1
483+
}
484+
.into_string()
485+
)
486+
}
487+
465488
fn document(
466489
w: &mut Buffer,
467490
cx: &Context<'_>,
@@ -2672,9 +2695,9 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) {
26722695
<span></span>\
26732696
<h5 id=\"{id}\">\
26742697
<a href=\"#{id}\">Examples found in repository</a>\
2675-
<a class=\"scrape-help\" href=\"{doc_prefix}/rustdoc/scraped-examples.html\" target=\"_blank\">?</a>\
2698+
<a class=\"scrape-help\" href=\"{root_path}scrape-examples-help.html\">?</a>\
26762699
</h5>",
2677-
doc_prefix = DOC_RUST_LANG_ORG_CHANNEL,
2700+
root_path = cx.root_path(),
26782701
id = id
26792702
);
26802703

src/librustdoc/html/static/css/rustdoc.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -2034,7 +2034,7 @@ details.rustdoc-toggle[open] > summary.hideme::after {
20342034

20352035
/* Begin: styles for --scrape-examples feature */
20362036

2037-
.scraped-example-list .section-header .scrape-help {
2037+
.scraped-example-list .scrape-help {
20382038
margin-left: 10px;
20392039
padding: 0 4px;
20402040
font-weight: normal;

src/librustdoc/html/static/css/themes/ayu.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -612,11 +612,11 @@ input:checked + .slider {
612612
}
613613

614614

615-
.scraped-example-list .section-header .scrape-help {
615+
.scraped-example-list .scrape-help {
616616
border-color: #aaa;
617617
color: #eee;
618618
}
619-
.scraped-example-list .section-header .scrape-help:hover {
619+
.scraped-example-list .scrape-help:hover {
620620
border-color: white;
621621
color: white;
622622
}

src/librustdoc/html/static/css/themes/dark.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,11 @@ div.files > .selected {
478478
border-bottom-color: #ddd;
479479
}
480480

481-
.scraped-example-list .section-header .scrape-help {
481+
.scraped-example-list .scrape-help {
482482
border-color: #aaa;
483483
color: #eee;
484484
}
485-
.scraped-example-list .section-header .scrape-help:hover {
485+
.scraped-example-list .scrape-help:hover {
486486
border-color: white;
487487
color: white;
488488
}

src/librustdoc/html/static/css/themes/light.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -463,11 +463,11 @@ div.files > .selected {
463463
border-bottom-color: #D5D5D5;
464464
}
465465

466-
.scraped-example-list .section-header .scrape-help {
466+
.scraped-example-list .scrape-help {
467467
border-color: #555;
468468
color: #333;
469469
}
470-
.scraped-example-list .section-header .scrape-help:hover {
470+
.scraped-example-list .scrape-help:hover {
471471
border-color: black;
472472
color: black;
473473
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
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:
2+
3+
```rust
4+
// src/lib.rs
5+
pub fn a_func() {}
6+
```
7+
8+
And you have an example calling this function:
9+
10+
```rust
11+
// examples/ex.rs
12+
fn main() {
13+
a_crate::a_func();
14+
}
15+
```
16+
17+
Then this code snippet will be included in the documentation for `a_func`.
18+
19+
## How to read scraped examples
20+
21+
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 &varr; in the top-right. If a file contains multiple instances of an item, you can use the &pr; and &sc; buttons to toggle through each instance.
22+
23+
If there is more than one file that contains examples, then you should click "More examples" to see these examples.
24+
25+
26+
## How Rustdoc scrapes examples
27+
28+
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.
29+
30+
Rustdoc has a few techniques to ensure this doesn't overwhelm documentation readers, and that it doesn't blow up the page size:
31+
32+
1. For a given item, a maximum of 5 examples are included in the page. The remaining examples are just links to source code.
33+
2. Only one example is shown by default, and the remaining examples are hidden behind a toggle.
34+
3. For a given file that contains examples, only the item containing the examples will be included in the generated documentation.

src/librustdoc/html/static_files.rs

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ crate static STORAGE_JS: &str = include_str!("static/js/storage.js");
3939
/// --scrape-examples flag that inserts automatically-found examples of usages of items.
4040
crate static SCRAPE_EXAMPLES_JS: &str = include_str!("static/js/scrape-examples.js");
4141

42+
crate static SCRAPE_EXAMPLES_HELP_MD: &str = include_str!("static/scrape-examples-help.md");
43+
4244
/// The file contents of `brush.svg`, the icon used for the theme-switch button.
4345
crate static BRUSH_SVG: &[u8] = include_bytes!("static/images/brush.svg");
4446

0 commit comments

Comments
 (0)