Skip to content

Commit dbcf689

Browse files
Add no-crate filter option on rustdoc
1 parent 50f3d6e commit dbcf689

File tree

9 files changed

+78
-19
lines changed

9 files changed

+78
-19
lines changed

src/librustdoc/config.rs

+5
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ pub struct RenderOptions {
195195
/// If present, playground URL to use in the "Run" button added to code samples generated from
196196
/// standalone Markdown files. If not present, `playground_url` is used.
197197
pub markdown_playground_url: Option<String>,
198+
/// If false, the `select` element to have search filtering by crates on rendered docs
199+
/// won't be generated.
200+
pub generate_search_filter: bool,
198201
}
199202

200203
impl Options {
@@ -437,6 +440,7 @@ impl Options {
437440
let crate_version = matches.opt_str("crate-version");
438441
let enable_index_page = matches.opt_present("enable-index-page") || index_page.is_some();
439442
let static_root_path = matches.opt_str("static-root-path");
443+
let generate_search_filter = !matches.opt_present("disable-per-crate-search");
440444

441445
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);
442446

@@ -479,6 +483,7 @@ impl Options {
479483
markdown_no_toc,
480484
markdown_css,
481485
markdown_playground_url,
486+
generate_search_filter,
482487
}
483488
})
484489
}

src/librustdoc/html/layout.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@ pub struct Page<'a> {
3535
}
3636

3737
pub fn render<T: fmt::Display, S: fmt::Display>(
38-
dst: &mut dyn io::Write, layout: &Layout, page: &Page, sidebar: &S, t: &T,
39-
css_file_extension: bool, themes: &[PathBuf])
40-
-> io::Result<()>
41-
{
38+
dst: &mut dyn io::Write,
39+
layout: &Layout,
40+
page: &Page,
41+
sidebar: &S,
42+
t: &T,
43+
css_file_extension: bool,
44+
themes: &[PathBuf],
45+
generate_search_filter: bool,
46+
) -> io::Result<()> {
4247
let static_root_path = page.static_root_path.unwrap_or(page.root_path);
4348
write!(dst,
4449
"<!DOCTYPE html>\
@@ -91,10 +96,7 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
9196
<nav class=\"sub\">\
9297
<form class=\"search-form js-only\">\
9398
<div class=\"search-container\">\
94-
<div>\
95-
<select id=\"crate-search\">\
96-
<option value=\"All crates\">All crates</option>\
97-
</select>\
99+
<div>{filter_crates}\
98100
<input class=\"search-input\" name=\"search\" \
99101
autocomplete=\"off\" \
100102
spellcheck=\"false\" \
@@ -224,6 +226,13 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
224226
root_path=page.root_path,
225227
extra_script=e)
226228
}).collect::<String>(),
229+
filter_crates=if generate_search_filter {
230+
"<select id=\"crate-search\">\
231+
<option value=\"All crates\">All crates</option>\
232+
</select>"
233+
} else {
234+
""
235+
},
227236
)
228237
}
229238

src/librustdoc/html/render.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ struct SharedContext {
143143
/// Optional path string to be used to load static files on output pages. If not set, uses
144144
/// combinations of `../` to reach the documentation root.
145145
pub static_root_path: Option<String>,
146+
/// If false, the `select` element to have search filtering by crates on rendered docs
147+
/// won't be generated.
148+
pub generate_search_filter: bool,
146149
}
147150

148151
impl SharedContext {
@@ -510,6 +513,7 @@ pub fn run(mut krate: clean::Crate,
510513
extern_html_root_urls,
511514
resource_suffix,
512515
static_root_path,
516+
generate_search_filter,
513517
..
514518
} = options;
515519

@@ -538,6 +542,7 @@ pub fn run(mut krate: clean::Crate,
538542
themes,
539543
resource_suffix,
540544
static_root_path,
545+
generate_search_filter,
541546
};
542547

543548
// If user passed in `--playground-url` arg, we fill in crate name here
@@ -1109,7 +1114,8 @@ themePicker.onblur = handleThemeButtonsBlur;
11091114
try_err!(layout::render(&mut w, &cx.shared.layout,
11101115
&page, &(""), &content,
11111116
cx.shared.css_file_extension.is_some(),
1112-
&cx.shared.themes), &dst);
1117+
&cx.shared.themes,
1118+
cx.shared.generate_search_filter), &dst);
11131119
try_err!(w.flush(), &dst);
11141120
}
11151121
}
@@ -1384,7 +1390,8 @@ impl<'a> SourceCollector<'a> {
13841390
layout::render(&mut w, &self.scx.layout,
13851391
&page, &(""), &Source(contents),
13861392
self.scx.css_file_extension.is_some(),
1387-
&self.scx.themes)?;
1393+
&self.scx.themes,
1394+
self.scx.generate_search_filter)?;
13881395
w.flush()?;
13891396
self.scx.local_sources.insert(p.clone(), href);
13901397
Ok(())
@@ -1986,7 +1993,8 @@ impl Context {
19861993
try_err!(layout::render(&mut w, &self.shared.layout,
19871994
&page, &sidebar, &all,
19881995
self.shared.css_file_extension.is_some(),
1989-
&self.shared.themes),
1996+
&self.shared.themes,
1997+
self.shared.generate_search_filter),
19901998
&final_file);
19911999

19922000
// Generating settings page.
@@ -2006,7 +2014,8 @@ impl Context {
20062014
try_err!(layout::render(&mut w, &layout,
20072015
&page, &sidebar, &settings,
20082016
self.shared.css_file_extension.is_some(),
2009-
&themes),
2017+
&themes,
2018+
self.shared.generate_search_filter),
20102019
&settings_file);
20112020

20122021
Ok(())
@@ -2067,7 +2076,8 @@ impl Context {
20672076
&Sidebar{ cx: self, item: it },
20682077
&Item{ cx: self, item: it },
20692078
self.shared.css_file_extension.is_some(),
2070-
&self.shared.themes)?;
2079+
&self.shared.themes,
2080+
self.shared.generate_search_filter)?;
20712081
} else {
20722082
let mut url = self.root_path();
20732083
if let Some(&(ref names, ty)) = cache().paths.get(&it.def_id) {

src/librustdoc/html/static/main.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -2434,9 +2434,11 @@ if (!DOMTokenList.prototype.remove) {
24342434
return;
24352435
}
24362436
var crates_text = [];
2437-
for (var crate in crates) {
2438-
if (crates.hasOwnProperty(crate)) {
2439-
crates_text.push(crate);
2437+
if (crates.length > 1) {
2438+
for (var crate in crates) {
2439+
if (crates.hasOwnProperty(crate)) {
2440+
crates_text.push(crate);
2441+
}
24402442
}
24412443
}
24422444
crates_text.sort(function(a, b) {

src/librustdoc/html/static/rustdoc.css

+5-1
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ a {
661661
box-sizing: border-box !important;
662662
outline: none;
663663
border: none;
664-
border-radius: 0 1px 1px 0;
664+
border-radius: 1px;
665665
margin-top: 5px;
666666
padding: 10px 16px;
667667
font-size: 17px;
@@ -671,6 +671,10 @@ a {
671671
width: 100%;
672672
}
673673

674+
#crate-search + .search-input {
675+
border-radius: 0 1px 1px 0;
676+
}
677+
674678
.search-input:focus {
675679
border-radius: 2px;
676680
border: 0;

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,18 @@ a.test-arrow {
194194

195195
.search-input {
196196
color: #111;
197-
box-shadow: 1px 0 0 1px #000, 0 0 0 2px transparent;
197+
box-shadow: 0 0 0 1px #000, 0 0 0 2px transparent;
198198
background-color: #f0f0f0;
199199
}
200200

201201
.search-input:focus {
202202
border-color: #008dfd;
203203
}
204204

205+
#crate-search + .search-input {
206+
box-shadow: 1px 0 0 1px #000, 0 0 0 2px transparent;
207+
}
208+
205209
.stab.unstable { background: #FFF5D6; border-color: #FFC600; color: #404040; }
206210
.stab.internal { background: #FFB9B3; border-color: #B71C1C; color: #404040; }
207211
.stab.deprecated { background: #F3DFFF; border-color: #7F0087; color: #404040; }

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,18 @@ a.test-arrow {
195195

196196
.search-input {
197197
color: #555;
198-
box-shadow: 1px 0 0 1px #e0e0e0, 0 0 0 2px transparent;
198+
box-shadow: 0 0 0 1px #e0e0e0, 0 0 0 2px transparent;
199199
background-color: white;
200200
}
201201

202202
.search-input:focus {
203203
border-color: #66afe9;
204204
}
205205

206+
#crate-search + .search-input {
207+
box-shadow: 1px 0 0 1px #e0e0e0, 0 0 0 2px transparent;
208+
}
209+
206210
.stab.unstable { background: #FFF5D6; border-color: #FFC600; }
207211
.stab.internal { background: #FFB9B3; border-color: #B71C1C; }
208212
.stab.deprecated { background: #F3DFFF; border-color: #7F0087; }

src/librustdoc/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ fn opts() -> Vec<RustcOptGroup> {
346346
If not set, uses combinations of '../' to reach the documentation root.",
347347
"PATH")
348348
}),
349+
unstable("disable-per-crate-search", |o| {
350+
o.optflag("",
351+
"disable-per-crate-search",
352+
"disables generating the crate selector on the search box")
353+
}),
349354
]
350355
}
351356

src/test/rustdoc/no-crate-filter.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 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+
#![crate_name = "foo"]
12+
13+
// compile-flags: -Z unstable-options --disable-per-crate-search
14+
15+
// @!has 'foo/struct.Foo.html' '//*[id="crate-search"]'
16+
pub struct Foo;

0 commit comments

Comments
 (0)