-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathime_parser.js
117 lines (107 loc) · 3.19 KB
/
ime_parser.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
// Author: Jishnu Mohan <[email protected]>
var http = require('http'),
async = require('async'),
fs = require('fs'),
file = process.argv[2];
if(!file) {
return console.log("Error: Pass file name as parameter");
}
var sort = [
'Q','W','E','R','T','Y','U','I','O','P','\\{','\\}','\\|',
'q','w','e','r','t','y','u','i','o','p','\\[','\\]','\\\\',
'A','S','D','F','G','H','J','K','L','\\:', '\"',null,null,
'a','s','d','f','g','h','j','k','l',';', '\'',null,null,
'Z','X','C','V','B','N','M','\\<','\\>','\\?',null,null,null,
'z','x','c','v','b','n','m',',','.','/',null,null,null,
'`','1','2','3','4','5','6','7','8','9','0','\\-','\\=',
'~','\\!','\\@','\\#','\\$','\\%','\\^','\\&','\\*','\\(','\\)','_','\\+'
];
var toHex = function(char) {
var hex = char.charCodeAt(0).toString(16).toUpperCase();
if(char.length < 4) {
hex = "0" + hex;
}
return "&#x" + hex + ";";
};
var charDetails = function(char, callback) {
var query = JSON.stringify({
method: "chardetails.getdetails",
params: [char],
id: ""
});
var req = http.request({
host: "silpa.org.in",
//host: "localhost",
path: "/JSONRPC",
port: '80',
//port: '5000',
method: "POST",
headers: {
"Accept": "application/json, text/javascript, */*;",
"Content-Type": "application/json; charset=UTF-8",
"Content-Length": query.length + 2
}
}, function (res) {
var data = "";
res.addListener('data', function(chunk) {
data += chunk;
});
res.addListener('end', function() {
var val=" ";
console.log(data);
try {
val = JSON.parse(data);
val = val.result[char].Name;
} catch (e) {
}
callback(val);
});
}).end(query);
};
jQuery = {
ime: {
register: function(data) {
var out = [],
extra = [],
calls = [],
count = 0;
data.patterns.forEach(function(key) {
if(key[1] === "") {
return;
}
calls.push(function(callback) {
var char = key[1],
pos = key[0],
index = sort.indexOf(pos);
console.log('---------------------------');
console.log(++count, pos, char);
charDetails(char, function(name) {
if(index !== -1) {
out[sort.indexOf(pos)] = '\t\t\t<!--' + pos + ' ' + char + ' ' + name + '-->\n' +
'\t\t\t<Key\n' +
'\t\t\t\tlatin:keySpec="' + toHex(char) +'"\n' +
'\t\t\t\tlatin:keyLabelFlags="fontNormal|followKeyLetterRatio" />\n';
} else {
extra.push(pos + " " + toHex(char) + " " + char + name + "\n");
}
callback();
});
});
});
async.series(calls, function(err, results) {
var stream = fs.createWriteStream(file.split('.')[0] + '.xml');
stream.once('open', function(fd) {
out.forEach(function(line) {
stream.write(line.replace(/\t/g, " "));
});
stream.write("\n\n\n");
extra.forEach(function(line) {
stream.write(line.replace(/\t/g, " "));
});
stream.end();
});
});
}
}
};
var a = require("./" + file);