Skip to content

Commit ae4ad9a

Browse files
committed
rustdoc: make the help button a link to a page
This allows you to open the help section in a new browser tab, which is a pretty reasonable thing to want for a documentation page.
1 parent ee1c3b3 commit ae4ad9a

File tree

10 files changed

+128
-47
lines changed

10 files changed

+128
-47
lines changed

src/librustdoc/html/markdown.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,7 @@ static DEFAULT_ID_MAP: Lazy<FxHashMap<Cow<'static, str>, usize>> = Lazy::new(||
14331433
fn init_id_map() -> FxHashMap<Cow<'static, str>, usize> {
14341434
let mut map = FxHashMap::default();
14351435
// This is the list of IDs used in Javascript.
1436+
map.insert("help".into(), 1);
14361437
map.insert("settings".into(), 1);
14371438
map.insert("not-displayed".into(), 1);
14381439
map.insert("alternative-display".into(), 1);

src/librustdoc/html/render/context.rs

+34
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
581581
let crate_name = self.tcx().crate_name(LOCAL_CRATE);
582582
let final_file = self.dst.join(crate_name.as_str()).join("all.html");
583583
let settings_file = self.dst.join("settings.html");
584+
let help_file = self.dst.join("help.html");
584585
let scrape_examples_help_file = self.dst.join("scrape-examples-help.html");
585586

586587
let mut root_path = self.dst.to_str().expect("invalid path").to_owned();
@@ -657,6 +658,39 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
657658
);
658659
shared.fs.write(settings_file, v)?;
659660

661+
// Generating help page.
662+
page.title = "Rustdoc help";
663+
page.description = "Documentation for Rustdoc";
664+
page.root_path = "./";
665+
666+
let sidebar = "<h2 class=\"location\">Help</h2><div class=\"sidebar-elems\"></div>";
667+
let v = layout::render(
668+
&shared.layout,
669+
&page,
670+
sidebar,
671+
|buf: &mut Buffer| {
672+
write!(
673+
buf,
674+
"<div class=\"main-heading\">\
675+
<h1 class=\"fqn\">Rustdoc help</h1>\
676+
<span class=\"out-of-band\">\
677+
<a id=\"back\" href=\"javascript:void(0)\" onclick=\"history.back();\">\
678+
Back\
679+
</a>\
680+
</span>\
681+
</div>\
682+
<noscript>\
683+
<section>\
684+
<p>You need to enable Javascript to use keyboard commands or search.</p>\
685+
<p>For more information, browse the <a href=\"https://doc.rust-lang.org/rustdoc/\">rustdoc handbook</a>.</p>\
686+
</section>\
687+
</noscript>",
688+
)
689+
},
690+
&shared.style_files,
691+
);
692+
shared.fs.write(help_file, v)?;
693+
660694
if shared.layout.scrape_examples_extension {
661695
page.title = "About scraped examples";
662696
page.description = "How the scraped examples feature works in Rustdoc";

src/librustdoc/html/static/css/rustdoc.css

+11-10
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ h1, h2, h3, h4, h5, h6,
199199
.out-of-band,
200200
span.since,
201201
a.srclink,
202-
#help-button > button,
202+
#help-button > a,
203203
details.rustdoc-toggle.top-doc > summary,
204204
details.rustdoc-toggle.non-exhaustive > summary,
205205
.scraped-example-title,
@@ -974,32 +974,33 @@ so that we can apply CSS-filters to change the arrow color in themes */
974974
color: var(--main-color);
975975
}
976976

977-
#help-button .popover {
977+
/* use larger max-width for help popover, but not for help.html */
978+
#help.popover {
978979
max-width: 600px;
979980
}
980981

981-
#help-button .popover::before {
982+
#help.popover::before {
982983
right: 48px;
983984
}
984985

985-
#help-button dt {
986+
#help dt {
986987
float: left;
987988
clear: left;
988989
display: block;
989990
margin-right: 0.5rem;
990991
}
991-
#help-button span.top, #help-button span.bottom {
992+
#help span.top, #help span.bottom {
992993
text-align: center;
993994
display: block;
994995
font-size: 1.125rem;
995996
}
996-
#help-button span.top {
997+
#help span.top {
997998
margin: 10px 0;
998999
border-bottom: 1px solid var(--border-color);
9991000
padding-bottom: 4px;
10001001
margin-bottom: 6px;
10011002
}
1002-
#help-button span.bottom {
1003+
#help span.bottom {
10031004
clear: both;
10041005
border-top: 1px solid var(--border-color);
10051006
}
@@ -1433,7 +1434,7 @@ h3.variant {
14331434
outline: none;
14341435
}
14351436

1436-
#settings-menu > a, #help-button > button, #copy-path {
1437+
#settings-menu > a, #help-button > a, #copy-path {
14371438
padding: 5px;
14381439
width: 33px;
14391440
border: 1px solid var(--border-color);
@@ -1442,7 +1443,7 @@ h3.variant {
14421443
line-height: 1.5;
14431444
}
14441445

1445-
#settings-menu > a, #help-button > button {
1446+
#settings-menu > a, #help-button > a {
14461447
padding: 5px;
14471448
height: 100%;
14481449
display: block;
@@ -1490,7 +1491,7 @@ input:checked + .slider {
14901491
background-color: var(--settings-input-color);
14911492
}
14921493

1493-
#help-button > button {
1494+
#help-button > a {
14941495
text-align: center;
14951496
/* Rare exception to specifying font sizes in rem. Since this is acting
14961497
as an icon, it's okay to specify their sizes in pixels. */

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ kbd {
248248
box-shadow: inset 0 -1px 0 #5c6773;
249249
}
250250

251-
#settings-menu > a, #help-button > button {
251+
#settings-menu > a, #help-button > a {
252252
color: #fff;
253253
}
254254

@@ -257,7 +257,7 @@ kbd {
257257
}
258258

259259
#settings-menu > a:hover, #settings-menu > a:focus,
260-
#help-button > button:hover, #help-button > button:focus {
260+
#help-button > a:hover, #help-button > a:focus {
261261
border-color: #e0e0e0;
262262
}
263263

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ kbd {
153153
box-shadow: inset 0 -1px 0 #c6cbd1;
154154
}
155155

156-
#settings-menu > a, #help-button > button {
156+
#settings-menu > a, #help-button > a {
157157
color: #000;
158158
}
159159

160160
#settings-menu > a:hover, #settings-menu > a:focus,
161-
#help-button > button:hover, #help-button > button:focus {
161+
#help-button > a:hover, #help-button > a:focus {
162162
border-color: #ffb900;
163163
}
164164

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,12 @@ kbd {
147147
box-shadow: inset 0 -1px 0 #c6cbd1;
148148
}
149149

150+
#settings-menu > a, #help-button > a {
151+
color: #000;
152+
}
153+
150154
#settings-menu > a:hover, #settings-menu > a:focus,
151-
#help-button > button:hover, #help-button > button:focus {
155+
#help-button > a:hover, #help-button > a:focus {
152156
border-color: #717171;
153157
}
154158

src/librustdoc/html/static/js/main.js

+40-23
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ function loadCss(cssFileName) {
192192
}
193193

194194
(function() {
195+
const isHelpPage = window.location.pathname.endsWith("/help.html");
196+
195197
function loadScript(url) {
196198
const script = document.createElement("script");
197199
script.src = url;
@@ -873,7 +875,10 @@ function loadCss(cssFileName) {
873875
rustdoc_version.appendChild(rustdoc_version_code);
874876

875877
const container = document.createElement("div");
876-
container.className = "popover";
878+
if (!isHelpPage) {
879+
container.className = "popover";
880+
}
881+
container.id = "help";
877882
container.style.display = "none";
878883

879884
const side_by_side = document.createElement("div");
@@ -885,15 +890,22 @@ function loadCss(cssFileName) {
885890
container.appendChild(side_by_side);
886891
container.appendChild(rustdoc_version);
887892

888-
const help_button = getHelpButton();
889-
help_button.appendChild(container);
890-
891-
container.onblur = helpBlurHandler;
892-
container.onclick = event => {
893-
event.preventDefault();
894-
};
895-
help_button.onblur = helpBlurHandler;
896-
help_button.children[0].onblur = helpBlurHandler;
893+
if (isHelpPage) {
894+
const help_section = document.createElement("section");
895+
help_section.appendChild(container);
896+
document.getElementById("main-content").appendChild(help_section);
897+
container.style.display = "block";
898+
} else {
899+
const help_button = getHelpButton();
900+
help_button.appendChild(container);
901+
902+
container.onblur = helpBlurHandler;
903+
container.onclick = event => {
904+
event.preventDefault();
905+
};
906+
help_button.onblur = helpBlurHandler;
907+
help_button.children[0].onblur = helpBlurHandler;
908+
}
897909

898910
return container;
899911
}
@@ -934,19 +946,24 @@ function loadCss(cssFileName) {
934946
}
935947
}
936948

937-
document.querySelector(`#${HELP_BUTTON_ID} > button`).addEventListener("click", event => {
938-
const target = event.target;
939-
if (target.tagName !== "BUTTON" || target.parentElement.id !== HELP_BUTTON_ID) {
940-
return;
941-
}
942-
const menu = getHelpMenu(true);
943-
const shouldShowHelp = menu.style.display === "none";
944-
if (shouldShowHelp) {
945-
showHelp();
946-
} else {
947-
window.hidePopoverMenus();
948-
}
949-
});
949+
if (isHelpPage) {
950+
showHelp();
951+
} else {
952+
document.querySelector(`#${HELP_BUTTON_ID} > a`).addEventListener("click", event => {
953+
const target = event.target;
954+
if (target.tagName !== "A" || target.parentElement.id !== HELP_BUTTON_ID) {
955+
return;
956+
}
957+
event.preventDefault();
958+
const menu = getHelpMenu(true);
959+
const shouldShowHelp = menu.style.display === "none";
960+
if (shouldShowHelp) {
961+
showHelp();
962+
} else {
963+
window.hidePopoverMenus();
964+
}
965+
});
966+
}
950967

951968
setMobileTopbar();
952969
addSidebarItems();

src/librustdoc/html/templates/page.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ <h2 class="location"></h2> {#- -#}
122122
placeholder="Click or press ‘S’ to search, ‘?’ for more options…" {# -#}
123123
type="search"> {#- -#}
124124
<div id="help-button" title="help" tabindex="-1"> {#- -#}
125-
<button type="button">?</button> {#- -#}
125+
<a href="{{page.root_path|safe}}help.html">?</a> {#- -#}
126126
</div> {#- -#}
127127
<div id="settings-menu" tabindex="-1"> {#- -#}
128128
<a href="{{page.root_path|safe}}settings.html" title="settings"> {#- -#}

src/test/rustdoc-gui/help-page.goml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This test ensures that opening the help page in its own tab works.
2+
goto: "file://" + |DOC_PATH| + "/help.html"
3+
size: (1000, 1000) // Try desktop size first.
4+
wait-for: "#help"
5+
assert-css: ("#help", {"display": "block"})
6+
click: "#help-button > a"
7+
assert-css: ("#help", {"display": "block"})
8+
compare-elements-property: (".sub-container", "#help", ["offsetWidth"])
9+
compare-elements-position: (".sub-container", "#help", ("x"))
10+
size: (500, 1000) // Try mobile next.
11+
assert-css: ("#help", {"display": "block"})
12+
compare-elements-property: (".sub-container", "#help", ["offsetWidth"])
13+
compare-elements-position: (".sub-container", "#help", ("x"))
14+
15+
// This test ensures that opening the help popover without switching pages works.
16+
goto: "file://" + |DOC_PATH| + "/test_docs/index.html"
17+
size: (1000, 1000) // Only supported on desktop.
18+
assert-false: "#help"
19+
click: "#help-button > a"
20+
assert-css: ("#help", {"display": "block"})
21+
click: "#help-button > a"
22+
assert-css: ("#help", {"display": "none"})
23+
compare-elements-property-false: (".sub-container", "#help", ["offsetWidth"])
24+
compare-elements-position-false: (".sub-container", "#help", ("x"))

src/test/rustdoc-gui/search-form-elements.goml

+8-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ assert-css: (
3333
{"border-color": "rgb(197, 197, 197)"},
3434
)
3535
assert-css: (
36-
"#help-button > button",
36+
"#help-button > a",
3737
{
3838
"color": "rgb(255, 255, 255)",
3939
"border-color": "rgb(92, 103, 115)",
@@ -47,7 +47,7 @@ assert-css: (
4747
)
4848
// Only "border-color" should change.
4949
assert-css: (
50-
"#help-button:hover > button",
50+
"#help-button:hover > a",
5151
{
5252
"color": "rgb(255, 255, 255)",
5353
"border-color": "rgb(224, 224, 224)",
@@ -113,7 +113,7 @@ assert-css: (
113113
{"border-color": "rgb(221, 221, 221)"},
114114
)
115115
assert-css: (
116-
"#help-button > button",
116+
"#help-button > a",
117117
{
118118
"color": "rgb(0, 0, 0)",
119119
"border-color": "rgb(224, 224, 224)",
@@ -127,7 +127,7 @@ assert-css: (
127127
)
128128
// Only "border-color" should change.
129129
assert-css: (
130-
"#help-button:hover > button",
130+
"#help-button:hover > a",
131131
{
132132
"color": "rgb(0, 0, 0)",
133133
"border-color": "rgb(255, 185, 0)",
@@ -193,7 +193,7 @@ assert-css: (
193193
{"border-color": "rgb(0, 0, 0)"},
194194
)
195195
assert-css: (
196-
"#help-button > button",
196+
"#help-button > a",
197197
{
198198
"color": "rgb(0, 0, 0)",
199199
"border-color": "rgb(224, 224, 224)",
@@ -207,7 +207,7 @@ assert-css: (
207207
)
208208
// Only "border-color" should change.
209209
assert-css: (
210-
"#help-button:hover > button",
210+
"#help-button:hover > a",
211211
{
212212
"color": "rgb(0, 0, 0)",
213213
"border-color": "rgb(113, 113, 113)",
@@ -222,7 +222,7 @@ assert-css: (
222222
assert-css: (
223223
"#settings-menu > a",
224224
{
225-
"color": "rgb(56, 115, 173)",
225+
"color": "rgb(0, 0, 0)",
226226
"border-color": "rgb(224, 224, 224)",
227227
"background-color": "rgb(255, 255, 255)",
228228
},
@@ -236,7 +236,7 @@ assert-css: (
236236
assert-css: (
237237
"#settings-menu:hover > a",
238238
{
239-
"color": "rgb(56, 115, 173)",
239+
"color": "rgb(0, 0, 0)",
240240
"border-color": "rgb(113, 113, 113)",
241241
"background-color": "rgb(255, 255, 255)",
242242
},

0 commit comments

Comments
 (0)