forked from piranha/puttext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpo2json.js
executable file
·54 lines (42 loc) · 1.3 KB
/
po2json.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
#!/usr/bin/env node
var GetOpt = require('node-getopt'),
p2j = require('po2json');
function unparseHeader(header) {
return Object.keys(header)
.map(function(x) { return x + ': '+ header[x]; })
.join('\n') +
// it always ends with an \n in xgettext-created files
'\n';
}
function run() {
var getOpt = new GetOpt([
['', 'space=[NUMBER]', 'print formatted JSON with spaces (default 0, non-formatted JSON)'],
['f', 'formatted', 'Same as --space=2'],
['h', 'help', 'display this help']
]).bindHelp();
var opts = getOpt.parseSystem();
if (!opts.argv.length) {
return getOpt.showHelp();
}
var space = opts.options.formatted === true? 2 : parseInt(opts.options.space) || false;
try {
var parsed = p2j.parseFileSync(opts.argv[0]);
} catch (e) {
console.error(["Can't parse PO file: ", opts.argv[0]].join());
console.log(e);
return;
}
var data = {}, item;
for (var key in parsed) {
item = parsed[key];
if (key === '') {
data[key] = unparseHeader(item);
} else if (item[0] === null) {
data[key] = item[1];
} else {
data[key] = item.slice(1);
}
}
console.log(JSON.stringify(data, null, space));
}
run();