forked from forio/contour
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig-dox.js
118 lines (93 loc) · 3.15 KB
/
config-dox.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
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
var _ = require('underscore');
String.prototype.regexIndexOf = function(regex, start) {
var indexOf = this.substring(start || 0).search(regex);
return (indexOf >= 0) ? (indexOf + (start || 0)) : indexOf;
};
var isSafeForEval = function(text) {
// from json2 parser
// for details goto: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
// return /^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@')
// .replace(/["'][^"\\\n\r]*["']|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']')
// .replace(/(?:^|:|,)(?:\s*\[)+/g, ''));
return true;
};
/*jslint evil: true, regexp: true */
var getConfigObject = function (js, options) {
options = options || {};
var searchPatterns = [
'\\/\\*\\*\\s?@config',
'defaults\\s?=\\s?\\{'
].join('|');
var defaults = {
patterns: searchPatterns
};
options = _.extend(defaults, options);
// search for the 'config object' accounting to the given patterns
var regExp = new RegExp(options.patterns, 'g');
if(!(regExp.test(js))) return {};
var start = js.regexIndexOf(regExp);
if(js[start] === '/') {
start = js.regexIndexOf(/\*\//, start);
}
var firstBraket = js.regexIndexOf(/{/, start);
var buf = [], brakets = 0, cur = firstBraket;
var whithinComment = false, whithinMultilineComment = false;
do {
var token = js[cur++];
var next = js[cur];
switch(token) {
case '\n':
whithinComment = false;
break;
case '*':
if (next === '/') whithinMultilineComment = false;
break;
case '/':
if (next === '/') whithinComment = true;
if (next === '*') whithinMultilineComment = true;
break;
case '{':
// only count the braket if we our not in a comment block|line
brakets += !(whithinComment || whithinMultilineComment) | 0;
break;
case '}':
// only count the braket if we our not in a comment block|line
brakets -= !(whithinComment || whithinMultilineComment) | 0;
break;
}
buf.push(token);
} while(brakets);
var text = buf.join('');
if(!isSafeForEval(text)) throw new SyntaxError('Can\'t parse config object');
// aaaahhhh!!! people are going to die with this!!!
return eval('(' + text + ')');
};
var res = {
"tags":[
{
"type": "config-object",
"description": "configuration object for ABC visualization"
}
],
"desciption": {
"full": "full description",
"summary": "first line of description",
"body": "body of description"
},
"ctx": {
"type": "property",
"receiver": "window",
"name": "config"
}
};
exports.parseConfigObject = function(js, options) {
options = options || {};
var config = getConfigObject(js);
var doc = {
"tags": [{ "type": "config-object", "description": "" }],
"ctx": { "type": "declaration", "name": "defaults" }
};
for (var key in config) {
}
return config;
};