From 820474537f54dd9608620ee93b4df90734833204 Mon Sep 17 00:00:00 2001 From: KaiserY Date: Thu, 6 Dec 2018 22:53:14 +0800 Subject: [PATCH] ferris technology --- .travis.yml | 2 +- book.toml | 4 ++++ ferris.css | 33 +++++++++++++++++++++++++++++++++ ferris.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 ferris.css create mode 100644 ferris.js diff --git a/.travis.yml b/.travis.yml index 1ea0074..9f7930e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ dist: trusty language: rust cache: cargo rust: - - nightly + - beta # Change this to stable when Rust 1.31.0 is out branches: only: - master diff --git a/book.toml b/book.toml index f34accd..f3bcb0e 100644 --- a/book.toml +++ b/book.toml @@ -5,3 +5,7 @@ description = "Rust 程序设计语言 简体中文版" [build-dir] destination = "mdbook" + +[output.html] +additional-css = ["ferris.css"] +additional-js = ["ferris.js"] \ No newline at end of file diff --git a/ferris.css b/ferris.css new file mode 100644 index 0000000..b856d47 --- /dev/null +++ b/ferris.css @@ -0,0 +1,33 @@ +body.light .does_not_compile, +body.light .panics, +body.light .not_desired_behavior, +body.rust .does_not_compile, +body.rust .panics, +body.rust .not_desired_behavior { + background: #fff1f1; +} + +body.coal .does_not_compile, +body.coal .panics, +body.coal .not_desired_behavior, +body.navy .does_not_compile, +body.navy .panics, +body.navy .not_desired_behavior, +body.ayu .does_not_compile, +body.ayu .panics, +body.ayu .not_desired_behavior { + background: #501f21; +} + +.ferris { + position: absolute; + z-index: 99; + right: 5px; + top: 30px; + width: 10%; + height: auto; +} + +.ferris-explain { + width: 100px; +} \ No newline at end of file diff --git a/ferris.js b/ferris.js new file mode 100644 index 0000000..5d014f3 --- /dev/null +++ b/ferris.js @@ -0,0 +1,51 @@ +var ferrisTypes = [ + { + attr: 'does_not_compile', + title: 'This code does not compile!' + }, + { + attr: 'panics', + title: 'This code panics!' + }, + { + attr: 'unsafe', + title: 'This code block contains unsafe code.' + }, + { + attr: 'not_desired_behavior', + title: 'This code does not produce the desired behavior.' + } + ] + + document.addEventListener('DOMContentLoaded', () => { + for (var ferrisType of ferrisTypes) { + attachFerrises(ferrisType) + } + }) + + function attachFerrises (type) { + var elements = document.getElementsByClassName(type.attr) + + for (var codeBlock of elements) { + var lines = codeBlock.textContent.split(/\r|\r\n|\n/).length - 1; + + if (lines >= 4) { + attachFerris(codeBlock, type) + } + } + } + + function attachFerris (element, type) { + var a = document.createElement('a') + a.setAttribute('href', 'ch00-00-introduction.html#ferris') + a.setAttribute('target', '_blank') + + var img = document.createElement('img') + img.setAttribute('src', 'img/ferris/' + type.attr + '.svg') + img.setAttribute('title', type.title) + img.className = 'ferris' + + a.appendChild(img) + + element.parentElement.insertBefore(a, element) + } \ No newline at end of file