Skip to content

Commit 0bd24c5

Browse files
authored
Rollup merge of #103060 - notriddle:notridddle/help-page, r=GuillaumeGomez
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. Preview: http://notriddle.com/notriddle-rustdoc-demos/help-page/std/index.html
2 parents d6506cc + 8344481 commit 0bd24c5

File tree

10 files changed

+173
-50
lines changed

10 files changed

+173
-50
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

+62-23
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,18 @@ 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;
198200
document.head.append(script);
199201
}
200202

201203
getSettingsButton().onclick = event => {
204+
if (event.ctrlKey || event.altKey || event.metaKey) {
205+
return;
206+
}
202207
addClass(getSettingsButton(), "rotate");
203208
event.preventDefault();
204209
// Sending request for the CSS and the JS files at the same time so it will
@@ -873,7 +878,10 @@ function loadCss(cssFileName) {
873878
rustdoc_version.appendChild(rustdoc_version_code);
874879

875880
const container = document.createElement("div");
876-
container.className = "popover";
881+
if (!isHelpPage) {
882+
container.className = "popover";
883+
}
884+
container.id = "help";
877885
container.style.display = "none";
878886

879887
const side_by_side = document.createElement("div");
@@ -885,15 +893,22 @@ function loadCss(cssFileName) {
885893
container.appendChild(side_by_side);
886894
container.appendChild(rustdoc_version);
887895

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;
896+
if (isHelpPage) {
897+
const help_section = document.createElement("section");
898+
help_section.appendChild(container);
899+
document.getElementById("main-content").appendChild(help_section);
900+
container.style.display = "block";
901+
} else {
902+
const help_button = getHelpButton();
903+
help_button.appendChild(container);
904+
905+
container.onblur = helpBlurHandler;
906+
container.onclick = event => {
907+
event.preventDefault();
908+
};
909+
help_button.onblur = helpBlurHandler;
910+
help_button.children[0].onblur = helpBlurHandler;
911+
}
897912

898913
return container;
899914
}
@@ -934,19 +949,43 @@ function loadCss(cssFileName) {
934949
}
935950
}
936951

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-
});
952+
if (isHelpPage) {
953+
showHelp();
954+
document.querySelector(`#${HELP_BUTTON_ID} > a`).addEventListener("click", event => {
955+
// Already on the help page, make help button a no-op.
956+
const target = event.target;
957+
if (target.tagName !== "A" ||
958+
target.parentElement.id !== HELP_BUTTON_ID ||
959+
event.ctrlKey ||
960+
event.altKey ||
961+
event.metaKey) {
962+
return;
963+
}
964+
event.preventDefault();
965+
});
966+
} else {
967+
document.querySelector(`#${HELP_BUTTON_ID} > a`).addEventListener("click", event => {
968+
// By default, have help button open docs in a popover.
969+
// If user clicks with a moderator, though, use default browser behavior,
970+
// probably opening in a new window or tab.
971+
const target = event.target;
972+
if (target.tagName !== "A" ||
973+
target.parentElement.id !== HELP_BUTTON_ID ||
974+
event.ctrlKey ||
975+
event.altKey ||
976+
event.metaKey) {
977+
return;
978+
}
979+
event.preventDefault();
980+
const menu = getHelpMenu(true);
981+
const shouldShowHelp = menu.style.display === "none";
982+
if (shouldShowHelp) {
983+
showHelp();
984+
} else {
985+
window.hidePopoverMenus();
986+
}
987+
});
988+
}
950989

951990
setMobileTopbar();
952991
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"))

0 commit comments

Comments
 (0)