forked from michaelsbradleyjr/gfms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfms.js
368 lines (291 loc) · 10.2 KB
/
gfms.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
var laeh = require('laeh2').leanStacks(true);
var _e = laeh._e;
var _x = laeh._x;
var express = require('express');
var stylus = require('stylus');
var nib = require('nib');
var app = express();
var http = require('http');
var server = http.createServer(app);
var markdown = //require('github-flavored-markdown').parse;
require('./showdown.js').parse;
var _ = require('underscore');
var fs = require('fs');
var ews = require('ws');
var ws = require('ws-rpc').extend(ews);
var wss = new ws.Server({ server: server });
var request = require('request');
var async = require('async-mini');
var utilz = require('utilz');
var optimist = require('optimist');
var watched = {};
var styles = {};
var cssUpdateInterval = 1000 * 60 * 60 * 24;
var cssCheckInterval = 1000 * 60 * 5;
var lastCssUpdate = 0;
var updatingCss;
var pkgJson = require('./package.json');
function cb(err, msg) {
console.log(err ? (err.stack || err) : msg || 'done');
}
var argv = optimist
.usage('\nGithub Flavored Markdown Server.\nRun in your project\'s root directory.\nUsage: $0')
.demand('p')
.alias('p', 'port')
.describe('p', 'Port number to listen at.')
.alias('h', 'host')
.describe('h', 'Host address to bind to.')
.default('h', 'localhost')
.describe('proxy', 'if behind a proxy, proxy url.')
.boolean('a')
.describe('a', 'Render using Github API.')
.alias('a', 'api')
.boolean('n')
.describe('n', 'Disable usage of Github API when the doc is manually reloaded.')
.alias('n', 'no-api-on-reload')
.argv;
app.configure(function() {
var pub = __dirname + '/public';
var views = __dirname + '/views';
app.set('views', views);
app.set('view engine', 'jade');
app.set('view options', { layout: false });
if(process.env.NODE_ENV === 'development') {
// only use Stylus in development, because when gfms is installed
// globally with sudo, and then run by an user, it cannot create
// the generate .css files (and I'm too tired to look for a solution now).
app.use(stylus.middleware({
src: views,
dest: pub,
compile: function(str, path) {
return stylus(str)
.set('filename', path)
.set('compress', true)
.use(nib())
.import('nib');
}
}));
}
app.use(wss.middleware(express));
app.use(express.favicon());
app.use(app.router);
app.use(express.static(pub));
app.use(express.errorHandler({ dump: true, stack: true }));
});
app.configure('development', function() {
utilz.watchFile(__filename);
});
function basename(fn) {
var m = fn.match(/.*?([^\/]+)\/?$/);
return m ? m[1] : fn;
}
function is_markdown(v) {
return v.match(/.*?(?:\.md|\.markdown)$/) ? true : false;
}
function is_image(v) {
return v.match(/.*?(?:\.png|\.jpg|\.gif|\.svg)$/) ? true : false;
}
app.get('*', function(req, res, next) {
if(req.path.indexOf('/styles/') === 0) {
var style = styles[req.path];
if(!style) {
res.send(404);
}
else {
res.set('Content-Type', 'text/css');
// res.set('ETag', utilz.randomString());
return res.send(200, style);
}
}
var base = req.path.replace('..', 'DENIED').replace(/\/$/, '');
var query = req.query || {}
var dir = decodeURI(process.cwd() + base);
var stat;
try {
stat = fs.statSync(dir);
}
catch(e) {
return next();
}
if(stat.isDirectory()) {
var files = _.chain(fs.readdirSync(dir)).filter(function(v) {
var stat = fs.statSync(dir + '/' + v);
return stat.isDirectory() || (stat.isFile() && (is_markdown(v) || is_image(v)));
}).map(function(v) {
return {
url: base + '/' + v,
name: v
};
}).value();
res.render('directory', {
files: files,
dir: dir,
styles: Object.keys(styles),
title: basename(dir)
});
} else if(query.raw === "true") {
var content = fs.readFileSync(dir);
res.writeHead('200');
res.end(content,'binary');
} else if(is_markdown(dir)) {
if(!watched[dir]) {
fs.watchFile(dir, { interval: 500 }, function(curr, prev) {
if(curr.mtime.getTime() !== prev.mtime.getTime()) {
console.log('file ' + dir + ' has changed');
renderFile(dir, argv.a, _x(console.log, false, function(err, rendered) {
wss.message('update', { update: dir, content: err || rendered });
}));
}
});
watched[dir] = true;
}
renderFile(dir, argv.a || argv.b, _x(next, true, function(err, rendered) {
res.render('file', {
file: rendered,
title: basename(dir),
styles: Object.keys(styles),
fullname: dir
});
}));
}
else if(is_image(dir)) {
if(!watched[dir]) {
fs.watchFile(dir, { interval: 500 }, function(curr, prev) {
if(curr.mtime.getTime() !== prev.mtime.getTime()) {
console.log('file ' + dir + ' has changed');
renderImageFile(base, argv.a, _x(console.log, false, function(err, rendered) {
wss.message('update', { update: dir, content: err || rendered });
}));
}
});
watched[dir] = true;
}
renderImageFile(base, argv.a || argv.b, _x(next, true, function(err, rendered) {
res.render('file', {
file: rendered,
title: basename(dir),
styles: Object.keys(styles),
fullname: dir
});
}));
}
else
return next();
});
function renderFile(file, api, cb) { // cb(err, res)
var contents = fs.readFileSync(file, 'utf8');
var func = api ? renderWithGithub : renderWithShowdown;
func(contents, _x(cb, true, cb));
}
function renderImageFile(file, api, cb) { // cb(err, res)
var html = '<div class="image js-image"><span class="border-wrap"><img src="' + file + '?raw=true"></span></div>';
cb(null, html);
}
function renderWithShowdown(contents, cb) { // cb(err, res)
var res = markdown(contents);
cb(null, res);
}
function renderWithGithub(contents, cb) { // cb(err, res)
var opts = {
method: 'post',
url: 'https://api.github.com/markdown',
json: {
text: contents,
mode: 'markdown'
},
headers: {
'User-Agent': 'gfms/' + pkgJson.version + ' https://github.com/ypocat/gfms'
},
encoding: 'utf8'
};
if(argv.proxy && argv.proxy.length > 0) {
opts.proxy = argv.proxy;
}
request(opts, _x(cb, true, function(err, res, body) {
console.log('remaining API requests: %d', res.headers['x-ratelimit-remaining']);
cb(null, body);
}));
}
process.on('SIGINT', function() {
console.log('\nGFMS exit.');
return process.exit();
});
function loadStyle(style, cb) {
request(style, _x(cb, true, function(err, res, body) {
if(res.statusCode != 200)
throw 'Cannot load stylesheet: ' + style;
cb(null, body);
}));
}
function getStylesheetBaseName(url) {
var m = /\/([^\/]+)$/.exec(url);
if(!m)
_e('unexpected stylesheet url: ' + url);
return m[1];
}
function loadStyles(_cb) {
if(updatingCss)
return;
updatingCss = true;
function cb(err) {
updatingCss = false;
_cb(err);
}
_x(cb, false, function() {
console.log('Loading Github CSS...');
var opts = {url:'http://www.github.com'};
if(argv.proxy && argv.proxy.length > 0) {
opts.proxy = argv.proxy;
}
request(opts, _x(cb, true, function(err, res, body) {
if(res.statusCode != 200)
throw 'Cannot load .css links from Github';
var ff = {}
var m;
var re = /href="([^"]+?\/assets\/[^"]+?\.css)"/g;
while(m = re.exec(body)) {
(function(url) {
var base = '/styles/' + getStylesheetBaseName(url);
ff[base] = _x(null, false, function(cb) {
var opts = {url:url};
if(argv.proxy && argv.proxy.length > 0) {
opts.proxy = argv.proxy;
}
loadStyle(opts, cb);
});
})(m[1]);
}
async.parallel(ff, _x(cb, true, function(err, res) {
styles = res;
cb();
}));
}));
})();
}
function startCssUpdater(interval) {
console.log('Auto-updating CSS every ' + utilz.timeSpan(interval) + '.');
setInterval(_x(cb, false, function() {
if(Date.now() - lastCssUpdate > interval) {
loadStyles(_x(cb, true, function() {
lastCssUpdate = Date.now();
cb(null, 'Auto-updated CSS.');
}));
}
}), cssCheckInterval);
}
if(!argv.a && !argv.n)
argv.b = true;
_x(cb, false, function() {
loadStyles(_x(cb, true, function() {
if(!Object.keys(styles).length)
_e('Cannot parse .css links from Github');
if(argv.a)
console.log('Using Github API to render markdown for all updates.');
else if(argv.b)
console.log('Using Github API to render markdown for manual reload updates.');
server.listen(argv.p, argv.h);
console.log('GFMS ' + pkgJson.version + ' serving ' + process.cwd() + ' at http://' + argv.h + ':' + argv.p + '/ - press CTRL+C to exit.');
lastCssUpdate = Date.now();
startCssUpdater(cssUpdateInterval);
}));
})();