forked from yui/yuidoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
228 lines (200 loc) · 7.96 KB
/
server.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
YUI.add('server', function(Y) {
var path = require('path');
/**
* Provides the `--server` server option for YUIDoc
* @class Server
* @module yuidoc
*/
var Server = {
/**
* Cache for external mixed in data.
* @property _externalData
* @private
* @type Object
*/
_externalData: null,
/**
* Middleware to parse the API docs per request
* @method parse
* @param {Request} req Express request object
* @param {Response} res Express response object
* @param {Function} next Express next callback
*/
parse: function(req, res, next) {
var json = (new Y.YUIDoc(Server.options)).run();
Server.options = Y.Project.mix(json, Server.options);
Server.builder = new Y.DocBuilder(Server.options, json);
if (Server._externalData) {
Server.options.externalData = Server._externalData;
Server.builder._mixExternal();
}
next();
},
/**
* Create the routes used to serve YUIDoc files dynamically
* @method routes
*/
routes: function() {
var app = Server.app;
app.get('/', Server.parse, function(req, res) {
Server.home(req, res);
});
app.get('/api.js', function(req, res) {
Server.builder.renderAPIMeta(function(js) {
res.contentType('.js');
res.send(js);
});
});
app.get('/classes/:class.html', Server.parse, function(req, res, next) {
Server.clazz(req, res, next);
});
app.get('/modules/:module.html', Server.parse, function(req, res, next) {
Server.module(req, res, next);
});
app.get('/files/:file.html', Server.parse, function(req, res, next) {
Server.files(req, res, next);
});
//These routes are special catch routes..
app.get('//api.js', function(req, res) {
res.redirect('/api.js');
});
app.get('//classes/:class.html', Server.parse, function(req, res, next) {
Server.clazz(req, res, next);
});
app.get('//modules/:module.html', Server.parse, function(req, res, next) {
Server.module(req, res, next);
});
app.get('//files/:file.html', Server.parse, function(req, res, next) {
Server.files(req, res, next);
});
app.get('*', function(req, res) {
var type = req.url.split('/')[1],
oType = type;
var html = ['<h1>Item Not Found in internal meta-data</h1>'];
if (type === 'class') {
type = 'classes';
}
if (Server.builder && Server.builder.data && Server.builder.data[type]) {
if (Object.keys(Server.builder.data[type]).length) {
html.push('<p>But I know about these? Misname your module?</p>');
html.push('<ul>');
Object.keys(Server.builder.data[type]).forEach(function(item) {
html.push('<li><a href="../' + path.dirname(req.url) + '/' + item + '.html">' + item + '</a></li>');
});
html.push('</ul>');
}
}
res.send(html.join('\n'), 404);
});
},
/**
* `/files` endpoint
* @method files
* @param {Request} req Express request object
* @param {Response} res Express response object
*/
files: function(req, res, next) {
var fileName = req.params.file;
var data;
Object.keys(Server.builder.data.files).forEach(function(file) {
if (fileName === Server.builder.filterFileName(file)) {
data = Server.builder.data.files[file];
}
});
if (!data) {
return next();
}
Y.log('Serving /files/' + data.name, 'info', 'server');
Server.builder.renderFile(function(html) {
res.send(html);
}, data, (req.xhr ? 'xhr' : 'main'));
},
/**
* `/classes` endpoint
* @method clazz
* @param {Request} req Express request object
* @param {Response} res Express response object
*/
clazz: function(req, res, next) {
var className = req.params['class'];
Y.log('Serving /classes/' + className + '.html', 'info', 'server');
if (!Server.builder.data.classes[className]) {
return next();
}
Server.builder.renderClass(function(html) {
res.send(html);
}, Server.builder.data.classes[className], (req.xhr ? 'xhr' : 'main'));
},
/**
* `/modules` endpoint
* @method modules
* @param {Request} req Express request object
* @param {Response} res Express response object
*/
module: function(req, res, next) {
var modName = req.params.module;
Y.log('Serving /modules/' + modName + '.html', 'info', 'server');
if (!Server.builder.data.modules[modName]) {
return next();
}
Server.builder.renderModule(function(html) {
res.send(html);
}, Server.builder.data.modules[modName], (req.xhr ? 'xhr' : 'main'));
},
/**
* `/` endpoint
* @method home
* @param {Request} req Express request object
* @param {Response} res Express response object
*/
home: function(req, res, next) {
Y.log('Serving index.html', 'info', 'server');
Server.builder.renderIndex(function(html) {
res.send(html);
});
},
/**
* Creates the Express server and prep's YUI for serving
* @method init
*/
init: function() {
var express = require('express'),
path = require('path');
Server.app = express();
//console.log(Server.options);
var stat = Server.options.themedir || path.join(__dirname, '../', 'themes', 'default');
Server.app.use(express.static(stat));
Server.routes();
Server.app.listen(Server.options.port);
Y.log('Starting server: http:/'+'/127.0.0.1:' + Server.options.port, 'info', 'server');
},
/**
* Start the server with the supplied options.
* @method start
* @param {Object} options Server options
*/
start: function(options) {
options = Y.Project.init(options);
Server.options = options;
Server.options.cacheTemplates = false; //Don't cache the Handlebars templates
Server.options.writeJSON = false; //Don't write the JSON file out
Y.config.logExclude.yuidoc = true;
Y.config.logExclude.docparser = true;
Y.config.logExclude.builder = true;
if (Server.options.external) {
Y.log('Fetching external data, this may take a minute', 'warn', 'server');
var json = (new Y.YUIDoc(Server.options)).run();
Server.options = Y.Project.mix(json, Server.options);
var builder = new Y.DocBuilder(Server.options, json);
builder.mixExternal(function() {
Y.log('External data fetched, launching server..', 'info', 'server');
Server._externalData = builder.options.externalData;
Server.init();
});
} else {
Server.init();
}
}
};
Y.Server = Server;
});