-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvocalcode.js
More file actions
250 lines (202 loc) · 5.7 KB
/
vocalcode.js
File metadata and controls
250 lines (202 loc) · 5.7 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
meSpeak.loadConfig( 'mespeak/mespeak_config.json' );
meSpeak.loadVoice( 'mespeak/voices/en/en-us.json' );
window.VocalCodeChunkStream = function( root ) {
var lineBreakRegex = /[\r\n]*[^\r\n]+/g;
var prefixSplitRegex = /^([\r\n]*)([ \t]*)([^\r\n]*)$/;
return {
_root: root,
_next: root,
_indent: null,
_style: null,
_pushback: [],
_highlight: null,
_findStyle: function( node ) {
while( node && !( node instanceof Element ) ) {
node = node.parentNode;
}
return node ? node.style : null;
},
highlight: function( node ) {
if ( this._highlight ) {
this._highlight.className =
this._highlight.className.replace( /\bvcHighlight\b/, '' );
}
while( node && !( node instanceof Element ) ) {
node = node.parentNode;
}
// FIXME: identifiers don't have a wrapping <span>, so we run up to the <pre>
// tag here, and don't have anything we can highlight. We should wrap such
// text nodes on a span (but keep the whitespace out of the span).
if ( node && node.tagName === 'PRE' ) {
node = null;
}
this._highlight = node;
if ( this._highlight ) {
this._highlight.className =
this._highlight.className + ' vcHighlight';
}
},
nextLeaf: function() {
while ( this._next && this._next.firstChild ) {
this._next = this._next.firstChild;
}
var current = this._next;
while ( this._next && this._next != this._root ) {
if ( this._next.nextSibling ) {
this._next = this._next.nextSibling;
break;
} else {
this._next = this._next.parentNode;
}
}
if ( this._next == this._root ) {
this._next = null; // EOF
}
return current;
},
nextLine: function() {
if ( this._pushback.length > 0 ) {
return this._pushback.shift();
}
var textNode = this.nextLeaf();
while ( textNode && !( textNode instanceof Text ) ) {
textNode = this.nextLeaf();
}
// TODO merge adjecent nodes if they have the same style
if ( textNode == null ) {
return null; // EOF
}
this._style = this._findStyle( textNode );
this.highlight( textNode );
var text = textNode.nodeValue;
// FIXME: this doesn't seem to work
text = text.replace( "\t", " " ); // 4 spaces per tab
var lines = text.match( lineBreakRegex );
if ( lines ) {
text = lines.shift();
for ( var i = lines.length-1; i >= 0; i-- ) {
this._pushback.unshift( lines[i] );
}
}
return [ text, this._style ];
},
nextFrame: function() {
var text = '';
var style = null;
var prefix;
var linebreaks;
while ( text == '' ) {
var line = this.nextLine();
if ( line == null ) {
return null; // EOF
}
text = line[0];
style = line[1];
var split = prefixSplitRegex.exec( text );
linebreaks = split[1];
prefix = split[2];
text = split[3];
if ( this._indent == null || linebreaks.length > 0 ) {
this._indent = prefix;
}
}
return [ this._indent, text, style ];
}
};
};
window.vocalcode = function( meSpeak ) {
return {
_stop: false,
_meSpeak: meSpeak,
_mangleText: function( text ) {
// todo: depends on language!
switch ( text ) {
case '!=': return ' not equal to ';
case '===': return ' same as ';
case '==': return ' equal to ';
case '.': return '\'s ';
case '->': return '\'s ';
case '::': return '\`s';
case '&&': return ' and ';
case '||': return ' or ';
case '++': return ' increment ';
case '--': return ' decrement ';
case '=>': return ' is ';
case ':': return ' is ';
case '$': return '';
case ',': return ', ';
case '_': return ' ';
case ';': return '. ';
case '()': return '';
case '(': return ' open ';
case ')': return ' close ';
case '[': return ' start ';
case ']': return ' finish ';
case '{': return ' begin ';
case '}': return ' end. ';
default:
return text.replace( '/*', '' )
.replace( '*/', '' )
.replace( '//', '' )
.replace( '#', '' )
.replace( /\$\B/, '' )
.replace( /^(\.|->)/, '' )
.replace( /(\.|->)$/, '\'s' )
;
}
},
_applyStyle: function( options, style ) {
// todo: mangle text and style at the same time
if ( style.fontStyle == 'italic' ) {
// comment
options.variant = 'f2'; // female 2
} else if ( style.color == 'rgb(51, 102, 204)' ) {
// string literal
options.variant = 'm4'; // male 4
} else if ( style.fontWeight == 'bold' ) {
// comment
options.variant = 'croak';
options.speed = 250;
} else if ( style.color == 'rgb(51, 153, 51)' ) {
// operator or punctuation
options.speed = 250;
}
return options;
},
stop: function( root ) {
this._stop = true;
},
walkAndTalk: function( root ) {
//this._meSpeak.speak( 'hello world' );
var stream = VocalCodeChunkStream( root );
var self = this;
this._stop = false;
var speakNext = function( ok ) {
if ( !ok || self._stop ) {
console.log( "ABORT!" );
stream.highlight( null );
return;
}
var frame = stream.nextFrame()
if ( frame ) {
var indent = frame[0], text = frame[1], style = frame[2];
var options = {
variant: 'm1',
nostop: true,
punct: false,
capitals: 1,
pitch: 40 + ( indent.length / 4 ) * 20
};
text = self._mangleText( text );
options = self._applyStyle( options, style );
console.log( "say: " + text + " using " + JSON.stringify( options ) );
self._meSpeak.speak( text, options, speakNext );
} else {
stream.highlight( null );
console.log( "done." );
}
};
speakNext( true );
}
};
}( window.meSpeak );