-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
114 lines (90 loc) · 2.82 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
var express = require('express');
var errorHandler = require('errorhandler');
var logger = require('morgan');
var path = require('path');
var sprintf = require('sprintf').sprintf;
var ucs2 = require('punycode').ucs2;
var config = require('./config');
var panlex = require('./panlex');
var index = require('./index');
var td = index.td;
var gp = index.gp;
var uidTt = index.uidTt;
var tdg = [];
gp.forEach(function (item, i) {
tdg[i] = { beg: truncate(td[item[0]].beg), end: truncate(td[item[1]].end) };
});
var app = express();
app.set('port', process.env.PORT || config.port || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
if (app.get('env') === 'development') app.use(logger('dev'));
app.use(setHeaders);
app.use(express.static(path.join(__dirname, 'public')));
if (app.get('env') === 'development') app.use(errorHandler());
app.locals.title = 'PanLinx';
app.locals.startPage = false;
app.locals.base = config.base;
app.locals.robot = true;
app.locals.uidTt = uidTt;
app.get('/', indexRoute);
app.get('/gp/:gp', gpRoute);
app.get('/gp/:gp/sub/:id', subgpRoute);
app.get('/ex/:ex', exRoute);
app.listen(app.get('port'), '127.0.0.1', function(){
console.log("Express server listening on port " + app.get('port'));
});
function setHeaders(req, res, next) {
res.set('Expires', 0);
next();
}
function indexRoute(req, res, next) {
res.render('index', { tdg: tdg, startPage: true });
}
function gpRoute(req, res, next) {
if (!req.params.gp.match(/^\d+$/)) return next();
var num = Number(req.params.gp);
var subr = [];
var first = gp[num][0], last = gp[num][1];
for (var i = first; i <= last; i++) {
subr.push({
id: td[i].id,
beg: truncate(td[i].beg),
end: truncate(td[i].end)
});
}
res.render('gp', { gp: num, subr: subr });
}
function subgpRoute(req, res, next) {
if (!req.params.id.match(/^\d+$/)) return next();
var id = Number(req.params.id);
var tuple = td[id];
panlex.queryAll('/ex',
{ include: 'uid', sort: ['td', 'tt', 'uid'], range: ['td', tuple.beg, tuple.end] },
function (err, data) {
if (err) return next(err);
res.render('subgp', { exxr: data.result });
});
}
function exRoute(req, res, next) {
if (!req.params.ex.match(/^\d+$/)) return next('invalid expression ID');
var ex = Number(req.params.ex);
panlex.query('/ex/' + ex, { include: 'uid' },
function (err, data) {
if (err) return next(err);
var exx = data.ex;
panlex.queryAll('/ex', { trex: [ex], include: 'uid', sort: ['uid','tt'] },
function (err, data) {
if (err) return next(err);
res.render('ex', {
title: 'PanLinx: ' + exx.tt,
exx: exx,
trxr: data.result,
robot: false
});
});
});
}
function truncate(str) {
return ucs2.encode(ucs2.decode(str).slice(0, 15));
}