trpl-zh-cn/ferris.js

51 lines
1.3 KiB
JavaScript
Raw Normal View History

2018-12-06 22:53:14 +08:00
var ferrisTypes = [
{
attr: 'does_not_compile',
2018-12-16 14:46:49 +08:00
title: '这些代码不能编译!'
2018-12-06 22:53:14 +08:00
},
{
attr: 'panics',
2018-12-16 14:46:49 +08:00
title: '这些代码会 panic'
2018-12-06 22:53:14 +08:00
},
{
attr: 'unsafe',
2018-12-16 14:46:49 +08:00
title: '这些代码块包含不安全unsafe代码。'
2018-12-06 22:53:14 +08:00
},
{
attr: 'not_desired_behavior',
2018-12-16 14:46:49 +08:00
title: '这些代码不会产生期望的行为。'
2018-12-06 22:53:14 +08:00
}
]
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)
}