forked from bitovi/documentjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.js
211 lines (189 loc) · 5.68 KB
/
application.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
steal.then(function() {
DocumentJS.extend(DocumentJS,
/* @Static */
{
renderTo: function( file, ejs, data ) {
new DocumentJS.File(file).save(new DocumentJS.EJS({
text: readFile(ejs)
}).render(data));
},
/**
* Will replace with a link to a class or function if appropriate.
* @param {Object} content
*/
objects: {}
});
/**
* @constructor
* @hide
* Creates documentation for an application
* @init
* Generates documentation from the passed in files.
* @param {Array} total An array of path names or objects with a path and text.
* @param {Object} appName The application name.
*/
DocumentJS.Application = function( total, appName ) {
this.name = appName;
this.total = total;
//this.files = [];
this.objects = {}; //all the objects live here, have a unique name
DocumentJS.Application.objects = this.objects;
//create each Script, which will create each class/constructor, etc
for ( var s = 0; s < total.length; s++ ) {
DocumentJS.Script.process(total[s], this.objects)
}
//sort class and constructors so they are easy to find
//this.all_sorted = DocumentJS.Class.listing.concat( DocumentJS.Constructor.listing ).sort( DocumentJS.Pair.sort_by_name )
}
DocumentJS.Application.prototype =
/* @prototype */
{
/**
* Creates the documentation files.
* @param {String} path where to put the docs
*/
generate: function( path, convert ) {
print("generating ...")
//go through all the objects
for ( var name in DocumentJS.Application.objects ) {
if (DocumentJS.Application.objects.hasOwnProperty(name)){
var obj = DocumentJS.extend({}, DocumentJS.Application.objects[name]),
toJSON;
if ( obj.type == 'script' || typeof obj != "object" ) {
continue;
}
//get all children
var children = this.linker(obj);
obj.children = children;
var converted = name.replace(/ /g, "_").replace(/./g, ".").replace(/>/g, "_gt_").replace(/\*/g, "_star_")
toJSON = this.toJSON(obj);
new DocumentJS.File(path + "/" + converted + ".json").save(toJSON);
}
}
this.searchData(path, convert);
this.summaryPage(path, convert)
},
shallowParent: function( item, parent ) {
if ( item.parents && parent ) {
for ( var i = 0; i < item.parents.length; i++ ) {
if ( item.parents[i] == parent.name ) {
return true;
}
}
}
return false;
},
linker: function( item, stealSelf, parent ) {
var result = stealSelf ? [item.name] : [];
if ( item.children && !this.shallowParent(item, parent) ) {
for ( var c = 0; c < item.children.length; c++ ) {
var child = DocumentJS.Application.objects[item.children[c]];
var adds = this.linker(child, true, item);
if ( adds ) {
result = result.concat(adds);
}
}
}
return result;
},
/**
* Creates a page for all classes and constructors
* @param {String} summary the left hand side.
*/
summaryPage: function( path, convert ) {
//find index page
var base = path.replace(/[^\/]*$/, "");
this.indexPage = DocumentJS.Application.objects.index
//checks if you have a summary
if ( readFile(base + "summary.ejs") ) {
DocumentJS.renderTo(base + "docs.html", base + "summary.ejs", {
pathToRoot: new DocumentJS.File(base.replace(/\/[^\/]*$/, "")).pathToRoot(),
path: path
})
} else {
print("Using default page layout. Overwrite by creating: " + base + "summary.ejs");
DocumentJS.renderTo(base + "docs.html", "documentjs/jmvcdoc/summary.ejs", {
pathToRoot: new DocumentJS.File(base.replace(/\/[^\/]*$/, "")).pathToRoot(),
path: path
}); //default
}
},
indexOf: function( array, item ) {
var i = 0,
length = array.length;
for (; i < length; i++ ){
if ( array[i] === item ){
return i;
}
}
return -1;
},
addTagToSearchData: function( data, tag, searchData ) {
var letter, l, depth = 2,
current = searchData;
for ( l = 0; l < depth; l++ ) {
letter = tag.substring(l, l + 1);
if (!current[letter] ) {
current[letter] = {};
current[letter].list = [];
}
if ( this.indexOf(current[letter].list, data) == -1 ) {
current[letter].list.push(data);
}
current = current[letter];
}
},
addToSearchData: function( list, searchData ) {
var c, parts, part, p, fullName;
for ( var name in list ) {
if (list.hasOwnProperty(name)){
c = list[name];
if ( c.type == 'script' ) {
continue;
}
//break up into parts
fullName = c.name;
searchData.list[fullName] = {
name: c.name,
type: c.type
};
if ( c.title ) {
searchData.list[fullName].title = c.title
}
if ( c.tags ) {
searchData.list[fullName].tags = c.tags
}
if ( c.hide ) {
searchData.list[fullName].hide = c.hide
}
parts = fullName.split(".");
for ( p = 0; p < parts.length; p++ ) {
part = parts[p].toLowerCase();
if ( part == "jquery" ){
continue;
}
this.addTagToSearchData(fullName, part, searchData)
}
//now add tags if there are tags
if ( c.tags ) {
for ( var t = 0; t < c.tags.length; t++ ){
this.addTagToSearchData(fullName, c.tags[t], searchData);
}
}
}
}
},
searchData: function( path, convert ) {
//var sortedClasses = DocumentJS.Class.listing.sort( DocumentJS.Pair.sort_by_name )
//go through and create 2 level hash structure
var searchData = {
list: {}
};
this.addToSearchData(DocumentJS.Application.objects, searchData)
return new DocumentJS.File(path + "/searchData.json").save(this.toJSON(searchData, false));
},
toJSON: function() {
return "C(" + DocumentJS.toJSON.apply(DocumentJS.toJSON, arguments) + ")"
}
}
})