forked from brbcoding/Readability
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
205 lines (180 loc) · 5.79 KB
/
index.html
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
<link href='https://fonts.googleapis.com/css?family=Cardo:400,400italic,700' rel='stylesheet' type='text/css'>
<style>
* { box-sizing: border-box; }
body { background: #efefef; }
#demo {
width: 50%;
margin: 0 auto;
padding-right: 1em;
}
#demo h2 {
text-align: center;
font-family: Arial;
font-size: 8vw;
margin-bottom: 1vw;
margin-top: 1vw;
font-family: 'Cardo', serif;
}
#demo textarea {
border: none;
border-radius: 4px;
width: 100%;
min-height: 20vh;
font-size: 1.25em;
margin-bottom: 1.83em;
font-family: 'Cardo', serif;
padding: 1em;
}
.output-container {
border-bottom: 1px solid rgba(0, 0, 0, 0.1);
margin-bottom: 0.75em;
}
.output-container:hover > .output-value {
color: #2BA4FF;
}
.output-label, .output-value {
display: inline-block;
width: 50%;
font-family: 'Cardo', serif;
font-size: 1.25em;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.output-label {
font-style: italic;
font-family: 'Cardo', italic;
}
.output-label a {
color: #000;
text-decoration: none;
position: relative;
width: auto;
}
.output-label a:hover {
color: #2BA4FF;
}
.output-label a:after {
content: '';
position: absolute;
background: #2BA4FF;
width: 0;
left: 0;
height: 0.1em;
bottom: 0.2em;
}
.output-label a:hover:after {
transition: width 0.25s ease-in;
width: 100%;
}
.output-value {
text-align: right;
}
</style>
</head>
<body>
<div id="demo">
<h2>Readability</h2>
<textarea id="example-input"></textarea>
<div id="example-output"></div>
</div>
<script src="./build/index.js"></script>
<script>
(function init() {
var testPhrase = [
'Anyone who feels that if so many more students whom we haven’t actually admitted',
'are sitting in on the course than ones we have that the room had to be changed,',
'then probably auditors will have to be excluded, is likely to agree that the curriculum needs revision.'
].join(' ');
document.querySelector('#example-input').innerHTML = testPhrase;
updateOutput(testPhrase);
})();
function getDefaultMethods() {
return [
{
name: 'Automated Readability Index',
func: 'automatedReadability',
url: 'https://en.wikipedia.org/wiki/Automated_readability_index'
},
{
name: 'SMOG Grade (Index)',
func: 'smog',
url: 'https://en.wikipedia.org/wiki/SMOG'
},
{
name: 'Gunning Fog Index',
func: 'gunningFog',
url: 'https://en.wikipedia.org/wiki/Gunning_fog_index'
},
{
name: 'Coleman Liau',
func: 'colemanLiau',
url: 'https://en.wikipedia.org/wiki/Coleman%E2%80%93Liau_index'
},
{
name: 'Flesch-Kincaid Ease',
func: 'fleschKincaid.ease',
url: 'https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests'
},
{
name: 'Flesch-Kincaid Level',
func: 'fleschKincaid.gradeLevel',
url: 'https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests'
}
];
}
function updateOutput(testPhrase) {
var fragment = document.createDocumentFragment();
var outputElement = document.querySelector('#example-output');
while(outputElement.firstChild) outputElement.removeChild(outputElement.firstChild);
var outputContainer, outputLabel, outputValue, func, val;
var methods = getDefaultMethods();
methods.forEach(function (method) {
func = method.func.split('.');
val = func.length > 1 ?
Readability[func[0]](testPhrase)[func[1]] :
Readability[func[0]](testPhrase);
outputLabel = document.createElement('div');
outputValue = document.createElement('div');
outputLink = document.createElement('a');
outputContainer = document.createElement('div');
outputLabel.classList.add('output-label');
outputValue.classList.add('output-value');
outputContainer.classList.add('output-container');
outputLink.innerHTML = method.name;
outputLink.setAttribute('href', method.url);
outputLink.setAttribute('target', '_blank');
outputValue.innerHTML = val;
outputLabel.appendChild(outputLink);
outputContainer.appendChild(outputLabel);
outputContainer.appendChild(outputValue);
fragment.appendChild(outputContainer);
outputElement.appendChild(fragment);
});
};
var _onKeyUp = debounce(function () {
updateOutput(this.value);
}, 150, false);
document.querySelector('#example-input').addEventListener('keyup', _onKeyUp, false);
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
</script>
</body>
</html>