-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.js
More file actions
44 lines (40 loc) · 1.09 KB
/
handlers.js
File metadata and controls
44 lines (40 loc) · 1.09 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
var handlers = [];
handlers.push({
matcher: /^html (.+)/,
handler: function(match) { return [{type: 'html', data: match[1]}]; }
});
handlers.push({
matcher: /^tr (.+)/,
handler: function(match) {
return [{type: 'table', data: {type: 'row', cells: match[1].split(/\|/g)}}];
}
});
handlers.push({
matcher: /^bar (.+)/,
handler: function(match) {
var fields = match[1].trim().split(/ /);
var label = '', value;
if (fields.length == 1) {
value = parseFloat(fields[0]);
} else {
label = fields[0];
value = parseFloat(fields[1]);
}
return [{type: 'bar', data: {label: label, value: value}}];
}
});
// Given a line, returns an array of {type: string, data: string}.
module.exports = function(line) {
var lineString = line.toString('utf8');
var match = /^<<<(.*)>>>$/.exec(lineString);
if (match) {
for (var i = 0; i < handlers.length; i++) {
var handler = handlers[i];
var match2 = handler.matcher.exec(match[1]);
if (match2) {
return handler.handler(match2);
}
}
}
return [{type: 'raw', data: lineString}];
};