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
210 lines (175 loc) · 5.38 KB
/
Copy pathscript.js
File metadata and controls
210 lines (175 loc) · 5.38 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
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
steal.then(function() {
var ignoreCheck = /\@documentjs-ignore/,
commentReg = /\r?\n(?:\s*\*+)?/g,
spaceReg = /\S/g,
newLine = /\n/g,
lineNumber = function( source ) {
// reset lastIndex
newLine.lastIndex = 0;
var curLine = 0,
curIndex, lines, len;
return function( index ) {
if (!lines ) {
lines = source.split('\n');
curIndex = lines[0].length + 1;
len = lines.length;
}
// if we haven't already, split the
if ( index <= curIndex ) {
return curLine;
}
curLine++;
while ( curLine < len && (curIndex += lines[curLine].length + 1) <= index ) {
curLine++;
}
return curLine;
}
};
//commentTime = 0;
//processTime = 0;
/**
* Represents a file.
* Breaks up file into comment and code parts.
* Creates new [DocumentJS.Pair | Doc.Pairs].
* @hide
*/
DocumentJS.Script = {
// removes indent inline
removeIndent: function( lines ) {
// first calculate the amount of space to remove
// and get lines starting with text content
var removeSpace = Infinity,
noSpace = spaceReg,
match, contentLines = [],
hasContent = false,
line, l;
// for each line
for ( l = 0; l < lines.length; l++ ) {
line = lines[l];
// test if it has something other than a space
match = noSpace.exec(line);
// if it does, and it's less than our current maximum
if ( match && line && noSpace.lastIndex < removeSpace ) {
// update our current maximum
removeSpace = noSpace.lastIndex;
// mark as starting to have content
hasContent = true;
}
// if we have content now, add to contentLines
if ( hasContent ) {
contentLines.push(line);
}
// update the regexp position
noSpace.lastIndex = 0;
}
// remove from the position before the last char
removeSpace = removeSpace - 1;
// go through content lines and remove the removeSpace
if ( isFinite(removeSpace) && removeSpace !== 0 ) {
for ( l = 0; l < contentLines.length; l++ ) {
contentLines[l] = contentLines[l].substr(removeSpace);
}
}
return contentLines;
},
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 ( ignoreCheck.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];
//var start = new Date;
type = DocumentJS.Type.create(comment.comment, comment.code, scope, objects);
//processTime = processTime + (new Date - start)
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;
type.line = comment.line;
}
}
},
// gets an array of comments from this source
// each comment has
// - comment : an array of lines that make up the comment
// - code : the line of code after the comment
// - line : the line number of the comment
getComments: function( source ) {
var start = new Date;
//var source = source.replace('\r\n','\n')
var comments = [],
match, getLine = lineNumber(source);
this.group.lastIndex = 0;
while ( match = this.group.exec(source) ) {
//print("|TTT\n"+match[0]+"\n-------")
var lastIndex =this.group.lastIndex,
origComment = match[0],
splits =origComment.match(this.splitter),
// the comment after removing leading *
comment = splits[1].replace(commentReg, '\n'),
code = splits[2],
lines = comment.split("\n");
lines = this.removeIndent(lines);
// probably want line numbers and such
// an empty line
if (!lines.length ) {
continue;
}
var line =getLine(lastIndex - origComment.length );
//print((lastIndex - origComment.length)+"->"+line+ " "+source.substr(lastIndex - origComment.length,50));
comments.push({
comment: lines,
code: code,
line: line
})
}
//commentTime = commentTime + (new Date - start)
return comments;
}
};
DocumentJS.Type("script", {
useName: false,
hasChildren: true
})
})