forked from Zolmeister/zethos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.mod.js
84 lines (70 loc) · 2.27 KB
/
main.mod.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
76
77
78
79
80
81
82
83
84
d()
b.innerHTML +=
"<style>body{width:600px;margin:0 auto;font-family:Courier}#o{font-size:40px;border:2px solid;width:25rem;display:inline-block}#i{overflow:auto;padding:20px;width:600px;height:350px;border:1px solid}v{color:red}</style>" +
"<br><br>"+
"<button onclick=p()>Play</button> "+
"<button onclick=pausecont()>Pause/Continue</button> "+
"<br><br>"+
"<c id=o>.</c>"+
"<br><br>"+
"<textarea id=i>Paste text here";
var playing,index,loop,parse,focus,hyphenate,str,words;
parse = function(words, str) {
// Logic
// strings will be broken out into words
// find the focus point of the word
// if, when the word is shifted to its focus point
// one end prodtrudes from either end more than 7 chars
// re-run parser after hyphenating the words
// focus point
// start in middle of word (default focus point)
// move left until you hit a vowel, then stop
// hyphenating
// if <= 7 chars
// return self
// if <= 10
// return x, {3}
// if <= 14 chars
// return {7},{7}
// else
// return {7},hyphenated{x}
hyphenate = function(words, str) {
with(words)
return length < 8 ? words : length < 11 ? slice(0,length - 3)+'- '+slice(length-3) : slice(0,7)+'- '+hyphenate(slice(7))
}
// return 2d array with word and focus point
return words.trim().replace(/([.?!])([A-Z-])/g, '$1 $2').split(/\s+/).reduce(function(words, str) {
with(str) {
// focus point
for(j=focus=(length-1)/2|0;j>=0;j--)
if (/[aeiou]/.test(str[j])) {
focus = j
break
}
var wpm = WPM || 500;
t = 60000/wpm // 500 wpm
if (length > 6)
t += t/4
if (~indexOf(','))
t += t/2
if(/[.?!]/.test(str))
t+= t*1.5
return length > 14 || length - focus > 7 ? words.concat(parse(hyphenate(str))) : words.concat([[str, focus, t]])
}
}, [])
}
p = function(words,str) {
index = 0
playing = !playing
playing && loop()
}
loop = function(words,str) {
w = parse(i.value)[index++] || p()
o.innerHTML = Array(8 - w[1]).join(' ')+w[0].slice(0,w[1])+'<v>'+w[0][w[1]]+'</v>'+w[0].slice(w[1]+1)
playing && setTimeout(loop, w[2])
}
pausecont = function() {
playing = !playing
playing && loop()
}
p()