Skip to content

Commit 1ca3e90

Browse files
Generate scraped examples buttons in JS
1 parent 59d4114 commit 1ca3e90

File tree

5 files changed

+44
-38
lines changed

5 files changed

+44
-38
lines changed

Diff for: src/librustdoc/html/render/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2537,7 +2537,6 @@ fn render_call_locations<W: fmt::Write>(mut w: W, cx: &mut Context<'_>, item: &c
25372537
&cx.root_path(),
25382538
highlight::DecorationInfo(decoration_info),
25392539
sources::SourceContext::Embedded(sources::ScrapedInfo {
2540-
needs_prev_next_buttons: line_ranges.len() > 1,
25412540
needs_expansion,
25422541
offset: line_min,
25432542
name: &call_data.display_name,

Diff for: src/librustdoc/html/sources.rs

-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ where
292292

293293
pub(crate) struct ScrapedInfo<'a> {
294294
pub(crate) offset: usize,
295-
pub(crate) needs_prev_next_buttons: bool,
296295
pub(crate) name: &'a str,
297296
pub(crate) url: &'a str,
298297
pub(crate) title: &'a str,

Diff for: src/librustdoc/html/static/js/main.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1855,12 +1855,8 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
18551855
// Since the button will be added, no need to keep this listener around.
18561856
elem.removeEventListener("mouseover", addCopyButton);
18571857

1858-
// If this is a scrapped example, there will already be a "button-holder" element.
1859-
let parent = elem.querySelector(".button-holder");
1860-
if (!parent) {
1861-
parent = document.createElement("div");
1862-
parent.className = "button-holder";
1863-
}
1858+
const parent = document.createElement("div");
1859+
parent.className = "button-holder";
18641860

18651861
const runButton = elem.querySelector(".test-arrow");
18661862
if (runButton !== null) {
@@ -1876,6 +1872,12 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
18761872
copyButtonAnimation(copyButton);
18771873
});
18781874
parent.appendChild(copyButton);
1875+
1876+
if (!elem.parentElement.classList.contains("scraped-example")) {
1877+
return;
1878+
}
1879+
const scrapedWrapped = elem.parentElement;
1880+
window.updateScrapedExample(scrapedWrapped, parent);
18791881
}
18801882

18811883
function showHideCodeExampleButtons(event) {

Diff for: src/librustdoc/html/static/js/scrape-examples.js

+34-17
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,30 @@
3636
elt.querySelector(".rust").scrollTo(0, scrollOffset);
3737
}
3838

39-
function updateScrapedExample(example, isHidden) {
40-
const locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent);
39+
function createScrapeButton(parent, className, content) {
40+
const button = document.createElement("button");
41+
button.className = className;
42+
button.innerText = content;
43+
parent.insertBefore(button, parent.firstChild);
44+
return button;
45+
}
46+
47+
window.updateScrapedExample = (example, buttonHolder) => {
4148
let locIndex = 0;
4249
const highlights = Array.prototype.slice.call(example.querySelectorAll(".highlight"));
4350
const link = example.querySelector(".scraped-example-title a");
51+
let expandButton = null;
52+
53+
if (!example.classList.contains("expanded")) {
54+
expandButton = createScrapeButton(buttonHolder, "expand", "↕");
55+
}
56+
const isHidden = example.parentElement.classList.contains("more-scraped-examples");
4457

58+
const locs = example.locs;
4559
if (locs.length > 1) {
60+
const next = createScrapeButton(buttonHolder, "next", "≻");
61+
const prev = createScrapeButton(buttonHolder, "prev", "≺");
62+
4663
// Toggle through list of examples in a given file
4764
const onChangeLoc = changeIndex => {
4865
removeClass(highlights[locIndex], "focus");
@@ -57,22 +74,19 @@
5774
link.innerHTML = title;
5875
};
5976

60-
example.querySelector(".prev")
61-
.addEventListener("click", () => {
62-
onChangeLoc(() => {
63-
locIndex = (locIndex - 1 + locs.length) % locs.length;
64-
});
77+
prev.addEventListener("click", () => {
78+
onChangeLoc(() => {
79+
locIndex = (locIndex - 1 + locs.length) % locs.length;
6580
});
81+
});
6682

67-
example.querySelector(".next")
68-
.addEventListener("click", () => {
69-
onChangeLoc(() => {
70-
locIndex = (locIndex + 1) % locs.length;
71-
});
83+
next.addEventListener("click", () => {
84+
onChangeLoc(() => {
85+
locIndex = (locIndex + 1) % locs.length;
7286
});
87+
});
7388
}
7489

75-
const expandButton = example.querySelector(".expand");
7690
if (expandButton) {
7791
expandButton.addEventListener("click", () => {
7892
if (hasClass(example, "expanded")) {
@@ -83,13 +97,16 @@
8397
}
8498
});
8599
}
100+
};
86101

102+
function setupLoc(example, isHidden) {
103+
example.locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent);
87104
// Start with the first example in view
88-
scrollToLoc(example, locs[0][0], isHidden);
105+
scrollToLoc(example, example.locs[0][0], isHidden);
89106
}
90107

91108
const firstExamples = document.querySelectorAll(".scraped-example-list > .scraped-example");
92-
onEachLazy(firstExamples, el => updateScrapedExample(el, false));
109+
onEachLazy(firstExamples, el => setupLoc(el, false));
93110
onEachLazy(document.querySelectorAll(".more-examples-toggle"), toggle => {
94111
// Allow users to click the left border of the <details> section to close it,
95112
// since the section can be large and finding the [+] button is annoying.
@@ -102,11 +119,11 @@
102119
const moreExamples = toggle.querySelectorAll(".scraped-example");
103120
toggle.querySelector("summary").addEventListener("click", () => {
104121
// Wrapping in setTimeout ensures the update happens after the elements are actually
105-
// visible. This is necessary since updateScrapedExample calls scrollToLoc which
122+
// visible. This is necessary since setupLoc calls scrollToLoc which
106123
// depends on offsetHeight, a property that requires an element to be visible to
107124
// compute correctly.
108125
setTimeout(() => {
109-
onEachLazy(moreExamples, el => updateScrapedExample(el, true));
126+
onEachLazy(moreExamples, el => setupLoc(el, true));
110127
});
111128
}, {once: true});
112129
});

Diff for: src/librustdoc/html/templates/scraped_source.html

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<div class="scraped-example{% if !info.needs_expansion +%} expanded{% endif %}" data-locs="{{info.locations}}"> {# #}
22
<div class="scraped-example-title">
33
{{info.name +}} (<a href="{{info.url}}">{{info.title}}</a>) {# #}
4-
</div>
5-
<div class="example-wrap"> {# #}
4+
</div> {# #}
5+
<div class="example-wrap">
66
{# https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr
77
Do not show "1 2 3 4 5 ..." in web search results. #}
88
<div class="src-line-numbers" data-nosnippet> {# #}
@@ -18,16 +18,5 @@
1818
{{code_html|safe}}
1919
</code> {# #}
2020
</pre> {# #}
21-
{% if info.needs_prev_next_buttons || info.needs_expansion %}
22-
<div class="button-holder">
23-
{% if info.needs_prev_next_buttons %}
24-
<button class="prev">&pr;</button> {# #}
25-
<button class="next">&sc;</button>
26-
{% endif %}
27-
{% if info.needs_expansion %}
28-
<button class="expand">&varr;</button>
29-
{% endif %}
30-
</div>
31-
{% endif %}
3221
</div> {# #}
3322
</div> {# #}

0 commit comments

Comments
 (0)