forked from longnow/TeraDict
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
156 lines (125 loc) · 4.14 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
149
150
151
152
153
154
155
156
var express = require('express');
var logger = require('morgan');
var bodyParser = require('body-parser');
var errorHandler = require('errorhandler');
var config = require('./config');
var panlex = require('panlex');
var path = require('path');
var fs = require('fs');
var qs = require('querystring');
var sprintf = require('sprintf').sprintf;
var XRegExp = require('xregexp').XRegExp;
var unorm = require('unorm');
panlex.limit = false;
panlex.setUserAgent('TeraDict', require('./package.json').version);
var app = express();
app.set('port', config.port || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
if (app.get('env') === 'development') app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(setHeaders);
app.use(prepareRequest);
app.use(express.static(path.join(__dirname, 'public')));
app.use(errorResponse);
if (app.get('env') === 'development') app.use(errorHandler());
app.locals.base = config.base;
app.locals.urlroot = '/demo';
app.locals.lcvcUid = function(obj) {
return sprintf('%s-%03d', obj.lc, obj.vc);
};
loadLanguages();
app.get('/', index);
app.post('/1', op1);
app.post('/2', op2);
app.listen(app.get('port'), '127.0.0.1', function(){
console.log("Express server listening on port " + app.get('port'));
});
function loadLanguages() {
var lgs = {};
fs.readdirSync(path.join(__dirname, 'i18n')).forEach(function (file) {
if (!file.match(/\.json$/)) return;
var lg = file.replace(/\.json/, '');
lgs[lg] = require(path.join(__dirname, 'i18n', file));
lgs[lg].lc = lg;
});
app.set('lgs', lgs);
}
function setHeaders(req, res, next) {
res.set('Expires', 0);
next();
}
function prepareRequest(req, res, next) {
var lgs = app.get('lgs'),
lg = req.query.lg || 'eng';
if (lgs[lg] === undefined) next('TeraDict error: unknown language "' + lg + '"');
res.locals.lg = res.lg = lgs[lg];
res.locals.subheading = res.lg.teradict_description;
res.locals.qs = makeQuery(req, res);
for (var i in req.body) {
if (req.body[i] !== undefined) {
req.body[i] = req.body[i].trim();
res.locals[i] = req.body[i];
}
}
next();
}
function makeQuery(req) {
return function(p) {
var txt;
if (p) {
var query = {};
for (var i in req.query) query[i] = req.query[i];
for (var i in p) {
if (p[i] === undefined) delete query[i];
else query[i] = p[i];
}
txt = qs.stringify(query);
}
else txt = qs.stringify(req.query);
if (txt !== '') txt = '?' + txt;
return txt;
}
}
function errorResponse(err, req, res, next) {
res.render('error', { error: err });
}
function index(req, res, next) {
res.render('index', { et0: '' });
}
function op1(req, res, next) {
if (blank(req.body, ['et0'])) return res.render('index');
var et0 = normalize(req.body.et0);
panlex.query('/ex', { lv: [res.lg.lv], tt: [et0] }, function (err, data) {
if (err) return next(err);
if (data.resultNum === 0) return res.render('index', { subheading: res.lg.ex_not_found});
var ex0 = data.result[0].ex;
panlex.queryAll('/lv', { trex: [ex0], sort: ["lc","vc"] }, function (err, data) {
if (err) return next(err);
if (data.resultNum === 0) res.render('index', { subheading: res.lg.tr_not_found });
else res.render('op1', { result: data.result, ex0: ex0 });
});
});
}
function op2(req, res, next) {
if (blank(req.body, ['et0', 'ex0', 'lv1']))
return next('TeraDict error: missing required parameter');
var ex0 = Number(req.body.ex0),
lv1 = res.locals.lv1 = JSON.parse(req.body.lv1);
panlex.queryAll('/ex', { trex: [ex0], lv: [lv1.lv], sort: ["tt"] }, function (err, data) {
if (err) return next(err);
res.render('op2', { result: data.result });
});
}
function blank(obj, params) {
params.forEach(function (item) {
if (obj[item] === undefined || obj[item] === '') return true;
});
return false;
}
function normalize(txt) {
txt = unorm.nfc(txt);
txt = txt.replace(XRegExp('\\p{C}+', 'g'), '');
txt = txt.replace(XRegExp('\\p{Z}+', 'g'), ' ');
return txt.trim();
}