]> git.proxmox.com Git - rustc.git/blob - src/vendor/mdbook/src/theme/book.js
New upstream version 1.31.0+dfsg1
[rustc.git] / src / vendor / mdbook / src / theme / book.js
1 "use strict";
2
3 // Fix back button cache problem
4 window.onunload = function () { };
5
6 // Global variable, shared between modules
7 function playpen_text(playpen) {
8 let code_block = playpen.querySelector("code");
9
10 if (window.ace && code_block.classList.contains("editable")) {
11 let editor = window.ace.edit(code_block);
12 return editor.getValue();
13 } else {
14 return code_block.textContent;
15 }
16 }
17
18 (function codeSnippets() {
19 // Hide Rust code lines prepended with a specific character
20 var hiding_character = "#";
21 var request = fetch("https://play.rust-lang.org/meta/crates", {
22 headers: {
23 'Content-Type': "application/json",
24 },
25 method: 'POST',
26 mode: 'cors',
27 });
28
29 function handle_crate_list_update(playpen_block, playground_crates) {
30 // update the play buttons after receiving the response
31 update_play_button(playpen_block, playground_crates);
32
33 // and install on change listener to dynamically update ACE editors
34 if (window.ace) {
35 let code_block = playpen_block.querySelector("code");
36 if (code_block.classList.contains("editable")) {
37 let editor = window.ace.edit(code_block);
38 editor.addEventListener("change", function (e) {
39 update_play_button(playpen_block, playground_crates);
40 });
41 }
42 }
43 }
44
45 // updates the visibility of play button based on `no_run` class and
46 // used crates vs ones available on http://play.rust-lang.org
47 function update_play_button(pre_block, playground_crates) {
48 var play_button = pre_block.querySelector(".play-button");
49
50 // skip if code is `no_run`
51 if (pre_block.querySelector('code').classList.contains("no_run")) {
52 play_button.classList.add("hidden");
53 return;
54 }
55
56 // get list of `extern crate`'s from snippet
57 var txt = playpen_text(pre_block);
58 var re = /extern\s+crate\s+([a-zA-Z_0-9]+)\s*;/g;
59 var snippet_crates = [];
60 var item;
61 while (item = re.exec(txt)) {
62 snippet_crates.push(item[1]);
63 }
64
65 // check if all used crates are available on play.rust-lang.org
66 var all_available = snippet_crates.every(function (elem) {
67 return playground_crates.indexOf(elem) > -1;
68 });
69
70 if (all_available) {
71 play_button.classList.remove("hidden");
72 } else {
73 play_button.classList.add("hidden");
74 }
75 }
76
77 function run_rust_code(code_block) {
78 var result_block = code_block.querySelector(".result");
79 if (!result_block) {
80 result_block = document.createElement('code');
81 result_block.className = 'result hljs language-bash';
82
83 code_block.append(result_block);
84 }
85
86 let text = playpen_text(code_block);
87
88 var params = {
89 channel: "stable",
90 mode: "debug",
91 crateType: "bin",
92 tests: false,
93 code: text,
94 }
95
96 if (text.indexOf("#![feature") !== -1) {
97 params.channel = "nightly";
98 }
99
100 result_block.innerText = "Running...";
101
102 var request = fetch("https://play.rust-lang.org/execute", {
103 headers: {
104 'Content-Type': "application/json",
105 },
106 method: 'POST',
107 mode: 'cors',
108 body: JSON.stringify(params)
109 });
110
111 request
112 .then(function (response) { return response.json(); })
113 .then(function (response) { result_block.innerText = response.success ? response.stdout : response.stderr; })
114 .catch(function (error) { result_block.innerText = "Playground communication" + error.message; });
115 }
116
117 // Syntax highlighting Configuration
118 hljs.configure({
119 tabReplace: ' ', // 4 spaces
120 languages: [], // Languages used for auto-detection
121 });
122
123 if (window.ace) {
124 // language-rust class needs to be removed for editable
125 // blocks or highlightjs will capture events
126 Array
127 .from(document.querySelectorAll('code.editable'))
128 .forEach(function (block) { block.classList.remove('language-rust'); });
129
130 Array
131 .from(document.querySelectorAll('code:not(.editable)'))
132 .forEach(function (block) { hljs.highlightBlock(block); });
133 } else {
134 Array
135 .from(document.querySelectorAll('code'))
136 .forEach(function (block) { hljs.highlightBlock(block); });
137 }
138
139 // Adding the hljs class gives code blocks the color css
140 // even if highlighting doesn't apply
141 Array
142 .from(document.querySelectorAll('code'))
143 .forEach(function (block) { block.classList.add('hljs'); });
144
145 Array.from(document.querySelectorAll("code.language-rust")).forEach(function (block) {
146
147 var code_block = block;
148 var pre_block = block.parentNode;
149 // hide lines
150 var lines = code_block.innerHTML.split("\n");
151 var first_non_hidden_line = false;
152 var lines_hidden = false;
153
154 for (var n = 0; n < lines.length; n++) {
155 if (lines[n].trim()[0] == hiding_character) {
156 if (first_non_hidden_line) {
157 lines[n] = "<span class=\"hidden\">" + "\n" + lines[n].replace(/(\s*)# ?/, "$1") + "</span>";
158 }
159 else {
160 lines[n] = "<span class=\"hidden\">" + lines[n].replace(/(\s*)# ?/, "$1") + "\n" + "</span>";
161 }
162 lines_hidden = true;
163 }
164 else if (first_non_hidden_line) {
165 lines[n] = "\n" + lines[n];
166 }
167 else {
168 first_non_hidden_line = true;
169 }
170 }
171 code_block.innerHTML = lines.join("");
172
173 // If no lines were hidden, return
174 if (!lines_hidden) { return; }
175
176 var buttons = document.createElement('div');
177 buttons.className = 'buttons';
178 buttons.innerHTML = "<button class=\"fa fa-expand\" title=\"Show hidden lines\" aria-label=\"Show hidden lines\"></button>";
179
180 // add expand button
181 pre_block.insertBefore(buttons, pre_block.firstChild);
182
183 pre_block.querySelector('.buttons').addEventListener('click', function (e) {
184 if (e.target.classList.contains('fa-expand')) {
185 var lines = pre_block.querySelectorAll('span.hidden');
186
187 e.target.classList.remove('fa-expand');
188 e.target.classList.add('fa-compress');
189 e.target.title = 'Hide lines';
190 e.target.setAttribute('aria-label', e.target.title);
191
192 Array.from(lines).forEach(function (line) {
193 line.classList.remove('hidden');
194 line.classList.add('unhidden');
195 });
196 } else if (e.target.classList.contains('fa-compress')) {
197 var lines = pre_block.querySelectorAll('span.unhidden');
198
199 e.target.classList.remove('fa-compress');
200 e.target.classList.add('fa-expand');
201 e.target.title = 'Show hidden lines';
202 e.target.setAttribute('aria-label', e.target.title);
203
204 Array.from(lines).forEach(function (line) {
205 line.classList.remove('unhidden');
206 line.classList.add('hidden');
207 });
208 }
209 });
210 });
211
212 Array.from(document.querySelectorAll('pre code')).forEach(function (block) {
213 var pre_block = block.parentNode;
214 if (!pre_block.classList.contains('playpen')) {
215 var buttons = pre_block.querySelector(".buttons");
216 if (!buttons) {
217 buttons = document.createElement('div');
218 buttons.className = 'buttons';
219 pre_block.insertBefore(buttons, pre_block.firstChild);
220 }
221
222 var clipButton = document.createElement('button');
223 clipButton.className = 'fa fa-copy clip-button';
224 clipButton.title = 'Copy to clipboard';
225 clipButton.setAttribute('aria-label', clipButton.title);
226 clipButton.innerHTML = '<i class=\"tooltiptext\"></i>';
227
228 buttons.insertBefore(clipButton, buttons.firstChild);
229 }
230 });
231
232 // Process playpen code blocks
233 Array.from(document.querySelectorAll(".playpen")).forEach(function (pre_block) {
234 // Add play button
235 var buttons = pre_block.querySelector(".buttons");
236 if (!buttons) {
237 buttons = document.createElement('div');
238 buttons.className = 'buttons';
239 pre_block.insertBefore(buttons, pre_block.firstChild);
240 }
241
242 var runCodeButton = document.createElement('button');
243 runCodeButton.className = 'fa fa-play play-button';
244 runCodeButton.hidden = true;
245 runCodeButton.title = 'Run this code';
246 runCodeButton.setAttribute('aria-label', runCodeButton.title);
247
248 var copyCodeClipboardButton = document.createElement('button');
249 copyCodeClipboardButton.className = 'fa fa-copy clip-button';
250 copyCodeClipboardButton.innerHTML = '<i class="tooltiptext"></i>';
251 copyCodeClipboardButton.title = 'Copy to clipboard';
252 copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title);
253
254 buttons.insertBefore(runCodeButton, buttons.firstChild);
255 buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild);
256
257 runCodeButton.addEventListener('click', function (e) {
258 run_rust_code(pre_block);
259 });
260
261 let code_block = pre_block.querySelector("code");
262 if (window.ace && code_block.classList.contains("editable")) {
263 var undoChangesButton = document.createElement('button');
264 undoChangesButton.className = 'fa fa-history reset-button';
265 undoChangesButton.title = 'Undo changes';
266 undoChangesButton.setAttribute('aria-label', undoChangesButton.title);
267
268 buttons.insertBefore(undoChangesButton, buttons.firstChild);
269
270 undoChangesButton.addEventListener('click', function () {
271 let editor = window.ace.edit(code_block);
272 editor.setValue(editor.originalCode);
273 editor.clearSelection();
274 });
275 }
276 });
277
278 request
279 .then(function (response) { return response.json(); })
280 .then(function (response) {
281 // get list of crates available in the rust playground
282 let playground_crates = response.crates.map(function (item) { return item["id"]; });
283 Array.from(document.querySelectorAll(".playpen")).forEach(function (block) {
284 handle_crate_list_update(block, playground_crates);
285 });
286 });
287
288 })();
289
290 (function themes() {
291 var html = document.querySelector('html');
292 var themeToggleButton = document.getElementById('theme-toggle');
293 var themePopup = document.getElementById('theme-list');
294 var themeColorMetaTag = document.querySelector('meta[name="theme-color"]');
295 var stylesheets = {
296 ayuHighlight: document.querySelector("[href='ayu-highlight.css']"),
297 tomorrowNight: document.querySelector("[href='tomorrow-night.css']"),
298 highlight: document.querySelector("[href='highlight.css']"),
299 };
300
301 function showThemes() {
302 themePopup.style.display = 'block';
303 themeToggleButton.setAttribute('aria-expanded', true);
304 themePopup.querySelector("button#" + document.body.className).focus();
305 }
306
307 function hideThemes() {
308 themePopup.style.display = 'none';
309 themeToggleButton.setAttribute('aria-expanded', false);
310 themeToggleButton.focus();
311 }
312
313 function set_theme(theme) {
314 let ace_theme;
315
316 if (theme == 'coal' || theme == 'navy') {
317 stylesheets.ayuHighlight.disabled = true;
318 stylesheets.tomorrowNight.disabled = false;
319 stylesheets.highlight.disabled = true;
320
321 ace_theme = "ace/theme/tomorrow_night";
322 } else if (theme == 'ayu') {
323 stylesheets.ayuHighlight.disabled = false;
324 stylesheets.tomorrowNight.disabled = true;
325 stylesheets.highlight.disabled = true;
326
327 ace_theme = "ace/theme/tomorrow_night";
328 } else {
329 stylesheets.ayuHighlight.disabled = true;
330 stylesheets.tomorrowNight.disabled = true;
331 stylesheets.highlight.disabled = false;
332
333 ace_theme = "ace/theme/dawn";
334 }
335
336 setTimeout(function () {
337 themeColorMetaTag.content = getComputedStyle(document.body).backgroundColor;
338 }, 1);
339
340 if (window.ace && window.editors) {
341 window.editors.forEach(function (editor) {
342 editor.setTheme(ace_theme);
343 });
344 }
345
346 var previousTheme;
347 try { previousTheme = localStorage.getItem('mdbook-theme'); } catch (e) { }
348 if (previousTheme === null || previousTheme === undefined) { previousTheme = 'light'; }
349
350 try { localStorage.setItem('mdbook-theme', theme); } catch (e) { }
351
352 document.body.className = theme;
353 html.classList.remove(previousTheme);
354 html.classList.add(theme);
355 }
356
357 // Set theme
358 var theme;
359 try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
360 if (theme === null || theme === undefined) { theme = 'light'; }
361
362 set_theme(theme);
363
364 themeToggleButton.addEventListener('click', function () {
365 if (themePopup.style.display === 'block') {
366 hideThemes();
367 } else {
368 showThemes();
369 }
370 });
371
372 themePopup.addEventListener('click', function (e) {
373 var theme = e.target.id || e.target.parentElement.id;
374 set_theme(theme);
375 });
376
377 themePopup.addEventListener('focusout', function(e) {
378 // e.relatedTarget is null in Safari and Firefox on macOS (see workaround below)
379 if (!!e.relatedTarget && !themePopup.contains(e.relatedTarget)) {
380 hideThemes();
381 }
382 });
383
384 // Should not be needed, but it works around an issue on macOS & iOS: https://github.com/rust-lang-nursery/mdBook/issues/628
385 document.addEventListener('click', function(e) {
386 if (themePopup.style.display === 'block' && !themeToggleButton.contains(e.target) && !themePopup.contains(e.target)) {
387 hideThemes();
388 }
389 });
390
391 document.addEventListener('keydown', function (e) {
392 if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
393 if (!themePopup.contains(e.target)) { return; }
394
395 switch (e.key) {
396 case 'Escape':
397 e.preventDefault();
398 hideThemes();
399 break;
400 case 'ArrowUp':
401 e.preventDefault();
402 var li = document.activeElement.parentElement;
403 if (li && li.previousElementSibling) {
404 li.previousElementSibling.querySelector('button').focus();
405 }
406 break;
407 case 'ArrowDown':
408 e.preventDefault();
409 var li = document.activeElement.parentElement;
410 if (li && li.nextElementSibling) {
411 li.nextElementSibling.querySelector('button').focus();
412 }
413 break;
414 case 'Home':
415 e.preventDefault();
416 themePopup.querySelector('li:first-child button').focus();
417 break;
418 case 'End':
419 e.preventDefault();
420 themePopup.querySelector('li:last-child button').focus();
421 break;
422 }
423 });
424 })();
425
426 (function sidebar() {
427 var html = document.querySelector("html");
428 var sidebar = document.getElementById("sidebar");
429 var sidebarLinks = document.querySelectorAll('#sidebar a');
430 var sidebarToggleButton = document.getElementById("sidebar-toggle");
431 var firstContact = null;
432
433 function showSidebar() {
434 html.classList.remove('sidebar-hidden')
435 html.classList.add('sidebar-visible');
436 Array.from(sidebarLinks).forEach(function (link) {
437 link.setAttribute('tabIndex', 0);
438 });
439 sidebarToggleButton.setAttribute('aria-expanded', true);
440 sidebar.setAttribute('aria-hidden', false);
441 try { localStorage.setItem('mdbook-sidebar', 'visible'); } catch (e) { }
442 }
443
444 function hideSidebar() {
445 html.classList.remove('sidebar-visible')
446 html.classList.add('sidebar-hidden');
447 Array.from(sidebarLinks).forEach(function (link) {
448 link.setAttribute('tabIndex', -1);
449 });
450 sidebarToggleButton.setAttribute('aria-expanded', false);
451 sidebar.setAttribute('aria-hidden', true);
452 try { localStorage.setItem('mdbook-sidebar', 'hidden'); } catch (e) { }
453 }
454
455 // Toggle sidebar
456 sidebarToggleButton.addEventListener('click', function sidebarToggle() {
457 if (html.classList.contains("sidebar-hidden")) {
458 showSidebar();
459 } else if (html.classList.contains("sidebar-visible")) {
460 hideSidebar();
461 } else {
462 if (getComputedStyle(sidebar)['transform'] === 'none') {
463 hideSidebar();
464 } else {
465 showSidebar();
466 }
467 }
468 });
469
470 document.addEventListener('touchstart', function (e) {
471 firstContact = {
472 x: e.touches[0].clientX,
473 time: Date.now()
474 };
475 }, { passive: true });
476
477 document.addEventListener('touchmove', function (e) {
478 if (!firstContact)
479 return;
480
481 var curX = e.touches[0].clientX;
482 var xDiff = curX - firstContact.x,
483 tDiff = Date.now() - firstContact.time;
484
485 if (tDiff < 250 && Math.abs(xDiff) >= 150) {
486 if (xDiff >= 0 && firstContact.x < Math.min(document.body.clientWidth * 0.25, 300))
487 showSidebar();
488 else if (xDiff < 0 && curX < 300)
489 hideSidebar();
490
491 firstContact = null;
492 }
493 }, { passive: true });
494
495 // Scroll sidebar to current active section
496 var activeSection = sidebar.querySelector(".active");
497 if (activeSection) {
498 sidebar.scrollTop = activeSection.offsetTop;
499 }
500 })();
501
502 (function chapterNavigation() {
503 document.addEventListener('keydown', function (e) {
504 if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
505 if (window.search && window.search.hasFocus()) { return; }
506
507 switch (e.key) {
508 case 'ArrowRight':
509 e.preventDefault();
510 var nextButton = document.querySelector('.nav-chapters.next');
511 if (nextButton) {
512 window.location.href = nextButton.href;
513 }
514 break;
515 case 'ArrowLeft':
516 e.preventDefault();
517 var previousButton = document.querySelector('.nav-chapters.previous');
518 if (previousButton) {
519 window.location.href = previousButton.href;
520 }
521 break;
522 }
523 });
524 })();
525
526 (function clipboard() {
527 var clipButtons = document.querySelectorAll('.clip-button');
528
529 function hideTooltip(elem) {
530 elem.firstChild.innerText = "";
531 elem.className = 'fa fa-copy clip-button';
532 }
533
534 function showTooltip(elem, msg) {
535 elem.firstChild.innerText = msg;
536 elem.className = 'fa fa-copy tooltipped';
537 }
538
539 var clipboardSnippets = new Clipboard('.clip-button', {
540 text: function (trigger) {
541 hideTooltip(trigger);
542 let playpen = trigger.closest("pre");
543 return playpen_text(playpen);
544 }
545 });
546
547 Array.from(clipButtons).forEach(function (clipButton) {
548 clipButton.addEventListener('mouseout', function (e) {
549 hideTooltip(e.currentTarget);
550 });
551 });
552
553 clipboardSnippets.on('success', function (e) {
554 e.clearSelection();
555 showTooltip(e.trigger, "Copied!");
556 });
557
558 clipboardSnippets.on('error', function (e) {
559 showTooltip(e.trigger, "Clipboard error!");
560 });
561 })();
562
563 (function scrollToTop () {
564 var menuTitle = document.querySelector('.menu-title');
565
566 menuTitle.addEventListener('click', function () {
567 document.scrollingElement.scrollTo({ top: 0, behavior: 'smooth' });
568 });
569 })();
570
571 (function autoHideMenu() {
572 var menu = document.getElementById('menu-bar');
573
574 var previousScrollTop = document.scrollingElement.scrollTop;
575
576 document.addEventListener('scroll', function () {
577 if (menu.classList.contains('folded') && document.scrollingElement.scrollTop < previousScrollTop) {
578 menu.classList.remove('folded');
579 } else if (!menu.classList.contains('folded') && document.scrollingElement.scrollTop > previousScrollTop) {
580 menu.classList.add('folded');
581 }
582
583 if (!menu.classList.contains('bordered') && document.scrollingElement.scrollTop > 0) {
584 menu.classList.add('bordered');
585 }
586
587 if (menu.classList.contains('bordered') && document.scrollingElement.scrollTop === 0) {
588 menu.classList.remove('bordered');
589 }
590
591 previousScrollTop = document.scrollingElement.scrollTop;
592 }, { passive: true });
593 })();