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