-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathttyplayer.autoload.js
75 lines (63 loc) · 1.6 KB
/
ttyplayer.autoload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
;(function() {
var TTYPlayer = window.TTYPlayer
function toInt(x) {
var y = parseInt(x, 10)
return isNaN(y) ? null : y
}
function attr(target, name) {
return target.getAttribute(name)
}
function parseCodeBlock(src) {
var options = {}
src.split('\n').forEach(function(line) {
var mathded = line.trim().match(/^(\w+)\s*:\s*(.*)$/)
if (!mathded) return
options[mathded[1]] = mathded[2]
})
return options
}
function each(arrayLike, fun) {
for (var i = 0, len = arrayLike.length; i < len; i++) {
fun(arrayLike[i], i)
}
}
function initAll() {
var attrSrc = 'tp-src'
/**
* <div tp-src="./a.rec" tp-cols="120" tp-rows="40" ></div>
*/
each(document.querySelectorAll('[' + attrSrc + ']'), function(target) {
var cols = toInt(attr(target, 'tp-cols'))
var rows = toInt(attr(target, 'tp-rows'))
new TTYPlayer({
parent: target,
cols: cols,
rows: rows,
}).load(attr(target, attrSrc))
})
/**
* ```tty
* src: path/to/your.rec
* ```
*/
each(document.querySelectorAll('.language-tty'), function(codeBlock) {
var pre = codeBlock.parentNode
var doc = pre.parentNode
if (pre.tagName.toUpperCase() !== 'PRE') return
var options = parseCodeBlock(codeBlock.textContent)
var p = document.createElement('p')
doc.insertBefore(p, pre)
pre.style.display = 'none'
new TTYPlayer({
parent: p,
cols: toInt(options.cols),
rows: toInt(options.rows),
}).load(options.src)
})
}
if (document.readyState === 'compvare') {
initAll()
} else {
document.addEventListener('DOMContentLoaded', initAll, false)
}
})()