-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
411 lines (348 loc) · 10.4 KB
/
build.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
var http = require('http');
var fs = require('fs');
var zlib = require('zlib');
var tar = require('tar');
var async = require('async');
var request = require('request');
var byline = require('byline');
var DUMP_URL = 'http://glyphwiki.org/dump.tar.gz';
var IVD_URL = 'http://www.unicode.org/ivd/data/2016-08-15/IVD_Sequences.txt';
var SOURCES_DIR = './sources';
var DATA_DIR = './data';
// make SOURCES_DIR if not exists
if (!fs.existsSync(SOURCES_DIR)) {
fs.mkdirSync(SOURCES_DIR);
}
if (!fs.existsSync(DATA_DIR)) {
fs.mkdirSync(DATA_DIR);
}
var alias = {};
var IVD = {};
var aliasGroup = {};
var minAliasGroup = {};
var minMap = {};
var resolveAlias = function (charCode) {
while (alias[charCode] !== undefined) charCode = alias[charCode];
return charCode;
};
var recordInGroup = function (alias, string) {
if (aliasGroup[alias] === undefined) {
aliasGroup[alias] = [];
}
aliasGroup[alias].push(string);
};
// pseudo-base64
var base64 = function (n) {
var ret = '';
var charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@_'
if (n === 0) return charset[0];
while (n !== 0) {
ret = charset[n % 64] + ret;
n = Math.floor(n / 64);
}
return ret;
}
// ES6 String.fromCodePoint polyfilling
/*! http://mths.be/fromcodepoint v0.1.0 by @mathias */
var fromCodePoint = function (codePoints) {
var MAX_SIZE = 0x4000;
var codeUnits = [];
var highSurrogate;
var lowSurrogate;
var index = -1;
var length = arguments.length;
if (!length) {
return '';
}
var result = '';
while (++index < length) {
var codePoint = Number(arguments[index]);
if (
!isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
codePoint < 0 || // not a valid Unicode code point
codePoint > 0x10FFFF || // not a valid Unicode code point
Math.floor(codePoint) != codePoint // not an integer
) {
throw RangeError('Invalid code point: ' + codePoint);
}
if (codePoint <= 0xFFFF) { // BMP code point
codeUnits.push(codePoint);
} else { // Astral code point; split in surrogate halves
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
codePoint -= 0x10000;
highSurrogate = (codePoint >> 10) + 0xD800;
lowSurrogate = (codePoint % 0x400) + 0xDC00;
codeUnits.push(highSurrogate, lowSurrogate);
}
if (index + 1 == length || codeUnits.length > MAX_SIZE) {
result += String.fromCharCode.apply(null, codeUnits);
codeUnits.length = 0;
}
}
return result;
};
// ES6 String.prototype.codePointAt pseudo-polyfilling
/*! http://mths.be/codepointat v0.1.0 by @mathias */
var toCodePoint = function (string) {
var size = string.length;
var index = 0;
// Get the first code unit
var first = string.charCodeAt(index);
var second;
if ( // check if it’s the start of a surrogate pair
first >= 0xD800 && first <= 0xDBFF && // high surrogate
size > index + 1 // there is a next code unit
) {
second = string.charCodeAt(index + 1);
if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
}
}
return first;
};
module.exports = function (done) {
// fetch sources
async.parallel([
// fetch the dump
function (done) {
async.waterfall([
// check if dump file exists
function (done) {
fs.exists(SOURCES_DIR + '/dump_newest_only.txt', function (exists) {
done(null, exists);
});
},
// download dump file
function (exists, done) {
if (exists) {
console.log('Dump file exists. Continue.')
done();
} else {
var download = request(DUMP_URL);
download.on('error', done);
download.on('end', function () {
console.log('Dump file download ended.');
});
var gunzip = zlib.createGunzip();
gunzip.on('error', done);
gunzip.on('finish', function () {
console.log('Dump file gzip inflation finished.');
});
var extract = new tar.Parse();
extract.on('error', done);
extract.on('entry', function (entry) {
if (entry.path === 'dump_newest_only.txt') {
var write = fs.createWriteStream(SOURCES_DIR + '/dump_newest_only.txt');
write.on('error', done);
write.on('finish', function () {
console.log('Dump file writing finished.');
done();
});
entry.pipe(write);
}
});
download.pipe(gunzip).pipe(extract);
console.log('Dump file downloading...');
}
},
// parse dump file
function (done) {
var read = fs.createReadStream(SOURCES_DIR + '/dump_newest_only.txt');
read.on('error', done);
var parseLine = byline.createStream();
parseLine.on('error', done);
var kanjiSpans = [
{from: 0x3400, to: 0x4DBF}, // CJK Extension A
{from: 0x4E00, to: 0x9FFF}, // CJK
{from: 0xF900, to: 0xFAFF}, // CJK Compatibility
{from: 0x20000, to: 0x2A6DF}, // CJK Extension B
{from: 0x2A700, to: 0x2B73F}, // CJK Extension C
{from: 0x2B740, to: 0x2B81F}, // CJK Extension D
];
parseLine.on('data', function (line) {
var columns = line.toString().split('|');
if (columns.length !== 3) {
return;
}
columns = columns.map(function (column) {
return column.trim();
});
// register kanji to IVD
if (columns[0].match(new RegExp('^u[0-9a-f]{4,5}$'))) {
var codePoint = parseInt(columns[0].slice(1), 16);
if (kanjiSpans.some(function (span) {
return span.from <= codePoint && codePoint <= span.to;
})) {
var kanji = fromCodePoint(codePoint);
if (IVD[kanji] === undefined) {
IVD[kanji] = {};
}
}
}
var aliasMatch = columns[2].match(new RegExp('^99:0:0:0:0:200:200:([^:]+)$'));
if (aliasMatch) {
var aliasedTo = aliasMatch[1];
alias[columns[0]] = aliasedTo;
}
});
parseLine.on('finish', function () {
console.log('Dump file parsing finished.');
done();
});
read.pipe(parseLine);
}
], done);
},
// fetch IVD
function (done) {
async.waterfall([
// check if IVD file exists
function (done) {
fs.exists(SOURCES_DIR + '/IVD_Sequences.txt', function (exists) {
done(null, exists);
});
},
// download IVD data
function (exists, done) {
if (exists) {
console.log('IVD file exists. Continue.')
done();
} else {
var download = request(IVD_URL);
download.on('error', done);
download.on('finish', function () {
console.log('IVD file download finished.');
});
var write = fs.createWriteStream(SOURCES_DIR + '/IVD_Sequences.txt');
write.on('error', done);
write.on('finish', function () {
console.log('IVD file writing finished');
done();
});
download.pipe(write);
console.log('IVD file downloading...');
}
},
// parse IVD data
function (done) {
var read = fs.createReadStream(SOURCES_DIR + '/IVD_Sequences.txt');
read.on('error', done);
var parseLine = byline.createStream();
parseLine.on('error', done);
parseLine.on('data', function (line) {
line = line.toString();
if (line.slice(0, 1) === '#') {
return;
}
var columns = line.split(';');
if (columns.length !== 3) {
return;
}
columns = columns.map(function (column) {
return column.trim();
});
var shortName;
switch (columns[1]) {
case 'Adobe-Japan1':
shortName = 'AJ';
break;
case 'Hanyo-Denshi':
case 'Moji_Joho':
shortName = 'HD';
break;
}
if (!shortName) {
return;
}
var codePoints = columns[0].split(' ');
codePoints = codePoints.map(function (codePoint) {
return fromCodePoint(parseInt(codePoint, 16));
});
if (IVD[codePoints[0]] === undefined) {
IVD[codePoints[0]] = {};
}
IVD[codePoints[0]][codePoints[1]] = [shortName];
});
parseLine.on('finish', function () {
console.log('IVD file parsing finished.');
done();
});
read.pipe(parseLine);
}
], done);
}
], function (error) {
if (error) {
console.error(error);
return;
}
console.log('Parsing completed.');
// resolve aliases in IVD
for (var firstCode in IVD) if (IVD.hasOwnProperty(firstCode)) {
var firstCodePoint = toCodePoint(firstCode).toString(16).toLowerCase();
for (var secondCode in IVD[firstCode]) if (IVD[firstCode].hasOwnProperty(secondCode)) {
var secondCodePoint = toCodePoint(secondCode).toString(16).toLowerCase();
var glyphName = 'u' + firstCodePoint + '-u' + secondCodePoint;
var resolved = resolveAlias(glyphName);
IVD[firstCode][secondCode].push(resolved);
recordInGroup(resolved, firstCode + secondCode);
}
var standardGlyph = 'u' + firstCodePoint;
IVD[firstCode].std = resolveAlias(standardGlyph);
recordInGroup(IVD[firstCode].std, firstCode);
}
// minify aliases
var cnt = 0;
for (var aliasName in aliasGroup) if (aliasGroup.hasOwnProperty(aliasName)) {
var aliases = aliasGroup[aliasName];
if (aliases.length >= 2) {
var minAliasName = base64(cnt);
cnt++;
minAliasGroup[minAliasName] = aliases;
minMap[aliasName] = minAliasName;
}
}
// minify aliases in IVD
for (var firstCode in IVD) if (IVD.hasOwnProperty(firstCode)) {
for (var secondCode in IVD[firstCode]) if (IVD[firstCode].hasOwnProperty(secondCode)) {
if (secondCode === 'std') {
aliasName = IVD[firstCode][secondCode];
minAliasName = minMap[aliasName];
if (minAliasName === undefined) {
IVD[firstCode][secondCode] = '';
} else {
IVD[firstCode][secondCode] = minAliasName;
}
} else {
aliasName = IVD[firstCode][secondCode][1];
minAliasName = minMap[aliasName];
if (minAliasName === undefined) {
IVD[firstCode][secondCode][1] = '';
} else {
IVD[firstCode][secondCode][1] = minAliasName;
}
}
}
}
// remove unlinked kanjies from IVD
for (var firstCode in IVD) if (IVD.hasOwnProperty(firstCode)) {
if (Object.keys(IVD[firstCode]).length === 1 && IVD[firstCode].std === '') {
delete IVD[firstCode];
}
}
console.log('Compile finished.')
// write down to json
fs.writeFile(DATA_DIR + '/ivd.json', JSON.stringify({
IVD: IVD,
aliases: minAliasGroup
}), function (error) {
if (error) {
done(error);
return;
}
console.log('Compiled file created.');
done();
});
});
};