Skip to content

Add --no-source option flag to rustdoc #75522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/doc/rustdoc/src/command-line-arguments.md
Original file line number Diff line number Diff line change
@@ -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
```
4 changes: 4 additions & 0 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
@@ -268,6 +268,8 @@ crate struct RenderOptions {
crate generate_redirect_map: bool,
crate unstable_features: rustc_feature::UnstableFeatures,
crate emit: Vec<EmitType>,
/// 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,
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
@@ -535,6 +535,14 @@ fn opts() -> Vec<RustcOptGroup> {
"[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.",
)
}),
]
}

6 changes: 6 additions & 0 deletions src/test/rustdoc/html_no_source_attr.rs
Original file line number Diff line number Diff line change
@@ -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]'
7 changes: 7 additions & 0 deletions src/test/rustdoc/html_no_source_flag.rs
Original file line number Diff line number Diff line change
@@ -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]'
5 changes: 5 additions & 0 deletions src/test/rustdoc/html_source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![crate_name = "foo"]

// @has src/foo/html_source.rs.html
// @has foo/index.html
// @has - '//*[@class="srclink"]' '[src]'