mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-01-22 23:10:20 +08:00
Shrink and move ferris when possible
This commit is contained in:
parent
54249dabc2
commit
6a007e3f1e
38
ferris.js
38
ferris.js
@ -7,10 +7,6 @@ var ferrisTypes = [
|
|||||||
attr: 'panics',
|
attr: 'panics',
|
||||||
title: 'This code panics!'
|
title: 'This code panics!'
|
||||||
},
|
},
|
||||||
{
|
|
||||||
attr: 'unsafe',
|
|
||||||
title: 'This code block contains unsafe code.'
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
attr: 'not_desired_behavior',
|
attr: 'not_desired_behavior',
|
||||||
title: 'This code does not produce the desired behavior.'
|
title: 'This code does not produce the desired behavior.'
|
||||||
@ -23,19 +19,36 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
function attachFerrises (type) {
|
function attachFerrises(type) {
|
||||||
var elements = document.getElementsByClassName(type.attr)
|
var elements = document.getElementsByClassName(type.attr)
|
||||||
|
|
||||||
for (var codeBlock of elements) {
|
for (var codeBlock of elements) {
|
||||||
var lines = codeBlock.textContent.split(/\r|\r\n|\n/).length - 1;
|
var lines = codeBlock.innerText.replace(/\n$/, '').split(/\n/).length
|
||||||
|
var size = 'large'
|
||||||
if (lines >= 4) {
|
if (lines < 4) {
|
||||||
attachFerris(codeBlock, type)
|
size = 'small'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var container = prepareFerrisContainer(codeBlock, size == 'small')
|
||||||
|
container.appendChild(createFerris(type, size))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function attachFerris (element, type) {
|
function prepareFerrisContainer(element, useButtons) {
|
||||||
|
var foundButtons = element.parentElement.querySelector('.buttons')
|
||||||
|
if (useButtons && foundButtons) {
|
||||||
|
return foundButtons
|
||||||
|
}
|
||||||
|
|
||||||
|
var div = document.createElement('div')
|
||||||
|
div.classList.add('ferris-container')
|
||||||
|
|
||||||
|
element.parentElement.insertBefore(div, element)
|
||||||
|
|
||||||
|
return div
|
||||||
|
}
|
||||||
|
|
||||||
|
function createFerris(type, size) {
|
||||||
var a = document.createElement('a')
|
var a = document.createElement('a')
|
||||||
a.setAttribute('href', 'ch00-00-introduction.html#ferris')
|
a.setAttribute('href', 'ch00-00-introduction.html#ferris')
|
||||||
a.setAttribute('target', '_blank')
|
a.setAttribute('target', '_blank')
|
||||||
@ -43,9 +56,10 @@ function attachFerris (element, type) {
|
|||||||
var img = document.createElement('img')
|
var img = document.createElement('img')
|
||||||
img.setAttribute('src', 'img/ferris/' + type.attr + '.svg')
|
img.setAttribute('src', 'img/ferris/' + type.attr + '.svg')
|
||||||
img.setAttribute('title', type.title)
|
img.setAttribute('title', type.title)
|
||||||
img.className = 'ferris'
|
img.classList.add('ferris')
|
||||||
|
img.classList.add('ferris-' + size)
|
||||||
|
|
||||||
a.appendChild(img)
|
a.appendChild(img)
|
||||||
|
|
||||||
element.parentElement.insertBefore(a, element)
|
return a
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user