From adc02e40160961154b799710dce0da0558e341f1 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 1 Apr 2021 16:52:09 +0200 Subject: [PATCH 1/3] Add --no-source option flag to rustdoc --- src/librustdoc/config.rs | 4 ++++ src/librustdoc/html/render/context.rs | 2 +- src/librustdoc/lib.rs | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index 246e0ebbb2ba0..080633af9a6e4 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -268,6 +268,8 @@ crate struct RenderOptions { crate generate_redirect_map: bool, crate unstable_features: rustc_feature::UnstableFeatures, crate emit: Vec, + /// Whether the source code should be rendered as HTML. + pub no_source: bool, } #[derive(Copy, Clone, Debug, PartialEq, Eq)] @@ -622,6 +624,7 @@ impl Options { let document_hidden = matches.opt_present("document-hidden-items"); let run_check = matches.opt_present("check"); let generate_redirect_map = matches.opt_present("generate-redirect-map"); + let no_source = matches.opt_present("no-source"); let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format); @@ -684,6 +687,7 @@ impl Options { crate_name.as_deref(), ), emit, + no_source, }, crate_name, output_format, diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 07c850a20a9a4..0d1f05d551570 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -331,7 +331,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { generate_search_filter, }; let mut issue_tracker_base_url = None; - let mut include_sources = true; + let mut include_sources = !options.no_source; // Crawl the crate attributes looking for attributes which control how we're // going to emit HTML diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 54a6fc625a615..72ccbd9d69d62 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -533,6 +533,12 @@ fn opts() -> Vec { "emit", "Comma separated list of types of output for rustdoc to emit", "[unversioned-shared-resources,toolchain-shared-resources,invocation-specific]", + stable("no-source", |o| { + o.optflag( + "", + "no-source", + "Prevent source code generation in HTML output format generation. \ + This is the same as using the `#![doc(html_no_source)]` attribute.", ) }), ] From 80443cb0a1fe2e47bd7808dfc9b8c9579ad703a2 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 1 Apr 2021 16:55:32 +0200 Subject: [PATCH 2/3] Add test for --no-source option flag --- src/test/rustdoc/html_no_source_attr.rs | 6 ++++++ src/test/rustdoc/html_no_source_flag.rs | 7 +++++++ src/test/rustdoc/html_source.rs | 5 +++++ 3 files changed, 18 insertions(+) create mode 100644 src/test/rustdoc/html_no_source_attr.rs create mode 100644 src/test/rustdoc/html_no_source_flag.rs create mode 100644 src/test/rustdoc/html_source.rs diff --git a/src/test/rustdoc/html_no_source_attr.rs b/src/test/rustdoc/html_no_source_attr.rs new file mode 100644 index 0000000000000..8c6cd013ffaf3 --- /dev/null +++ b/src/test/rustdoc/html_no_source_attr.rs @@ -0,0 +1,6 @@ +#![doc(html_no_source)] +#![crate_name = "foo"] + +// @!has src/foo/html_no_source_attr.rs.html +// @has foo/index.html +// @!has - '//*[@class="srclink"]' '[src]' diff --git a/src/test/rustdoc/html_no_source_flag.rs b/src/test/rustdoc/html_no_source_flag.rs new file mode 100644 index 0000000000000..7be74f3f62506 --- /dev/null +++ b/src/test/rustdoc/html_no_source_flag.rs @@ -0,0 +1,7 @@ +// compile-flags: --no-source + +#![crate_name = "foo"] + +// @!has src/foo/html_no_source_attr.rs.html +// @has foo/index.html +// @!has - '//*[@class="srclink"]' '[src]' diff --git a/src/test/rustdoc/html_source.rs b/src/test/rustdoc/html_source.rs new file mode 100644 index 0000000000000..f675d51dfa4df --- /dev/null +++ b/src/test/rustdoc/html_source.rs @@ -0,0 +1,5 @@ +#![crate_name = "foo"] + +// @has src/foo/html_source.rs.html +// @has foo/index.html +// @has - '//*[@class="srclink"]' '[src]' From 6b16940d358426279a76d49a5926bc87e064a4a7 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 1 Apr 2021 16:59:02 +0200 Subject: [PATCH 3/3] Add documentation for rustdoc --no-source flag --- src/doc/rustdoc/src/command-line-arguments.md | 18 ++++++++++++++++++ src/librustdoc/lib.rs | 2 ++ 2 files changed, 20 insertions(+) diff --git a/src/doc/rustdoc/src/command-line-arguments.md b/src/doc/rustdoc/src/command-line-arguments.md index 2e4016e24bc3f..4de568ded507b 100644 --- a/src/doc/rustdoc/src/command-line-arguments.md +++ b/src/doc/rustdoc/src/command-line-arguments.md @@ -417,3 +417,21 @@ This flag is **deprecated** and **has no effect**. Rustdoc only supports Rust source code and Markdown input formats. If the file ends in `.md` or `.markdown`, `rustdoc` treats it as a Markdown file. Otherwise, it assumes that the input file is Rust. + +## `--no-source`: prevent source code generation in HTML output format generation + +Using this flag looks like this: + +```bash +$ rustdoc src/lib.rs --no-source +``` + +By default, `rustdoc` renders the source code as HTML and links to it from the documentation. When +this flag is used, `rustdoc` won't render the source code. `--no-source` is equivalent to the +`#![doc(html_no_source)]` attribute: + +```rust +// lib.rs +#![doc(html_no_source)] +// your code +``` diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 72ccbd9d69d62..8530411d82ca6 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -533,6 +533,8 @@ fn opts() -> Vec { "emit", "Comma separated list of types of output for rustdoc to emit", "[unversioned-shared-resources,toolchain-shared-resources,invocation-specific]", + ) + }), stable("no-source", |o| { o.optflag( "",