Skip to content

Commit 25323ec

Browse files
committed
Move JS into a standalone file
1 parent df5e3a6 commit 25323ec

File tree

7 files changed

+124
-115
lines changed

7 files changed

+124
-115
lines changed

src/librustdoc/html/layout.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ crate struct Layout {
2222
/// If false, the `select` element to have search filtering by crates on rendered docs
2323
/// won't be generated.
2424
crate generate_search_filter: bool,
25+
/// If true, then scrape-examples.js will be included in the output HTML file
26+
crate scrape_examples_extension: bool,
2527
}
2628

2729
#[derive(Serialize)]

src/librustdoc/html/render/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
417417
krate: krate.name.to_string(),
418418
css_file_extension: extension_css,
419419
generate_search_filter,
420+
scrape_examples_extension: call_locations.len() > 0,
420421
};
421422
let mut issue_tracker_base_url = None;
422423
let mut include_sources = true;

src/librustdoc/html/render/write_shared.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ pub(super) fn write_shared(
303303
)?;
304304
}
305305

306+
if cx.shared.layout.scrape_examples_extension {
307+
write_minify("scrape-examples.js", static_files::SCRAPE_EXAMPLES_JS, cx, options)?;
308+
}
309+
306310
if let Some(ref css) = cx.shared.layout.css_file_extension {
307311
let buffer = try_err!(fs::read_to_string(css), css);
308312
// This varies based on the invocation, so it can't go through the write_minify wrapper.

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

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -979,121 +979,6 @@ function hideThemeButtonState() {
979979
onHashChange(null);
980980
window.addEventListener("hashchange", onHashChange);
981981
searchState.setup();
982-
983-
/////// --scrape-examples interactions
984-
985-
// Scroll code block to put the given code location in the middle of the viewer
986-
function scrollToLoc(elt, loc) {
987-
var wrapper = elt.querySelector(".code-wrapper");
988-
var halfHeight = wrapper.offsetHeight / 2;
989-
var lines = elt.querySelector('.line-numbers');
990-
var offsetMid = (lines.children[loc[0]].offsetTop
991-
+ lines.children[loc[1]].offsetTop) / 2;
992-
var scrollOffset = offsetMid - halfHeight;
993-
lines.scrollTo(0, scrollOffset);
994-
elt.querySelector(".rust").scrollTo(0, scrollOffset);
995-
}
996-
997-
function updateScrapedExample(example) {
998-
var locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent);
999-
var offset = parseInt(example.attributes.getNamedItem("data-offset").textContent);
1000-
1001-
var locIndex = 0;
1002-
var highlights = example.querySelectorAll('.highlight');
1003-
var link = example.querySelector('.scraped-example-title a');
1004-
addClass(highlights[0], 'focus');
1005-
if (locs.length > 1) {
1006-
// Toggle through list of examples in a given file
1007-
var onChangeLoc = function(f) {
1008-
removeClass(highlights[locIndex], 'focus');
1009-
f();
1010-
scrollToLoc(example, locs[locIndex]);
1011-
addClass(highlights[locIndex], 'focus');
1012-
1013-
var curLoc = locs[locIndex];
1014-
var minLine = curLoc[0] + offset + 1;
1015-
var maxLine = curLoc[1] + offset + 1;
1016-
1017-
var text;
1018-
var anchor;
1019-
if (minLine == maxLine) {
1020-
text = 'line ' + minLine.toString();
1021-
anchor = minLine.toString();
1022-
} else {
1023-
var range = minLine.toString() + '-' + maxLine.toString();
1024-
text = 'lines ' + range;
1025-
anchor = range;
1026-
}
1027-
1028-
var url = new URL(link.href);
1029-
url.hash = anchor;
1030-
1031-
link.href = url.toString();
1032-
link.innerHTML = text;
1033-
};
1034-
1035-
example.querySelector('.prev')
1036-
.addEventListener('click', function() {
1037-
onChangeLoc(function() {
1038-
locIndex = (locIndex - 1 + locs.length) % locs.length;
1039-
});
1040-
});
1041-
1042-
example.querySelector('.next')
1043-
.addEventListener('click', function() {
1044-
onChangeLoc(function() { locIndex = (locIndex + 1) % locs.length; });
1045-
});
1046-
} else {
1047-
// Remove buttons if there's only one example in the file
1048-
example.querySelector('.prev').remove();
1049-
example.querySelector('.next').remove();
1050-
}
1051-
1052-
var codeEl = example.querySelector('.rust');
1053-
var codeOverflows = codeEl.scrollHeight > codeEl.clientHeight;
1054-
var expandButton = example.querySelector('.expand');
1055-
if (codeOverflows) {
1056-
// If file is larger than default height, give option to expand the viewer
1057-
expandButton.addEventListener('click', function () {
1058-
if (hasClass(example, "expanded")) {
1059-
removeClass(example, "expanded");
1060-
scrollToLoc(example, locs[0]);
1061-
} else {
1062-
addClass(example, "expanded");
1063-
}
1064-
});
1065-
} else {
1066-
// Otherwise remove expansion buttons
1067-
addClass(example, 'expanded');
1068-
expandButton.remove();
1069-
}
1070-
1071-
// Start with the first example in view
1072-
scrollToLoc(example, locs[0]);
1073-
}
1074-
1075-
function updateScrapedExamples() {
1076-
var firstExamples = document.querySelectorAll('.scraped-example-list > .scraped-example');
1077-
onEach(firstExamples, updateScrapedExample);
1078-
onEach(document.querySelectorAll('.more-examples-toggle'), function(toggle) {
1079-
// Allow users to click the left border of the <details> section to close it,
1080-
// since the section can be large and finding the [+] button is annoying.
1081-
toggle.querySelector('.toggle-line').addEventListener('click', function() {
1082-
toggle.open = false;
1083-
});
1084-
1085-
var moreExamples = toggle.querySelectorAll('.scraped-example');
1086-
toggle.querySelector('summary').addEventListener('click', function() {
1087-
// Wrapping in setTimeout ensures the update happens after the elements are actually
1088-
// visible. This is necessary since updateScrapedExample calls scrollToLoc which
1089-
// depends on offsetHeight, a property that requires an element to be visible to
1090-
// compute correctly.
1091-
setTimeout(function() { onEach(moreExamples, updateScrapedExample); });
1092-
}, {once: true});
1093-
});
1094-
}
1095-
1096-
updateScrapedExamples();
1097982
}());
1098983

1099984
(function () {
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
(function () {
2+
// Scroll code block to put the given code location in the middle of the viewer
3+
function scrollToLoc(elt, loc) {
4+
var wrapper = elt.querySelector(".code-wrapper");
5+
var halfHeight = wrapper.offsetHeight / 2;
6+
var lines = elt.querySelector('.line-numbers');
7+
var offsetMid = (lines.children[loc[0]].offsetTop
8+
+ lines.children[loc[1]].offsetTop) / 2;
9+
var scrollOffset = offsetMid - halfHeight;
10+
lines.scrollTo(0, scrollOffset);
11+
elt.querySelector(".rust").scrollTo(0, scrollOffset);
12+
}
13+
14+
function updateScrapedExample(example) {
15+
var locs = JSON.parse(example.attributes.getNamedItem("data-locs").textContent);
16+
var offset = parseInt(example.attributes.getNamedItem("data-offset").textContent);
17+
18+
var locIndex = 0;
19+
var highlights = example.querySelectorAll('.highlight');
20+
var link = example.querySelector('.scraped-example-title a');
21+
addClass(highlights[0], 'focus');
22+
if (locs.length > 1) {
23+
// Toggle through list of examples in a given file
24+
var onChangeLoc = function(f) {
25+
removeClass(highlights[locIndex], 'focus');
26+
f();
27+
scrollToLoc(example, locs[locIndex]);
28+
addClass(highlights[locIndex], 'focus');
29+
30+
var curLoc = locs[locIndex];
31+
var minLine = curLoc[0] + offset + 1;
32+
var maxLine = curLoc[1] + offset + 1;
33+
34+
var text;
35+
var anchor;
36+
if (minLine == maxLine) {
37+
text = 'line ' + minLine.toString();
38+
anchor = minLine.toString();
39+
} else {
40+
var range = minLine.toString() + '-' + maxLine.toString();
41+
text = 'lines ' + range;
42+
anchor = range;
43+
}
44+
45+
var url = new URL(link.href);
46+
url.hash = anchor;
47+
48+
link.href = url.toString();
49+
link.innerHTML = text;
50+
};
51+
52+
example.querySelector('.prev')
53+
.addEventListener('click', function() {
54+
onChangeLoc(function() {
55+
locIndex = (locIndex - 1 + locs.length) % locs.length;
56+
});
57+
});
58+
59+
example.querySelector('.next')
60+
.addEventListener('click', function() {
61+
onChangeLoc(function() { locIndex = (locIndex + 1) % locs.length; });
62+
});
63+
} else {
64+
// Remove buttons if there's only one example in the file
65+
example.querySelector('.prev').remove();
66+
example.querySelector('.next').remove();
67+
}
68+
69+
var codeEl = example.querySelector('.rust');
70+
var codeOverflows = codeEl.scrollHeight > codeEl.clientHeight;
71+
var expandButton = example.querySelector('.expand');
72+
if (codeOverflows) {
73+
// If file is larger than default height, give option to expand the viewer
74+
expandButton.addEventListener('click', function () {
75+
if (hasClass(example, "expanded")) {
76+
removeClass(example, "expanded");
77+
scrollToLoc(example, locs[0]);
78+
} else {
79+
addClass(example, "expanded");
80+
}
81+
});
82+
} else {
83+
// Otherwise remove expansion buttons
84+
addClass(example, 'expanded');
85+
expandButton.remove();
86+
}
87+
88+
// Start with the first example in view
89+
scrollToLoc(example, locs[0]);
90+
}
91+
92+
var firstExamples = document.querySelectorAll('.scraped-example-list > .scraped-example');
93+
onEach(firstExamples, updateScrapedExample);
94+
onEach(document.querySelectorAll('.more-examples-toggle'), function(toggle) {
95+
// Allow users to click the left border of the <details> section to close it,
96+
// since the section can be large and finding the [+] button is annoying.
97+
toggle.querySelector('.toggle-line').addEventListener('click', function() {
98+
toggle.open = false;
99+
});
100+
101+
var moreExamples = toggle.querySelectorAll('.scraped-example');
102+
toggle.querySelector('summary').addEventListener('click', function() {
103+
// Wrapping in setTimeout ensures the update happens after the elements are actually
104+
// visible. This is necessary since updateScrapedExample calls scrollToLoc which
105+
// depends on offsetHeight, a property that requires an element to be visible to
106+
// compute correctly.
107+
setTimeout(function() { onEach(moreExamples, updateScrapedExample); });
108+
}, {once: true});
109+
});
110+
})();

src/librustdoc/html/static_files.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ crate static SETTINGS_JS: &str = include_str!("static/js/settings.js");
3535
/// Storage, used to store documentation settings.
3636
crate static STORAGE_JS: &str = include_str!("static/js/storage.js");
3737

38+
/// The file contents of `scraped-examples.js`, which contains functionality related to the
39+
/// --scrape-examples flag that inserts automatically-found examples of usages of items.
40+
crate static SCRAPE_EXAMPLES_JS: &str = include_str!("static/js/scrape-examples.js");
41+
3842
/// The file contents of `brush.svg`, the icon used for the theme-switch button.
3943
crate static BRUSH_SVG: &[u8] = include_bytes!("static/images/brush.svg");
4044

src/librustdoc/html/templates/page.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@
109109
data-search-js="{{static_root_path | safe}}search{{page.resource_suffix}}.js"> {#- -#}
110110
</div>
111111
<script src="{{static_root_path | safe}}main{{page.resource_suffix}}.js"></script> {#- -#}
112+
{%- if layout.scrape_examples_extension -%}
113+
<script src="{{static_root_path | safe}}scrape-examples{{page.resource_suffix}}.js"></script> {#- -#}
114+
{%- endif -%}
112115
{%- for script in page.static_extra_scripts -%}
113116
<script src="{{static_root_path | safe}}{{script}}.js"></script> {#- -#}
114117
{% endfor %}

0 commit comments

Comments
 (0)