forked from ekalinin/nodeguide.ru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
148 lines (133 loc) · 4.55 KB
/
app.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
// Modules
var express = require('express')
, path = require('path')
, sm = require('sitemap')
, fs = require('fs');
// Documentation base path
var doc_base = path.join(__dirname, 'build', 'json');
// Documentation environment
// (read from file that builded by sphinx)
var env;
fs.readFile(path.join(doc_base, 'globalcontext.json'), 'utf8', function (err, data) {
env = JSON.parse(data);
});
// Application configuration
var app = module.exports = express.createServer();
app.configure( function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.logger());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
// Route middleware
function handleTrailSlash(req, res, next) {
if ( req.url[req.url.length - 1] !== '/' && // no trailing slash
req.url.indexOf('/index') === -1 ) { // this is not a section index page
res.redirect( req.url + '/' )
} else {
next();
}
}
// Sitemap
var sitemap = sm.createSitemap({hostname: 'http://nodeguide.ru/doc'});
app.get('/sitemap.xml', function (req, res) {
// some type of cache
if ( sitemap.urls.length === 0 ) {
// add index page
sitemap.add({ url: '/', safe: true, priority: 1, changefreq: 'daily' });
// add other pages
walk(__dirname+'/guides', function(files) {
files.forEach( function (file) {
if (!file || file.indexOf('.rst') == -1) { return; }
// delete some stuff
var clear_url = file.replace(__dirname, '')
.replace('.rst', '')
.replace('guides/', '');
// trailing slash fix
if ( clear_url[clear_url.length -1 ] != '/' &&
clear_url.indexOf('index') == -1 ) {
clear_url += '/';
}
sitemap.add({
url: clear_url, safe: true,
priority: (clear_url.indexOf('index') > -1) ? 0.5 : 0.8,
changefreq: (clear_url.indexOf('index') > -1) ? 'daily' : 'weekly'
});
})
});
}
sitemap.toXML( function (xml) {
res.header('Content-Type', 'application/xml');
res.send( xml );
});
});
// Routes
app.get('/', function (req, res) { res.redirect('/doc/'); });
app.get(/doc\/dailyjs\/web-app-(\d)/, function (req, res) {
res.redirect('/doc/dailyjs-nodepad/node-tutorial-'+req.params[0]); });
app.get(/doc\/dailyjs\/index/, function (req, res) {
res.redirect('/doc/dailyjs-nodepad/index'); });
app.get(/doc$/, function (req, res) { res.redirect('/doc/'); });
app.get(/doc\/([\w-\/]*)?$/, handleTrailSlash, function (req, res, next) {
var url = req.params[0],
port = app.address().port,
// url point to file
doc_name = (url ? url.replace(/\/$/,'') : 'index') + '.fjson',
filename = path.join(doc_base, doc_name);
//console.log(' * filename #1: ' + filename);
path.exists(filename, function(exists) {
// found
if (exists) {
fs.readFile(filename, 'utf8', function (err, data) {
res.render('layout', { doc: JSON.parse(data),
env: env, port: port});
});
return;
}
// may be it was directory?
// add index.fjson
filename = path.join(doc_base, url, 'index.fjson');
//console.log(' * filename #2: ' + filename);
path.exists(filename, function (exists) {
if (!exists) {
res.send(404);
return;
}
fs.readFile(filename, 'utf8', function (err, data) {
res.render('layout', { doc: JSON.parse(data),
env: env, port: port});
});
});
});
});
// Only listen on $ node app.js
if (!module.parent) {
app.listen(3000);
console.log("Express server listening on port %d", app.address().port);
}
// Utils
var walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err || !list) return done(results);
(function next(i) {
var f = list[i];
if (!f) return done(results);
fs.stat(dir + '/' + f, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(dir + '/' + f, function(r) {
results = results.concat(r);
next(++i);
});
} else {
results.push(dir + '/' + f);
next(++i);
}
});
})(0);
});
};