forked from bitovi/documentjs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
function.js
112 lines (101 loc) · 3.7 KB
/
function.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
/**
* Documents a function.
* Doc can guess at a functions name and params if the source following a comment
* matches something like:
* @codestart
* myFuncOne : function(param1, param2){} //or
* myFuncTwo = function(param1, param2){}
* @codeend
* <h3>Directives</h3>
* Use the following directives to document a function.
<pre>
[DocumentJS.Function|@function] <i>function_name</i> -> Forces a function
[DocumentJS.Directive.Param|@param] {<i>optional:type</i>} <i>param_name</i> <i>Description</i> -> Describes a parameter
[DocumentJS.Directive.Return|@return] {<i>type</i>} <i>Description</i> -> Describes the return value
</pre>
* Add <i>optional:</i> for optional params. Other available directives:
* [DocumentJS.Directive.Plugin|@plugin],[DocumentJS.Directive.CodeStart|@codestart]
* <h3>Example</h3>
@codestart
/* Adds, Mr. or Ms. before someone's name
* * @param {String} name the persons name
* * @param {optional:Boolean} gender true if a man, false if female. Defaults to true.
* * @return {String} returns the appropriate honorific before the person's name.
* *|
honorific = function(name, gender){
@codeend
*/
DocumentJS.Pair.extend('DocumentJS.Function',
/* @static */
{
code_match: /(?:([\w\.]+)|(["'][^"']+["']))\s*[:=]\s*function\s?\(([^\)]*)/,
init : function(){
this.add(DocumentJS.Directive.Return,
DocumentJS.Directive.Param,
DocumentJS.Directive.CodeStart,
DocumentJS.Directive.CodeEnd,
DocumentJS.Directive.Plugin,
DocumentJS.Directive.Hide,
DocumentJS.Directive.Tag,
DocumentJS.Directive.iFrame,
DocumentJS.Directive.Demo,
DocumentJS.Directive.Parent,
DocumentJS.Directive.Scope,
DocumentJS.Directive.Download,
DocumentJS.Directive.Test,
DocumentJS.Directive.Author)
this._super();
this.serialize('plugin',['full_name','name'],'html','shortName','ret','params',['real_comment','comment'],
'tags','download','test')
}
},
/* @prototype */
{
code_setup: function(){
var parts = this.Class.code_match(this.code);
if(!parts){
parts = this.code.match(/\s*function\s+([\w\.\$]+)\s*(~)?\(([^\)]*)/)
}
this.name = parts[1] ? parts[1].replace(/^this\./,"") : parts[2];
//clean up name if it has ""
if(/^["']/.test(this.name)){
this.name = this.name.substr(1, this.name.length-2).replace(/\./g,".").replace(/>/g,">");
}
this.params = {};
this.ret = {type: 'undefined',description: ""}
var params = parts[3].match(/\w+/g);
if(!params) return;
for(var i = 0 ; i < params.length; i++){
this.params[params[i]] = {description: "", type: "", optional: false, order: i, name: params[i]};
}
},
/**
* Sets the function's name if one can't be determined from the source
* @param {Object} line
*/
function_add: function(line){
var m = line.match(/^@\w+\s+([\w\.\$]+)/)
if(m) this.name = m[1];
},
/**
* Returns the HTML signiture of the function.
*/
signiture : function(){
var res = [];
var ordered = this.ordered_params();
for(var n = 0; n < ordered.length; n++){
res.push(ordered[n].name)
}
var n = this.name;
return n+"("+res.join(", ")+") -> "+this.ret.type;
},
toFile : function(name){
try{
var res = this.jsonp();
new steal.File('apps/'+name+'/docs/'+this.full_name().replace(/ /g,"_").replace(/./g,".")+".json").save(res);
}catch(e ){
print("Unable to generate class for "+this.full_name()+" !")
print(" Error: "+e)
}
}
});