forked from bitovi/documentjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (114 loc) · 3.4 KB
/
Copy pathscript.js
File metadata and controls
136 lines (114 loc) · 3.4 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
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
steal.then(function() {
/**
* Represents a file.
* Breaks up file into comment and code parts.
* Creates new [DocumentJS.Pair | Doc.Pairs].
* @hide
*/
DocumentJS.Script = {
// returns the min intented amount for these lines.
minSpace : function(lines){
var removeSpace = Infinity,
noSpace = /\S/g,
match;
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;
}
return removeSpace - 1;
},
// removes Indent inline
removeIndent : function(lines){
var removeSpace = this.minSpace(lines);
if ( isFinite(removeSpace) ) {
for ( l = 0; l < lines.length; l++ ) {
lines[l] = lines[l].substr(removeSpace)
}
}
return lines;
},
getCommentCodePairs : function(){
},
group: new RegExp("(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/\[^\\w\\{\\(\\[/]*[^\\n]*)", "g"),
// (?:/\*+((?:[^*]|(?:\*+[^*/]))*)\*+/[^\w\{\(\[\"'\$]*([^\r\n]*))
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, objects ) {
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
},
scope = script,
comments,
type;
print(" " + script.name);
objects[script.name] = script;
// handle markdown docs
if(/\.md$/.test(docScript.src)){
type = DocumentJS.Type.create(source, "", scope, objects, 'page', docScript.src.match(/([^\/]+)\.md$/)[1] );
if ( type ) {
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;
type.src = docScript.src;
}
return;
}
comments = this.getComments(source);
//clean comments
for(var i =0 ; i < comments.length; i++){
var comment = comments[i];
type = DocumentJS.Type.create(comment.comment,
comment.code, scope, objects);
if ( type ) {
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;
type.src = docScript.src;
}
}
},
//
getComments : function(source){
var pairs = source.match(this.group) || [],
comments = [];
//clean comments
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],
lines = comment.split("\n");
this.removeIndent(lines);
comment = lines.join("\n");
// probably want line numbers and such
comments.push({
comment: comment,
code: code
})
}
return comments;
}
};
DocumentJS.Type("script", {
useName: false,
hasChildren: true
})
})