2015-07-29 19:34:48 +08:00
|
|
|
$( document ).ready(function() {
|
|
|
|
|
2015-09-05 23:26:17 +08:00
|
|
|
// url
|
|
|
|
var url = window.location.pathname;
|
|
|
|
|
2015-09-25 04:32:41 +08:00
|
|
|
// Fix back button cache problem
|
|
|
|
window.onunload = function(){};
|
2015-09-24 22:47:33 +08:00
|
|
|
|
2015-09-14 14:39:53 +08:00
|
|
|
// Set theme
|
|
|
|
var theme = localStorage.getItem('theme');
|
2015-12-28 23:39:14 +08:00
|
|
|
if (theme === null) { theme = 'light'; }
|
2015-09-14 14:39:53 +08:00
|
|
|
|
|
|
|
set_theme(theme);
|
|
|
|
|
2015-09-24 22:47:33 +08:00
|
|
|
|
2015-08-06 00:28:59 +08:00
|
|
|
// Syntax highlighting Configuration
|
|
|
|
hljs.configure({
|
|
|
|
tabReplace: ' ', // 4 spaces
|
|
|
|
languages: [], // Languages used for auto-detection
|
|
|
|
});
|
|
|
|
|
|
|
|
$('code').each(function(i, block) {
|
|
|
|
hljs.highlightBlock(block);
|
|
|
|
});
|
Add hljs class to all code blocks, regardless of highlighting
Fixes #179.
Highlight.js does not apply syntax highlighting to code blocks marked
no-highlight, nohighlight, plain, or text. When it finds blocks of those
languages, it does not add the `hljs` class to those code blocks either.
highlight.css and tomorrow-night.css use the `hljs` class to give code
blocks their backrgound color and text color, and we want that to apply
even if the code doesn't get syntax highlighting markup.
This is a somewhat hacky solution to get just that behavior! After this
commit, code blocks with no-highlight, nohighlight, plain, or text
language set on them will indeed get the hljs colors.
2016-11-14 10:14:00 +08:00
|
|
|
|
|
|
|
// Adding the hljs class gives code blocks the color css
|
|
|
|
// even if highlighting doesn't apply
|
|
|
|
$('code').addClass('hljs');
|
2015-08-06 00:28:59 +08:00
|
|
|
|
2015-12-30 23:30:08 +08:00
|
|
|
var KEY_CODES = {
|
|
|
|
PREVIOUS_KEY: 37,
|
|
|
|
NEXT_KEY: 39
|
|
|
|
};
|
|
|
|
|
|
|
|
$(document).on('keydown', function (e) {
|
|
|
|
switch (e.keyCode) {
|
|
|
|
case KEY_CODES.NEXT_KEY:
|
|
|
|
e.preventDefault();
|
2015-12-31 00:19:43 +08:00
|
|
|
if($('.nav-chapters.next').length) {
|
|
|
|
window.location.href = $('.nav-chapters.next').attr('href');
|
|
|
|
}
|
2015-12-30 23:30:08 +08:00
|
|
|
break;
|
|
|
|
case KEY_CODES.PREVIOUS_KEY:
|
|
|
|
e.preventDefault();
|
2015-12-31 00:19:43 +08:00
|
|
|
if($('.nav-chapters.previous').length) {
|
|
|
|
window.location.href = $('.nav-chapters.previous').attr('href');
|
|
|
|
}
|
2015-12-30 23:30:08 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2015-08-06 00:28:59 +08:00
|
|
|
|
2015-07-29 19:34:48 +08:00
|
|
|
// Interesting DOM Elements
|
2015-09-24 22:47:33 +08:00
|
|
|
var html = $("html");
|
2015-07-29 19:34:48 +08:00
|
|
|
var sidebar = $("#sidebar");
|
|
|
|
var page_wrapper = $("#page-wrapper");
|
2015-12-28 06:17:59 +08:00
|
|
|
var content = $("#content");
|
|
|
|
|
|
|
|
|
|
|
|
// Add anchors for all content headers
|
|
|
|
content.find("h1, h2, h3, h4, h5").wrap(function(){
|
2015-12-30 22:41:49 +08:00
|
|
|
var wrapper = $("<a class=\"header\">");
|
2016-10-21 13:02:16 +08:00
|
|
|
var header_name = $(this).text().trim().replace(/\W/g, '-')
|
|
|
|
wrapper.attr("name", header_name);
|
2016-01-01 09:17:40 +08:00
|
|
|
// Add so that when you click the link actually shows up in the url bar...
|
2016-07-31 20:18:36 +08:00
|
|
|
// Remove any existing anchor then append the new one
|
|
|
|
// ensuring eg. no spaces are present within it ie. they become %20
|
2016-10-21 13:02:16 +08:00
|
|
|
wrapper.attr("href", $(location).attr('href').split("#")[0] + "#" + header_name );
|
2015-12-28 06:17:59 +08:00
|
|
|
return wrapper;
|
|
|
|
});
|
2015-07-29 19:34:48 +08:00
|
|
|
|
2015-09-24 22:47:33 +08:00
|
|
|
|
|
|
|
// Toggle sidebar
|
2015-07-29 19:34:48 +08:00
|
|
|
$("#sidebar-toggle").click(function(event){
|
2015-09-24 22:47:33 +08:00
|
|
|
if ( html.hasClass("sidebar-hidden") ) {
|
|
|
|
html.removeClass("sidebar-hidden").addClass("sidebar-visible");
|
|
|
|
localStorage.setItem('sidebar', 'visible');
|
|
|
|
} else if ( html.hasClass("sidebar-visible") ) {
|
|
|
|
html.removeClass("sidebar-visible").addClass("sidebar-hidden");
|
|
|
|
localStorage.setItem('sidebar', 'hidden');
|
2015-07-29 19:34:48 +08:00
|
|
|
} else {
|
2015-09-24 22:47:33 +08:00
|
|
|
if(sidebar.position().left === 0){
|
|
|
|
html.addClass("sidebar-hidden");
|
|
|
|
localStorage.setItem('sidebar', 'hidden');
|
|
|
|
} else {
|
|
|
|
html.addClass("sidebar-visible");
|
|
|
|
localStorage.setItem('sidebar', 'visible');
|
|
|
|
}
|
2015-07-29 19:34:48 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-09-24 22:47:33 +08:00
|
|
|
|
2015-12-27 11:45:50 +08:00
|
|
|
// Scroll sidebar to current active section
|
|
|
|
var activeSection = sidebar.find(".active");
|
|
|
|
if(activeSection.length) {
|
|
|
|
sidebar.scrollTop(activeSection.offset().top);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-05 23:26:17 +08:00
|
|
|
// Print button
|
|
|
|
$("#print-button").click(function(){
|
|
|
|
var printWindow = window.open("print.html");
|
|
|
|
});
|
|
|
|
|
|
|
|
if( url.substring(url.lastIndexOf('/')+1) == "print.html" ) {
|
|
|
|
window.print();
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:47:33 +08:00
|
|
|
|
2015-09-11 07:16:29 +08:00
|
|
|
// Theme button
|
|
|
|
$("#theme-toggle").click(function(){
|
|
|
|
if($('.theme-popup').length) {
|
|
|
|
$('.theme-popup').remove();
|
|
|
|
} else {
|
|
|
|
var popup = $('<div class="theme-popup"></div>')
|
2016-02-04 00:55:19 +08:00
|
|
|
.append($('<div class="theme" id="light">Light <span class="default">(default)</span><div>'))
|
2015-09-14 17:25:42 +08:00
|
|
|
.append($('<div class="theme" id="rust">Rust</div>'))
|
2015-09-14 14:39:53 +08:00
|
|
|
.append($('<div class="theme" id="coal">Coal</div>'))
|
|
|
|
.append($('<div class="theme" id="navy">Navy</div>'));
|
2015-09-11 07:16:29 +08:00
|
|
|
|
|
|
|
|
2016-02-04 00:55:19 +08:00
|
|
|
popup.insertAfter(this);
|
2015-09-11 07:16:29 +08:00
|
|
|
|
|
|
|
$('.theme').click(function(){
|
|
|
|
var theme = $(this).attr('id');
|
2015-09-14 14:39:53 +08:00
|
|
|
|
2015-12-28 23:39:14 +08:00
|
|
|
set_theme(theme);
|
2015-09-11 07:16:29 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-09-14 14:39:53 +08:00
|
|
|
function set_theme(theme) {
|
|
|
|
if (theme == 'coal' || theme == 'navy') {
|
2017-01-14 16:46:01 +08:00
|
|
|
$("[href='css/tomorrow-night.css']").prop('disabled', false);
|
|
|
|
$("[href='css/highlight.css']").prop('disabled', true);
|
2015-09-14 14:39:53 +08:00
|
|
|
} else {
|
2017-01-14 16:46:01 +08:00
|
|
|
$("[href='css/tomorrow-night.css']").prop('disabled', true);
|
|
|
|
$("[href='css/highlight.css']").prop('disabled', false);
|
2015-09-14 14:39:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
localStorage.setItem('theme', theme);
|
|
|
|
|
|
|
|
$('body').removeClass().addClass(theme);
|
|
|
|
}
|
2015-12-29 06:52:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Hide Rust code lines prepended with a specific character
|
|
|
|
var hiding_character = "#";
|
|
|
|
|
|
|
|
$("code.language-rust").each(function(i, block){
|
2015-12-29 20:08:25 +08:00
|
|
|
|
2016-01-01 07:32:12 +08:00
|
|
|
var code_block = $(this);
|
|
|
|
var pre_block = $(this).parent();
|
2015-12-29 20:08:25 +08:00
|
|
|
// hide lines
|
2016-01-01 07:32:12 +08:00
|
|
|
var lines = code_block.html().split("\n");
|
2015-12-29 19:26:32 +08:00
|
|
|
var first_non_hidden_line = false;
|
2015-12-29 20:08:25 +08:00
|
|
|
var lines_hidden = false;
|
2015-12-29 06:52:05 +08:00
|
|
|
|
|
|
|
for(var n = 0; n < lines.length; n++){
|
|
|
|
if($.trim(lines[n])[0] == hiding_character){
|
2015-12-29 20:08:25 +08:00
|
|
|
if(first_non_hidden_line){
|
2016-01-01 07:32:12 +08:00
|
|
|
lines[n] = "<span class=\"hidden\">" + "\n" + lines[n].replace(/(\s*)#/, "$1") + "</span>";
|
2015-12-29 20:08:25 +08:00
|
|
|
}
|
|
|
|
else {
|
2016-01-01 07:32:12 +08:00
|
|
|
lines[n] = "<span class=\"hidden\">" + lines[n].replace(/(\s*)#/, "$1") + "\n" + "</span>";
|
2015-12-29 20:08:25 +08:00
|
|
|
}
|
|
|
|
lines_hidden = true;
|
2015-12-29 19:26:32 +08:00
|
|
|
}
|
|
|
|
else if(first_non_hidden_line) {
|
|
|
|
lines[n] = "\n" + lines[n];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
first_non_hidden_line = true;
|
2015-12-29 06:52:05 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-01 07:32:12 +08:00
|
|
|
code_block.html(lines.join(""));
|
2015-12-29 20:08:25 +08:00
|
|
|
|
|
|
|
// If no lines were hidden, return
|
|
|
|
if(!lines_hidden) { return; }
|
|
|
|
|
|
|
|
// add expand button
|
2016-01-01 07:32:12 +08:00
|
|
|
pre_block.prepend("<div class=\"buttons\"><i class=\"fa fa-expand\"></i></div>");
|
2015-12-29 20:08:25 +08:00
|
|
|
|
2016-01-01 07:32:12 +08:00
|
|
|
pre_block.find("i").click(function(e){
|
2015-12-29 20:08:25 +08:00
|
|
|
if( $(this).hasClass("fa-expand") ) {
|
|
|
|
$(this).removeClass("fa-expand").addClass("fa-compress");
|
2016-01-01 07:32:12 +08:00
|
|
|
pre_block.find("span.hidden").removeClass("hidden").addClass("unhidden");
|
2015-12-29 20:08:25 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$(this).removeClass("fa-compress").addClass("fa-expand");
|
2016-01-01 07:32:12 +08:00
|
|
|
pre_block.find("span.unhidden").removeClass("unhidden").addClass("hidden");
|
2015-12-29 20:08:25 +08:00
|
|
|
}
|
|
|
|
});
|
2015-12-29 06:52:05 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-01-01 07:32:12 +08:00
|
|
|
// Process playpen code blocks
|
|
|
|
$(".playpen").each(function(block){
|
|
|
|
var pre_block = $(this);
|
|
|
|
// Add play button
|
|
|
|
var buttons = pre_block.find(".buttons");
|
|
|
|
if( buttons.length === 0 ) {
|
|
|
|
pre_block.prepend("<div class=\"buttons\"></div>");
|
|
|
|
buttons = pre_block.find(".buttons");
|
|
|
|
}
|
|
|
|
buttons.prepend("<i class=\"fa fa-play play-button\"></i>");
|
|
|
|
|
|
|
|
buttons.find(".play-button").click(function(e){
|
|
|
|
run_rust_code(pre_block);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-07-29 19:34:48 +08:00
|
|
|
});
|
2016-01-01 07:32:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
function run_rust_code(code_block) {
|
|
|
|
var result_block = code_block.find(".result");
|
|
|
|
if(result_block.length === 0) {
|
|
|
|
code_block.append("<code class=\"result hljs language-bash\"></code>");
|
|
|
|
result_block = code_block.find(".result");
|
|
|
|
}
|
|
|
|
|
|
|
|
result_block.text("Running...");
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: "https://play.rust-lang.org/evaluate.json",
|
|
|
|
method: "POST",
|
|
|
|
crossDomain: true,
|
|
|
|
dataType: "json",
|
|
|
|
contentType: "application/json",
|
|
|
|
data: JSON.stringify({version: "stable", optimize: "0", code: code_block.find(".language-rust").text() }),
|
|
|
|
success: function(response){
|
|
|
|
result_block.text(response.result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|