forked from bitovi/documentjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pair.js
302 lines (285 loc) · 9.06 KB
/
pair.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/**
* A base class for a comment and the line of code following it.
* @hide
*/
DocumentJS.Class.extend("DocumentJS.Pair",
/* @Static */
{
code_match: function(){ return null},
classes: [],
/**
* From the comment and code, guesses at the type of comment and creates a new
* instance of that type.
* @param {String} comment - the comment
* @param {String} code - the first line of source following the comment
* @param {DocumentJS.Pair} scope - The current scope of documentation.
* This is typically a Class, Constructor, Static, or Prototype
* @return {DocumentJS.Pair} - If a type can be found, the new Doc object; otherwise, null.
*/
create: function(comment, code, scope){
var check = comment.match(/^@(\w+)/), type
if(!(type = this.has_type(check ? check[1] : null)) ){ //try code
type = this.guess_type(code);
}
if(!type) return null;
return new type(comment, code, scope)
},
/**
* Looks for a Doc class with a shortName for the given type
* @param {String} type a potential shortName
*/
has_type: function(type){
if(!type) return null;
for(var i=0;i< this.classes.length; i++){
if(this.classes[i].shortName.toLowerCase() == type.toLowerCase() )
return this.classes[i];
}
return null;
},
/**
* Tries to guess at a piece of code's type.
* @param {Object} code
*/
guess_type: function(code){
for(var i=0;i< this.classes.length; i++){
if(this.classes[i].code_match(code) )
return this.classes[i];
}
return null;
},
suggest_type : function(incorrect, line){
var lowest = 1000, suggest = "";
for(var i=0;i< this.classes.length; i++){
var dist = DocumentJS.distance(incorrect.toLowerCase(), this.classes[i].shortName.toLowerCase())
if(dist < lowest ){
lowest = dist
suggest = this.classes[i].shortName.toLowerCase()
}
}
for(var i=0;i< DocumentJS.Directive.directives.length; i++){
var dist = DocumentJS.distance(incorrect.toLowerCase(), DocumentJS.Directive.directives[i].toLowerCase())
if(dist < lowest ){
lowest = dist
suggest = DocumentJS.Directive.directives[i].toLowerCase()
}
}
if(suggest){
print("\nWarning!!\nThere is no @"+incorrect+" directive. did you mean @"+suggest+" ?\n")
}
},
starts_scope: false,
/**
* Given a and b, sorts by their full_name property.
* @param {Object} a
* @param {Object} b
*/
sort_by_full_name : function(a, b){
var af = a.full_name ? a.full_name.toLowerCase() : a.full_name
var bf = b.full_name ? b.full_name.toLowerCase() : a.full_name
if(af == bf) return 0;
return af > bf ? 1: -1;
},
sort_by_name : function(a, b){
var af = a.name ? a.name.toLowerCase() : a.name
var bf = b.name ? b.name.toLowerCase() : a.name
if(af == bf) return 0;
return af > bf ? 1: -1;
},
/**
* Loads a template to use to render different doc types.
*/
init : function(){
if(this.shortName){
if(DocumentJS.Pair)
DocumentJS.Pair.classes.push(this)
}
this.listing = [];
},
/**
* Adds [DocumentJS.Directive|directives] to this class.
* @codestart
* {
* init: function(){
* this._super();
* this.add(DocumentJS.Directive.Return, DocumentJS.Directive.Param)
* }
* }
* @codeend
*/
add : function(){
var args = DocumentJS.makeArray(arguments)
for(var i = 0; i < args.length; i++){
this._add(args[i]);
}
},
_add : function(directive){
var start = directive.shortName.toLowerCase()+"_"
this.prototype[start+"add"] = directive.prototype.add
if(directive.prototype.add_more)
this.prototype[start+"add_more"] = directive.prototype.add_more
},
matchDirective : /^\s*@(\w+)/
},
/* @Prototype */
{
/**
* Saves coment, code. Adds self to parent. Calls code_setup and comment_setup.
* Finally, adds to DocumentJS.objects.
* @param {String} comment
* @param {String} code
* @param {DocumentJS.Pair} scope
*/
init : function(comment, code, scope ){
this.children = [];
this.shallowParents = [];
this.comment = comment;
this.code = code;
if(this.Class.code_match(this.code))
this.code_setup();
this.comment_setup();
//we need to add to a class if we
this.add_parent(scope);
var par = this;
while(par && !par.url){
par = par.parent;
}
if(par){
DocumentJS.objects[this.full_name()] = par.url()+(this.url ? "" : "#"+this.full_name() );
}
this.Class.listing.push(this);
if(DocumentJS.Directive.Parent.waiting.hasOwnProperty(this.name)){
var addOns = DocumentJS.Directive.Parent.waiting[this.name]
this.children = this.children.concat(addOns)
for(var i=0;i< addOns.length;i++){
addOns[i].shallowParents.push(this);
}
}
},
add: function(child){
this.children.push(child);
},
add_parent : function(scope){
this.parent = scope;
this.parent.add(this);
},
scope: function(){
return this.Class.starts_scope ? this : this.parent
},
code_setup: function(){},
jsonp: function(){
return "C("+$.toJSON(this.json())+")";
},
full_name: function(){
var par = ""
//print("has parent "+this.parent)
if(!this.parent){
print(this.name+" has no parent ")
}else
par = this.parent.full_name();
//print(par+" - "+this.name)
return (par ? par+"." : "")+this.name ;
},
make : function(arr){
var res = ["<div>"];
//we should alphabetize by name
for(var c=0; c<arr.length; c++){
var child = arr[c];
res.push(child.toHTML());
}
res.push("</div>");
return res.join("");
},
/**
* Checks if this child should not add its children for the given parent
*/
shallowParent : function(parent){
for(var i = 0; i < this.shallowParents.length;i++){
if(this.shallowParents[i] === parent){
return true;
}
}
return false;
},
linker : function(stealSelf, parent){
var result = stealSelf ? [ {name: this.full_name(), shortName : this.Class.shortName.toLowerCase(), title: this.title, hide: (this.hide ? true: false) }] : [];
if(this.children && ! this.shallowParent(parent)){
//print(this.name)
for(var c=0; c<this.children.length; c++){
var adds = this.children[c].linker(true, this);
if(adds)
result = result.concat( adds );
}
}
return result;
},
/**
* Orders params into an array.
*/
ordered_params : function(){
var arr = [];
for(var n in this.params){
var param = this.params[n];
arr[param.order] = param;
}
return arr;
},
/**
* Goes through the comment line by line. Searches for lines starting with a <i>@directive</i>.
* If a line with a directive is found, it sees if the instance has a function that matches
* <i>directive</i>_add exists. If it does, <i>directive</i>_add is called on that object.
* If following lines do not have a directive, the <i>directive</i>_add_more function is called
* on the instance
* <br/>
* Initial comments are added to real_comment.<br>
* This function is shared by Class and Constructor.
*/
comment_setup: function(){
var i = 0,
lines = this.comment.split("\n"),
last,
last_data; //what data we are going to be called with
this.real_comment = '';
if(!this.params) this.params = {};
if(!this.ret) this.ret = {type: 'undefined',description: ""};
this._last; //what we should be adding too.
for(var l=0; l < lines.length; l++){
var line = lines[l],
match = line.match(DocumentJS.Pair.matchDirective)
if (match) {
var fname = (match[1] + '_add').toLowerCase();
if (!this[fname]) {
if (!DocumentJS.Pair.has_type(match[1])) {
DocumentJS.Pair.suggest_type(match[1])
}
this.real_comment += line + "\n"
continue;
}
last_data = this[fname](line, last_data);
//horrible ... fix
if (last_data && last_data.length == 2 && last_data[0] == 'keep') {
last_data = last_data[1]
}
else
if (last_data) {
this._last = match[1].toLowerCase();
}
else {
this._last = null;
}
}
else {
line = line.replace(/@@/g,"@")
if (!line.match(/^constructor/i) && !this._last) {
this.real_comment += line + "\n"
}
else if (this._last && this[this._last + '_add_more']) {
this[this._last + '_add_more'](line, last_data);
}
}
}
if(this.comment_setup_complete) this.comment_setup_complete();
},
shortName : function(){
return this.Class.shortName;
}
})