forked from bitovi/documentjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
86 lines (73 loc) · 2.23 KB
/
script.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
steal.then(function() {
/**
* Represents a file.
* Breaks up file into comment and code parts.
* Creates new [DocumentJS.Pair | Doc.Pairs].
* @hide
*/
DocumentJS.Script = {
group: new RegExp("(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/\[^\\w\\{\\(\\[/]*[^\\n]*)", "g"),
splitter: new RegExp("(?:/\\*+((?:[^*]|(?:\\*+[^*/]))*)\\*+/\[^\\w\\{\\(\\[\"'\$]*([^\\r\\n]*))"),
/**
* Generates docs for a file.
* @param {Object} docScript an object that has src and text attributes. It can also just be
* the path of a file.
*/
process: function( docScript ) {
if(typeof docScript == 'string'){
docScript = {src: docScript}
}
var source = docScript.text || readFile(docScript.src);
//check if the source has @documentjs-ignore
if (/\@documentjs-ignore/.test(source) ) {
return;
}
var script = {
type: "script",
name: docScript.src
}
print(" " + script.name);
DocumentJS.objects[script.name] = script;
var pairs = source.match(this.group);
//clean comments
var scope = script;
if (!pairs ) return;
for ( var i = 0; i < pairs.length; i++ ) {
var splits = pairs[i].match(this.splitter),
comment = splits[1].replace(/\r?\n(\s*\*+)?/g, '\n');
//print(splits[1].replace(/^[^\w@]*/,''))
var code = splits[2],
removeSpace = Infinity,
lines = comment.split("\n"),
noSpace = /\S/g,
match,
l;
for ( l = 0; l < lines.length; l++ ) {
match = noSpace.exec(lines[l]);
if ( match && lines[l] && noSpace.lastIndex < removeSpace ) {
removeSpace = noSpace.lastIndex;
}
noSpace.lastIndex = 0;
}
//print(removeSpace)
if ( isFinite(removeSpace) ) {
for ( l = 0; l < lines.length; l++ ) {
lines[l] = lines[l].substr(removeSpace - 1)
}
}
comment = lines.join("\n")
var type = DocumentJS.Type.create(comment, code, scope, DocumentJS.objects);
if ( type ) {
DocumentJS.objects[type.name] = type;
//get the new scope if you need it
// if we don't have a type, assume we can have children
scope = !type.type || DocumentJS.types[type.type].hasChildren ? type : scope;
}
}
}
};
DocumentJS.Type("script", {
useName: false,
hasChildren: true
})
})