Skip to content

Commit 827eba4

Browse files
committed
Auto merge of #37911 - liigo:rustdoc-playground, r=alexcrichton
rustdoc: get back missing crate-name when --playground-url is used follow up PR #37763 r? @alexcrichton (since you r+ed to #37763 ) ---- Edit: When `#![doc(html_playground_url="")]` is used, the current crate name is saved to `PLAYGROUND`, so rustdoc may generate `extern crate NAME;` into code snips automatically. But when `--playground-url` was introduced in PR #37763, I forgot saving crate name to `PLAYGROUND`. This PR fix that. ---- Update: - add test - unstable `--playground-url`
2 parents 070fad1 + d5785a3 commit 827eba4

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

src/librustdoc/html/render.rs

+8
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ pub fn derive_id(candidate: String) -> String {
428428
/// Generates the documentation for `crate` into the directory `dst`
429429
pub fn run(mut krate: clean::Crate,
430430
external_html: &ExternalHtml,
431+
playground_url: Option<String>,
431432
dst: PathBuf,
432433
passes: FxHashSet<String>,
433434
css_file_extension: Option<PathBuf>,
@@ -451,6 +452,13 @@ pub fn run(mut krate: clean::Crate,
451452
css_file_extension: css_file_extension.clone(),
452453
};
453454

455+
// If user passed in `--playground-url` arg, we fill in crate name here
456+
if let Some(url) = playground_url {
457+
markdown::PLAYGROUND.with(|slot| {
458+
*slot.borrow_mut() = Some((Some(krate.name.clone()), url));
459+
});
460+
}
461+
454462
// Crawl the crate attributes looking for attributes which control how we're
455463
// going to emit HTML
456464
if let Some(attrs) = krate.module.as_ref().map(|m| &m.attrs) {

src/librustdoc/lib.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ pub fn opts() -> Vec<RustcOptGroup> {
160160
unstable(optmulti("Z", "",
161161
"internal and debugging options (only on nightly build)", "FLAG")),
162162
stable(optopt("", "sysroot", "Override the system root", "PATH")),
163-
stable(optopt("", "playground-url",
164-
"URL to send code snippets to, may be reset by --markdown-playground-url \
165-
or `#![doc(html_playground_url=...)]`",
166-
"URL")),
163+
unstable(optopt("", "playground-url",
164+
"URL to send code snippets to, may be reset by --markdown-playground-url \
165+
or `#![doc(html_playground_url=...)]`",
166+
"URL")),
167167
]
168168
}
169169

@@ -232,10 +232,6 @@ pub fn main_args(args: &[String]) -> isize {
232232
}
233233
};
234234

235-
if let Some(playground) = matches.opt_str("playground-url") {
236-
html::markdown::PLAYGROUND.with(|s| { *s.borrow_mut() = Some((None, playground)); });
237-
}
238-
239235
let test_args = matches.opt_strs("test-args");
240236
let test_args: Vec<String> = test_args.iter()
241237
.flat_map(|s| s.split_whitespace())
@@ -264,6 +260,7 @@ pub fn main_args(args: &[String]) -> isize {
264260
None => return 3
265261
};
266262
let crate_name = matches.opt_str("crate-name");
263+
let playground_url = matches.opt_str("playground-url");
267264

268265
match (should_test, markdown_input) {
269266
(true, true) => {
@@ -285,7 +282,7 @@ pub fn main_args(args: &[String]) -> isize {
285282
info!("going to format");
286283
match output_format.as_ref().map(|s| &**s) {
287284
Some("html") | None => {
288-
html::render::run(krate, &external_html,
285+
html::render::run(krate, &external_html, playground_url,
289286
output.unwrap_or(PathBuf::from("doc")),
290287
passes.into_iter().collect(),
291288
css_file_extension,

src/librustdoc/markdown.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
6363
Err(LoadStringError::ReadFail) => return 1,
6464
Err(LoadStringError::BadUtf8) => return 2,
6565
};
66-
if let Some(playground) = matches.opt_str("markdown-playground-url") {
66+
if let Some(playground) = matches.opt_str("markdown-playground-url").or(
67+
matches.opt_str("playground-url")) {
6768
markdown::PLAYGROUND.with(|s| { *s.borrow_mut() = Some((None, playground)); });
6869
}
6970

src/test/rustdoc/playground-arg.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --playground-url=https://example.com/ -Z unstable-options
12+
// ignore-tidy-linelength
13+
14+
#![crate_name = "foo"]
15+
16+
//! ```
17+
//! use foo::dummy;
18+
//! dummy();
19+
//! ```
20+
21+
pub fn dummy() {}
22+
23+
// ensure that `extern crate foo;` was inserted into code snips automatically:
24+
// @matches foo/index.html '//a[@class="test-arrow"][@href="https://example.com/?code=extern%20crate%20foo%3B%0Afn%20main()%20%7B%0Ause%20foo%3A%3Adummy%3B%0Adummy()%3B%0A%7D"]' "Run"

0 commit comments

Comments
 (0)