Skip to content

Commit 3d339f2

Browse files
Add generate-old-style-files option to rustdoc
1 parent b3a1039 commit 3d339f2

File tree

9 files changed

+65
-2
lines changed

9 files changed

+65
-2
lines changed

src/bootstrap/doc.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ fn invoke_rustdoc(builder: &Builder, compiler: Compiler, target: Interned<String
328328
.arg("-o").arg(&out)
329329
.arg(&path)
330330
.arg("--markdown-css")
331-
.arg("../rust.css");
331+
.arg("../rust.css")
332+
.arg("--generate-redirect-pages");
332333

333334
builder.run(&mut cmd);
334335
}
@@ -503,6 +504,7 @@ impl Step for Std {
503504
cargo.arg("--")
504505
.arg("--markdown-css").arg("rust.css")
505506
.arg("--markdown-no-toc")
507+
.arg("--generate-redirect-pages")
506508
.arg("--index-page").arg(&builder.src.join("src/doc/index.md"));
507509

508510
builder.run(&mut cargo);

src/librustdoc/config.rs

+4
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ pub struct RenderOptions {
192192
/// If present, playground URL to use in the "Run" button added to code samples generated from
193193
/// standalone Markdown files. If not present, `playground_url` is used.
194194
pub markdown_playground_url: Option<String>,
195+
/// Option (disabled by default) to generate files used by RLS and some other tools.
196+
pub generate_redirect_pages: bool,
195197
}
196198

197199
impl Options {
@@ -434,6 +436,7 @@ impl Options {
434436
let markdown_playground_url = matches.opt_str("markdown-playground-url");
435437
let crate_version = matches.opt_str("crate-version");
436438
let enable_index_page = matches.opt_present("enable-index-page") || index_page.is_some();
439+
let generate_redirect_pages = matches.opt_present("generate-redirect-pages");
437440

438441
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);
439442

@@ -475,6 +478,7 @@ impl Options {
475478
markdown_no_toc,
476479
markdown_css,
477480
markdown_playground_url,
481+
generate_redirect_pages,
478482
}
479483
})
480484
}

src/librustdoc/html/render.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use std::default::Default;
4343
use std::error;
4444
use std::fmt::{self, Display, Formatter, Write as FmtWrite};
4545
use std::ffi::OsStr;
46-
use std::fs::{self, File};
46+
use std::fs::{self, File, OpenOptions};
4747
use std::io::prelude::*;
4848
use std::io::{self, BufWriter, BufReader};
4949
use std::mem;
@@ -140,6 +140,8 @@ struct SharedContext {
140140
/// Suffix to be added on resource files (if suffix is "-v2" then "light.css" becomes
141141
/// "light-v2.css").
142142
pub resource_suffix: String,
143+
/// Option disabled by default to generate files used by RLS and some other tools.
144+
pub generate_redirect_pages: bool,
143145
}
144146

145147
impl SharedContext {
@@ -506,6 +508,7 @@ pub fn run(mut krate: clean::Crate,
506508
extension_css,
507509
extern_html_root_urls,
508510
resource_suffix,
511+
generate_redirect_pages,
509512
..
510513
} = options;
511514

@@ -533,6 +536,7 @@ pub fn run(mut krate: clean::Crate,
533536
sort_modules_alphabetically,
534537
themes,
535538
resource_suffix,
539+
generate_redirect_pages,
536540
};
537541

538542
// If user passed in `--playground-url` arg, we fill in crate name here
@@ -2143,6 +2147,27 @@ impl Context {
21432147
if !self.render_redirect_pages {
21442148
all.append(full_path(self, &item), &item_type);
21452149
}
2150+
if self.shared.generate_redirect_pages {
2151+
// Redirect from a sane URL using the namespace to Rustdoc's
2152+
// URL for the page.
2153+
let redir_name = format!("{}.{}.html", name, item_type.name_space());
2154+
let redir_dst = self.dst.join(redir_name);
2155+
if let Ok(redirect_out) = OpenOptions::new().create_new(true)
2156+
.write(true)
2157+
.open(&redir_dst) {
2158+
let mut redirect_out = BufWriter::new(redirect_out);
2159+
try_err!(layout::redirect(&mut redirect_out, file_name), &redir_dst);
2160+
}
2161+
// If the item is a macro, redirect from the old macro URL (with !)
2162+
// to the new one (without).
2163+
if item_type == ItemType::Macro {
2164+
let redir_name = format!("{}.{}!.html", item_type, name);
2165+
let redir_dst = self.dst.join(redir_name);
2166+
let redirect_out = try_err!(File::create(&redir_dst), &redir_dst);
2167+
let mut redirect_out = BufWriter::new(redirect_out);
2168+
try_err!(layout::redirect(&mut redirect_out, file_name), &redir_dst);
2169+
}
2170+
}
21462171
}
21472172
}
21482173
Ok(())

src/librustdoc/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,11 @@ fn opts() -> Vec<RustcOptGroup> {
338338
"enable-index-page",
339339
"To enable generation of the index page")
340340
}),
341+
stable("generate-redirect-pages", |o| {
342+
o.optflag("",
343+
"generate-redirect-pages",
344+
"Generate extra pages to support legacy URLs and tool links")
345+
}),
341346
]
342347
}
343348

src/test/rustdoc/issue-19190.rs

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags:--generate-redirect-pages
12+
1113
use std::ops::Deref;
1214

1315
pub struct Foo;
@@ -23,6 +25,9 @@ impl Deref for Bar {
2325
fn deref(&self) -> &Foo { loop {} }
2426
}
2527

28+
// @has issue_19190/Bar.t.html
2629
// @has issue_19190/struct.Bar.html
30+
// @has - '//*[@id="foo.v"]' 'fn foo(&self)'
2731
// @has - '//*[@id="method.foo"]' 'fn foo(&self)'
32+
// @!has - '//*[@id="static_foo.v"]' 'fn static_foo()'
2833
// @!has - '//*[@id="method.static_foo"]' 'fn static_foo()'

src/test/rustdoc/issue-35169-2.rs

+7
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,17 @@ impl DerefMut for Bar {
3434
}
3535

3636
// @has issue_35169_2/struct.Bar.html
37+
// @has - '//*[@id="by_ref.v"]' 'fn by_ref(&self)'
3738
// @has - '//*[@id="method.by_ref"]' 'fn by_ref(&self)'
39+
// @has - '//*[@id="by_explicit_ref.v"]' 'fn by_explicit_ref(self: &Foo)'
3840
// @has - '//*[@id="method.by_explicit_ref"]' 'fn by_explicit_ref(self: &Foo)'
41+
// @has - '//*[@id="by_mut_ref.v"]' 'fn by_mut_ref(&mut self)'
3942
// @has - '//*[@id="method.by_mut_ref"]' 'fn by_mut_ref(&mut self)'
43+
// @has - '//*[@id="by_explicit_mut_ref.v"]' 'fn by_explicit_mut_ref(self: &mut Foo)'
4044
// @has - '//*[@id="method.by_explicit_mut_ref"]' 'fn by_explicit_mut_ref(self: &mut Foo)'
45+
// @!has - '//*[@id="by_explicit_box.v"]' 'fn by_explicit_box(self: Box<Foo>)'
4146
// @!has - '//*[@id="method.by_explicit_box"]' 'fn by_explicit_box(self: Box<Foo>)'
47+
// @!has - '//*[@id="by_explicit_self_box.v"]' 'fn by_explicit_self_box(self: Box<Self>)'
4248
// @!has - '//*[@id="method.by_explicit_self_box"]' 'fn by_explicit_self_box(self: Box<Self>)'
49+
// @!has - '//*[@id="static_foo.v"]' 'fn static_foo()'
4350
// @!has - '//*[@id="method.static_foo"]' 'fn static_foo()'

src/test/rustdoc/issue-35169.rs

+7
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ impl Deref for Bar {
2929
}
3030

3131
// @has issue_35169/struct.Bar.html
32+
// @has - '//*[@id="by_ref.v"]' 'fn by_ref(&self)'
3233
// @has - '//*[@id="method.by_ref"]' 'fn by_ref(&self)'
34+
// @has - '//*[@id="by_explicit_ref.v"]' 'fn by_explicit_ref(self: &Foo)'
3335
// @has - '//*[@id="method.by_explicit_ref"]' 'fn by_explicit_ref(self: &Foo)'
36+
// @!has - '//*[@id="by_mut_ref.v"]' 'fn by_mut_ref(&mut self)'
3437
// @!has - '//*[@id="method.by_mut_ref"]' 'fn by_mut_ref(&mut self)'
38+
// @!has - '//*[@id="by_explicit_mut_ref.v"]' 'fn by_explicit_mut_ref(self: &mut Foo)'
3539
// @!has - '//*[@id="method.by_explicit_mut_ref"]' 'fn by_explicit_mut_ref(self: &mut Foo)'
40+
// @!has - '//*[@id="by_explicit_box.v"]' 'fn by_explicit_box(self: Box<Foo>)'
3641
// @!has - '//*[@id="method.by_explicit_box"]' 'fn by_explicit_box(self: Box<Foo>)'
42+
// @!has - '//*[@id="by_explicit_self_box.v"]' 'fn by_explicit_self_box(self: Box<Self>)'
3743
// @!has - '//*[@id="method.by_explicit_self_box"]' 'fn by_explicit_self_box(self: Box<Self>)'
44+
// @!has - '//*[@id="static_foo.v"]' 'fn static_foo()'
3845
// @!has - '//*[@id="method.static_foo"]' 'fn static_foo()'

src/test/rustdoc/structfields.rs

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// compile-flags:--generate-redirect-pages
12+
13+
// @has structfields/Foo.t.html
14+
// @has - struct.Foo.html
1115
// @has structfields/struct.Foo.html
1216
pub struct Foo {
1317
// @has - //pre "pub a: ()"
@@ -22,13 +26,17 @@ pub struct Foo {
2226
pub d: usize,
2327
}
2428

29+
// @has structfields/Bar.t.html
30+
// @has - struct.Bar.html
2531
// @has structfields/struct.Bar.html
2632
pub struct Bar {
2733
// @has - //pre "pub a: ()"
2834
pub a: (),
2935
// @!has - //pre "// some fields omitted"
3036
}
3137

38+
// @has structfields/Qux.t.html
39+
// @has - enum.Qux.html
3240
// @has structfields/enum.Qux.html
3341
pub enum Qux {
3442
Quz {
File renamed without changes.

0 commit comments

Comments
 (0)